Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have some doubt for retrieving nth height salary from table i write query its working f9

i wrote code shown below it's working but i have one doubt why we are using at last
VB
a
ORDER BY salary
in 'a' tell me plzzzzzzzzzzzz


SELECT TOP 1 salary
FROM (
SELECT DISTINCT TOP 5 salary
FROM employee
ORDER BY salary DESC) a
ORDER BY salary
Posted
Comments
Prerak Patel 24-Feb-11 5:44am    
you don't need "ORDER BY salary" at end.

CREATE TABLE Employee
(
Id bigint identity not null primary key,
Name nvarchar(255) not null,
Salary DECIMAL not null
)
go

INSERT INTO Employee(Name,Salary) VALUES('Emp1',100001)
go
INSERT INTO Employee(Name,Salary) VALUES('Emp2',100002)
go
INSERT INTO Employee(Name,Salary) VALUES('Emp3',100003)
go
INSERT INTO Employee(Name,Salary) VALUES('Emp4',100004)
go
INSERT INTO Employee(Name,Salary) VALUES('Emp5',100005)
go
INSERT INTO Employee(Name,Salary) VALUES('Emp5-1',100005)
go
INSERT INTO Employee(Name,Salary) VALUES('Emp6',100006)
go
INSERT INTO Employee(Name,Salary) VALUES('Emp6-1',100006)
go
INSERT INTO Employee(Name,Salary) VALUES('Emp7',100007)
go


Executing
SELECT TOP 1 salary
 FROM (
  SELECT DISTINCT TOP 5 salary
  FROM employee
  ORDER BY salary DESC) a
ORDER BY salary

returns the much hoped for salary 100003 - the 5th highest salary.

Removing the 'a' gives us the error: Incorrect syntax near the keyword 'ORDER' since the it's needed by the SQL parser a as a placeholder for the result of the inner select.

http://www.4guysfromrolla.com/webtech/010406-1.shtml[^]

MS SQL Server supports the standard fast variant[^]

Apart from that, putting the salary in the employee table strikes me as a strange design - or is it a temporary table?

I would rather have devided the design into several tables - something like:
Person (Id integer NOT NULL PRIMARY KEY, ...)
Position( Id integer NOT NULL PRIMARY KEY, ...)
Wages(Id integer NOT NULL PRIMARY KEY, Salary DECIMAL ,...)

Employment(Id integer NOT NULL PRIMARY KEY,PersonId integer, PositionId integer, DateTime2 NOT NULL, ThroughTime DateTime2,...)

PositionWage(Id integer NOT NULL PRIMARY KEY, PositionId Integer, WageId integer,FromTime DateTime2 NOT NULL, ThroughTime DateTime2, .... )

This will allow:
a person to hold multiple positions - change jobs.

a position to be filled by multiple employees - and
reflect changes over time - the position stays even
if it's filled by a different person

allow wages to change over time

A "real" solotion needs to be quite a bit more complicated to deal with the "real world" - My point is that having salary as a member of employee will cause your system to loose valuable information over time.

Most things are temporal in nature, your database design should be able to deal with this without loosing/overwriting valuable information.


Regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 24-Feb-11 6:43am    
Espen, OP was asking for the nth highest salary so your SQL does not solve OP's problem. He said his example was already working but he wanted an explanation what the 'a' was for.
Espen Harlinn 24-Feb-11 6:50am    
You're right Manfred - just looked at the somewhat odd sql, and started to wonder about the design
Espen Harlinn 24-Feb-11 7:21am    
Updated to answer the actual question :)
Manfred Rudolf Bihy 24-Feb-11 7:30am    
Good answer! 5+
Espen Harlinn 24-Feb-11 7:38am    
Thanks Manfred!
To be sure and correct, it should be:
SQL
SELECT TOP 1 salary
FROM (
       SELECT DISTINCT TOP 5 salary
       FROM employee
     ) a
ORDER BY salary DESC

OR
SQL
SELECT TOP 1 salary
FROM (
       SELECT DISTINCT TOP 5 salary
       FROM employee
       ORDER BY salary DESC
      ) a


'a' can be considered as a alias/temporary name given to the resultset formed from the inner query.

Take an example and see for yourself.
 
Share this answer
 
v2
Comments
Rupa1 24-Feb-11 6:03am    
ya i got but y we are using 'a'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900