Bird
Raised Fist0
DBMS Theoryknowledge~20 mins

SELECT with WHERE, ORDER BY, GROUP BY in DBMS Theory - Practice Problems & Coding Challenges

Choose your learning style10 modes available
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
intermediate
2: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);
A[('Eve', 75000), ('Alice', 70000), ('Charlie', 60000)]
B[('Charlie', 60000), ('Alice', 70000), ('Eve', 75000)]
C[('Bob', 50000), ('David', 80000), ('Eve', 75000)]
D[('Alice', 70000), ('Charlie', 60000), ('Eve', 75000)]
Attempts:
2 left
💡 Hint
Focus on filtering by department 'Sales' and ordering salaries from highest to lowest.
❓ query_result
intermediate
2: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);
A4
B3
C1
D2
Attempts:
2 left
💡 Hint
Sum amounts per customer and count how many sums exceed 1000.
📋 Factual
advanced
2: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));
ASELECT department, COUNT(*) FROM Employees GROUP BY department;
BSELECT department, COUNT(*) FROM Employees GROUP BY;
CSELECT department, COUNT(*) FROM Employees GROUP BY department ORDER BY COUNT(*) DESC;
DSELECT department, COUNT(*) FROM Employees GROUP BY department, name;
Attempts:
2 left
💡 Hint
GROUP BY must specify at least one column.
❓ optimization
advanced
2: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)
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);
ACorrect and efficient: filters after grouping using HAVING
BIncorrect: filters individual sales > 5000 before grouping, missing some totals
CCorrect but less efficient: uses subquery to filter products
DSyntax error: WHERE cannot be used after GROUP BY
Attempts:
2 left
💡 Hint
Filtering aggregated results requires HAVING, not WHERE.
🧠 Conceptual
expert
2:00remaining
Understanding ORDER BY with GROUP BY and aggregate functions
Given the table Transactions with columns 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);
AError: ORDER BY amount invalid because amount is not selected or grouped
BSyntax error: ORDER BY must come after GROUP BY
CCorrect: groups by customer and orders by total amount descending
DIncorrect: does not group or aggregate totals per customer
Attempts:
2 left
💡 Hint
Remember the order of clauses: GROUP BY before ORDER BY, and ORDER BY can use aggregate functions.

Practice

(1/5)
1. What does the WHERE clause do in a SQL SELECT statement?
easy
A. Filters rows based on a condition
B. Groups rows with the same values
C. Sorts the result set
D. Joins two tables together

Solution

  1. Step 1: Understand the role of WHERE clause

    The WHERE clause filters rows that meet a specific condition before any grouping or sorting.
  2. Step 2: Differentiate from GROUP BY and ORDER BY

    GROUP BY groups rows, ORDER BY sorts rows, but WHERE filters rows.
  3. Final Answer:

    Filters rows based on a condition -> Option A
  4. Quick Check:

    WHERE = Filters rows [OK]
Hint: WHERE filters rows before grouping or sorting [OK]
Common Mistakes:
  • Confusing WHERE with GROUP BY
  • Thinking WHERE sorts data
  • Assuming WHERE joins tables
2. Which of the following is the correct syntax to select all columns from a table named Employees where the Department is 'Sales'?
easy
A. SELECT * FROM Employees WHERE Department = 'Sales';
B. SELECT * Employees WHERE Department == 'Sales';
C. SELECT * FROM Employees WHERE Department := 'Sales';
D. SELECT * FROM Employees WHERE Department = Sales;

Solution

  1. Step 1: Check correct SELECT syntax

    The correct syntax uses SELECT * FROM table WHERE condition with single equals and quotes around string values.
  2. Step 2: Identify syntax errors in other options

    SELECT * Employees WHERE Department == 'Sales'; uses '==' which is invalid in SQL; SELECT * FROM Employees WHERE Department := 'Sales'; uses ':=' which is not standard; SELECT * FROM Employees WHERE Department = Sales; misses quotes around 'Sales'.
  3. Final Answer:

    SELECT * FROM Employees WHERE Department = 'Sales'; -> Option A
  4. Quick Check:

    Correct WHERE syntax uses = and quotes for strings [OK]
Hint: Use single = and quotes for string in WHERE [OK]
Common Mistakes:
  • Using == instead of =
  • Missing quotes around string values
  • Wrong assignment operator :=
