Bird
Raised Fist0
SQLquery~5 mins

ORDER BY multiple columns in SQL - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the SQL clause ORDER BY do?
The ORDER BY clause sorts the rows returned by a query based on one or more columns, either in ascending (default) or descending order.
Click to reveal answer
beginner
How do you sort query results by multiple columns in SQL?
You list the columns separated by commas in the ORDER BY clause. For example: ORDER BY column1 ASC, column2 DESC sorts first by column1 ascending, then by column2 descending if there are ties.
Click to reveal answer
intermediate
Why would you use multiple columns in an ORDER BY clause?
Using multiple columns helps organize data more precisely. For example, sorting a list of people by last name and then by first name ensures all last names are grouped and within each group, first names are sorted.
Click to reveal answer
beginner
What is the default sort order in SQL when using ORDER BY?
The default sort order is ascending (smallest to largest or A to Z) if you do not specify ASC or DESC.
Click to reveal answer
beginner
Write a simple SQL query to select all columns from a table named Employees and sort by Department ascending and Salary descending.
SELECT * FROM Employees ORDER BY Department ASC, Salary DESC;
Click to reveal answer
What happens if you use ORDER BY column1, column2 in a SQL query?
ARows are sorted first by column1, then by column2 if there are ties in column1.
BRows are sorted only by column2.
CRows are sorted randomly.
DRows are sorted only by column1.
What is the default sorting order when you use ORDER BY column without specifying ASC or DESC?
ADescending order
BRandom order
CNo sorting
DAscending order
Which SQL clause is used to sort query results by multiple columns?
AWHERE
BGROUP BY
CORDER BY
DHAVING
How do you sort by Age descending and then by Name ascending?
AORDER BY Age DESC, Name ASC
BORDER BY Name DESC, Age ASC
CORDER BY Age ASC, Name DESC
DORDER BY Name ASC, Age DESC
If two rows have the same value in the first column of an ORDER BY clause, how are they sorted?
AThey remain in the original order
BThey are sorted by the next column in the ORDER BY list
CThey are sorted randomly
DThey are removed from the result
Explain how the ORDER BY clause works when sorting by multiple columns in SQL.
Think about sorting a list of names by last name then first name.
You got /4 concepts.
    Write an SQL query to select all data from a table called Products and sort by Category ascending and Price descending.
    Remember to separate columns with commas in ORDER BY.
    You got /2 concepts.

      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