Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

query on old employee table Expand / Collapse
Author
Message
Posted Thursday, May 27, 2010 8:58 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, November 08, 2010 10:45 PM
Points: 122, Visits: 70
Hi all

one more tricky query I have encountered..(@ an interview)..

we have employee table with manager name and salary..

now the question is..write a query to display employee records order by salary
and the manager record should be the first record in the result...



Thanks ,

Shekhar

Post #929050
Posted Thursday, May 27, 2010 9:00 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:42 AM
Points: 12,744, Visits: 31,078
select 1 as myorderBy,name,salary from sometable where manager='Y'
UNION ALL
select 2 as myorderBy,name,salary from sometable where manager='N'
ORDER BY myorderBy,salary



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #929051
Posted Thursday, May 27, 2010 9:03 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, November 08, 2010 10:45 PM
Points: 122, Visits: 70
hey Lowell


that was awe some... thank u very much.. :)



Thanks ,

Shekhar

Post #929058
Posted Thursday, May 27, 2010 9:14 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:42 AM
Points: 12,744, Visits: 31,078
another way without the UNION:
select 
CASE
WHEN manager='Y'
THEN 1
ELSE 2
END
as myorderBy,name,salary
from sometable
ORDER BY myorderBy,salary



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #929073
Posted Friday, May 28, 2010 12:38 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, November 08, 2010 10:45 PM
Points: 122, Visits: 70
Thank you very much lowell... :)


Thanks ,

Shekhar

Post #929477
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse