Click here to Skip to main content
15,914,225 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The following solution is for getting 6th highest salary from Employee table ,

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

In above query what is the meaning of 'a'
Posted

'a' stands for a alias. You can use any valid variable name instead of 'a' and the result will be the same.

The pseudo code equivalent to the above query which will help you understand.....
SQL
SELECT DISTINCT TOP 6 salary
FROM employee
ORDER BY salary DESC as  a

-- The result of above query is assigned to 'a' and then the 'a' is queried as shown below.

SELECT TOP 1 salary
FROM a
ORDER BY salary
 
Share this answer
 
Comments
Mehdi Gholam 16-Aug-12 6:30am    
5'ed, just posted the same!
ridoy 16-Aug-12 6:39am    
good explanation..
bbirajdar 16-Aug-12 7:22am    
Thank you :)
"a" is an alias usually you would write "as a" for clarity. This is used if you need to reference inner query columns like:
SQL
select t.name from (select * from tablename) as t
 
Share this answer
 
Comments
bbirajdar 16-Aug-12 6:34am    
Yes.. correct answer.. +5
ridoy 16-Aug-12 6:40am    
it's right too..
a its just an alias.

For the inner subquery, you are just setting an alias as a.

This is similar to how we are setting alias to table.
SQL
select * from tbl a.


here 'a' is an alias for table tbl...
Like this 'a' is an alias for the query

SQL
SELECT DISTINCT TOP 6 salary
FROM employee
ORDER BY salary DESC


You can set any name to the alias.
 
Share this answer
 

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