Difference between Where and Having Clause SQL

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  

1.) The HAVING clause can refer to aggregate functions, which the WHERE clause cannot

SELECT user, MAX(salary) FROM users
GROUP BY user HAVING MAX(salary) > 10;

2.) Do not use HAVING for items that should be in the WHERE clause. For example, do not write the following:

SELECT col_name FROM tbl_name HAVING col_name > 0;

Write this instead:

SELECT col_name FROM tbl_name WHERE col_name > 0;

3.) A HAVING clause can refer to any column or alias named in a select_expr in the SELECT list or in outer subqueries, and to aggregate functions. However, the SQL standard requires that HAVING must reference only columns in the GROUP BY clause or columns used in aggregate functions.


Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.