Question

  Asked: Jun 18 2008   3:46 PM GMT
  Asked by: SQL Server Ask the Experts


Using co-related queries in SQL


SQL, Query, SQL Query, Co-related queries

How do you use co-related queries in SQL and why would they be useful? Is there any effect on performance when executing a co-related query?

Subscribe to Alerts! Get questions and answers delivered to your Inbox.


E-mail me updates on this question



   SUBSCRIBE

hidden modal window

Answer Wiki (Improve, edit or add to this answer)


 RATE THIS ANSWER
0
Click to Vote:
  •   0
  •  0



here's one example:
SELECT team
, player
, goals
FROM stats AS t
WHERE goals =
( SELECT MAX(goals) FROM stats WHERE team = t.team )
gets the team and player who scored the most goals for his team

why would this be useful? bcoz it just is!!! ;o)

effect on performance? yes, usually a join to a derived table is faster
SELECT t.team
, t.player
, t.goals
FROM stats AS t
INNER
JOIN ( SELECT team
, MAX(goals) AS maxgoals
FROM stats
GROUP BY team ) AS m
ON m.team = t.team
AND m.maxgoals = t.goals
  • AddThis Social Bookmark Button

Browse more Questions and Answers on Development.

Looking for relevant Development Whitepapers? Visit the SearchWinDevelopment.com Research Library.


Discuss This Answer


You must be logged-in to discuss a question. Log-in/Register