Challenge - 5 Problems
Sorting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output order of this query?
Consider a 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 ASC;
MySQL
SELECT name FROM Employees ORDER BY salary ASC;
Attempts:
2 left
💡 Hint
ASC means ascending order, from smallest to largest.
✗ Incorrect
The ORDER BY clause with ASC sorts the rows by the salary column from the smallest value to the largest. So names appear in order of increasing salary.
❓ query_result
intermediate2:00remaining
What does DESC do in this query?
Given a table Products with columns
product_id and price, what will be the order of product_id in this query?SELECT product_id FROM Products ORDER BY price DESC;
MySQL
SELECT product_id FROM Products ORDER BY price DESC;
Attempts:
2 left
💡 Hint
DESC means descending order, from largest to smallest.
✗ Incorrect
ORDER BY price DESC sorts the products by price from the highest value to the lowest. So product IDs appear in order of decreasing price.
📝 Syntax
advanced2:00remaining
Which query correctly orders by two columns with different directions?
You want to order a table Orders by
customer_id ascending and then by order_date descending. Which query is correct?Attempts:
2 left
💡 Hint
Separate columns with commas and specify direction after each column.
✗ Incorrect
Option B correctly orders by customer_id ascending and then order_date descending. Option B reverses directions. Option B has invalid syntax with DESC ASC. Option B misses a comma.
❓ optimization
advanced2:00remaining
How to optimize sorting large data with ASC and DESC?
You have a large table Sales with millions of rows. You often run:
What is the best way to optimize this query?
SELECT * FROM Sales ORDER BY sale_date DESC;
What is the best way to optimize this query?
Attempts:
2 left
💡 Hint
Indexes help sorting if they match the order direction.
✗ Incorrect
Creating an index on sale_date descending helps the database quickly retrieve rows in descending order without sorting all rows. Ascending index won't help for DESC order. Removing ORDER BY changes results. GROUP BY is for aggregation, not sorting.
🧠 Conceptual
expert2:00remaining
What is the result of mixing ASC and DESC in ORDER BY with NULL values?
Consider a table Items with a column
Assuming default NULL sorting behavior in MySQL, where do NULL prices appear?
price that contains some NULL values. What is the order of rows when running:SELECT * FROM Items ORDER BY price ASC, name DESC;
Assuming default NULL sorting behavior in MySQL, where do NULL prices appear?
Attempts:
2 left
💡 Hint
MySQL sorts NULLs last by default in ascending order.
✗ Incorrect
In MySQL, when ordering ascending, NULL values appear last. So rows with NULL price come after all non-NULL prices. Within same price, names are sorted descending. The query runs without error.