user3311539
user3311539

Reputation:

logic behind SQL code not clear

I have come across an SQL code as below

SELECT  DISTINCT FLD1, FLD2, FLD3, FLD4 FROM            
TBL1 WHERE FLD1 = 'MFG' AND FLD2 = '1'

My doubt is on the DISTINCT keyword here applied before FLD1. There is a filtration condition to select only those records where FLD1 = 'MFG'. So does the distinct make any difference there?

I have run the same SQL without the DISTINCT and the number of records retrieved is the same with the DISTINCT.

This is written for DB2 database on iSeries.

Upvotes: 0

Views: 56

Answers (1)

kritikaTalwar
kritikaTalwar

Reputation: 1740

it eliminates all the duplicate records and fetch only unique records.

example:

Table Employee

Id     Name      Salary
1      Alex      2000
2      Alxender  1000
3      Paul      2000
4      Alex      2000
select distinct Salary 
from Employees;

it will return:

Salary
2000
1000
select distinct Name,Salary 
from Employees;

will return:

Name      Salary
Alex      2000
Alxender  1000
Paul      2000

The query:

select distinct Salary,Name 
from Employees 
where salary = 2000;

will return:

Name      Salary
Alex      2000    
Paul      2000

Upvotes: 3

Related Questions