TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

PostgreSQL Having Clause

PostgreSQL Having Clause for beginners and professionals with examples database, table, create, select, insert, update, delete, join, function, index, clause, trigger, view, procedure etc.

<< Back to POSTGRESQL

PostgreSQL Having clause

In this section, we are going to understand the working of the HAVING clause in PostgreSQL.

The having clause is used to specify a search condition for a group or an aggregate. And it is regularly used with the GROUP BY clause to filter groups or aggregates based on a detailed condition.

Syntax of PostgreSQL having clause

The basic syntax of PostgreSQL HAVING clause is as follows:

SELECT column1, aggregate_function (column2)
FROM table1, table2  
WHERE [ conditions ]  
GROUP BY column1, column2  
HAVING [ conditions ]  
ORDER BY column1, column2  

In the above syntax, we used the following parameters:

Parameters Description
GROUP BY clause It is used to return rows grouped by column1.
Having clause It is used to define a condition which filter the sets.

Note: In PostgreSQL, we can add other clauses of the SELECT command such as LIMIT, JOIN, and FETCH. Subsequently, the HAVING clause is working before the SELECT clause.

In PostgreSQL, the HAVING clause works in below format:

PostgreSQL Having clause

We cannot use the column aliases in the HAVING clause because, when assessing the HAVING clause, the column aliases defined in the SELECT clause are not accessible.

Difference between having and where clauses

Let us see the difference between HAVING Clause and WHERE Clause:

Having clause Where clause
The HAVING clause allows us to filter groups of rows as per the defined condition. The WHERE clause permits us to filter rows according to a defined condition.
The HAVING clause is useful to groups of rows. The WHERE clause is applied to rows only.

Examples of PostgreSQL HAVING clause

Let us see some examples of having clause in PostgreSQL. So, here we will take the employee table, which we created in the earlier topics of PostgreSQL tutorial.

PostgreSQL Having clause

Example of SUM() function using PostgreSQL HAVING clause

In the below example, we are using the GROUP BY clause with the SUM() function to identify every employee's total salary:

SELECT emp_id, SUM (salary)
FROM employee
GROUP BY emp_id;

Output

In the below example, we are using the GROUP BY clause with the SUM() function to identify every employee's total salary:

SELECT emp_id, SUM (salary)
FROM employee
GROUP BY emp_id;

Output

After executing the above command, we will get the below output, which displays the sum of each employee's salary based on their emp_id.

PostgreSQL Having clause

After that, we will add the HAVING clause in the above command for selectingthose employees whose salary is more than 25000:

SELECT emp_id, first_name, SUM (salary)
FROM employee
GROUP BY first_name, emp_id
HAVING SUM (salary) > 25000
order by first_name DESC;

Output

After executing the above command, we will get the below result, which displays those employees whose salary is more than 25000:

PostgreSQL Having clause

Example of COUNT() function using PostgreSQL HAVING clause

The below command is used to identify the number of employees with the help of the GROUP BY clause:

SELECT first_name, COUNT (emp_id)
FROM employee
GROUP BY first_name;

Output

Once we implemented the above command, we will get the below output; which displays employees' count:

PostgreSQL Having clause

After that, we will add the HAVING clause in the above command to select the first_name with less than two employees:

SELECT first_name, COUNT (emp_id)
FROM employee
GROUP BY first_name
HAVING COUNT (emp_id)< 2;

Output

After executing the above command, we will get the below result:

PostgreSQL Having clause




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf