Selecting from one table, ordering according to other one

Posted on 2008-06-17
Medium Priority
Last Modified: 2010-04-21
tbl_rides: ID | more_fields
tbl_results: rideID | rank | more_fields

SELECT * FROM tbl_rides WHERE ID IN (SELECT rideID FROM tbl_results)

I need to order the above statement according to tbl_results.rank field desc. (Higher rank appears top). How is it done?

Question by:shmulik15
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 120

Accepted Solution

Rey Obrero (Capricorn1) earned 2000 total points
ID: 21804626
try this

SELECT tbl_rides.* , tbl_results.rank
FROM tbl_rides
inner join tbl_results on
order by tbl_results.rank desc
LVL 28

Expert Comment

by:Bill Bach
ID: 21804672
With SQL, you are dealing with "unordered sets" of data.  Therefore, when you build the set of data for the inner SELECT statement, order is meaningless.

To get the data exactly in the order you need, you must join the two sets first, then apply the order by, and then select out your fields.  It would look like this:

SELECT tbl_rides.*
FROM tbl_rides
INNER JOIN tbl_results ON tbl_rides.ID = tbl_results.rideID
ORDER BY tbl_results.rank

Please note that this may NOT give you back the exact same data set, as it may duplicate some data, you you may need to add a DISTINCT clause to it, but this will get you close.
LVL 51

Expert Comment

by:Mark Wills
ID: 21810090
If you want only those rows in tbl_rides where an ID exists in tbl_results, then an inner join is the way to go. Otherwise you may want to consider a left outer join...

and just realised I type the same code as capricorn1 for the inner join...

SELECT tbl_rides.* , tbl_results.rank
FROM tbl_rides
inner join tbl_results on tbl_rides.ID=tbl_results.rideID
order by rank desc

so, the outer join, putting unranked rides at the bottom...

SELECT tbl_rides.* , iif( isnull(tbl_results.rank),0,tbl_results.rank) as RANK
FROM tbl_rides
left outer join tbl_results on tbl_rides.ID=tbl_results.rideID
order by rank desc
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21810302
just for the "fun", you CAN of course do it without a "join" :)
FROM tbl_rides r
WHERE r.ID IN (SELECT rideID FROM tbl_results)
ORDER BY (SELECT TOP 1 rank FROM tbl_results re WHERE re.rideID = r.ID )
resp with a exists:
FROM tbl_rides r
WHERE EXISTS( SELECT null FROM tbl_results re WHERE re.rideID = r.ID )
ORDER BY (SELECT TOP 1 rank FROM tbl_results re WHERE re.rideID = r.ID )

Open in new window


Author Closing Comment

ID: 31468013
Thanks a lot, your help is appreciated.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question