It sorts the rows returned by a query based on one or more columns.
Click to reveal answer
beginner
What is the default sorting order when using ORDER BY without specifying ASC or DESC?
The default sorting order is ascending (ASC).
Click to reveal answer
beginner
How do you sort query results in descending order?
Use ORDER BY column_name DESC to sort results from highest to lowest or Z to A.
Click to reveal answer
intermediate
Can you sort by multiple columns using ORDER BY? How?
Yes. List columns separated by commas, e.g., ORDER BY column1 ASC, column2 DESC.
Click to reveal answer
intermediate
What happens if you mix ASC and DESC in the same ORDER BY clause?
Each column sorts independently in the specified order, e.g., first ascending, then descending.
Click to reveal answer
What does ORDER BY name DESC do?
ASorts names from A to Z
BSorts names from Z to A
CFilters names starting with D
DGroups names by letter
✗ Incorrect
DESC means descending order, so names are sorted from Z to A.
Which is the correct way to sort by age ascending and then by name descending?
AORDER BY age ASC, name DESC
BORDER BY age, name
CORDER BY name DESC, age ASC
DORDER BY age DESC, name ASC
✗ Incorrect
Use ORDER BY age ASC, name DESC to sort age low to high, then name Z to A.
If you write ORDER BY salary without ASC or DESC, what happens?
ANo sorting applied
BSorts salary descending
CSorts salary ascending
DCauses an error
✗ Incorrect
Default sorting order is ascending if not specified.
Can ORDER BY sort by columns not in the SELECT list?
AYes, always
BOnly if using GROUP BY
CNo, never
DDepends on SQL dialect
✗ Incorrect
Some SQL versions allow it, others require the column in SELECT.
What is the effect of ORDER BY date DESC, price ASC?
ASorts by date descending, then price ascending
BSorts by date ascending, then price descending
CSorts by price descending, then date ascending
DSorts by price ascending only
✗ Incorrect
First sorts dates newest to oldest, then prices low to high within same date.
Explain how to use ORDER BY to sort query results by multiple columns with different orders.
Think about sorting a list first by one rule, then by another.
You got /4 concepts.
Describe what happens if you omit ASC or DESC in an ORDER BY clause.
What is the usual way to sort numbers or letters?
You got /3 concepts.
Practice
(1/5)
1. What does the ORDER BY clause do in an SQL query?
easy
A. It sorts the query results in a specified order.
B. It filters rows based on a condition.
C. It groups rows with the same values.
D. It limits the number of rows returned.
Solution
Step 1: Understand the purpose of ORDER BY
The ORDER BY clause is used to arrange the rows returned by a query in a specific order.
Step 2: Differentiate from other clauses
Filtering is done by WHERE, grouping by GROUP BY, and limiting rows by LIMIT. Only ORDER BY sorts the results.
Final Answer:
It sorts the query results in a specified order. -> Option A
Quick Check:
ORDER BY = sorts results [OK]
Hint: ORDER BY always sorts rows; remember ASC or DESC [OK]
Common Mistakes:
Confusing ORDER BY with WHERE clause
Thinking ORDER BY filters rows
Mixing ORDER BY with GROUP BY
2. Which of the following is the correct syntax to sort a table named Employees by the column Salary in descending order?
easy
A. SELECT * FROM Employees ORDER BY Salary DESC;
B. SELECT * FROM Employees SORT BY Salary DESC;
C. SELECT * FROM Employees ORDER Salary DESC;
D. SELECT * FROM Employees ORDER BY Salary DOWN;
Solution
Step 1: Recall correct ORDER BY syntax
The correct syntax to sort is ORDER BY column_name ASC|DESC. Here, DESC means descending order.
Step 2: Check each option
SELECT * FROM Employees ORDER BY Salary DESC; uses correct keywords and syntax. Options B, C, and D use invalid keywords like SORT BY, missing BY, or incorrect direction word.
Final Answer:
SELECT * FROM Employees ORDER BY Salary DESC; -> Option A
Quick Check:
ORDER BY + DESC = correct syntax [OK]
Hint: ORDER BY must be followed by column and ASC or DESC [OK]
Common Mistakes:
Using SORT BY instead of ORDER BY
Omitting BY after ORDER
Using DOWN instead of DESC
3. Given the table Products with columns ProductID and Price, what will be the order of ProductID after running this query?
SELECT ProductID FROM Products ORDER BY Price ASC;
medium
A. Products sorted by ProductID descending.
B. Products sorted by Price from highest to lowest.
C. Products sorted by ProductID ascending.
D. Products sorted by Price from lowest to highest.
Solution
Step 1: Analyze ORDER BY clause
The query orders rows by Price in ascending order, meaning from smallest to largest price.
Step 2: Understand output column
The query selects ProductID but orders by Price. So the ProductID values will appear sorted according to their prices ascending.
Final Answer:
Products sorted by Price from lowest to highest. -> Option D
Quick Check:
ORDER BY Price ASC = lowest to highest [OK]
Hint: ORDER BY sorts by specified column, not selected column [OK]
Common Mistakes:
Assuming sorting is by selected column instead of ORDER BY column
Confusing ASC with DESC
Thinking ORDER BY changes selected columns
4. Identify the error in this SQL query:
SELECT Name FROM Customers ORDER BY Age DESCENDING;
medium
A. Missing ASC keyword after ORDER BY.
B. The keyword DESCENDING is invalid; use DESC instead.
C. ORDER BY cannot be used with DESC.
D. The column Age does not exist.
Solution
Step 1: Check ORDER BY direction keywords
SQL uses ASC for ascending and DESC for descending order. DESCENDING is not a valid keyword.
Step 2: Validate other parts
ORDER BY works with DESC. ASC is optional. We assume Age exists as no schema given.
Final Answer:
The keyword DESCENDING is invalid; use DESC instead. -> Option B
Quick Check:
Use DESC, not DESCENDING [OK]
Hint: Use DESC, not DESCENDING for descending order [OK]
Common Mistakes:
Typing DESCENDING instead of DESC
Thinking DESC is not allowed
Assuming ASC is mandatory
5. You have a table Students with columns Name, Grade, and Age. You want to list students sorted first by Grade descending (highest grade first), and then by Age ascending (youngest first) within the same grade. Which query achieves this?
hard
A. SELECT * FROM Students ORDER BY Age DESC, Grade ASC;
B. SELECT * FROM Students ORDER BY Grade ASC, Age DESC;
C. SELECT * FROM Students ORDER BY Grade DESC, Age ASC;
D. SELECT * FROM Students ORDER BY Age ASC, Grade DESC;
Solution
Step 1: Understand sorting priorities
We want to sort by Grade descending first, so highest grades come first.
Step 2: Apply secondary sorting
Within the same grade, sort by Age ascending, so younger students appear first.
Step 3: Match query syntax
SELECT * FROM Students ORDER BY Grade DESC, Age ASC; matches the required order: ORDER BY Grade DESC, Age ASC. Other options reverse order or directions.
Final Answer:
SELECT * FROM Students ORDER BY Grade DESC, Age ASC; -> Option C
Quick Check:
ORDER BY Grade DESC, Age ASC = correct multi-level sort [OK]
Hint: List columns in ORDER BY by priority with ASC or DESC [OK]
Common Mistakes:
Reversing ASC and DESC directions
Swapping column order in ORDER BY
Using only one column for sorting
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.