SQL GROUP BY- NEED and USES

sql-group-by
Spread the love

What are SQL joins?

SQL GROUP BY is used to group rows with similar values for aggregate functions. Without GROUP BY user cannot use aggregate functions.

Syntax:

SELECT AGGREGATE(COL)
FROM demo_tab
GROUP BY col_name;
SELECT DEPT_NAME, AVG(SALARY)
FROM EMPLOYEE
GROUP BY DEPT_NAME;

Now lets use GROUP BY with HAVING

SELECT DEPT_NAME, AVG(SALARY)
FROM EMPLOYEE
GROUP BY DEPT_NAME
HAVING AVG(SALARY) > 4000 ;

Thanks For Reading!

SOURCE : https://learn.microsoft.com/en-us/sql/t-sql/queries/select-group-by-transact-sql?view=sql-server-ver16

READ MORE:
SQL BASICS
SQL WINDOW FUNCTIONS
SQL ORDER BY