[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


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

Industry Leaders: 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

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

650 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