ORDER BY multiple columns in MySQL - Time & Space Complexity
When sorting data by more than one column, it is important to understand how the sorting time changes as the data grows.
We want to know how the sorting work increases when we order by multiple columns instead of just one.
Analyze the time complexity of the following code snippet.
SELECT *
FROM employees
ORDER BY department ASC, salary DESC;
This query sorts employees first by their department in ascending order, then by salary in descending order within each department.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sorting the entire list of rows based on multiple columns.
- How many times: The sorting algorithm compares and rearranges rows multiple times, depending on the number of rows.
Sorting takes more work as the number of rows grows, but adding more columns to sort by does not multiply the work by much.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 to 40 comparisons |
| 100 | About 700 to 900 comparisons |
| 1000 | About 10,000 to 15,000 comparisons |
Pattern observation: The number of operations grows a bit faster than the number of rows, but adding more columns to sort by only adds small extra work per comparison.
Time Complexity: O(n log n)
This means the sorting work grows a little more than linearly with the number of rows, even when sorting by multiple columns.
[X] Wrong: "Sorting by two columns takes twice as long as sorting by one column."
[OK] Correct: Sorting compares rows multiple times anyway, so adding a second column only adds a small extra step per comparison, not doubling the total work.
Understanding how sorting scales with data size and multiple columns helps you explain query performance clearly and confidently in real situations.
"What if we added an index on the columns used in ORDER BY? How would the time complexity change?"
Practice
ORDER BY clause with multiple columns do in a MySQL query?Solution
Step 1: Understand the purpose of ORDER BY with multiple columns
TheORDER BYclause sorts rows in the result set. When multiple columns are listed, sorting happens first by the first column.Step 2: Recognize tie-breaking with the second column
If two or more rows have the same value in the first column, the second column is used to sort those tied rows.Final Answer:
Sorts the result set by the first column, then by the second column if there are ties. -> Option AQuick Check:
ORDER BY multiple columns sorts stepwise [OK]
- Confusing ORDER BY with filtering (WHERE clause)
- Thinking ORDER BY changes data, not just order
- Assuming ORDER BY groups rows
age ascending and then by name descending in MySQL?Solution
Step 1: Recall correct keywords for sorting order
MySQL usesASCfor ascending andDESCfor descending order.Step 2: Check syntax for multiple columns
Multiple columns are separated by commas, each followed optionally byASCorDESC.Final Answer:
ORDER BY age ASC, name DESC -> Option BQuick Check:
ASC and DESC keywords are short and exact [OK]
- Using DESCENDING instead of DESC
- Mixing ASC and DESC keywords incorrectly
- Omitting commas between columns
Employees with columns department and salary, what will the result order be for this query?SELECT * FROM Employees ORDER BY department ASC, salary DESC;
Solution
Step 1: Analyze the ORDER BY clause
The query orders rows first bydepartmentin ascending (alphabetical) order.Step 2: Understand secondary sorting by salary
Within each department group, rows are sorted bysalaryin descending order (highest salary first).Final Answer:
Rows sorted by department alphabetically, then highest salary first within each department. -> Option DQuick Check:
ORDER BY column1 ASC, column2 DESC sorts stepwise [OK]
- Reversing the order of columns in ORDER BY
- Confusing ASC and DESC directions
- Assuming ORDER BY sorts only by one column
SELECT * FROM Products ORDER BY price ASC name DESC;
Solution
Step 1: Check syntax for multiple columns in ORDER BY
Multiple columns must be separated by commas in the ORDER BY clause.Step 2: Identify missing comma
The query misses a comma betweenprice ASCandname DESC, causing syntax error.Final Answer:
Missing comma between price ASC and name DESC. -> Option AQuick Check:
Separate columns with commas in ORDER BY [OK]
- Forgetting commas between columns
- Misplacing ASC/DESC keywords
- Thinking ORDER BY limits to one column
Students with columns grade (integer), last_name, and first_name. You want to list students sorted by grade descending, then by last name ascending, and finally by first name ascending. Which query achieves this?Solution
Step 1: Understand sorting priorities
The primary sort is bygradedescending, so highest grades come first.Step 2: Apply secondary and tertiary sorting
Within the same grade, sort bylast_nameascending (A to Z), then byfirst_nameascending.Step 3: Check syntax correctness
SELECT * FROM Students ORDER BY grade DESC, last_name ASC, first_name ASC; uses correct syntax with commas separating columns and proper ASC/DESC keywords.Final Answer:
SELECT * FROM Students ORDER BY grade DESC, last_name ASC, first_name ASC; -> Option CQuick Check:
Order by multiple columns with correct directions and commas [OK]
- Wrong order of columns in ORDER BY
- Missing commas between columns
- Mixing ASC and DESC incorrectly
- Omitting ASC/DESC keywords
Ready to prove it?
Pass the test and this pattern moves unknown
