Professional Documents
Culture Documents
Mysql 7
Mysql 7
Aggregate functions
MySQL aggregate functions retrieve a single value after
performing a calculation on a set of values.
GROUP BY clause
The usage of SQL GROUP BY clause is, to divide the rows in a
table into smaller groups.
The grouping can happen after retrieves the rows from a table.
When some rows are retrieved from a grouped result against some
condition, that is possible with HAVING clause.
The GROUP BY clause is used with the SELECT statement to
make a group of rows based on the values of a specific column or
expression.
Syntax:
SELECT <grouping column_list> |AGGREGATE FUNCTION
FROM < table name >
WHERE <condition>GROUP BY <columns>
[HAVING] <condition>;
egs)
mysql> mysql> SELECT gender,count(*) FROM emp GROUP BY
gender;
+--------+----------+
| gender | count(*) |
+--------+----------+
| m | 4 |
| f | 3 |
+--------+----------+
2 rows in set (0.01 sec)
mysql> SELECT gender,count(*) FROM emp GROUP BY gender
HAVING count(*)>3;
+--------+----------+
| gender | count(*) |
+--------+----------+
| m | 4 |
+--------+----------+
1 row in set (0.00 sec)
+-------------+----------+
| designation | max(sal) |
+-------------+----------+
| manager | 30000.00 |
| accountant | 20000.00 |
| clerk | 25000.00 |
| researcher | 20000.00 |
| salesman | 18000.00 |
+-------------+----------+
5 rows in set (0.00 sec)
+-------------+----------+
| designation | max(sal) |
+-------------+----------+
| manager | 30000.00 |
| clerk | 25000.00 |
+-------------+----------+
2 rows in set (0.00 sec)