To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here

HOME News MS SQL Oracle DB2 Access MySQL PHP Scripts Books Links DBA Talk


Go Back   Database Journal Forums > Related Sites > SQL Course

SQL Course SQL Course > Ask questions about the lessons on SQL Course 1 and 2. If you have problems > with the interface, please post in the Feedback forum

Reply Post New Thread
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old 02-15-2003, 03:00 AM
janicewt janicewt is offline
Junior Member
 
Join Date: Feb 2003
Posts: 1
Select from 1 table using max value from another table

Hi, I have two simple tables T1 and T2 as follows:

T1 T2
sid results sid name
1 90 1 John
2 100 2 Mary
3 80 3 Peter
4 92 4 Jim

I want to select the name of the student who has the highest score, and I wrote:

SELECT name
FROM T2
WHERE sid =
(
SELECT sid
FROM T1
WHERE results =
(
SELECT MAX(results)
FROM T1
)
)

is there any more efficient way of doing this? Thanks!
Reply With Quote
  #2  
Old 02-18-2003, 05:20 AM
PinkPanther2003 PinkPanther2003 is offline
Senior Member
 
Join Date: Feb 2003
Posts: 102
Subquery In WHERE

Yep,

Use a join.

SELECT t2.Name
FROM t2 INNER JOIN t1
ON t2.SID = t1.SID
WHERE t1.Results = (SELECT MAX(Results) as MaxResult FROM t1)

This method should be faster on most RDBMS's.

HTH,

Peter
Reply With Quote
Reply Post New Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Forum Jump


All times are GMT -5. The time now is 10:16 PM.


DatabaseJournal Recent Articles


 » Grouping with SQL Server 2008

 » Conducting Service Broker Conversation Usi...

 » SQL Server Consolidation Initiative Under ...

 » Transaction Log Growth, do you need it?

 » New FileMaker Pro 10 Ships With Sleek New ...

Search Database Journal:
 





Acceptable Use Policy

JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers

Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.