PostgreSQL-Database/C2/GROUP-BY-and-HAVING-Clause/English
Title of the Script: GROUP BY and HAVING Clause
Author: Ketki Bhamble / EduPyramids
Keywords: PostgreSQL, pgAdmin 4, query, GROUP BY clause, HAVING clause, aggregate functions, COUNT(*), EduPyramids, Video Tutorial.
| Visual Cue | Narration |
| Slide 1
Title Slide |
Welcome to this Spoken Tutorial on GROUP BY and HAVING Clause in PostgreSQL. |
| Slide 2
Learning Objectives |
In this tutorial, we will learn how to
|
| Slide 3
System Requirements
|
To record this tutorial, I am using the following setup. |
| Slide 4
Pre-requisites
|
To follow this tutorial,
|
| Slide 5
Code Files The following code file is required to practice this tutorial:
This file is provided in the Code Files link on this tutorial page. |
The following code file is required to practice this tutorial.
Please download and extract the file. |
| Let us get started.
I have opened the pgAdmin4 interface and connected to the localhost server. | |
| Display the students table on the screen
SELECT * FROM students; |
We are using the students table.
It contains student ID, name, city, gender, date of birth, and CGPA. |
| Slide 6
GROUP BY |
The GROUP BY clause groups rows that have the same values into summary rows.
It is used along with aggregate functions to organise results. |
| Slide 7
Syntax of GROUP BY
FROM table_name GROUP BY column_name; |
This is the syntax of the GROUP BY clause.
|
| Let us find the number of students from each city. | |
| Type:
SELECT city, COUNT(*) FROM students GROUP BY city; Click the Execute query icon. Point to the results. |
Type the following query.
Let us execute the query. The result shows each city with the total number of students from that city. |
| Now let us calculate the average CGPA of students in each city. | |
| Type:
FROM students GROUP BY city;
|
Type the following query.
This query groups students by city and calculates the average CGPA for each group.Let us execute this query. |
| We can also group records using other columns such as gender. | |
| Type:
SELECT gender, COUNT(*) FROM students GROUP BY gender;
Point to the results in the table. |
Type this query.
This query groups students based on gender.
|
| Slide 8
|
The HAVING clause filters grouped results.
It is used along with the GROUP BY clause and aggregate functions. |
| Type:
FROM students GROUP BY city HAVING COUNT(*) > 2; Click the Execute query icon. Point to the results in the table. |
Type the following query.
This query groups the records by city.
|
| Type:
FROM students GROUP BY city HAVING ROUND(AVG(cgpa), 2) > 8.5;
Point to the results in the table. |
Type the following query.
|
| Type:
FROM students WHERE cgpa > 8 GROUP BY city HAVING COUNT(*) > 1;
|
Let us type this query.
In this query, we will analyze student data city-wise.
The WHERE clause filters rows before grouping. However the HAVING clause filters groups after aggregation. Then we will count students in each city. Let us execute the query.
|
| With this we come to the end of this tutorial. | |
| Slide 9
Summary In this tutorial, we learnt how to:
|
Let us summarize. |
| Slide 10 + 11
|
We encourage you to do this assignment.
|
| Slide 12
Thank you This Spoken Tutorial is brought to you by EduPyramids Educational Private Limited. SINE IIT Bombay.
|
Thank you for joining. |