Thursday, 15 December 2011

SQL Distinct

The SELECT keyword allows us to grab all information from a column (or columns) on a table. This, of course, necessarily mean that there will be redundancies. What if we only want to select each DISTINCT element? This is easy to accomplish in SQL. All we need to do is to add DISTINCTafter SELECT. The syntax is as follows:

SELECT DISTINCT "column_name"
FROM "table_name"

For example, 



custcode
custname
city
qty
rate
1
Siva
Srivilliputtur
50
70
2
Bala
Sivakasi
30
56
3
Kanna
Madurai
100
200
4
Vijay
Chennai
30
30
5
Kodee
Srivilliputtur
60
30
5
Kodee
Srivilliputtur
60
30




Ex:1

Select Distinct city from custmast

Output:
city
Srivilliputtur
Sivakasi
Madurai
Chennai
  

Ex:2

Select Distinct * from custmast

OutPut :

custcode
custname
city
qty
rate
1
Siva
Srivilliputtur
50
70
2
Bala
Sivakasi
30
56
3
Kanna
Madurai
100
200
4
Vijay
Chennai
30
30
5
Kodee
Srivilliputtur
60
30



No comments:

Post a Comment


”Back