Bird
Raised Fist0
SQLquery~10 mins

ORDER BY multiple columns in SQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - ORDER BY multiple columns
Start Query
↓
Fetch Rows
↓
Sort by First Column
↓
Sort by Second Column (if tie)
↓
Return Sorted Result
The query fetches rows, sorts them first by the first column, then by the second column if there are ties, and returns the sorted result.
Execution Sample
SQL
SELECT name, age, city
FROM people
ORDER BY city ASC, age DESC;
This query sorts people first by city alphabetically, then by age from oldest to youngest within each city.
Execution Table
StepActionIntermediate ResultExplanation
1Fetch all rows[('Alice', 30, 'Boston'), ('Bob', 25, 'Austin'), ('Carol', 22, 'Boston'), ('Dave', 35, 'Austin')]All rows retrieved from table
2Sort by city ASC[('Bob', 25, 'Austin'), ('Dave', 35, 'Austin'), ('Alice', 30, 'Boston'), ('Carol', 22, 'Boston')]Rows ordered alphabetically by city
3Sort by age DESC within city[('Dave', 35, 'Austin'), ('Bob', 25, 'Austin'), ('Alice', 30, 'Boston'), ('Carol', 22, 'Boston')]Within each city, rows sorted by age descending
4Return final sorted rows[('Dave', 35, 'Austin'), ('Bob', 25, 'Austin'), ('Alice', 30, 'Boston'), ('Carol', 22, 'Boston')]Final output after multi-column ordering
💡 All rows sorted by city ascending, then by age descending within each city
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
rowsempty[('Alice', 30, 'Boston'), ('Bob', 25, 'Austin'), ('Carol', 22, 'Boston'), ('Dave', 35, 'Austin')][('Bob', 25, 'Austin'), ('Dave', 35, 'Austin'), ('Alice', 30, 'Boston'), ('Carol', 22, 'Boston')][('Dave', 35, 'Austin'), ('Bob', 25, 'Austin'), ('Alice', 30, 'Boston'), ('Carol', 22, 'Boston')][('Dave', 35, 'Austin'), ('Bob', 25, 'Austin'), ('Alice', 30, 'Boston'), ('Carol', 22, 'Boston')]
Key Moments - 2 Insights
Why does the order change after sorting by the second column?
Because the second column sorting only affects rows that have the same value in the first column, as shown in step 3 of the execution_table where age sorts rows within each city.
What happens if we reverse the order of columns in ORDER BY?
The sorting priority changes: the first column listed is sorted first, then the second. Changing the order changes the final sorted result, as the first column has higher priority.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the order of rows after step 2?
A[('Dave', 35, 'Austin'), ('Bob', 25, 'Austin'), ('Alice', 30, 'Boston'), ('Carol', 22, 'Boston')]
B[('Bob', 25, 'Austin'), ('Dave', 35, 'Austin'), ('Alice', 30, 'Boston'), ('Carol', 22, 'Boston')]
C[('Alice', 30, 'Boston'), ('Carol', 22, 'Boston'), ('Bob', 25, 'Austin'), ('Dave', 35, 'Austin')]
D[('Carol', 22, 'Boston'), ('Alice', 30, 'Boston'), ('Dave', 35, 'Austin'), ('Bob', 25, 'Austin')]
💡 Hint
Check the 'Intermediate Result' column for step 2 in the execution_table.
At which step does the sorting by the second column happen?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look for the step mentioning sorting by age DESC within city in the execution_table.
If we change ORDER BY to 'age DESC, city ASC', what changes in the variable_tracker?
ARows are first sorted by city, then by age within city
BRows remain sorted by city ascending, age descending
CRows are first sorted by age descending, then by city ascending within same age
DRows are sorted randomly
💡 Hint
The first column in ORDER BY has the highest sorting priority, check variable_tracker for sorting order.
Concept Snapshot
ORDER BY multiple columns sorts query results by the first column listed, then by the second column if there are ties.
Syntax: ORDER BY column1 ASC|DESC, column2 ASC|DESC
First column has higher priority.
Useful to organize data by multiple criteria.
Example: ORDER BY city ASC, age DESC sorts by city alphabetically, then age descending within city.
Full Transcript
This visual execution shows how SQL orders rows when using ORDER BY with multiple columns. First, all rows are fetched from the table. Then, the rows are sorted by the first column specified in the ORDER BY clause. If there are rows with the same value in that first column, those rows are further sorted by the second column. The example query sorts people by city alphabetically, then by age descending within each city. The execution table traces each step, showing how the rows change order after sorting by each column. The variable tracker records the state of the rows after each step. Key moments clarify why the second column sorting only affects ties and how changing the order of columns changes the final result. The quiz tests understanding of the sorting steps and effects of changing column order. The snapshot summarizes the syntax and behavior of ORDER BY with multiple columns.

