SQL SELECT DISTINCT Statement
- SELECT DISTINCT returns only distinct (different) values.
- SELECT DISTINCT eliminates duplicate records from the results.
- DISTINCT can be used with aggregates: COUNT, AVG, MAX, etc.
- DISTINCT operates on a single column. DISTINCT for multiple columns is not supported.
The SQL SELECT DISTINCT syntax
The general syntax is:
- SELECT DISTINCT column-nam FROM table-name
Can be used with COUNT and other aggregates
- SELECT COUNT (DISTINCT column-name) FROM table-name
SQL SELECT Examples
1) The general syntax
mysql> select distinct name from person;
+-----------------+
| name |
+-----------------+
| subham ball |
| jit |
| sajal |
| sourav biswas |
| chayan das |
| shantanu biswas |
| sudip saha |
| debo |
| NULL |
| mono |
| monojit |
+-----------------+
11 rows in set (0.00 sec)
2) with COUNT and other aggregates
mysql> select count(distinct name) from person;
+----------------------+
| count(distinct name) |
+----------------------+
| 10 |
+----------------------+
1 row in set (0.00 sec)
+----------------------+
| count(distinct name) |
+----------------------+
| 10 |
+----------------------+
1 row in set (0.00 sec)
Comments
Post a Comment