Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have customer table and product table.
SQL
Customer table column : (c_id,f_name,l_name,email,group,state, c_id is primary key)

Product Table column: (p_id,c_id,p_name,p_desc,time,c_id is foreign key).


I want to know how to get the result for below question:

1)display all of the products that each customer is buying.

2)Using question(1) display how many customers are from each state from this query.

I am just getting into sql and started learning myself.I dont know weather I am correct with the below queries. Please do help me.
Thanks for the help.

What I have tried:

Answer:

1st question answer:
SQL
SELECT DISTINCT p_name FROM PRODUCT;


2nd question answer :

SQL
SELECT A.state,B.p_name,count(A.state) AS Total 
FROM CUSTOMER AS A 
INNER JOIN PRODUCT AS B 
ON A.c_id=B.c_id GROUP BY STATE;
Posted
Updated 19-May-21 6:59am

1. Display all of the products that each customer is buying.

*I assume that you want get the list of all the product that bought by each customer.
SQL
SELECT c_id, p_name
FROM PRODUCT
GROUP BY c_id


2. Using question(1) display how many customers are from each state from this query.
SQL
SELECT p.p_name, c.state
FROM PRODUCT p JOIN CUSTOMER c
ON p.c_id = c.c_id


Anyway, the tables don't fulfill the Normalization.
You should separate the into 3 tables, which are Customers, Products and Orders. Don't store the Products and the Orders into 1 table. Because if you delete the product, then the Orders' data will be gone too.

Hope this helps.
 
Share this answer
 
v2
Quote:
I don't know weather I am correct with the below queries.
There is an easy way to know if a query is correct or not: Just give it a try and you will see if the answer is as expected or not.
Experimenting is a great way to learn. To test a query, start by setting data that reflect 1 of the situations to handle then launch the query and check the answer, then start again with next dataset.

Here is a great tutorial site
SQL Tutorial[^]
 
Share this answer
 
v2
Write SQL statement that will lists the number of product foe each supplier
 
Share this answer
 
Comments
Richard Deeming 20-May-21 11:48am    
Your homework assignment is not a "solution" to this old, already-solved question.

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