Question:
SQL Subquery question for assignment?
JohnSmith
2011-04-26 21:41:34 UTC
The question is:

List the authors ho have written at least one "Popular Computing" book. Do NOT use a Join. Show AuthorID and their name.

here is what I have for a query so far..

select au_id [Author ID], au_fname + ' ' + au_lname [Author's Name]
from authors
where.......????

The database has 2 tables for this question. First table (authors) stores the author information. The second table (titles) stores the type of book being sold (type). I'm not sure how to reference the title table through a subquery on this question. Any help is appreciated.
Three answers:
kevin c
2011-04-29 12:40:02 UTC
I find the "IN" statement with subqueries to be horrid on some DB/s, and also that's a mighty interesting assignment constraint to not use joins... oh well. This doesn't use the JOIN verb, but it does check for au_id.titles = table.titles; You might want to try:



SELECT au_id S "Author ID", au_fname + ' ' + au_lname AS "Author's Name", count(*)

FROM authors, table

WHERE table.type = "Popular Computing" AND au_id.titles = table.titles

HAVING count(*) >= 1
TheMadProfessor
2011-04-27 13:30:14 UTC
The above will work but requires the subquery be evaluated for every row of the primary query...I prefer the method below as it only requires the subquery be evaluated once (to build the criteria list):



SELECT au_id AS "Author ID", au_fname + ' ' + au_lname AS "Author's Name"

FROM authors WHERE au_id IN

(SELECT DISTINCT au_id FROM titles WHERE type = "Popular Computing")
Serge M
2011-04-27 06:12:40 UTC
select au_id [Author ID], au_fname + ' ' + au_lname [Author's Name]

from authors

where exists (select 1 from titles where type='Popular Computing' and titles.au_id = authors.au_id)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...