HAVING clause in PostgreSQL

PostgreSQL HAVING PostgreSQL HAVING clause is used to return the groups of rows only when the condition is TRUE. It is used with the PostgreSQL GROUP BY Clause. It, however, does not have a mandatory existence with the PostgreSQL GROUP BY Clause.

Syntax: To group the rows by values in multiple columns.

SELECT expressions
FROM table_name
GROUP BY columns
HAVING having_conditions;

Parameters: having_conditions: It is used to specify the conditions that are applied to the result obtained by the GROUP BY command, to restrict the result set.

Example: Employment Table:

ID STATE RATE
1 A 60
2 B 70
3 C 60
4 D 80
5 E 70

Query:

SELECT “RATE”  
FROM “EMPLOYMENT”
GROUP BY “RATE” 
HAVING COUNT (“RATE”) > 1
ORDER BY “RATE”;

Output:

RATE
60
70

Explanation: The EMPLOYMENT is an already existing table from which we are retrieving unique employment rates with count greater than 1 using the HAVING condition with the GROUP BY clause.

Please follow and like us:
Content Protection by DMCA.com