We use ORDER BY multiple columns to sort data by more than one rule. This helps organize results clearly.
ORDER BY multiple columns in SQL
SELECT column1, column2, ... FROM table_name ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;
You can choose ascending (ASC) or descending (DESC) order for each column.
If you don't specify ASC or DESC, ascending order is the default.
SELECT first_name, last_name FROM employees ORDER BY last_name, first_name;
SELECT product_name, category, price FROM products ORDER BY category ASC, price DESC;
SELECT event_name, event_date, start_time FROM events ORDER BY event_date DESC, start_time ASC;
This creates a table of employees, adds some data, and then selects their names and departments sorted by last name and first name both ascending.
CREATE TABLE employees ( id INT, first_name VARCHAR(50), last_name VARCHAR(50), department VARCHAR(50) ); INSERT INTO employees (id, first_name, last_name, department) VALUES (1, 'John', 'Smith', 'Sales'), (2, 'Jane', 'Doe', 'Marketing'), (3, 'Alice', 'Smith', 'HR'), (4, 'Bob', 'Brown', 'Sales'); SELECT first_name, last_name, department FROM employees ORDER BY last_name ASC, first_name ASC;
Ordering by multiple columns helps when one column alone can't sort data clearly.
Use ASC for ascending order and DESC for descending order on each column.
ORDER BY is usually the last clause in a SELECT query.
ORDER BY multiple columns sorts data by more than one rule.
You can mix ascending and descending order for each column.
This helps organize data clearly when one column is not enough.
Practice
What does the SQL clause ORDER BY column1, column2 do?
Solution
Step 1: Understand ORDER BY with multiple columns
TheORDER BYclause sorts rows by the first column listed.Step 2: Sorting with tie-breaker column
If two or more rows have the same value in the first column, sorting continues by the second column.Final Answer:
Sorts the results first by column1, then by column2 if there are ties in column1 -> Option CQuick Check:
ORDER BY multiple columns sorts stepwise [OK]
- Thinking ORDER BY filters rows
- Assuming it groups data
- Believing it sorts only by the last column
Which of the following is the correct syntax to sort by age ascending and then by name descending?
SELECT * FROM users ORDER BY ...
Solution
Step 1: Specify sorting order for each column
UseASCfor ascending andDESCfor descending explicitly after each column.Step 2: Apply correct order for age and name
Since age is ascending and name is descending, writeage ASC, name DESC.Final Answer:
age ASC, name DESC -> Option AQuick Check:
Correct syntax uses ASC and DESC per column [OK]
- Omitting ASC or DESC and expecting default
- Mixing order keywords incorrectly
- Using ORDER BY twice in one query
Given the table products with columns category and price, what will this query return?
SELECT category, price FROM products ORDER BY category ASC, price DESC;
Solution
Step 1: Analyze ORDER BY clause
The query orders rows first bycategoryascending (A to Z).Step 2: Understand secondary sorting
Within each category, rows are sorted bypricedescending (highest to lowest).Final Answer:
Rows sorted by category alphabetically, then by price highest to lowest within each category -> Option AQuick Check:
ORDER BY sorts stepwise by listed columns [OK]
- Confusing ascending and descending order
- Thinking ORDER BY groups rows
- Ignoring second column sorting
Identify the error in this query:
SELECT * FROM employees ORDER BY salary DESC,;
Solution
Step 1: Check ORDER BY syntax
TheORDER BYclause lists columns separated by commas without trailing commas.Step 2: Identify trailing comma error
The comma afterDESCis extra and causes a syntax error.Final Answer:
Extra comma after DESC causes syntax error -> Option BQuick Check:
Trailing commas in ORDER BY cause syntax errors [OK]
- Leaving trailing commas in ORDER BY
- Forgetting ASC or DESC (defaults to ASC though)
- Assuming ORDER BY accepts invalid columns
You have a students table with columns grade (integer) and last_name (text). You want to list students sorted by grade descending, but for students with the same grade, sort by last name ascending. Which query achieves this?
Solution
Step 1: Determine primary sorting column and order
Primary sort is bygradedescending (highest grade first).Step 2: Determine secondary sorting column and order
For ties in grade, sort bylast_nameascending (A to Z).Step 3: Write correct ORDER BY syntax
UseORDER BY grade DESC, last_name ASCwith comma separating columns.Final Answer:
SELECT * FROM students ORDER BY grade DESC, last_name ASC; -> Option DQuick Check:
ORDER BY multiple columns with specified directions [OK]
- Swapping order of columns in ORDER BY
- Missing comma between columns
- Omitting ASC or DESC keywords
Ready to prove it?
Pass the test and this pattern moves unknown
