Counting
mysql>
mysql>
mysql>
mysql> CREATE TABLE sales_rep(
-> employee_number INT,
-> surname VARCHAR(40),
-> first_name VARCHAR(30),
-> commission TINYINT
-> );
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql>
mysql> INSERT INTO sales_rep values(4,'Rive','Mongane',10);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO sales_rep values(5,'Smith','Mike',12);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> SELECT COUNT(surname) FROM sales_rep;
+----------------+
| COUNT(surname) |
+----------------+
| 2 |
+----------------+
1 row in set (0.00 sec)
mysql>
mysql> SELECT COUNT(*) FROM sales_rep;
+----------+
| COUNT(*) |
+----------+
| 2 |
+----------+
1 row in set (0.00 sec)
mysql>
To count the number of distinct surnames, you combine COUNT() with DISTINCT, as follows:
mysql>
mysql> SELECT COUNT(DISTINCT surname) FROM sales_rep;
+-------------------------+
| COUNT(DISTINCT surname) |
+-------------------------+
| 2 |
+-------------------------+
1 row in set (0.00 sec)
mysql>
mysql> drop table sales_rep;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql>
Related examples in the same category