Sample questions
Question 1Easy
Which departments does this query return?
employees
id | name | dept | salary | manager_id
1 | Asha | Sales | 60000 | NULL
2 | Ravi | Sales | 45000 | 1
3 | Meera | HR | 50000 | 1
4 | John | IT | 70000 | NULL
5 | Kiran | IT | 45000 | 4
SELECT dept
FROM employees
GROUP BY dept
HAVING COUNT(*) > 1
ORDER BY dept;
- IT, Sales
- HR, IT, Sales
- HR
- Sales
Answer: A. IT, Sales
Explanation
HAVING keeps only the groups with more than one row. IT and Sales have 2 employees each and HR has 1.
Step-by-step solution
- Group sizes: HR 1, IT 2, Sales 2.
- HAVING COUNT(*) > 1 keeps the groups with 2 rows.
- IT and Sales, sorted: IT, Sales (option A).
Why the other options are wrong
- B. HR has one employee, so it fails COUNT(*) > 1.
- C. HR is the only group that fails; IT and Sales pass.
- D. IT also has two employees, so it passes and must be returned.
Tip: HAVING COUNT(*) > 1 finds duplicates by a grouped column.
Question 2Easy
What is the difference between WHERE and HAVING?
- HAVING filters rows before grouping; WHERE filters groups after
- There is no difference; they are interchangeable in any query
- HAVING can only be used together with ORDER BY, not GROUP BY
- WHERE filters rows before grouping; HAVING filters groups after
Answer: D. WHERE filters rows before grouping; HAVING filters groups after
Explanation
Aggregate conditions such as COUNT(*) > 1 must go in HAVING, because WHERE runs before the groups and their aggregates exist.
Step-by-step solution
- WHERE filters individual rows before GROUP BY runs.
- HAVING filters whole groups after aggregation, so it can use COUNT or SUM.
- So option D is the difference.
Why the other options are wrong
- A. This reverses the two clauses: WHERE filters rows first, HAVING filters groups after.
- B. They are not interchangeable; aggregate conditions need HAVING.
- C. HAVING is used together with GROUP BY, and that is its normal use.
Tip: WHERE for row conditions, HAVING for conditions on COUNT, SUM or AVG.
Question 3Easy
What does this query return? (columns: dept, count)
employees
id | name | dept | salary | manager_id
1 | Asha | Sales | 60000 | NULL
2 | Ravi | Sales | 45000 | 1
3 | Meera | HR | 50000 | 1
4 | John | IT | 70000 | NULL
5 | Kiran | IT | 45000 | 4
SELECT dept, COUNT(*)
FROM employees
GROUP BY dept
ORDER BY dept;
- HR, 1; IT, 1; Sales, 1
- HR, 5; IT, 5; Sales, 5
- Sales, 2; IT, 2; HR, 1
- HR, 1; IT, 2; Sales, 2
Answer: D. HR, 1; IT, 2; Sales, 2
Explanation
Rows are grouped by department and counted: HR has 1 employee, IT 2 and Sales 2. ORDER BY dept sorts the groups alphabetically.
Step-by-step solution
- Groups by dept: HR has Meera (1 row); IT has John and Kiran (2 rows); Sales has Asha and Ravi (2 rows).
- ORDER BY dept sorts alphabetically: HR, IT, Sales.
- Result: HR 1; IT 2; Sales 2 (option D).
Why the other options are wrong
- A. IT and Sales each have 2 employees, not 1.
- B. COUNT(*) per group gives each department's own count, not 5.
- C. HR has 1 employee and comes first alphabetically, so the order and counts are wrong.
Tip: COUNT(*) with GROUP BY counts rows per group; ORDER BY sorts the groups, not the rows.
Question 4Medium
In standard SQL (for example PostgreSQL), why does this query fail?
SELECT dept, name, COUNT(*)
FROM employees
GROUP BY dept;
- name is neither grouped nor aggregated, so it is ambiguous
- GROUP BY must be written before the FROM clause in standard SQL
- COUNT(*) cannot be combined with a GROUP BY clause in the same query
- dept must be wrapped in an aggregate function such as MAX(dept)
Answer: A. name is neither grouped nor aggregated, so it is ambiguous
Explanation
Each output row represents a whole group, so every selected column must be grouped or aggregated. A department has several names, so name is ambiguous.
Step-by-step solution
- With GROUP BY dept, each output row is one department.
- Name is not in the GROUP BY and not aggregated, so one department has several names.
- The query is rejected because name is ambiguous (option A).
Why the other options are wrong
- B. GROUP BY comes after FROM and WHERE in standard SQL, so the order is correct.
- C. COUNT(*) is an aggregate and works with GROUP BY.
- D. Wrapping dept in MAX would not fix the un-grouped name column.
Tip: Every selected column must be in GROUP BY or inside an aggregate.
Question 5Medium
What does this query return?
employees
id | name | dept | salary | manager_id
1 | Asha | Sales | 60000 | NULL
2 | Ravi | Sales | 45000 | 1
3 | Meera | HR | 50000 | 1
4 | John | IT | 70000 | NULL
5 | Kiran | IT | 45000 | 4
SELECT COUNT(*), COUNT(manager_id) FROM employees;
- 3, 3
- 5, 3
- 5, 5
- 5, 2
Answer: B. 5, 3
Explanation
COUNT(*) counts all rows. COUNT(column) skips NULLs, and two employees have a NULL manager_id.
Step-by-step solution
- There are 5 rows, so COUNT(*) = 5.
- manager_id is NULL for Asha and John.
- COUNT(manager_id) skips NULLs and counts 3.
- Result: 5, 3.
Why the other options are wrong
- A. COUNT(*) counts all 5 rows.
- C. COUNT(column) ignores NULLs.
- D. 2 is the number of NULLs, not non-NULLs.
Tip: Rows with NULL are skipped by COUNT(column).
Question 6Medium
What does this query return?
bonus
emp_id | amount
1 | 100
2 | NULL
3 | 200
SELECT AVG(amount) FROM bonus;
- 300
- NULL
- 100
- 150
Answer: D. 150
Explanation
Aggregate functions ignore NULLs, so AVG = (100 + 200) / 2 = 150, not 300 / 3.
Step-by-step solution
- AVG ignores NULL values, so only 100 and 200 count.
- Sum is 300 and the count of non-NULL values is 2.
- 300 ÷ 2 = 150 (option D).
Why the other options are wrong
- A. 300 is the sum; AVG divides by the count of non-NULL values.
- B. AVG returns NULL only when every value is NULL.
- C. 100 is only the first value, not the average.
Tip: AVG divides by the number of non-NULL values, not by the row count.
Question 7Hard
How many groups (rows) does this query return?
items
id | color
1 | red
2 | NULL
3 | blue
4 | NULL
5 | red
SELECT color, COUNT(*)
FROM items
GROUP BY color;
- 3
- 5
- 2
- 4
Answer: A. 3
Explanation
GROUP BY puts all NULLs into a single group, so the groups are red, blue and NULL.
Step-by-step solution
- GROUP BY puts all NULL colors into one group.
- Groups are red (rows 1 and 5), NULL (rows 2 and 4) and blue (row 3).
- That is 3 groups (option A).
Why the other options are wrong
- B. 5 is the number of rows, not the number of groups.
- C. 2 would drop either the NULL group or the blue group.
- D. 4 would treat each NULL as its own group, but GROUP BY puts all NULLs together.
Tip: GROUP BY treats NULLs as one group; to count distinct values, COUNT(DISTINCT col) skips NULLs.
Question 8Hard
What does this query return? (columns: dept, total)
employees
id | name | dept | salary | manager_id
1 | Asha | Sales | 60000 | NULL
2 | Ravi | Sales | 45000 | 1
3 | Meera | HR | 50000 | 1
4 | John | IT | 70000 | NULL
5 | Kiran | IT | 45000 | 4
SELECT dept, SUM(salary) AS total
FROM employees
WHERE salary > 45000
GROUP BY dept
HAVING SUM(salary) > 50000
ORDER BY dept;
- IT, 115000; Sales, 105000
- IT, 70000
- IT, 70000; Sales, 60000
- HR, 50000; IT, 70000; Sales, 60000
Answer: C. IT, 70000; Sales, 60000
Explanation
WHERE first removes Ravi and Kiran (45000). The remaining sums are Sales 60000, HR 50000 and IT 70000. HAVING then drops HR, because 50000 is not greater than 50000.
Step-by-step solution
- WHERE salary > 45000 keeps Asha 60000, Meera 50000, John 70000.
- Group: Sales 60000, HR 50000, IT 70000.
- HAVING > 50000 removes HR.
- Order by dept: IT 70000, Sales 60000.
Why the other options are wrong
- A. That uses all rows, ignoring WHERE.
- B. Sales 60000 also passes HAVING.
- D. HR fails HAVING (50000 is not above 50000).
Tip: Order: FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY.
Practise all 8 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →