SQL SELECT COUNT, SUM, AVG
- SELECT COUNT returns a count of the number of data values.
- SELECT SUM returns the sum of the data values.
- SELECT AVG returns the average of the data values.
The SQL SELECT COUNT, SUM, and AVG syntax
The general COUNT syntax is:
- SELECT COUNT(column-name) FROM table-name
The general SUM syntax is:
- SELECT SUM(column-name) FROM table-name
The general AVG syntax is:
- SELECT AVG(column-name) FROM table-name
SQL SELECT COUNT, SUM, and AVG Examples
1) example of Count Statement
mysql> select count(name) from person;
+-------------+
| count(name) |
+-------------+
| 11 |
+-------------+
1 row in set (0.00 sec)
+-------------+
| count(name) |
+-------------+
| 11 |
+-------------+
1 row in set (0.00 sec)
2) Example of Sum Statement
mysql> select sum(age) from person;
+----------+
| sum(age) |
+----------+
| 226 |
+----------+
1 row in set (0.00 sec)
+----------+
| sum(age) |
+----------+
| 226 |
+----------+
1 row in set (0.00 sec)
5) Example of Sum Statement with Distinct Clause
mysql> select sum(distinct age) from person;
+-------------------+
| sum(distinct age) |
+-------------------+
| 179 |
+-------------------+
1 row in set (0.00 sec)
3) Example of AVG Statement
+-------------------+
| sum(distinct age) |
+-------------------+
| 179 |
+-------------------+
1 row in set (0.00 sec)
3) Example of AVG Statement
mysql> select avg(age) from person;
+----------+
| avg(age) |
+----------+
| 22.6000 |
+----------+
1 row in set (0.00 sec)
4) Example of AVG statement with distinct Clause
mysql> select avg(distinct age) from person;
+-------------------+
| avg(distinct age) |
+-------------------+
| 22.3750 |
+-------------------+
1 row in set (0.00 sec)
+-------------------+
| avg(distinct age) |
+-------------------+
| 22.3750 |
+-------------------+
1 row in set (0.00 sec)
Comments
Post a Comment