![]() |
|
|
|||||||
![]() |
MCSD - Count(*) in a Subquery with multiple tables: How does SQL determine which table to generate the Count() from? |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
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 |
|
|
|
|
#2 |
|
Posts: n/a
|
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 |
|
|
|
#3 |
|
Posts: n/a
|
>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 |
|
|
|
#4 |
|
Posts: n/a
|
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 |
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |