Go Back   Velocity Reviews > Newsgroups > MCSD
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

MCSD - Count(*) in a Subquery with multiple tables: How does SQL determine which table to generate the Count() from?

 
Thread Tools Search this Thread
Old 12-23-2004, 10:22 PM   #1
Default Count(*) in a Subquery with multiple tables: How does SQL determine which table to generate the Count() from?


I am doing an exercise on subqueries from the Microsoft course # 2071.
There are 3 tables that I need to query in order to return 4 columns:
title_no, title, isbn and Total Reserved. The Total Reserved column is the
per-isbn(book) count of books on reserve with more than 50 reservations and
less than 5 copies of the book.

The tables are as follows
Reservation Title Loan
--------------- ------ --------
isbn title_no isbn
member_no title copy_no
log_date author title_no
log_date synopsis member_no
out_date
due_date

Here's the solution. My question is: How does SQL determing which table's
rows to count?

Select t.title_no, title, l.isbn, count(*) as 'Total Reserved'--how does sql
know which table to count from?
from title AS t
inner join loan AS l
ON t.title_no=l.title_no
inner join reservation r
ON l.isbn=r.isbn
WHERE r.isbn IN
(select isbn
from reservation
group by isbn
having Count(*)>50)
And l.copy_no<5
Group By t.title_no, title, l.isbn




Kaimuri
  Reply With Quote
Old 12-24-2004, 04:35 AM   #2
Cindy Winegarden
 
Posts: n/a
Default Re: Count(*) in a Subquery with multiple tables: How does SQL determine which table to generate the Count() from?
Hi Kaimuri,

Imagine that when SQL joins tables it makes one long row out of the matching
rows from the two (or more) tables. Then for "Count(*)" it just counts the
rows. However, if you specify Count(Distinct Tablex.Isbn) then it counts
something more specific.

--
Cindy Winegarden MCSD, Microsoft Visual FoxPro MVP
www.cindywinegarden.com


"Kaimuri" <> wrote in message
news:%...
> ...How does SQL determing which table's rows to count?
>
> Select t.title_no, title, l.isbn, count(*) as 'Total Reserved'--how does
> sql know which table to count from?....





Cindy Winegarden
  Reply With Quote
Old 12-27-2004, 11:03 AM   #3
The Poster Formerly Known as Kline Sphere
 
Posts: n/a
Default Re: Count(*) in a Subquery with multiple tables: How does SQL determine which table to generate the Count() from?
>Imagine that when SQL joins tables it makes one long row out of the matching
>rows from the two (or more) tables.


.... To clarify, where 'tables' mean the resulting tuples from one or
more query parts in the statement.

Too often the term 'table' is used to describe a relation, which is
incorrect.

>Then for "Count(*)" it just counts the
>rows. However, if you specify Count(Distinct Tablex.Isbn) then it counts
>something more specific.


.... and in the process, excludes non null values from the result.

Kline Sphere (Chalk) MCNGP #3


The Poster Formerly Known as Kline Sphere
  Reply With Quote
Old 12-29-2004, 06:38 PM   #4
Kaimuri
 
Posts: n/a
Default Re: Count(*) in a Subquery with multiple tables: How does SQL determine which table to generate the Count() from?
Thanks a bunch..Cindy and Kline!

"The Poster Formerly Known as Kline Sphere" <.> wrote in message
news:...
> >Imagine that when SQL joins tables it makes one long row out of the
> >matching
>>rows from the two (or more) tables.

>
> ... To clarify, where 'tables' mean the resulting tuples from one or
> more query parts in the statement.
>
> Too often the term 'table' is used to describe a relation, which is
> incorrect.
>
>>Then for "Count(*)" it just counts the
>>rows. However, if you specify Count(Distinct Tablex.Isbn) then it counts
>>something more specific.

>
> ... and in the process, excludes non null values from the result.
>
> Kline Sphere (Chalk) MCNGP #3





Kaimuri
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
MCITP and stored procedure permissions Darrilgibson@gmail.com MCITP 5 06-07-2008 12:37 PM
Uploading Excel file into sql table shalim Software 1 10-31-2006 08:57 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46