0
0
SQLquery~20 mins

Why ordering matters in SQL - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ordering Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this SQL query with ORDER BY?

Consider a table Employees with columns id and name:

id | name
---+-------
1  | Alice
2  | Bob
3  | Charlie
4  | Diana

What is the result of this query?

SELECT name FROM Employees ORDER BY id DESC;
SQL
SELECT name FROM Employees ORDER BY id DESC;
A["Charlie", "Diana", "Alice", "Bob"]
B["Diana", "Charlie", "Bob", "Alice"]
C["Alice", "Bob", "Charlie", "Diana"]
D["Bob", "Alice", "Diana", "Charlie"]
Attempts:
2 left
💡 Hint

ORDER BY with DESC sorts from highest to lowest.

query_result
intermediate
2:00remaining
How does ordering affect LIMIT results?

Given the same Employees table, what does this query return?

SELECT name FROM Employees ORDER BY name ASC LIMIT 2;
SQL
SELECT name FROM Employees ORDER BY name ASC LIMIT 2;
A["Diana", "Charlie"]
B["Bob", "Alice"]
C["Charlie", "Diana"]
D["Alice", "Bob"]
Attempts:
2 left
💡 Hint

ORDER BY name ASC sorts names alphabetically from A to Z.

📝 Syntax
advanced
2:00remaining
Which query correctly orders by multiple columns?

Which SQL query orders the Employees table first by name ascending, then by id descending?

ASELECT * FROM Employees ORDER BY name ASC, id DESC;
BSELECT * FROM Employees ORDER BY id DESC, name ASC;
CSELECT * FROM Employees ORDER BY name DESC, id ASC;
DSELECT * FROM Employees ORDER BY id ASC, name DESC;
Attempts:
2 left
💡 Hint

Ordering columns are listed in the order of priority.

🧠 Conceptual
advanced
2:00remaining
Why does ordering matter in aggregate queries?

Consider this query:

SELECT department, COUNT(*) FROM Employees GROUP BY department ORDER BY COUNT(*) DESC;

Why is the ORDER BY clause important here?

AIt sorts departments by the number of employees, showing the largest first.
BIt filters out departments with fewer employees.
CIt groups employees by department before counting.
DIt changes the count to a sum of employee IDs.
Attempts:
2 left
💡 Hint

Think about what ordering by an aggregate function does.

🔧 Debug
expert
2:00remaining
What error occurs with this ORDER BY usage?

Given the Employees table, what error does this query produce?

SELECT name FROM Employees ORDER BY salary;

Assume the salary column does not exist in the table.

SQL
SELECT name FROM Employees ORDER BY salary;
ANo error, query runs and returns all names unordered.
BSyntaxError: Incorrect SQL syntax near 'salary'.
CRuntime error: Column 'salary' does not exist.
DWarning: Ignored unknown column 'salary' and returned unordered results.
Attempts:
2 left
💡 Hint

Ordering by a non-existent column causes an error.