If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > subquery

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-23-11, 07:25
melgra70 melgra70 is offline
Registered User
 
Join Date: Mar 2011
Location: Sydney, Australia
Posts: 58
subquery

I'm writing my first subquery within an outer query and can't work out what I'm doing wrong:

from the entry table (event_id, horse_id, place)
and the horse table (horse_id, horse_name)
I have to list the horse_id and horse_name for each horse that has been placed in the top 3 (ie, got place 1, 2 or 3) two or more times.

this is my subquery, which worked:

select H.horse_id, H.horse_name
from entry E, Horse H
where E.horse_id = H.horse_id
and E.place < ‘4’;

this is the subquery within the outer query, which didn't work:

select H.horse_id, H.horse_name
from horse H
where 2 <= (select count(*)
from entry E, horse H
where E.horse_id = H.horse_id
and E.place < ‘4’);

Can anyone spot my error, please?
Reply With Quote
  #2 (permalink)  
Old 03-23-11, 08:28
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
Try this:

Code:
SELECT h.horse_id,
       h.horse_name
FROM   horse h,
       entry e
WHERE  e.horse_id = h.horse_id
       AND e.place <= 3
GROUP  BY h.horse_id,
          h.horse_name
HAVING COUNT(1) > 1;
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 03-23-11, 08:38
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by melgra70 View Post
Can anyone spot my error, please?
  • The subquery you have doesn't join to the horse table outside the brackets but to the horse table within the brackets.
  • As it-iss said you also don't need a subquery.
  • His query is far more efficient than yours (and easier to read).
  • Why is the '4' in quotes?
__________________
Mike
Reply With Quote
  #4 (permalink)  
Old 03-24-11, 07:01
melgra70 melgra70 is offline
Registered User
 
Join Date: Mar 2011
Location: Sydney, Australia
Posts: 58
subquery

Thanks, Ronan and Mike.
Your solution is great and works well, but this exercise requires that I show competency in subquery creation. Hence, I still need to manipulate my solution with the subquery added into an outer query. I've removed the '4'.

select H.horse_id, H.horse_name
from entry E, Horse H
where E.horse_id = H.horse_id
and E.place <=3;
Reply With Quote
  #5 (permalink)  
Old 03-24-11, 08:20
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by melgra70 View Post
this exercise requires that I show competency in subquery creation
I suggest you read the comments I made regards your code and fix it.
Alternatively you could just pay someone to do your homework for you.
__________________
Mike
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On