0
0
MySQLquery~20 mins

ORDER BY single column 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 of this ORDER BY query?

Given the table Employees with columns id, name, and salary, what will be the order of name after running this query?

SELECT name FROM Employees ORDER BY salary ASC;
MySQL
CREATE TABLE Employees (id INT, name VARCHAR(20), salary INT);
INSERT INTO Employees VALUES (1, 'Alice', 5000), (2, 'Bob', 3000), (3, 'Charlie', 4000);
A["Alice", "Charlie", "Bob"]
B["Alice", "Bob", "Charlie"]
C["Charlie", "Bob", "Alice"]
D["Bob", "Charlie", "Alice"]
Attempts:
2 left
💡 Hint

ORDER BY salary ASC sorts salaries from smallest to largest.

📝 Syntax
intermediate
2:00remaining
Which query has a syntax error in ORDER BY clause?

Identify the query that will cause a syntax error when ordering by a single column.

ASELECT * FROM Products ORDER BY;
BSELECT * FROM Products ORDER BY name ASC;
CSELECT * FROM Products ORDER BY price DESC;
DSELECT * FROM Products ORDER BY id;
Attempts:
2 left
💡 Hint

ORDER BY must be followed by a column name or expression.

optimization
advanced
2:00remaining
Which query is most efficient to order by a single indexed column?

Assuming Orders table has an index on order_date, which query will best use the index to order results?

ASELECT * FROM Orders ORDER BY order_date DESC;
BSELECT * FROM Orders ORDER BY order_date ASC;
CSELECT * FROM Orders ORDER BY order_date + INTERVAL 1 DAY;
DSELECT * FROM Orders ORDER BY YEAR(order_date);
Attempts:
2 left
💡 Hint

Ordering by the indexed column directly is faster than using functions on it.

🔧 Debug
advanced
2:00remaining
Why does this ORDER BY query return unexpected results?

Given this query:

SELECT product_name FROM Products ORDER BY price;

Prices are stored as VARCHAR, not numeric. What is the cause of unexpected order?

APrices are sorted as strings, so '100' comes before '20'.
BORDER BY cannot sort VARCHAR columns.
CThe query is missing ASC or DESC keyword.
DThe table lacks a primary key causing order issues.
Attempts:
2 left
💡 Hint

Think about how strings are sorted compared to numbers.

🧠 Conceptual
expert
2:00remaining
What is the effect of ORDER BY on query result without LIMIT?

Consider a table Customers with 1000 rows. What does ORDER BY last_name do when used without LIMIT?

AReturns rows in random order because LIMIT is missing.
BReturns only the first row sorted by last_name.
CReturns all rows sorted by last_name in ascending order.
DReturns rows sorted by last_name but only if an index exists.
Attempts:
2 left
💡 Hint

ORDER BY affects the order of all rows returned.