3. Given the table Sales with columns Region and Amount, what will this query return?
SELECT Region, SUM(Amount) FROM Sales WHERE Amount > 100 GROUP BY Region ORDER BY SUM(Amount) DESC;
medium
A. Sum of all sales amounts regardless of region
B. All sales records with amount over 100, sorted by region
C. Total sales amount per region for sales over 100, sorted highest to lowest
D. Syntax error due to ORDER BY on aggregate

Solution

  1. Step 1: Analyze WHERE and GROUP BY

    WHERE filters rows with Amount > 100, GROUP BY groups rows by Region, SUM(Amount) calculates total per group.
  2. Step 2: Understand ORDER BY with aggregate

    ORDER BY SUM(Amount) DESC sorts groups by total sales descending.
  3. Final Answer:

    Total sales amount per region for sales over 100, sorted highest to lowest -> Option C
  4. Quick Check:

    WHERE filters, GROUP BY groups, ORDER BY sorts aggregates [OK]
Hint: ORDER BY can sort aggregates after GROUP BY [OK]
Common Mistakes:
  • Thinking ORDER BY on aggregate causes error
  • Ignoring WHERE filter before grouping
  • Confusing sorting by region instead of sum
4. Identify the error in this SQL query:
SELECT Department, COUNT(*) FROM Employees ORDER BY Department WHERE COUNT(*) > 5 GROUP BY Department;
medium
A. Missing semicolon at the end
B. ORDER BY appears before WHERE and GROUP BY, causing syntax error
C. WHERE cannot filter on aggregate functions like COUNT(*)
D. COUNT(*) cannot be used with GROUP BY

Solution

  1. Step 1: Check SQL clause order

    The correct order is SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY. ORDER BY before WHERE is invalid.
  2. Step 2: Confirm other clauses

    COUNT(*) with GROUP BY is valid; filtering on aggregates requires HAVING, not WHERE; but main syntax error is clause order.
  3. Final Answer:

    ORDER BY appears before WHERE and GROUP BY, causing syntax error -> Option B
  4. Quick Check:

    Clause order matters: WHERE before GROUP BY, ORDER BY last [OK]
Hint: Remember SQL clause order: WHERE, GROUP BY, HAVING, ORDER BY [OK]
Common Mistakes:
  • Placing ORDER BY before WHERE
  • Using WHERE to filter aggregates instead of HAVING
  • Ignoring clause order rules
5. You have a table Orders with columns CustomerID, OrderDate, and Total. Write a query to find customers who placed more than 3 orders in 2023, showing their CustomerID and total number of orders, sorted by number of orders descending.
hard
A. SELECT CustomerID, COUNT(*) FROM Orders WHERE YEAR(OrderDate) == 2023 GROUP BY CustomerID HAVING COUNT(*) > 3 ORDER BY COUNT(*) DESC;
B. SELECT CustomerID, COUNT(*) FROM Orders GROUP BY CustomerID WHERE YEAR(OrderDate) = 2023 HAVING COUNT(*) > 3 ORDER BY COUNT(*) DESC;
C. SELECT CustomerID, COUNT(*) FROM Orders WHERE OrderDate >= '2023-01-01' AND OrderDate <= '2023-12-31' GROUP BY CustomerID HAVING COUNT(*) >= 3 ORDER BY COUNT(*) DESC;
D. SELECT CustomerID, COUNT(*) FROM Orders WHERE OrderDate BETWEEN '2023-01-01' AND '2023-12-31' GROUP BY CustomerID HAVING COUNT(*) > 3 ORDER BY COUNT(*) DESC;

Solution

  1. Step 1: Filter orders in 2023

    Use WHERE with OrderDate between '2023-01-01' and '2023-12-31' to select orders in 2023.
  2. Step 2: Group by CustomerID and filter groups

    GROUP BY CustomerID groups orders per customer; HAVING COUNT(*) > 3 filters customers with more than 3 orders.
  3. Step 3: Sort results

    ORDER BY COUNT(*) DESC sorts customers by number of orders from highest to lowest.
  4. Final Answer:

    SELECT CustomerID, COUNT(*) FROM Orders WHERE OrderDate BETWEEN '2023-01-01' AND '2023-12-31' GROUP BY CustomerID HAVING COUNT(*) > 3 ORDER BY COUNT(*) DESC; -> Option D
  5. Quick Check:

    Filter dates, group, HAVING filter, then ORDER BY count desc [OK]
Hint: Use HAVING to filter groups after GROUP BY [OK]
Common Mistakes:
  • Using WHERE to filter aggregates instead of HAVING
  • Incorrect date filtering syntax
  • Using >= 3 instead of > 3 for count condition
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard