Grouping and Aggregating Flashcards

1
Q

return the frequency of each make of car, where you have column ‘make’ in table ‘cars’

A
SELECT make, COUNT(*)
FROM cars
GROUPBY make
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What’s wrong with the following:
~~~
SELECT department, COUNT(employee_id)
FROM employees
GROUP BY department
WHERE 1=1
~~~

A

WHERE needs to go before GROUP BY

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s wrong with the following:
~~~
SELECT department, COUNT()
FROM employees
WHERE COUNT(
) < 35
GROUP BY department
ORDER BY department
~~~

A

This would fail, because WHERE clauses are for evaluating RECORDS, not AGGREGATIONS. If you want to evaluate aggregations, you need to use the keyword HAVING instead and put that clause AFTER the GROUP BY

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s the difference between WHERE and HAVING?

A

WHERE evaluates records, while HAVING evaluates aggregations. WHERE clauses will go before GROUP BY statements, while HAVING clauses will go after

How well did you know this?
1
Not at all
2
3
4
5
Perfectly