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 > Data Access, Manipulation & Batch Languages > ANSI SQL > Subquery in the SELECT list

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-20-04, 00:00
Nerddette Nerddette is offline
Registered User
 
Join Date: Apr 2004
Location: Australia
Posts: 7
Subquery in the SELECT list

My Uni assignment asks me to:

"Write a query that has a subquery in its SELECT list." It then says "a subquery in a SELECT list usually makes more sense if it is correlated however subqueries don't have to be correlated if a fixed value is wanted. "

I have written my first attempt at the query that I want for this question:

Select count (*) As CountTable (
Select CompetitorNum, FamilyName, Gender, CountryCode
From Competitors
Where Gender = 'Male')

I have two questions:

1. Can you please explain to me what it means about subqueries being correlated?

2. The query I have written returns a table, with a count of 1 for the first half. The second half returns all the records which match the criteria. Why do I get the table of 1? How can I change this query so that it satisifies the criteria but doesn't just return a table of 1?

I have attached the database script if necessary. I am using both SQL Server and Postgres, but I'm analysing these purely from a SQL viewpoint that's why I didn't post under those forums.

Thanks!!

Nerddette
Reply With Quote
  #2 (permalink)  
Old 05-20-04, 10:50
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Correlated means that the subquery is restricted by a value from the main query. My understanding of "subquery in the select list" would be what Oracle (at least) calls a "scalar subquery". An example (correlated, too):
Code:
select dept.dname
,      (select count(*) from emp
        where  emp.deptno = dept.deptno /* This is the correlation */
       ) as headcount
from   dept;
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 05-20-04, 11:43
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
select dept.dname
     , ( select count(*) 
           from emp
          where emp.deptno 
              = dept.deptno   /* correlated */
       ) as headcount
     , ( select count(*) 
           from emp
          where emp.deptno 
              = dept.deptno ) * 100
     / ( select count(*) 
           from emp        /* not correlated */
        ) as deptpercent 
  from dept;
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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