Challenge - 5 Problems
ORDER BY Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this query ordering by age ascending?
Given the table People with columns
id, name, and age, what will be the order of names after running this query?SELECT name FROM People ORDER BY age ASC;
SQL
CREATE TABLE People (id INT, name VARCHAR(20), age INT); INSERT INTO People VALUES (1, 'Alice', 30), (2, 'Bob', 25), (3, 'Charlie', 35);
Attempts:
2 left
💡 Hint
ORDER BY age ASC sorts from smallest to largest age.
✗ Incorrect
The query orders the rows by the age column in ascending order, so the names appear from youngest to oldest: Bob (25), Alice (30), Charlie (35).
❓ query_result
intermediate2:00remaining
What is the output order when ordering by salary descending?
Consider the table Employees with columns
emp_id, emp_name, and salary. What is the order of emp_name after running this query?SELECT emp_name FROM Employees ORDER BY salary DESC;
SQL
CREATE TABLE Employees (emp_id INT, emp_name VARCHAR(20), salary INT); INSERT INTO Employees VALUES (1, 'John', 5000), (2, 'Jane', 7000), (3, 'Doe', 6000);
Attempts:
2 left
💡 Hint
ORDER BY salary DESC sorts from highest to lowest salary.
✗ Incorrect
The query sorts employees by salary in descending order, so the highest salary comes first: Jane (7000), Doe (6000), John (5000).
📝 Syntax
advanced2:00remaining
Which query will cause a syntax error?
Identify the query that will cause a syntax error when ordering by the column
score.Attempts:
2 left
💡 Hint
Check the valid keywords for sorting order in SQL.
✗ Incorrect
The keyword 'ASCENDING' is not valid in SQL. The correct keyword is 'ASC'. Options A, B, and C are valid syntax.
❓ optimization
advanced2:00remaining
Which query is more efficient for ordering by a single indexed column?
Given a large table Sales with an index on the
date column, which query will likely perform best when ordering by date?Attempts:
2 left
💡 Hint
Using the indexed column directly helps the database use the index.
✗ Incorrect
Ordering by the indexed column directly (option B) allows the database to use the index efficiently. Options A and D apply expressions on the column, preventing index use. Option B is also efficient but descending order may require scanning index in reverse; still efficient but option B is the simplest ascending order.
🧠 Conceptual
expert2:00remaining
What happens if you ORDER BY a column with NULL values?
Consider a table Products with a column
price that contains some NULL values. What is the default behavior of ORDER BY price ASC regarding NULLs?Attempts:
2 left
💡 Hint
Think about how SQL treats NULLs in sorting by default.
✗ Incorrect
By default, when ordering ascending, NULL values appear last because NULL is treated as unknown and considered greater than any non-NULL value. They are not excluded or cause errors.