Practical 7
Practical 7
Group Functions:
The SQL GROUP BY Clause is used along with the group functions to retrieve data
grouped according to one or more columns.
Syntax:
Select column1, column2……. columnN, Aggregate Function (argument) From
TableName Group By column1, column2……. columnN;
Example:
If you want to know the total amount of salary spent on each department, the query
would be:
Example:
2) SQL ORDER BY
Syntax:
Select column1, column2……. columnN From TableName ORDER BY column1,
column2……. columnN;
Example:
Name Salary
Soumya 20000
Ramesh 25000
Priya 30000
Hrithik 35000
Having clause is used to filter data based on the group functions. This is similar to
WHERE condition but is used with group functions.
Group functions cannot be used in WHERE Clause but can be used in HAVING
clause.
The HAVING clause must follow the GROUP BY clause in a query and must also
precedes the ORDER BY clause if used.
The following is the syntax of the SELECT statement, including the HAVING
clause:
Syntax:
SELECT column1, column2 FROM TableName WHERE [conditions] GROUP BY
column1, column2 HAVING [conditions];
Example:
SELECT dept, SUM (salary) FROM employee GROUP BY dept HAVING SUM
(salary) > 25000;