tags:

views:

61

answers:

3

This is the DB Schema:

Books (bookid, title, author, year)
Customers (customerid, name, email)
Purchases (customerid, bookid, year)
Reviews (customerid, bookid, rating)
Pricing (bookid, format, price)

How do I find customers (show their names and email addresses) who purchased more than one book in year 2003?

Thanks!

+2  A: 

pretty much exactly like your english language question phrased it... just translated into SQL ...

Select * From Customers C
Where (Select Count(*) From Purchases
       Where customerid = C.customerid
           And Year = 2003) > 1
Charles Bretana
And yours woorks as well. +1 to you.
David Stratton
+4  A: 
SELECT name, email, COUNT(p.customerId) as purchases_in_2003
FROM Customers c
INNER JOIN Purchases p ON c.customerId = p.customerId
WHERE p.year = 2003
GROUP BY name, email
HAVING purchases_in_2003 > 1
soulmerge
you forgot the GROUP BY clause, but otherwise this is the best answer so far
kurosch
Thanks, updated answer
soulmerge
Deleted my own answer because I read the question wrong, and you answred correctly. +1 to you.
David Stratton
A: 

One more alternative:

select
  cu.name
 ,cu.email
 ,count(*) as books_purchased
from
  customers cu
 ,purchases pu
where cu.customerid = pu.customerid
  and pu.year = 2003
group by
  cu.name
 ,cu.email
having
  count(*) > 1
JosephStyons