Challenge - 5 Problems
Ordering Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this ORDER BY query?
Given the table Employees with columns
id, name, and salary, what will be the order of name in the result of this query?SELECT name FROM Employees ORDER BY salary DESC;MySQL
CREATE TABLE Employees (id INT, name VARCHAR(20), salary INT); INSERT INTO Employees VALUES (1, 'Alice', 5000), (2, 'Bob', 7000), (3, 'Charlie', 6000);
Attempts:
2 left
💡 Hint
ORDER BY salary DESC sorts salaries from highest to lowest.
✗ Incorrect
The ORDER BY clause sorts the rows by salary in descending order, so the highest salary (Bob:7000) comes first, then Charlie (6000), then Alice (5000).
🧠 Conceptual
intermediate1:30remaining
Why does ORDER BY affect the result order?
Why does using
ORDER BY in a SQL query change the order of the rows returned?Attempts:
2 left
💡 Hint
Think about what sorting means in real life, like sorting books by title.
✗ Incorrect
ORDER BY sorts the rows based on specified columns before sending them back, so the output order changes but the data stays the same.
📝 Syntax
advanced2:30remaining
Which query correctly orders by two columns?
You want to order the
Employees table first by department ascending, then by salary descending. Which query is correct?MySQL
CREATE TABLE Employees (id INT, name VARCHAR(20), department VARCHAR(20), salary INT);
Attempts:
2 left
💡 Hint
Multiple columns in ORDER BY are separated by commas, each with its own direction.
✗ Incorrect
Option D correctly uses commas and specifies ASC and DESC for each column. Option D has invalid syntax with 'DESC ASC'. Option D misses a comma. Option D reverses the order directions.
❓ optimization
advanced2:30remaining
How does indexing affect ORDER BY performance?
You have a large table with millions of rows. You run
SELECT * FROM Sales ORDER BY sale_date DESC; but it is slow. How can adding an index help?Attempts:
2 left
💡 Hint
Think about how a phone book is sorted and how you find names quickly.
✗ Incorrect
An index on the column used in ORDER BY allows the database to retrieve rows in sorted order efficiently, avoiding a full sort operation.
🔧 Debug
expert3:00remaining
Why does this ORDER BY query return unexpected results?
You run this query:
But the results are not sorted by age as expected. What could cause this?
SELECT name, age FROM Users ORDER BY age;But the results are not sorted by age as expected. What could cause this?
MySQL
CREATE TABLE Users (name VARCHAR(20), age VARCHAR(3)); INSERT INTO Users VALUES ('Anna', '25'), ('Ben', '5'), ('Cara', '100');
Attempts:
2 left
💡 Hint
Check the data type of the column used in ORDER BY.
✗ Incorrect
Since age is stored as text, sorting is alphabetical: '100' comes before '25' and '5'. To sort numerically, age should be stored as a number type.