Beginning SQL - The Distinct Statement
(Page 3 of 4 )
Let's say now that Homer Sampson has a twin brother. And let's further say that Homer Sampson's parents wanted to add to the confusion, and, much like the great boxer George Foreman, decided to give all their children the same exact name (perhaps they too, had been hit in the head one too many times). Go ahead, add him to your table, with the position of CFO and the same salary as his brother.
If you were to print a last of first and last names, and their respective positions, it would look like this:
Select FirstName, LastName, Position from Employees;
First Name | Last Name | Position |
Larry | Smith | President |
Homer | Sampson | CEO |
Homer | Sampson | CFO |
Your | Name | Whipping Boy |
As you can see, there are two Homer Sampsons, with different positions within the company. Now say that you wanted to print just a list of first names, without having duplicates. For this you would need the Distinct statement.
Select DISTINCT FirstName from Employees;
This will give us a result that shows only first names that are different from one another.
As you can see, there is now only one Homer listed.
Next: Placing Data into a Database with SQL >>
More BrainDump Articles
More By James Payne