Use Sql Distinct to select unique rows of a table.
The syntax:
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:
You get 3 rows in the result:
State |
---|
NSW |
VIC |
QLD |
Let's experiment by adding Postcode to the column list.
Write the following Sql:
And the result has 4 rows:
State | Postcode |
---|---|
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
Return from Sql Distinct to Sql Commands
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 |
Golden Gate Bridge Poster
James, Vincent
Buy at AllPosters.com
Buy at AllPosters.com
Sydney Harbor Bridge, Sydney, Australia