Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Order By Multiple Cols in SQL

Order By Multiple Cols in SQL

ORDER BY Multiple Columns in SQL

You can sort query results by more than one column using ORDER BY. This lets you define a priority of sorting.


? Syntax:

SELECT column1, column2, ...FROM table_nameORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;
  • The results are sorted first by column1.

  • If there are ties in column1, then sorting proceeds by column2.

  • And so on for more columns.


? Example:

Sort employees by department_id ascending, then by salary descending within each department:

SELECT employee_name, department_id, salaryFROM employeesORDER BY department_id ASC, salary DESC;

How this works:

  • Rows are grouped by department_id in ascending order.

  • Within each department group, rows are sorted by salary in descending order.


Notes:

  • You can mix ascending and descending order on different columns.

  • Ordering can be on any columns, including expressions or aliases.


Want an example with ORDER BY multiple columns plus LIMIT?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql