Practical No 8 Group Function
Practical No 8 Group Function
11
Group Function
Group functions are built-in SQL functions that operate on groups of rows and return one value for the entire
group. These functions are: COUNT, MAX, MIN, AVG, SUM, DISTINCT
SQL COUNT (): This function returns the number of rows in the table that satisfies the condition specified in
the WHERE condition.
For Example: If you want the number of employees in a particular department, the query would be:
For Example: If you want to select all distinct department names from employee table, the query would
be:
To get the count of employees with unique name, the query would be:
SQL AVG (): This function is used to get the average value of a numeric column.
To get the average salary, the query would be
SQL SUM (): This function is used to get the sum of a numeric column
To get the total salary given out to the employees,
SELECT SUM (salary) FROM employee;
GROUP BY Syntax
Now that we know what the GROUP By clause is, let's look at the syntax for a basic
group by query.
SELECT statements... GROUP BY column_name1 [, column_name2...] [HAVING condition];
Types
(INNER) JOIN: Returns records that have matching values in both tables
LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records
from the right table
RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records
from the left table
FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table