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 set a PostgreSQL interval to zero?
- How can I monitor PostgreSQL performance using Zabbix?
- How can I use PostgreSQL's "zero if null" feature?
- How can I integrate PostgreSQL with Yii2?
- How do I use PostgreSQL's XMIN and XMAX features?
- How can I use PostgreSQL XML functions to manipulate XML data?
- How do I use PostgreSQL's XMLTABLE to parse XML data?
- How do I use PostgreSQL with Qt?
- How can I use PostgreSQL XOR to compare two values?
- How can I view my PostgreSQL query history?
See more codes...