0
0
SQLquery~20 mins

ORDER BY with ASC and DESC in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ORDER BY Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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);
ABob, Charlie, Alice
BCharlie, Alice, Bob
CAlice, Bob, Charlie
DBob, Alice, Charlie
Attempts:
2 left
💡 Hint
ORDER BY salary ASC sorts from smallest to largest salary.
query_result
intermediate
2: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);
ACharlie, Alice, Bob
BCharlie, Bob, Alice
CBob, Alice, Charlie
DAlice, Bob, Charlie
Attempts:
2 left
💡 Hint
ORDER BY salary DESC sorts from largest to smallest salary.
📝 Syntax
advanced
2: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.
ASELECT * FROM Employees ORDER BY salary, name DESC ASC;
BSELECT * FROM Employees ORDER BY salary ASC, name DESC;
CSELECT * FROM Employees ORDER BY salary DESC, name DESC;
DSELECT * FROM Employees ORDER BY salary DESC, name ASC;
Attempts:
2 left
💡 Hint
Check the order and syntax of ASC and DESC keywords.
optimization
advanced
2: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?
ASELECT * FROM Employees ORDER BY name ASC, salary DESC;
BSELECT * FROM Employees ORDER BY salary ASC, name DESC;
CSELECT * FROM Employees ORDER BY salary DESC, name ASC;
DSELECT * FROM Employees ORDER BY salary DESC;
Attempts:
2 left
💡 Hint
Indexes are most efficient when query ORDER BY matches index order.
🧠 Conceptual
expert
3: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?
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'
A5 Elderberry, 3 Cherry, 2 Banana, 1 Apple, 4 Date
B5 Elderberry, 3 Cherry, 2 Banana, 4 Date, 1 Apple
C5 Elderberry, 2 Banana, 3 Cherry, 1 Apple, 4 Date
D5 Elderberry, 2 Banana, 3 Cherry, 4 Date, 1 Apple
Attempts:
2 left
💡 Hint
NULLS LAST puts NULL prices at the end; within same price, names are sorted descending.