Grouping and Aggregating Flashcards
return the frequency of each make of car, where you have column ‘make’ in table ‘cars’
SELECT make, COUNT(*) FROM cars GROUPBY make
What’s wrong with the following:
~~~
SELECT department, COUNT(employee_id)
FROM employees
GROUP BY department
WHERE 1=1
~~~
WHERE needs to go before GROUP BY
What’s wrong with the following:
~~~
SELECT department, COUNT()
FROM employees
WHERE COUNT() < 35
GROUP BY department
ORDER BY department
~~~
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
What’s the difference between WHERE and HAVING?
WHERE evaluates records, while HAVING evaluates aggregations. WHERE clauses will go before GROUP BY statements, while HAVING clauses will go after