SQL for Analytics Questions with Answers

24 practice questions on SQL for Analytics (Data Analytics) with answers and step-by-step solutions. Try 10 samples below, then practise all 24 free in the AptiNest app.

Sample questions

Question 1Easy

Which clause filters groups after aggregation?

  1. HAVING
  2. ORDER BY
  3. LIMIT
  4. WHERE

Answer: A. HAVING

Explanation

WHERE filters rows before grouping; HAVING filters the aggregated groups.

Step-by-step solution

  1. WHERE filters rows before grouping.
  2. HAVING filters groups after aggregation.

Why the other options are wrong

  • B. ORDER BY sorts; it does not filter.
  • C. LIMIT caps the number of rows returned.
  • D. WHERE runs before grouping and cannot use aggregates.

Tip: WHERE = rows; HAVING = groups.

Question 2Easy

Table sales (amount is NULL in row 4):

id | region | rep   | amount | sale_date
1  | North  | Asha  | 500    | 2024-01-05
2  | North  | Ravi  | 300    | 2024-01-20
3  | South  | Meena | 700    | 2024-02-03
4  | South  | Asha  | NULL   | 2024-02-10
5  | East   | Kiran | 200    | 2024-02-15
6  | North  | Asha  | 400    | 2024-03-01
7  | South  | Meena | 100    | 2024-03-12

What does SELECT COUNT(*), COUNT(amount) FROM sales; return?

  1. 7 and 7
  2. 6 and 7
  3. 7 and 6
  4. 6 and 6

Answer: C. 7 and 6

Explanation

COUNT(*) counts rows; COUNT(amount) skips the NULL.

Step-by-step solution

  1. COUNT(*) counts every row: 7.
  2. COUNT(amount) skips NULL values: row 4 is NULL, so 6.
  3. Result: 7 and 6.

Why the other options are wrong

  • A. COUNT(column) ignores NULLs.
  • B. The two counts are swapped.
  • D. COUNT(*) counts all rows, including the NULL one.

Tip: COUNT(*) - COUNT(col) = number of NULLs.

Question 3Easy

A Common Table Expression (CTE) starts with which keyword?

  1. CTE
  2. USING
  3. TEMP
  4. WITH

Answer: D. WITH

Explanation

WITH name AS (SELECT ...) defines a named subquery usable in the main query.

Step-by-step solution

  1. A CTE is a named temporary result defined before the main query.
  2. Syntax: ```sql WITH name AS (SELECT ...) SELECT ... FROM name. ```

Why the other options are wrong

  • A. CTE is the concept's name, not a keyword.
  • B. USING is for join columns.
  • C. TEMP creates temporary tables, not CTEs.

Tip: CTE = WITH ... AS (...).

Question 4Easy

Table sales (amount is NULL in row 4):

id | region | rep   | amount | sale_date
1  | North  | Asha  | 500    | 2024-01-05
2  | North  | Ravi  | 300    | 2024-01-20
3  | South  | Meena | 700    | 2024-02-03
4  | South  | Asha  | NULL   | 2024-02-10
5  | East   | Kiran | 200    | 2024-02-15
6  | North  | Asha  | 400    | 2024-03-01
7  | South  | Meena | 100    | 2024-03-12

What does SELECT COUNT(DISTINCT rep) FROM sales; return?

  1. 3
  2. 5
  3. 7
  4. 4

Answer: D. 4

Explanation

Distinct reps: Asha, Ravi, Meena, Kiran.

Step-by-step solution

  1. Reps: Asha, Ravi, Meena, Asha, Kiran, Asha, Meena.
  2. Distinct: Asha, Ravi, Meena, Kiran.
  3. COUNT(DISTINCT rep) = 4.

Why the other options are wrong

  • A. Kiran is easy to miss; there are 4.
  • B. 5 has no basis in the data.
  • C. 7 is the row count.

Tip: Unique count: COUNT(DISTINCT col).

Question 5Medium

To get month-over-month revenue change, which window function fetches the previous month's value?

  1. LAG
  2. FIRST_VALUE
  3. NTILE
  4. LEAD

Answer: A. LAG

Explanation

LAG looks backwards; LEAD looks forward.

Step-by-step solution

  1. LAG(revenue) OVER (ORDER BY month) returns the previous month's value.
  2. Change = revenue - LAG(revenue).

Why the other options are wrong

  • B. FIRST_VALUE returns the first value in the window.
  • C. NTILE divides rows into buckets.
  • D. LEAD returns the next row, not the previous one.

Tip: LAG looks back; LEAD looks ahead.

Question 6Medium

Table sales (amount is NULL in row 4):

id | region | rep   | amount | sale_date
1  | North  | Asha  | 500    | 2024-01-05
2  | North  | Ravi  | 300    | 2024-01-20
3  | South  | Meena | 700    | 2024-02-03
4  | South  | Asha  | NULL   | 2024-02-10
5  | East   | Kiran | 200    | 2024-02-15
6  | North  | Asha  | 400    | 2024-03-01
7  | South  | Meena | 100    | 2024-03-12

What does SELECT ROUND(AVG(amount), 2) FROM sales; return?

  1. NULL
  2. 314.29
  3. 366.67
  4. 366.0

Answer: C. 366.67

Explanation

AVG ignores NULL: 2200 / 6 = 366.67.

