Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

MySQL aggregate functions and grouping

Aggregate functions
MySQL aggregate functions retrieve a single value after
performing a calculation on a set of values.

In general, aggregate functions ignore null values.

1. COUNT() - retrieves a count of the number of rows returned.

2. COUNT(DISTINCT) - retrieves the count of a number of


different values.

3. AVG() - retrieves the average value of the argument.

4. SUM() - retrieves the sum.

5. MAX() - retrieves the maximum value.

6. MIN() - retrieves the minimum value.


mysql> SELECT COUNT(*) FROM emp;
+----------+
| COUNT(*) |
+----------+
| 7 |
+----------+
1 row in set (0.02 sec)
mysql> SELECT COUNT(comm) FROM emp;
+-------------+
| COUNT(comm) |
+-------------+
| 2 |
+-------------+
1 row in set (0.00 sec)

mysql> SELECT COUNT(designation) FROM emp;


+--------------------+
| COUNT(designation) |
+--------------------+
| 7 |
+--------------------+
1 row in set (0.00 sec)

mysql> SELECT COUNT(distinct designation) FROM emp;


+-----------------------------+
| COUNT(distinct designation) |
+-----------------------------+
| 5 |
+-----------------------------+
1 row in set (0.01 sec)

mysql> SELECT SUM(sal) FROM emp;


+-----------+
| SUM(sal) |
+-----------+
| 146000.00 |
+-----------+
1 row in set (0.00 sec)

mysql> SELECT AVG(sal) FROM emp;


+--------------+
| AVG(sal) |
+--------------+
| 20857.142857 |
+--------------+
1 row in set (0.00 sec)
mysql> SELECT MAX(sal) FROM emp;
+----------+
| MAX(sal) |
+----------+
| 30000.00 |
+----------+
1 row in set (0.01 sec)

mysql> SELECT MIN(sal) FROM emp;


+----------+
| MIN(sal) |
+----------+
| 15000.00 |
+----------+
1 row in set (0.00 sec)

GROUP BY clause
The usage of SQL GROUP BY clause is, to divide the rows in a
table into smaller groups.

The GROUP BY clause is used with the SQL SELECT statement.

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.

The SQL AGGREGATE function can be used to get summary


information for every group and these are applied to an individual
group.

In an SQL statement, if you are using GROUP BY, HAVING is


used instead of WHERE, after GROUP BY.

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)

mysql> SELECT designation,max(sal) FROM emp GROUP BY


designation;

+-------------+----------+
| 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)

mysql> SELECT designation,max(sal) FROM emp GROUP BY


designation HAVING max(sal)>=25000;

+-------------+----------+
| designation | max(sal) |
+-------------+----------+
| manager | 30000.00 |
| clerk | 25000.00 |
+-------------+----------+
2 rows in set (0.00 sec)

You might also like