Challenge - 5 Problems
ORDER BY Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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;
Attempts:
2 left
💡 Hint
ORDER BY sorts first by the first column, then by the second within each group.
✗ Incorrect
The query sorts rows first by Department alphabetically ascending: HR, IT, Sales. Within each Department, Salary is sorted descending. So HR salaries 6000 then 5500, IT only one row 8000, Sales 7000 then 5000.
🧠 Conceptual
intermediate2:00remaining
Understanding ORDER BY with NULL values
Consider a table Products with columns Category and Price. If you run:
and some Price values are NULL, where will those NULLs appear in the sorted result?
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?
Attempts:
2 left
💡 Hint
Think about how NULLs are treated in descending order sorting.
✗ Incorrect
In SQL, NULLs are treated as unknown and usually appear last when sorting descending. So within each Category, rows with NULL Price come after rows with actual prices.
📝 Syntax
advanced2: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?
Attempts:
2 left
💡 Hint
Check the punctuation between columns in ORDER BY.
✗ Incorrect
Option D is missing a comma between 'Age ASC' and 'Name DESC', causing a syntax error. Option D has a semicolon which ends the statement early, also invalid but not in ORDER BY clause itself.
❓ optimization
advanced2:00remaining
Optimizing ORDER BY with multiple columns
You have a large table Sales with columns Region, SalesDate, and Amount. You often run:
Which index will best improve the performance of this query?
SELECT * FROM Sales ORDER BY Region ASC, SalesDate DESC;
Which index will best improve the performance of this query?
Attempts:
2 left
💡 Hint
Indexes should match the ORDER BY column order and directions.
✗ Incorrect
An index on (Region ASC, SalesDate DESC) matches the ORDER BY clause exactly, allowing the database to efficiently retrieve rows in the desired order without extra sorting.
🔧 Debug
expert2:00remaining
Why does this ORDER BY query return unexpected results?
You run this query:
but the results are not sorted as expected by Score ascending and then Name ascending. What is the most likely cause?
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?
Attempts:
2 left
💡 Hint
Check the data type of the columns used in ORDER BY.
✗ Incorrect
If Score is stored as text, sorting will be alphabetical (e.g., '10' before '2'), causing unexpected order. Numeric sorting requires Score to be a numeric type.