Challenge - 5 Problems
Master of SELECT with WHERE, ORDER BY, GROUP BY
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of SELECT with WHERE and ORDER BY
Given the table Employees with columns
id, name, department, and salary, what is the output of this query?SELECT name, salary FROM Employees WHERE department = 'Sales' ORDER BY salary DESC;
DBMS Theory
CREATE TABLE Employees (id INT, name VARCHAR(50), department VARCHAR(50), salary INT); INSERT INTO Employees VALUES (1, 'Alice', 'Sales', 70000), (2, 'Bob', 'HR', 50000), (3, 'Charlie', 'Sales', 60000), (4, 'David', 'IT', 80000), (5, 'Eve', 'Sales', 75000);
Attempts:
2 left
💡 Hint
Focus on filtering by department 'Sales' and ordering salaries from highest to lowest.
✗ Incorrect
The query filters employees in the 'Sales' department and orders them by salary descending. Eve has the highest salary (75000), followed by Alice (70000), then Charlie (60000).
❓ query_result
intermediate2:00remaining
Result count with GROUP BY and HAVING
Consider the table Orders with columns
order_id, customer_id, and amount. What is the number of customers who have total order amount greater than 1000?SELECT COUNT(*) FROM (SELECT customer_id, SUM(amount) AS total FROM Orders GROUP BY customer_id HAVING total > 1000) AS subquery;
DBMS Theory
CREATE TABLE Orders (order_id INT, customer_id INT, amount INT); INSERT INTO Orders VALUES (1, 101, 500), (2, 102, 1200), (3, 101, 600), (4, 103, 700), (5, 102, 300), (6, 104, 400);
Attempts:
2 left
💡 Hint
Sum amounts per customer and count how many sums exceed 1000.
✗ Incorrect
Customer 101 has total 1100 (500+600), customer 102 has 1500 (1200+300), customer 103 has 700, and customer 104 has 400. Only customers 101 and 102 exceed 1000, so count is 2.
📋 Factual
advanced2:00remaining
Identify the syntax error in GROUP BY query
Which option contains a syntax error in this SQL query?
SELECT department, COUNT(*) FROM Employees GROUP BY;
DBMS Theory
CREATE TABLE Employees (id INT, name VARCHAR(50), department VARCHAR(50));
Attempts:
2 left
💡 Hint
GROUP BY must specify at least one column.
✗ Incorrect
Option B ends with GROUP BY without specifying any column, which is a syntax error. GROUP BY requires at least one column name.
❓ optimization
advanced2:00remaining
Optimize query with WHERE and GROUP BY
You want to find the total sales per product only for products with sales above 5000. Which query is more efficient?
Options:
A)
B)
C)
D)
Options:
A)
SELECT product_id, SUM(sales) FROM Sales GROUP BY product_id HAVING SUM(sales) > 5000;
B)
SELECT product_id, SUM(sales) FROM Sales WHERE sales > 5000 GROUP BY product_id;
C)
SELECT product_id, SUM(sales) FROM Sales WHERE product_id IN (SELECT product_id FROM Sales GROUP BY product_id HAVING SUM(sales) > 5000) GROUP BY product_id;
D)
SELECT product_id, SUM(sales) FROM Sales GROUP BY product_id WHERE SUM(sales) > 5000;
DBMS Theory
CREATE TABLE Sales (sale_id INT, product_id INT, sales INT);
Attempts:
2 left
💡 Hint
Filtering aggregated results requires HAVING, not WHERE.
✗ Incorrect
Option A correctly filters groups after aggregation using HAVING. Option A filters rows before aggregation, missing groups with total > 5000 but individual sales ≤ 5000. Option A works but is less efficient due to subquery. Option A is invalid syntax.
🧠 Conceptual
expert2:00remaining
Understanding ORDER BY with GROUP BY and aggregate functions
Given the table Transactions with columns
Options:
A)
B)
C)
D)
transaction_id, customer_id, and amount, which query will list customers with their total transaction amount, ordered by total amount descending?Options:
A)
SELECT customer_id, SUM(amount) FROM Transactions ORDER BY SUM(amount) DESC GROUP BY customer_id;
B)
SELECT customer_id, SUM(amount) FROM Transactions GROUP BY customer_id ORDER BY SUM(amount) DESC;
C)
SELECT customer_id, SUM(amount) FROM Transactions GROUP BY customer_id ORDER BY amount DESC;
D)
SELECT customer_id, SUM(amount) FROM Transactions ORDER BY amount DESC;
DBMS Theory
CREATE TABLE Transactions (transaction_id INT, customer_id INT, amount INT);
Attempts:
2 left
💡 Hint
Remember the order of clauses: GROUP BY before ORDER BY, and ORDER BY can use aggregate functions.
✗ Incorrect
Option C is correct syntax and logic: it groups by customer_id, sums amount, then orders by that sum descending. Option C has wrong clause order. Options C and D fail to properly aggregate and order by total amount.