0

What difference between HAVING, WHERE?

25th Nov 2018, 12:35 PM
Pavel Zubakha
Pavel Zubakha - avatar
2 ответов
+ 7
"The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions." Quoted from: https://www.w3schools.com/sql/sql_having.asp
25th Nov 2018, 2:35 PM
Ipang
+ 3
You use WHERE to filter your initial table, so that only the filtered records are aggregated. You use HAVING clause after GROUP BY, to further filter the aggregated results. Example: SELECT job, AVG(age) FROM employees WHERE department = 'IT' GROUP BY job HAVING AVG(age) > 30 This would first find the people who work in IT department, calculate their average age in each job, then give you the result for those jobs only where the calculation is over 30.
28th Nov 2018, 3:45 AM
Tibor Santa
Tibor Santa - avatar