Practice

(1/5)
1.

What does the SQL clause ORDER BY column1, column2 do?

easy
A. Filters rows where column1 equals column2
B. Sorts the results only by column2
C. Sorts the results first by column1, then by column2 if there are ties in column1
D. Groups rows by column1 and column2

Solution

  1. Step 1: Understand ORDER BY with multiple columns

    The ORDER BY clause sorts rows by the first column listed.
  2. 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.
  3. Final Answer:

    Sorts the results first by column1, then by column2 if there are ties in column1 -> Option C
  4. Quick Check:

    ORDER BY multiple columns sorts stepwise [OK]
Hint: ORDER BY sorts by first column, then next for ties [OK]
Common Mistakes:
  • Thinking ORDER BY filters rows
  • Assuming it groups data
  • Believing it sorts only by the last column
2.

Which of the following is the correct syntax to sort by age ascending and then by name descending?

SELECT * FROM users ORDER BY ...
easy
A. age ASC, name DESC
B. age DESC, name ASC
C. ORDER BY age DESC, name
D. age, name ASC

Solution

  1. Step 1: Specify sorting order for each column

    Use ASC for ascending and DESC for descending explicitly after each column.
  2. Step 2: Apply correct order for age and name

    Since age is ascending and name is descending, write age ASC, name DESC.
  3. Final Answer:

    age ASC, name DESC -> Option A
  4. Quick Check:

    Correct syntax uses ASC and DESC per column [OK]
Hint: Use ASC or DESC after each column in ORDER BY [OK]
Common Mistakes:
  • Omitting ASC or DESC and expecting default
  • Mixing order keywords incorrectly
  • Using ORDER BY twice in one query
3.

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;
medium
A. Rows sorted by category alphabetically, then by price highest to lowest within each category
B. Rows sorted by price descending only
C. Rows sorted by category descending, then price ascending
D. Rows grouped by category and price

Solution

  1. Step 1: Analyze ORDER BY clause

    The query orders rows first by category ascending (A to Z).
  2. Step 2: Understand secondary sorting

    Within each category, rows are sorted by price descending (highest to lowest).
  3. Final Answer:

    Rows sorted by category alphabetically, then by price highest to lowest within each category -> Option A
  4. Quick Check:

    ORDER BY sorts stepwise by listed columns [OK]
Hint: First column sorts main order, second breaks ties [OK]
Common Mistakes:
  • Confusing ascending and descending order
  • Thinking ORDER BY groups rows
  • Ignoring second column sorting
4.

Identify the error in this query:

SELECT * FROM employees ORDER BY salary DESC,;
medium
A. Missing ASC or DESC after salary
B. Extra comma after DESC causes syntax error
C. ORDER BY cannot have multiple columns
D. salary is not a valid column

Solution

  1. Step 1: Check ORDER BY syntax

    The ORDER BY clause lists columns separated by commas without trailing commas.
  2. Step 2: Identify trailing comma error

    The comma after DESC is extra and causes a syntax error.
  3. Final Answer:

    Extra comma after DESC causes syntax error -> Option B
  4. Quick Check:

    Trailing commas in ORDER BY cause syntax errors [OK]
Hint: No comma after last ORDER BY column [OK]
Common Mistakes:
  • Leaving trailing commas in ORDER BY
  • Forgetting ASC or DESC (defaults to ASC though)
  • Assuming ORDER BY accepts invalid columns
5.

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?

hard
A. SELECT * FROM students ORDER BY grade DESC last_name ASC;
B. SELECT * FROM students ORDER BY last_name ASC, grade DESC;
C. SELECT * FROM students ORDER BY grade ASC, last_name DESC;
D. SELECT * FROM students ORDER BY grade DESC, last_name ASC;

Solution

  1. Step 1: Determine primary sorting column and order

    Primary sort is by grade descending (highest grade first).
  2. Step 2: Determine secondary sorting column and order

    For ties in grade, sort by last_name ascending (A to Z).
  3. Step 3: Write correct ORDER BY syntax

    Use ORDER BY grade DESC, last_name ASC with comma separating columns.
  4. Final Answer:

    SELECT * FROM students ORDER BY grade DESC, last_name ASC; -> Option D
  5. Quick Check:

    ORDER BY multiple columns with specified directions [OK]
Hint: List columns in ORDER BY with desired ASC/DESC per column [OK]
Common Mistakes:
  • Swapping order of columns in ORDER BY
  • Missing comma between columns
  • Omitting ASC or DESC keywords
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard