Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Aggregate Functions group by having

Total questions: 21

Worksheet time: 11mins

Name
Class
Date
1.

Employee(emp_id, dept, salary, bonus)
• salary and bonus may contain NULLs
• dept includes some NULL department rows

Which query filters only departments where more than 3 employees exist?

a)

WHERE COUNT() > 3

b)

HAVING COUNT() > 3

c)

WHERE emp_id > 3

d)

FILTER COUNT(*) > 3

2.

COUNT(*) vs COUNT(salary) difference?

a)

COUNT(salary) ignores NULL

b)

COUNT(*) counts all rows

c)

Both A and B

d)

They are always identical

3.

Which returns departments with total bonus > total salary?

a)

Valid

b)

Invalid

4.

Which filters employees before grouping?

a)

WHERE

b)

HAVING

c)

GROUP WHERE

d)

DISTINCT

5.

Evaluate: SELECT dept, SUM(salary) FROM Employee GROUP BY dept HAVING SUM(salary) IS NULL;

a)

Shows only depts where salary all NULL

b)

Shows non-NULL salary depts

c)

Shows error

d)

Shows all rows

6.

Which counts NULL salary rows per dept?

a)

Correct

b)

Wrong

7.

HAVING always needs GROUP BY?

a)

No

b)

Yes

8.

Which query shows only departments where avg bonus > 10000?

a)

HAVING bonus > 10000

b)

HAVING AVG(bonus) > 10000

c)

WHERE AVG(bonus) > 10000

d)

GROUP WHERE AVG(bonus) > 10000

9.

NULL dept values in GROUP BY become:

a)

A separate NULL group

b)

Ignored

c)

Merged into highest dept value

d)

Returns error

10.

Which is most accurate?

a)

HAVING filters aggregated results

b)

HAVING works same as WHERE

c)

HAVING never uses aggregate functions

d)

HAVING comes before GROUP BY

11.

Which clause executes first?

a)

HAVING

b)

GROUP BY

c)

WHERE

d)

SELECT

12.

If a dept has rows but salary all NULL: SUM(salary) returns?

a)

NULL

b)

0

c)

Error

d)

Full salary

13.

What does AVG ignore?

a)

NULL salaries

b)

Zero salaries

c)

Both A & B

d)

Neither

14.

Find departments where highest salary 5000?

a)

HAVING COUNT(*) = 5 AND MAX(bonus) > 5000

b)

WHERE COUNT(*) = 5 AND MAX(bonus) > 5000

c)

HAVING COUNT(bonus) = 5

d)

COUNT(*) = 5 AND HAVING MAX(bonus) > 5000

15.

Distinct in aggregation: HAVING SUM(DISTINCT salary) > 50000 Meaning:

a)

Ignore duplicate salary values in sum

b)

COUNT only rows

c)

Works same as SUM(salary)

d)

Sums only highest salary

16.

Which gives count per dept including NULL dept?

a)

SELECT dept, COUNT(*) FROM Employee GROUP BY dept;

b)

SELECT dept, COUNT(dept) FROM Employee GROUP BY dept;

17.

HAVING with column alias is: SELECT dept, SUM(salary) AS total FROM Employee GROUP BY dept HAVING total > 50000;

a)

May work in some DBs, not standard SQL

b)

Always works everywhere

c)

Always error

d)

HAVING cannot reference SUM

18.

Which finds depts where at least one employee missing bonus?

a)

HAVING COUNT(bonus) = 50000? HAVING SUM(CASE WHEN salary>=50000 THEN 1 ELSE 0 END)=2

b)

Correct

c)

Incorrect

19.

Which gives only dept where no salary recorded?

a)

HAVING SUM(salary) = 0

b)

HAVING SUM(salary) IS NULL

c)

HAVING COUNT(*)=0

d)

WHERE salary IS NULL

20.

HAVING without any aggregate in GROUP BY query:

a)

Can use normal column conditions

b)

Equivalent to WHERE after grouping

c)

Might be valid logic

d)

All above

21.

Which returns departments with more than 1 distinct salary?

a)

HAVING COUNT(DISTINCT salary) > 1

b)

HAVING COUNT(salary) > 1

c)

WHERE DISTINCT salary > 1

d)

HAVING DISTINCT salary > 1