Example 1 of GROUP BY and HAVING in SQL

For Complete YouTube Video: Click Here

We will try to understand Example 1 of GROUP BY and HAVING in SQL in this class.

GROUP BY and HAVING concepts are already discussed in our previous class.

Example 1 of GROUP BY and HAVING in SQL

Let’s consider the question below.

Question: Write a Query to find the Count of Employees in each project location.

To understand this concept, we should know the cartesian product in SQL.

To get the output for this query, we need two tables, works_on and project table of the company database, as shown below.

Example 1 of GROUP BY and HAVING in SQL 2
Example 1 of GROUP BY and HAVING in SQL 2

After the FROM clause, works_on and project tables are used. So, the cartesian product of both the tables will be made.

On the cartesian product table, we have to apply the condition where p.pnumber = w.pno.

Based on the above condition, we have to use the GROUP BY clause to get the count of the employees on the resultant table.

The query and the output of the query are as shown below.

Example 1 of GROUP BY and HAVING in SQL 1
Example 1 of GROUP BY and HAVING 1