Let us say Employee and Shippers tables have following data.
EmployeeID Name
1 Davolio
2 Fuller
3 Leverling
4 Peacock
5 Buchanan
6 Suyama
7 King
8 Callahan
9 Dodsworth
10 West
ShipperID ShipperName
1 Speedy Express
2 United Package
3 Federal Shipping
The below query returns ShipperID in descending order.
select ShipperID from Shippers order by ShipperID desc;
Now, I want to retrieve Names from the Employee table in the same order the ShipperID's are retrieved (3,2,1
). My expected output is Leverling, Fuller, Davolio
.
select Name from Employee where EmployeeID in (select ShipperID from Shippers order by ShipperID desc)
The above query is not returning the data as I expect. How to fix this?
UPDATE:
This is not about the ordering of records in ascending or descending order. This is just an example I have posted here. To make it more clear, assume that the subquery is returning ShipperID as 2,3,1
. Now I want to retrieve the records from Employee table like Fuller, Leverling, Davolio
in
ordered? You only want to see if theEmployeeID
is present, the order doesn't matter. If you want your results ordered, you should order your outer query – HoneyBadger Feb 23 '17 at 9:27