Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The table should list several purchases made by few customers.
I have table
customer( customer_id, name, adress)
painting ( paint_id, artist_id, title)
sales( sl_id, customer_id, paint_id)
I need ( name, title)
Where there are 5 customers who have made 15 purchases.

What I have tried:

I tried
select name, paint_id
from customers
inner join sales ON paint_id = paint_id
Posted
Updated 8-May-20 4:53am
Comments
Richard MacCutchan 8-May-20 8:58am    
And what happened?
F-ES Sitecore 8-May-20 10:45am    
Google how to use the "having" clause.
[no name] 8-May-20 10:49am    
"Where there are 5 customers who have made 15 purchases":
So you are interested first in the purchases of the customer
-> JOIN the sales table with customer table
In case you are finally also interested in the paint table details
-> JOIN table painting to the above joined sales table
Zaidf 8-May-20 11:26am    
If I want to join, i have to type the details individually to match the name and the purchases made?
Richard Deeming 11-May-20 15:10pm    
Why have you tagged this as "C++"?

1 solution

There are no common columns between those 2 tables, so there is no way to directly join them; and that would throw an error upon execution.
If you were using an SQL IDE (eg SSMS), then intellisense would have pointed those out to you in the query text.

You also have a Sales table in there; and that does have references for both the Customer and the Painting. You could use this as a bridge table to make the connection from your Customers to your paintings.
SQL
FROM       Customer c
INNER JOIN Sales    s ON c.Customer_ID = s. Customer_ID
INNER JOIN Painting p ON s.Paint_ID    = p.Paint_ID
Going back to using an IDE dedicated for SQL to design your queries... You will notice that your SELECT list has Name highlighted. This is because it is a "Special" or "Reserved" word.
I generally will rename columns so that I don't have to deal with it, but if you do keep that column name it should be escaped when you use it within a query.
SQL
SELECT [name]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900