SQL: Functioning in the Real World - Count Distinct (
Page 4 of 5 )
Let's step away from the lawsuit scenario for a moment. Let's add a new column to our table called Position. Fill in your database with the added values below.
|
Name |
Age |
Salary |
Position |
|
Ang Ree |
92 |
$12,000 |
Director |
|
Godzilla Nokamura |
45 |
$50,000 |
Villain |
|
Mothra Fujiyama |
27 |
$80,000 |
Villain |
|
King Kong Wong |
19 |
$100,000 |
Hero |
|
Go-Go Roboto |
29 |
$100,000 |
Actor created solely for toy sales |
If we wanted to know how many unique positions there were (or any other unique items) we could use the Count(Distinct) function. As you can see, there are presently 5 positions in the company. What we want to know though is how many positions there are that don't duplicate themselves.
Select Count (Distinct Position) from Employees
The result:
4
This is because the Villain position is a duplicate, and thus one value is not unique.