SQL DISTINCT SELECT values from multiple tables
SQL DISTINCT SELECT eliminates duplicate records from the results, return only distinct (different) values.
DISTINCT aggregates: COUNT, AVG, MAX, etc.
so, it operates on one column and does not support multiple columns Suppose that in a table a column may contain many duplicate values
therefore sometimes you only want to list the different (distinct) values.
As a result SQL DISTINCT SELECT General syntax
|
1 2 |
SELECT DISTINCT column-name FROM table-name |
|
1 2 |
SELECT COUNT (DISTINCT column-name) FROM table-name |
Problem: List all Employee countries in alphabetical order.
|
1 2 3 |
SELECT DISTINCT Country FROM Employee ORDER BY COUNTRY |
Result: 10 rows
| Country |
|---|
| Australia |
| Brazil |
| Canada |
| Kenya |
| …. |
| Employee |
|---|
| Id |
| Position |
| City |
| Country |
Problem: List the number of Employee countries
|
1 2 |
SELECT COUNT (DISTINCT Country) FROM Employee |
| Employee |
|---|
| 10 |
Resource
SQL