postgresqlHow do I use the PostgreSQL HAVING clause?
The PostgreSQL HAVING clause is used to filter the result set of a SELECT statement based on grouping. It is used in conjunction with GROUP BY to filter the results of aggregate functions.
For example, the following query will show the average salary of each department:
SELECT department, AVG(salary)
FROM employees
GROUP BY department;
Output example
Department AVG(salary)
IT 50000
Sales 60000
Marketing 65000
The HAVING clause can be used to filter the result set further. For example, the following query will show the average salary of each department with a salary greater than $60,000:
SELECT department, AVG(salary)
FROM employees
GROUP BY department
HAVING AVG(salary) > 60000;
Output example
Department AVG(salary)
Marketing 65000
The parts of the query are:
- SELECT: This clause specifies the columns to be included in the result set.
- FROM: This clause specifies the table from which the data is selected.
- GROUP BY: This clause groups the rows by the specified column.
- HAVING: This clause filters the result set based on the specified condition.
For more information, please refer to the following link:
More of Postgresql
- How can I use PostgreSQL with Zabbix?
- How can I use PostgreSQL and ZFS snapshots together?
- How can I use PostgreSQL with YAML?
- How do I use PostgreSQL's XMIN and XMAX features?
- How do I use PostgreSQL's XMLTABLE to parse XML data?
- How can I retrieve data from PostgreSQL for yesterday's date?
- How do I set the PostgreSQL work_mem parameter?
- How can I use PostgreSQL's "zero if null" feature?
- How can I set a PostgreSQL interval to zero?
- How can I integrate PostgreSQL with Yii2?
See more codes...