0
0
MySQLquery~20 mins

ORDER BY multiple columns in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ORDER BY Master
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?

Consider the table Employees with columns Department and Salary. What will be the order of rows returned by this query?

SELECT * FROM Employees ORDER BY Department ASC, Salary DESC;
MySQL
CREATE TABLE Employees (Name VARCHAR(20), Department VARCHAR(20), Salary INT);
INSERT INTO Employees VALUES
('Alice', 'Sales', 5000),
('Bob', 'Sales', 6000),
('Charlie', 'HR', 5500),
('David', 'HR', 5200);

SELECT Name, Department, Salary FROM Employees ORDER BY Department ASC, Salary DESC;
ADavid (HR, 5200), Charlie (HR, 5500), Alice (Sales, 5000), Bob (Sales, 6000)
BCharlie (HR, 5500), David (HR, 5200), Bob (Sales, 6000), Alice (Sales, 5000)
CAlice (Sales, 5000), Bob (Sales, 6000), Charlie (HR, 5500), David (HR, 5200)
DBob (Sales, 6000), Alice (Sales, 5000), David (HR, 5200), Charlie (HR, 5500)
Attempts:
2 left
💡 Hint

Rows are sorted first by Department alphabetically, then by Salary in descending order within each department.

🧠 Conceptual
intermediate
1:30remaining
How does ORDER BY multiple columns work?

When you use ORDER BY with multiple columns, how does the database sort the rows?

AIt sorts rows by the first column, then breaks ties by the second column, and so on.
BIt sorts rows only by the last column listed.
CIt sorts rows by all columns simultaneously without priority.
DIt randomly picks one column to sort by.
Attempts:
2 left
💡 Hint

Think about sorting a list of people by last name, then first name.

📝 Syntax
advanced
1:30remaining
Which query correctly orders by two columns with different directions?

Choose the correct SQL query that orders Products by Category ascending and Price descending.

ASELECT * FROM Products ORDER BY Category ASCENDING, Price DESC;
BSELECT * FROM Products ORDER BY Category, Price DESCENDING;
CSELECT * FROM Products ORDER BY Category ASC, Price DESC;
DSELECT * FROM Products ORDER BY Category ASC, Price DESCENDING;
Attempts:
2 left
💡 Hint

Check the correct keywords for ascending and descending order in SQL.

optimization
advanced
2:00remaining
How can indexing improve ORDER BY multiple columns?

You have a large table Orders with columns CustomerID and OrderDate. You often run:

SELECT * FROM Orders ORDER BY CustomerID ASC, OrderDate DESC;

Which index will best improve this query's performance?

ACREATE INDEX idx_customer_order ON Orders(CustomerID ASC, OrderDate DESC);
BCREATE INDEX idx_order ON Orders(OrderDate);
CCREATE INDEX idx_customer ON Orders(CustomerID);
DCREATE INDEX idx_order_customer ON Orders(OrderDate DESC, CustomerID ASC);
Attempts:
2 left
💡 Hint

Indexes should match the order of columns in the ORDER BY clause.

🔧 Debug
expert
2:30remaining
Why does this ORDER BY query return unexpected results?

Given the table Students with columns Name, Grade, and Age, a developer runs:

SELECT * FROM Students ORDER BY Grade, Age DESC;

But the results are not sorted by Grade ascending as expected. What is the most likely cause?

AThe database ignores the first column in ORDER BY if the second column has DESC.
BThe query syntax is invalid because <code>Age DESC</code> cannot be used with <code>Grade</code>.
CThe table has no data, so sorting does nothing.
DThe <code>Grade</code> column contains NULL values which are sorted first by default.
Attempts:
2 left
💡 Hint

Think about how NULL values affect sorting order.