0
0
SQLquery~20 mins

GROUP BY with ORDER BY in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GROUP BY with ORDER BY Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of GROUP BY with ORDER BY on aggregated data
Given the table Sales with columns product and quantity, what is the output of this query?
SELECT product, SUM(quantity) AS total_quantity
FROM Sales
GROUP BY product
ORDER BY total_quantity DESC;
SQL
CREATE TABLE Sales (product VARCHAR(20), quantity INT);
INSERT INTO Sales VALUES ('Apple', 10), ('Banana', 5), ('Apple', 15), ('Banana', 10), ('Cherry', 7);
A[{"product": "Apple", "total_quantity": 25}, {"product": "Banana", "total_quantity": 15}, {"product": "Cherry", "total_quantity": 7}]
B[{"product": "Banana", "total_quantity": 15}, {"product": "Apple", "total_quantity": 25}, {"product": "Cherry", "total_quantity": 7}]
C[{"product": "Cherry", "total_quantity": 7}, {"product": "Banana", "total_quantity": 15}, {"product": "Apple", "total_quantity": 25}]
D[{"product": "Apple", "total_quantity": 25}, {"product": "Cherry", "total_quantity": 7}, {"product": "Banana", "total_quantity": 15}]
Attempts:
2 left
💡 Hint
Remember that ORDER BY sorts the results after grouping and aggregation.
query_result
intermediate
2:00remaining
Effect of ORDER BY on GROUP BY with multiple columns
Consider the table Orders with columns customer, product, and amount. What is the output of this query?
SELECT customer, product, SUM(amount) AS total_amount
FROM Orders
GROUP BY customer, product
ORDER BY customer ASC, total_amount DESC;
SQL
CREATE TABLE Orders (customer VARCHAR(20), product VARCHAR(20), amount INT);
INSERT INTO Orders VALUES ('Alice', 'Pen', 10), ('Alice', 'Pen', 5), ('Alice', 'Notebook', 7), ('Bob', 'Pen', 3), ('Bob', 'Notebook', 8);
A[{"customer": "Bob", "product": "Notebook", "total_amount": 8}, {"customer": "Bob", "product": "Pen", "total_amount": 3}, {"customer": "Alice", "product": "Pen", "total_amount": 15}, {"customer": "Alice", "product": "Notebook", "total_amount": 7}]
B[{"customer": "Alice", "product": "Pen", "total_amount": 15}, {"customer": "Alice", "product": "Notebook", "total_amount": 7}, {"customer": "Bob", "product": "Pen", "total_amount": 3}, {"customer": "Bob", "product": "Notebook", "total_amount": 8}]
C[{"customer": "Alice", "product": "Notebook", "total_amount": 7}, {"customer": "Alice", "product": "Pen", "total_amount": 15}, {"customer": "Bob", "product": "Notebook", "total_amount": 8}, {"customer": "Bob", "product": "Pen", "total_amount": 3}]
D[{"customer": "Alice", "product": "Pen", "total_amount": 15}, {"customer": "Alice", "product": "Notebook", "total_amount": 7}, {"customer": "Bob", "product": "Notebook", "total_amount": 8}, {"customer": "Bob", "product": "Pen", "total_amount": 3}]
Attempts:
2 left
💡 Hint
ORDER BY sorts first by customer ascending, then by total_amount descending within each customer.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in GROUP BY with ORDER BY
Which option contains a syntax error in this SQL query?
SELECT department, COUNT(*) AS emp_count
FROM Employees
GROUP BY department
ORDER BY emp_count DESC;
ASELECT department, COUNT(*) AS emp_count FROM Employees GROUP BY department ORDER BY emp_count DESC;
BSELECT department, COUNT(*) AS emp_count FROM Employees GROUP BY department ORDER BY COUNT(*) DESC;
CSELECT department, COUNT(*) AS emp_count FROM Employees GROUP BY department ORDER BY department DESC;
DSELECT department, COUNT(*) AS emp_count FROM Employees GROUP BY department ORDER BY emp_count;
Attempts:
2 left
💡 Hint
ORDER BY cannot use aggregate functions directly unless in SELECT or GROUP BY.
optimization
advanced
2:00remaining
Optimizing GROUP BY with ORDER BY on large data
You have a large table Transactions with columns category and amount. Which query is the most efficient to get total amount per category ordered by total amount descending?
ASELECT category, SUM(amount) AS total_amount FROM Transactions GROUP BY category;
BSELECT category, SUM(amount) AS total_amount FROM Transactions ORDER BY total_amount DESC GROUP BY category;
CSELECT category, SUM(amount) AS total_amount FROM Transactions GROUP BY category ORDER BY total_amount DESC;
DSELECT category, SUM(amount) AS total_amount FROM Transactions ORDER BY SUM(amount) DESC;
Attempts:
2 left
💡 Hint
GROUP BY must come before ORDER BY in SQL syntax.
🧠 Conceptual
expert
2:00remaining
Understanding ORDER BY with GROUP BY and NULL values
Given a table Employees with columns team and salary, some team values are NULL. What is the output order of this query?
SELECT team, AVG(salary) AS avg_salary
FROM Employees
GROUP BY team
ORDER BY avg_salary ASC;

Assuming teams with NULL are grouped together, how does ORDER BY handle NULL avg_salary values?
ARows with NULL avg_salary appear last in ascending order.
BRows with NULL avg_salary appear first in ascending order.
CRows with NULL avg_salary are excluded from the result.
DRows with NULL avg_salary appear randomly in the order.
Attempts:
2 left
💡 Hint
By default, NULLs sort last in ascending order in most SQL databases.