YouTip LogoYouTip

Postgresql Having

# PostgreSQL HAVING Clause The HAVING clause allows us to filter the data after the groups have been formed. The WHERE clause sets conditions on the selected columns, whereas the HAVING clause sets conditions on the groups created by the GROUP BY clause. ### Syntax The following is the position of the HAVING clause in a SELECT query: SELECT FROM WHERE GROUP BY HAVING ORDER BY The HAVING clause must be placed after the GROUP BY clause and before the ORDER BY clause. The basic syntax of the HAVING clause in a SELECT statement is as follows: SELECT column1, column2 FROM table1, table2 WHERE GROUP BY column1, column2 HAVING ORDER BY column1, column2; ### Example Create the COMPANY table ((https://static.jyshare.com/download/company.sql) ) with the following data: tutorialdb# select * from COMPANY; id | name | age | address | salary ----+-------+-----+------------+-------- 1 | Paul | 32 | California | 20000 2 | Allen | 25 | Texas | 15000 3 | Teddy | 23 | Norway | 20000 4 | Mark | 25 | Rich-Mond | 65000 5 | David | 27 | Texas | 85000 6 | Kim | 22 | South-Hall | 45000 7 | James | 24 | Houston | 10000 (7 rows) The following example will find the data where the count of names is less than 2, grouped by the NAME field: SELECT NAME FROM COMPANY GROUP BY name HAVING count(name) 1; This would produce the following result: name ------- Paul James (2 rows)
← Postgresql ConstraintsAndroid Tutorial β†’