0
0
SQLquery~20 mins

ORDER BY multiple columns in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ORDER BY Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of ORDER BY with two columns
Given the table Employees with columns Department and Salary, what is the output order of this query?
SELECT Department, Salary FROM Employees ORDER BY Department ASC, Salary DESC;
SQL
CREATE TABLE Employees (Department VARCHAR(20), Salary INT);
INSERT INTO Employees VALUES
('Sales', 5000),
('Sales', 7000),
('HR', 6000),
('HR', 5500),
('IT', 8000);

SELECT Department, Salary FROM Employees ORDER BY Department ASC, Salary DESC;
A[('HR', 6000), ('HR', 5500), ('IT', 8000), ('Sales', 7000), ('Sales', 5000)]
B[('Sales', 7000), ('Sales', 5000), ('HR', 6000), ('HR', 5500), ('IT', 8000)]
C[('HR', 5500), ('HR', 6000), ('IT', 8000), ('Sales', 5000), ('Sales', 7000)]
D[('IT', 8000), ('HR', 6000), ('HR', 5500), ('Sales', 7000), ('Sales', 5000)]
Attempts:
2 left
💡 Hint
ORDER BY sorts first by the first column, then by the second within each group.
🧠 Conceptual
intermediate
2:00remaining
Understanding ORDER BY with NULL values
Consider a table Products with columns Category and Price. If you run:
SELECT Category, Price FROM Products ORDER BY Category ASC, Price DESC;

and some Price values are NULL, where will those NULLs appear in the sorted result?
ANULL prices appear first within each Category when ordering Price DESC.
BNULL prices appear last within each Category when ordering Price DESC.
CNULL prices are ignored and do not appear in the result.
DNULL prices cause an error in ORDER BY.
Attempts:
2 left
💡 Hint
Think about how NULLs are treated in descending order sorting.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in ORDER BY multiple columns
Which option contains a syntax error in the ORDER BY clause when sorting by two columns Age ascending and Name descending?
AORDER BY Age ASC, Name DESC
BORDER BY Age, Name DESC
CORDER BY Age ASC; Name DESC
DORDER BY Age ASC Name DESC
Attempts:
2 left
💡 Hint
Check the punctuation between columns in ORDER BY.
optimization
advanced
2:00remaining
Optimizing ORDER BY with multiple columns
You have a large table Sales with columns Region, SalesDate, and Amount. You often run:
SELECT * FROM Sales ORDER BY Region ASC, SalesDate DESC;

Which index will best improve the performance of this query?
ACREATE INDEX idx_region ON Sales(Region);
BCREATE INDEX idx_salesdate_region ON Sales(SalesDate DESC, Region ASC);
CCREATE INDEX idx_region_salesdate ON Sales(Region ASC, SalesDate DESC);
DCREATE INDEX idx_amount ON Sales(Amount);
Attempts:
2 left
💡 Hint
Indexes should match the ORDER BY column order and directions.
🔧 Debug
expert
2:00remaining
Why does this ORDER BY query return unexpected results?
You run this query:
SELECT Name, Score FROM Players ORDER BY Score, Name;

but the results are not sorted as expected by Score ascending and then Name ascending. What is the most likely cause?
AThe Score column is stored as text, so sorting is lexicographical, not numeric.
BThe query is missing ASC keywords, so sorting defaults to descending.
CORDER BY cannot sort by multiple columns without parentheses.
DThe database does not support ORDER BY with multiple columns.
Attempts:
2 left
💡 Hint
Check the data type of the columns used in ORDER BY.