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 troubleshoot zero damaged pages in PostgreSQL?
- How can I use PostgreSQL and ZFS snapshots together?
- How can Zalando use PostgreSQL to improve its software development?
- How can I use PostgreSQL's "zero if null" feature?
- How do I use PostgreSQL and ZFS together?
- How do I use PostgreSQL ZonedDateTime to store date and time information?
- How can I use PostgreSQL with YAML?
- How do I use PostgreSQL's XMLTABLE to parse XML data?
- How do I store binary data in a Postgresql database using the bytea data type?
See more codes...