0
0
MySQLquery~20 mins

ASC and DESC direction in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sorting 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?
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;
ANames ordered from lowest salary to highest salary
BNames in random order
CNames ordered alphabetically by name
DNames ordered from highest salary to lowest salary
Attempts:
2 left
💡 Hint
ASC means ascending order, from smallest to largest.
query_result
intermediate
2: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;
AProduct IDs ordered from lowest price to highest price
BProduct IDs ordered randomly
CProduct IDs ordered by product_id ascending
DProduct IDs ordered from highest price to lowest price
Attempts:
2 left
💡 Hint
DESC means descending order, from largest to smallest.
📝 Syntax
advanced
2: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?
ASELECT * FROM Orders ORDER BY customer_id, order_date DESC ASC;
BSELECT * FROM Orders ORDER BY customer_id ASC, order_date DESC;
CSELECT * FROM Orders ORDER BY customer_id DESC, order_date ASC;
DSELECT * FROM Orders ORDER BY customer_id ASC order_date DESC;
Attempts:
2 left
💡 Hint
Separate columns with commas and specify direction after each column.
optimization
advanced
2:00remaining
How to optimize sorting large data with ASC and DESC?
You have a large table Sales with millions of rows. You often run:
SELECT * FROM Sales ORDER BY sale_date DESC;

What is the best way to optimize this query?
ARemove ORDER BY clause to speed up query
BCreate an index on sale_date in ascending order
CCreate an index on sale_date in descending order
DUse GROUP BY sale_date instead of ORDER BY
Attempts:
2 left
💡 Hint
Indexes help sorting if they match the order direction.
🧠 Conceptual
expert
2:00remaining
What is the result of mixing ASC and DESC in ORDER BY with NULL values?
Consider a table Items with a column 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?
ARows with NULL prices appear last, then prices in ascending order; names sorted descending within same price
BRows with NULL prices appear first, then prices in ascending order; names sorted descending within same price
CRows with NULL prices appear randomly mixed in the result
DQuery causes an error because NULLs cannot be sorted with ASC and DESC
Attempts:
2 left
💡 Hint
MySQL sorts NULLs last by default in ascending order.