0% found this document useful (0 votes)
17 views2 pages

SQL Clauses GroupBy Where Having

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

SQL Clauses GroupBy Where Having

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

SQL Clauses: GROUP BY, WHERE, and HAVING

1. What is the use of the GROUP BY clause?

The GROUP BY clause is used to group rows that have the same values in specified columns into

summary rows,

such as aggregating data (using functions like COUNT, SUM, AVG, etc.) for each group. It is

commonly used with aggregate

functions to perform operations on groups of data, rather than on individual rows.

Example:

SELECT department, COUNT(employee_id)

FROM employees

GROUP BY department;

This query will return the number of employees in each department.

2. What is the difference between the WHERE clause and the HAVING clause?

- WHERE clause is used to filter rows before any groupings are made, meaning it is applied to

individual rows.

- HAVING clause is used to filter groups after the GROUP BY clause has been applied, meaning it is

used to filter aggregate data.

Key Differences:

1. WHERE operates on individual rows, while HAVING operates on groups.

2. WHERE cannot be used with aggregate functions, whereas HAVING is specifically designed to

work with them.


Example:

SELECT department, COUNT(employee_id)

FROM employees

WHERE salary > 50000 -- Filters individual rows

GROUP BY department

HAVING COUNT(employee_id) > 5; -- Filters groups

In this example, the WHERE clause filters out employees with salaries less than 50,000, and the

HAVING clause filters out departments with fewer than 5 employees.

You might also like