Wednesday 29 May 2013

Fetch the top 3 maximum salaries wit out duplicates ?

CREATE TABLE sal 
  ( 
     name CHAR(5), 
     sal  INT 
  ) 

INSERT INTO sal 
VALUES     ('i', 
            500), 
            ('h', 
             300), 
            ('c', 
             300), 
            ('d', 
             400), 
            ('e', 
             100), 
            ('f', 
             200), 
            ('g', 
             400), 
            ('h', 
             300), 
            ('i', 
             500) 

-- To fetch the top 3 maximum salaries 
SELECT TOP 3 sal 
FROM   sal 
ORDER  BY sal DESC 

-- To fetch the top 3 maximum salaries with out duplicates 
SELECT TOP 3 sal 
FROM   sal 
GROUP  BY sal 
ORDER  BY 1 DESC 

No comments:

Post a Comment