Sql Distinct

Use Sql Distinct to select unique rows of a table.

The syntax:

FROM   Table_name(s) 

The "column_list" after the DISTINCT keyword determine the uniqueness of the rows. If you select only one column, the Sql will select unique entries of that column. If you select two columns, the Sql will select unique combination of those two columns.

Often the Sql Distinct is combined with the Sql Count, so instead of getting the list, it will show the total number of distinct rows.

Let's use the CONTACTS table again for illustration. The task is to list the values of State in that table.

To do this write this sql:

FROM   contacts 

You get 3 rows in the result:


Let's experiment by adding Postcode to the column list.

Write the following Sql:

FROM Contacts

And the result has 4 rows:

NSW 2000
NSW 2010
VIC 3000
QLD 4225

Why the 'NSW' appears twice? It's because the program checks uniqueness row-wise,i.e. the combination of State and Postcode.

When you use DISTINCT keyword, make sure you dont include the Primary Key of the table (CPId in this case) in the column list, otherwise it will just list everything in that table. Because the Primary Key is unique for all rows.

Quick link. From here, you can learn the following:

Sql Select
The core of Sql, selecting data from table.

Sql Where
Conditionally selecting rows from table.

Sql Like
Pattern searching for conditions in Where or Having clause

Sql Between
Added to conditions to show results between two values.

Sql In
It enables us to specify multiple values, either as a list or a subquery.

Sql And Or
Connecting two or more conditions in the Where or Having clause.

Sql Count
Counting number of rows.

Sql Sum
Getting the sum of a field

Sql Max
This function will return the highest value.

Sql Min
This function will return the lowest value.

Sql Avg
This function will return the average value.

Sql Order By
Sorting the output

Sql Group By
Grouping rows into sets.

Sql Having
To filter the result of aggregation

Buy at AllPosters.com
A bend in the road is not the end of the road... unless you fail to make the turn

Positive Resources

All of us have at least two or three things that excite us. We could talk about these subjects for hours. Good news!. Did you know that Site Built It! can turn this type of deep interest and enthusiasm into a revenue-generating business?

Review some of the Site Build It! case studies and you'll absolutely discover something that ignites more positive ideas into your mind.

SBI! Video Tour

Invest the most
important 30 minutes
you will spend online.

Click, sit back, and
understand why SBI!
makes you succeed.