Showing posts with label fetch distinct records from sql. Show all posts
Showing posts with label fetch distinct records from sql. Show all posts

Thursday, July 21, 2011

Distinct in SQL


Distinct : The DISTINCT keyword can be used to return only distinct (different) values.

Orders table :
OrderId
PersonName
Price
Quantity
101
John
12
1
102
Sam
4
1
103
John
14
2
104
Brendon
12
1
106
Allen
2
11
107
Allen
2
11


Query 1:Select distinct PersonName from Orders
Query 2:Select distinct PersonName,Price from Orders


Query 1 : Even though price is different we used ‘distinct’ only on PersonName so it return only 4 records.
Query2 : Return 4 rows because Allen is repeated twice with same price so it returns Allen only Once