Challenge - 5 Problems
ORDER BY Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output order of this query?
Given the table Employees with columns id, name, and salary, what will be the order of names returned by this query?
SELECT name FROM Employees ORDER BY salary ASC;
SQL
CREATE TABLE Employees (id INT, name VARCHAR(50), salary INT); INSERT INTO Employees VALUES (1, 'Alice', 5000), (2, 'Bob', 3000), (3, 'Charlie', 7000);
Attempts:
2 left
💡 Hint
ORDER BY salary ASC sorts from smallest to largest salary.
✗ Incorrect
The ORDER BY salary ASC sorts salaries from lowest to highest. Bob has 3000, Alice 5000, Charlie 7000, so the order is Bob, Alice, Charlie.
❓ query_result
intermediate2:00remaining
What is the output order of this query with DESC?
Using the same Employees table, what will be the order of names returned by this query?
SELECT name FROM Employees ORDER BY salary DESC;
SQL
CREATE TABLE Employees (id INT, name VARCHAR(50), salary INT); INSERT INTO Employees VALUES (1, 'Alice', 5000), (2, 'Bob', 3000), (3, 'Charlie', 7000);
Attempts:
2 left
💡 Hint
ORDER BY salary DESC sorts from largest to smallest salary.
✗ Incorrect
ORDER BY salary DESC sorts salaries from highest to lowest. Charlie has 7000, Alice 5000, Bob 3000, so the order is Charlie, Alice, Bob.
📝 Syntax
advanced2:00remaining
Which query will cause a syntax error?
Identify which of these SQL queries will cause a syntax error when ordering by two columns with different directions.
Attempts:
2 left
💡 Hint
Check the order and syntax of ASC and DESC keywords.
✗ Incorrect
Option A has 'name DESC ASC' which is invalid syntax because you cannot specify both DESC and ASC for the same column.
❓ optimization
advanced2:00remaining
Which query is more efficient for sorting by salary descending and then name ascending?
Assuming an index exists on (salary DESC, name ASC), which query will best use the index for sorting?
Attempts:
2 left
💡 Hint
Indexes are most efficient when query ORDER BY matches index order.
✗ Incorrect
Option C matches the index order exactly, so it can use the index efficiently for sorting.
🧠 Conceptual
expert3:00remaining
What is the result of this query with mixed ASC and DESC on NULL values?
Consider a table Products with columns id, price (nullable), and name. What is the order of rows returned by this query?
Assume the data:
1, NULL, 'Apple'
2, 10, 'Banana'
3, 10, 'Cherry'
4, NULL, 'Date'
5, 5, 'Elderberry'
SELECT * FROM Products ORDER BY price ASC NULLS LAST, name DESC;
Assume the data:
1, NULL, 'Apple'
2, 10, 'Banana'
3, 10, 'Cherry'
4, NULL, 'Date'
5, 5, 'Elderberry'
Attempts:
2 left
💡 Hint
NULLS LAST puts NULL prices at the end; within same price, names are sorted descending.
✗ Incorrect
Rows with price NULL come last. Among prices 5 and 10, 5 comes first. For price 10, names sorted descending: Cherry then Banana. NULL prices last with names descending: Date then Apple.