Sub topic 12
Sorting by Several Columns, Ascending and Descending
Assume you know the basics of tables, columns, SELECT, and FROM. You should also be able to run SQL against an employees table.
Sort by One Column
ORDER BY controls the order of rows in a query result. Without it, a database does not promise any particular order.
SELECT first_name, department, salary
FROM employees
ORDER BY salary;The default direction is ascending, written as ASC. For numbers, ascending means smallest to largest. For text, it usually means alphabetical order.
SELECT first_name, department, salary
FROM employees
ORDER BY salary ASC;Use DESC for descending order:
SELECT first_name, department, salary
FROM employees
ORDER BY salary DESC;This query shows the highest salaries first.
Add Tie-Breakers
Sorting by one column often leaves ties. For example, several employees may work in the same department. Add another sort expression after the first one to decide the order within each group.
SELECT first_name, department, salary
FROM employees
ORDER BY department ASC, salary DESC;The database sorts this result in two stages:
- It sorts departments alphabetically.
- Within each department, it sorts salaries from highest to lowest.
The direction belongs to the expression immediately before it. In this example, department is ascending and salary is descending.
You can add more expressions when needed:
SELECT first_name, last_name, department, salary
FROM employees
ORDER BY department ASC,
salary DESC,
last_name ASC;Employees appear by department, then by salary within each department, then alphabetically by last name when both department and salary are equal.
The order of expressions matters. This query has a different meaning:
SELECT first_name, department, salary
FROM employees
ORDER BY salary DESC, department ASC;Now salary is the primary sort. Department only breaks ties between employees with the same salary.
Visualize the Sort Keys
NULL Values in Sorted Results
NULL means that a value is missing or unknown. Its position in sorted output depends on the database system and sort direction. PostgreSQL lets you state the position explicitly:
SELECT first_name, manager_id
FROM employees
ORDER BY manager_id ASC NULLS LAST;Some databases use different syntax or defaults. Check your database documentation when missing values affect the result.
Combine Sorting with a Row Limit
To find the highest-paid employee in each overall result, sort descending and limit the number of rows:
SELECT first_name, last_name, salary
FROM employees
ORDER BY salary DESC, last_name ASC
LIMIT 5;The second expression makes the result deterministic when employees share the same salary. PostgreSQL, MySQL, and SQLite support LIMIT. Some other systems use the standard form:
SELECT first_name, last_name, salary
FROM employees
ORDER BY salary DESC, last_name ASC
FETCH FIRST 5 ROWS ONLY;ORDER BY must come before the row-limiting clause.
Exercise
Write a query that returns the employee names, departments, and salaries with Sales employees first, then Marketing employees, and with the highest salary first within each department.
Show answer
SELECT firstname, lastname, department, salary FROM employees WHERE department IN ('Sales', 'Marketing') ORDER BY department ASC, salary DESC;