"Real-world" SQL Examples and Solutions
From Internet Newsgroups

Sample questions

The following questions were taken from various database-related newsgroups on UNIX. Responses are shown as links to each question.

Posted solution, example 9

Posted Solutions

Possible solution to example 1, posted by a US user

It may be impossible if you want to get the result set from a single SQL statement. But by using such set operators as UNION you can meet the target. The statement has been listed below and it has been written in Sybase format.

    select c.nr, r.regcode
    from customer c, registration r
    where c.nr *= r.customer_nr
    UNION
    select r.customer_nr, rc.regcode
    from registrationcode rc, registration r
    where rc.regcode *= r.regcode
Return to example 1
Possible solution to example 2, posted by a US user

You're right. The number of returned records is not a coincidence. You are getting a Cartesian join (every record in one table joined with every record in another table). You are using the wrong syntax in your query. The way to specify that you are joining tables based on equality in certain fields is by using a JOIN statement. The syntax is

SELECT field1, field2 
FROM table1 INNER JOIN table2 ON
table1.field3 = table2.field3;
This returns records where the values of field3 are the same in both tables. LEFT JOIN will return rows from table1 even if there's no matching value in table2. RIGHT JOIN is the opposite. Since you are using four tables you need a series of three nested joins. For three tables it could look like this:
FROM (t1 LEFT JOIN t2 ON t1.f1 = t2.f1) 
LEFT JOIN t3 ON t2.f2 = t3.f2;
This one executes the join in parens first.

BTW, it would have been really fun with 5000 and 10000 records instead of 4 and 7! ;-)

Return to example 2


Possible solutions to example 3

Return to example 3


Possible solution to example 4, posted by a US user

SELECT Table1.AccountDate, Table2.AccountDate 
FROM Table1 LEFT JOIN Table2 ON 
Table1.AccountDate = Table2.AccountDate WHERE 
(((Table2.AccountDate) Is Null));

Return to example 4


Possible solution to example 5, posted by a US user
select distinctrow t1.*
from 
(((t1 left join t2 on t1.id = t2.id) 
  left join t3 on t1.id = t3.id)
  left join t4 on t1.id = t4.id) 
  left join t5 on t1.id = t5.id
where t2.id is null and t3.id is null and t4.id is null 
and t5.id is null
Return to example 5
Possible solution to example 6, posted by a user in Norway
select count(*) SEQ, a.BAR
from FOO a, FOO b
where a.BAR >= b.BAR
group by a.BAR
Return to example 6
Possible solutions to example 7, posted by a user in Australia

as i read this, you want one numeric result that is the total number staff members who have one and only one comment. if so:

select count(*)
from (select staff_id
        from comments
        where dept_no = 'G33'
        group by staff_id
        having count(*) = 1)
i'm pretty sure that you can also can use nested group functions, too, something like:
select count(count(*))
from comments
where dept_no = 'G33'
group by staff_id
having count(*) = 1
not in front of a SQL prompt to check, though. or you could try:
select count(distinct staff_id)
from comments
where dept_no = 'G33'
  and staff_id not in (select staff_id
                        from comments
                        where dept_no = 'G33'
                        group by staff_id 
                        having count(*) > 1)
there's a million of 'em.....

Return to example 7


Possible solutions to example 8

Return to example 8
Possible solution to example 9, posted by a Czech user

the following should do the trick. In fact, I tested it and it works. The only problem is that there may be more film makers with the maximum number of films (hence the "in" operator). If you want just one of them, add rownum=1 to the outer where condition.

select maker_name
from film_maker
where filmmaker_id in (
   select filmmaker_id from film
   group by filmmaker_id
   having count(*)= (
           select max(count(*))
           from film
           group by filmmaker_id));
Return to example 9
Page maintained by evelyn@udel.edu
Last modified on May 12, 1998