Step-by-step solution

  1. AVG ignores NULL rows.
  2. Sum of non-NULL amounts = 500 + 300 + 700 + 200 + 400 + 100 = 2200.
  3. AVG = 2200 / 6 = 366.67.

Why the other options are wrong

  • A. AVG ignores NULLs; it only returns NULL if all values are NULL.
  • B. 314.29 divides by 7, treating NULL as 0.
  • D. 366.0 is wrongly rounded.

Tip: AVG(col) = SUM(col) / COUNT(col).

Question 7Medium

Table sales (amount is NULL in row 4):

id | region | rep   | amount | sale_date
1  | North  | Asha  | 500    | 2024-01-05
2  | North  | Ravi  | 300    | 2024-01-20
3  | South  | Meena | 700    | 2024-02-03
4  | South  | Asha  | NULL   | 2024-02-10
5  | East   | Kiran | 200    | 2024-02-15
6  | North  | Asha  | 400    | 2024-03-01
7  | South  | Meena | 100    | 2024-03-12

Which region has the highest total for SELECT region, SUM(amount) FROM sales GROUP BY region;?

  1. South (1500)
  2. South (800)
  3. North (900)
  4. North (1200)

Answer: D. North (1200)

Explanation

North = 500 + 300 + 400 = 1200;

South = 700 + 100 = 800 (NULL ignored);

East = 200.

Step-by-step solution

  1. North: 500 + 300 + 400 = 1200.
  2. South: 700 + NULL + 100 = 800 (NULL ignored).
  3. East: 200.
  4. Highest: North (1200).

Why the other options are wrong

  • A. South is 800; 1500 is not any region's total.
  • B. South's 800 is less than North's 1200.
  • C. North's total includes row 6 (400), giving 1200.

Tip: List rows per group before summing.

Question 8Medium

Which window function would compute each row's share of its region's total?

  1. amount * 1.0 / SUM(amount) OVER (PARTITION BY region)
  2. amount * 1.0 / SUM(amount) OVER (ORDER BY region)
  3. amount * 1.0 / LAG(amount) OVER (PARTITION BY region)
  4. RANK() OVER (PARTITION BY region ORDER BY amount)

Answer: A. amount * 1.0 / SUM(amount) OVER (PARTITION BY region)

Explanation

SUM with PARTITION BY returns the group total on every row without collapsing rows.

Step-by-step solution

  1. SUM(amount) OVER (PARTITION BY region) gives each row its region's total.
  2. Divide the row's amount by it.
  3. Multiply by 1.0 to avoid integer division.

Why the other options are wrong

  • B. ORDER BY without PARTITION gives a running total, not the region total.
  • C. LAG fetches the previous row, not a total.
  • D. RANK gives order, not share.

Tip: Share of group = value / SUM(value) OVER (PARTITION BY group).

Question 9Hard

customers: (1, Anu), (2, Bala), (3, Chitra). orders(oid, cid, amt): (10, 1, 250), (11, 1, 150), (12, 2, 400). What does this return for Chitra?

SELECT c.name, COUNT(o.oid)
FROM customers c LEFT JOIN orders o ON c.cid = o.cid
GROUP BY c.name;
  1. 1
  2. 0
  3. Chitra is not returned
  4. NULL

Answer: B. 0

Explanation

The LEFT JOIN keeps Chitra with NULL order columns; COUNT(o.oid) ignores NULL, giving 0. COUNT(*) would give 1.

Step-by-step solution

  1. LEFT JOIN keeps all customers, including Chitra.
  2. Chitra has no orders, so o.oid is NULL.
  3. COUNT(o.oid) ignores NULL, giving 0.

Why the other options are wrong

  • A. COUNT(*) would give 1; COUNT(o.oid) gives 0.
  • C. LEFT JOIN keeps every customer.
  • D. COUNT never returns NULL.

Tip: LEFT JOIN + COUNT(right.col) for zero counts.

Question 10Hard

Which query finds reps whose total sales exceed the average sale amount?

  1. SELECT rep FROM sales WHERE SUM(amount) > (SELECT AVG(amount) FROM sales) GROUP BY rep;
  2. SELECT rep FROM sales HAVING amount > AVG(amount) GROUP BY rep ORDER BY rep;
  3. SELECT rep, AVG(amount) FROM sales WHERE amount > SUM(amount) GROUP BY rep;
  4. SELECT rep FROM sales GROUP BY rep HAVING SUM(amount) > (SELECT AVG(amount) FROM sales);

Answer: D. SELECT rep FROM sales GROUP BY rep HAVING SUM(amount) > (SELECT AVG(amount) FROM sales);

Explanation

Aggregates cannot appear in WHERE; compare the grouped sum against a scalar subquery in HAVING.

Step-by-step solution

  1. Group by rep to get each rep's total.
  2. Compare the group total in HAVING with a subquery for the overall average.
  3. HAVING SUM(amount) > (SELECT AVG(amount) FROM sales).

Why the other options are wrong

  • A. Aggregates like SUM cannot appear in WHERE, and GROUP BY is misplaced.
  • B. HAVING must come after GROUP BY, and amount is not grouped.
  • C. Aggregates cannot appear in WHERE.

Tip: Group total vs overall value: HAVING + subquery.

Practise all 24 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 →

Related topics

See all Data Analytics topics · All topics