What does the WHERE clause do in a SELECT statement?
The WHERE clause filters rows to include only those that meet a specified condition.
Click to reveal answer
beginner
How does the ORDER BY clause affect the result of a SELECT query?
The ORDER BY clause sorts the result rows by one or more columns, either ascending or descending.
Click to reveal answer
intermediate
What is the purpose of the GROUP BY clause in SQL?
The GROUP BY clause groups rows that have the same values in specified columns, often used with aggregate functions like COUNT or SUM.
Click to reveal answer
intermediate
Can you use WHERE and GROUP BY together? If yes, in what order?
Yes, you use WHERE first to filter rows, then GROUP BY to group the filtered rows.
Click to reveal answer
intermediate
What happens if you use ORDER BY after GROUP BY?
The query first groups rows, then sorts the grouped results according to the ORDER BY clause.
Click to reveal answer
Which clause filters rows before grouping in a SELECT query?
AGROUP BY
BORDER BY
CWHERE
DHAVING
✗ Incorrect
The WHERE clause filters rows before grouping. HAVING filters after grouping.
What does ORDER BY column_name DESC do?
ASorts rows by column_name in descending order
BFilters rows where column_name is DESC
CGroups rows by column_name
DSorts rows by column_name in ascending order
✗ Incorrect
DESC means descending order, so rows are sorted from highest to lowest.
Which clause is used to group rows with the same value in a column?
AWHERE
BGROUP BY
CORDER BY
DSELECT
✗ Incorrect
GROUP BY groups rows that share the same value in specified columns.
If you want to filter groups after grouping, which clause do you use?
AHAVING
BORDER BY
CGROUP BY
DWHERE
✗ Incorrect
HAVING filters groups after GROUP BY has been applied.
In which order are these clauses processed in a query: WHERE, GROUP BY, ORDER BY?
AORDER BY, WHERE, GROUP BY
BGROUP BY, ORDER BY, WHERE
CGROUP BY, WHERE, ORDER BY
DWHERE, GROUP BY, ORDER BY
✗ Incorrect
First WHERE filters rows, then GROUP BY groups them, then ORDER BY sorts the result.
Explain how the WHERE, GROUP BY, and ORDER BY clauses work together in a SELECT query.
Think about the order SQL processes these clauses.
You got /3 concepts.
Describe a real-life example where you would use WHERE, GROUP BY, and ORDER BY in a database query.
Imagine you want to see total sales per region, only for recent months, sorted by highest sales.
You got /4 concepts.
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
Step 1: Understand the role of WHERE clause
The WHERE clause filters rows that meet a specific condition before any grouping or sorting.
Step 2: Differentiate from GROUP BY and ORDER BY
GROUP BY groups rows, ORDER BY sorts rows, but WHERE filters rows.
Final Answer:
Filters rows based on a condition -> Option A
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
Step 1: Check correct SELECT syntax
The correct syntax uses SELECT * FROM table WHERE condition with single equals and quotes around string values.
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'.
Final Answer:
SELECT * FROM Employees WHERE Department = 'Sales'; -> Option A
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
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.
Step 2: Understand ORDER BY with aggregate
ORDER BY SUM(Amount) DESC sorts groups by total sales descending.
Final Answer:
Total sales amount per region for sales over 100, sorted highest to lowest -> Option C
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
Step 1: Check SQL clause order
The correct order is SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY. ORDER BY before WHERE is invalid.
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.
Final Answer:
ORDER BY appears before WHERE and GROUP BY, causing syntax error -> Option B
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
Step 1: Filter orders in 2023
Use WHERE with OrderDate between '2023-01-01' and '2023-12-31' to select orders in 2023.
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.
Step 3: Sort results
ORDER BY COUNT(*) DESC sorts customers by number of orders from highest to lowest.
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
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 unknownstrongwhile earning +1 Grip toward your certificate.