inner join sales and products - sql

hi i am having a problem with an inner join on my products and sales table i need to display a list of all the sales and also list the products which havn't been selling
this is the cod i have been trying-->
SELECT
products.prod_id,
products.prod_name,
sales.date_of_sale,
FROM
products
INNER JOIN
sales
ON
products.prod_id = sales.prod_id;
here is a picture of products table
here is a picture of the sales table
any help would be greatly appreciated.. thank you very much ..

To display the product and its sales
SELECT products.prod_id,
products.prod_name,
sales.date_of_sale
FROM products
INNER JOIN sales
ON products.prod_id = sales.prod_id;
To display products which don't have sales
SELECT products.prod_id,
products.prod_name
FROM products
WHERE NOT EXISTS (SELECT 1
FROM sales
WHERE products.prod_id = sales.prod_id);
If you want select in a single query
SELECT products.prod_id,
products.prod_name,
sales.date_of_sale
FROM products
LEFT OUTER JOIN sales
ON products.prod_id = sales.prod_id;

Related

I've tried to retrieve the all the product and their total sales in the result

I have the Product table with the product name and ID then also have the receipt_product table with product ID and receipt ID.
And finally, have the receipt table with the amount and the receipt ID. So I've tried to retrieve all the products and their total sales in the result.
This is my query and I know that my query is wrong and need to do something to make it correct.
Tables
select
p.name, sum(total) as total sales
from
receipt_product rp
right join
product p on p.product_id = rp.product_id
inner join
receipt r on rp.receipt_id = r.receipt_id
group by
p.name
having
total_sales < 100
You have the price of individual product and the quantity sold . So why are you taking the receipt table in consideration.
Try something like this.
select sum(p.price * r.quantity_sold) as "Total Sales",p.name
from product p join receipt_product r
on p.product_id = r.product_id
group by p.name;

Joining 2 Many-To-Many Relationship Tables

Problem:
Find the net balance of the total order amount and total payments for each customer.
There are 4 tables involved: OrderDetails, Orders, Payments and Customer.
The total amount order = quantity order * price each [in OrderDetails]
The total payment = sum of different payments for the same order.
Customers are linked to Payments and Orders with CustomerNumber. Orders are linked to OrderDetails with OrderNumber.
I tried joining the 4 tables with the INNER JOIN function.
SELECT
c.customername,
SUM(od.quantityordered * od.priceeach) - SUM(p.amount) AS Net_Balance
FROM
(
(
orderdetails od
INNER JOIN orders o ON od.ordernumber = o.ordernumber
)
INNER JOIN customers c ON o.customernumber = c.customernumber
)
INNER JOIN payments p ON c.customernumber = p.customernumber
GROUP BY c.customername;
The expected results should be 0 for almost every customers.
However, what I have got is the total amount order and total payment are multiplied by some constants. Specifically, the total payment shown is multiplied by the number of payments for each order.
Anybody have any ideas to save my life?
This is a typical issue when dealing with N-M relationships. To solve it, one solution is to move the aggregation to subqueries:
SELECT c.customername, o.amt - p.amt AS Net_Balance
FROM customers c
INNER JOIN (
SELECT ord.customernumber, SUM(det.quantityordered * det.priceeach) as amt
FROM orders ord
INNER JOIN orderdetails det ON ord.ordernumber = det.ordernumber
GROUP BY ord.customernumber
) o ON o.customernumber = c.customernumber
INNER JOIN (
SELECT customernumber, SUM(amount) as amt
FROM payments
GROUP BY customernumber
) p ON p.customernumber = c.customernumber
SELECT c.customername, SUM(od.quantityordered*od.priceeach) as ordersum , SUM(p.amount) as paymentsum'
What's the result of the two columns ?
Is it what you want?

product which has sold the most inner join

I need to show the most popular selling item from the products table joining the sales table.
I need to display the most popular selling items.
Here's the code I've tried :
SELECT products.prod_id, products.prod_name, sales.quantity,
FROM products
INNER JOIN sales ON products.prod_id = sales.prod_id
Here are pics products and sales
products
sales
SELECT products.prod_id, products.prod_name, SUM(sales.quantity) as sales_total,
FROM products
INNER JOIN sales ON products.prod_id = sales.prod_id
GROUP BY products.prod_id, products.prod_name
ORDER BY sales_total DESC
SELECT products.prod_id, products.prod_name, sum(sales.quantity) as sales_total
FROM products
INNER JOIN sales ON products.prod_id = sales.prod_id
GROUP BY products.prod_id, products.prod_name
SELECT products.prod_id, products.prod_name, SUM(sales.quantity),
FROM products
INNER JOIN sales ON products.prod_id = sales.prod_id
GROUP BY prod_id
ORDER BY 3 DESC
LIMIT 25
You will want an index such as (prod_id, quantity) on sales.
SELECT b.prod_id, p.prod_name, b.Qty
FROM (
SELECT a.prod_id, a.Qty, MAX(a.Qty) OVER () AS MaxQty
FROM (
SELECT s.prod_id, SUM(s.quantity) AS Qty
FROM sales s
GROUP BY s.prod_id ) a ) b
INNER JOIN Products p ON p.prod_id = b.prod_id
WHERE b.Qty = b.MaxQty

SQL nested SELECT with JOIN

I wasted all the day on one query without success , SOS I need a help :) with a given #CustomerId , I need to query all the Products that linked to customer seller can sell but not sold to him before , the Commissions table is indication of what products seller can sell
Thanks in advance
SELECT sellableProduct
FROM (SELECT Comissions.ProductId AS sellableProduct, Sellers.SellerId AS sellableSeller FROM Comissions INNER JOIN Sellers ON Comissions.SellerId=Sellers.SellerId INNER JOIN Customers ON Sellers.SellerId=Customers.SellerId WHERE Customers.CustomerId = #customerid) AS tblSellable
LEFT JOIN (SELECT Sales.ProductId AS soldProduct, Customers.SellerId as soldSeller FROM Customers INNER JOIN Sales ON Customers.CustomerId=Sales.CustomerId WHERE Customers.CustomerId = #customerid) AS tblSold
ON tblSellable.sellableProduct=tblSold.soldProduct AND tblSellable.sellableSeller=tblSold.soldSeller
WHERE tblSold.soldProduct IS NULL
this way you avoid time-consuming IN statements
If a Customer can only have one Seller, then you can omit the seller link:
SELECT sellableProduct
FROM (SELECT Comissions.ProductId AS sellableProduct FROM Comissions INNER JOIN Sellers ON Comissions.SellerId=Sellers.SellerId INNER JOIN Customers ON Sellers.SellerId=Customers.SellerId WHERE Customers.CustomerId = #customerid) AS tblSellable
LEFT JOIN (SELECT Sales.ProductId AS soldProduct FROM Sales WHERE Sales.CustomerId = #customerid) AS tblSold
ON tblSellable.sellableProduct=tblSold.soldProduct
WHERE tblSold.soldProduct IS NULL
Basically, you're looking for products that have a record in commissions, but not in sales. Using :id to denote the specific ID:
SELECT *
FROM products
WHERE productid IN (SELECT productid
FROM commissions
WHERE sellerid = :id) AND
productid NOT IN (SELECT productid
FROM sales
JOIN customers ON sales.customerid = cusomers.customerid
WHERE sellerid = :id)
Would this work?
SELECT sell.*, prod.* FROM
Sellers sell
INNER JOIN Customers cust ON cust.SellerId = sell.SellerId
LEFT JOIN Commissions comm ON sell.SellerId = comm.SellerId
LEFT JOIN Products prod ON prod.ProductId = comm.ProductId
WHERE prod.ProductId NOT IN (
SELECT ProductId
FROM Products p INNER JOIN
Sales s ON s.ProductId = p.ProductId
WHERE s.CustomerId = #CustomerId
It get all the sellers and the respective product from commission, where the product Id is NOT associated with any sale of the client

SQL Join with SUM()

I have two Tables
Product Details(About Product)
Sale Order Details(What Price is sold, quantity of products sold per order).
I am trying to do Join on Table 1 and Table 2 which should give the all the product details and sum(Quantity), Sum(Price)
Problem Facing: There are some products in Table 1 which are never sold, and those rows are missing in the result set, but I want details for all the rows in Table 1 with rows of Products never purchased should be NULL or 'o'
Query I am Using:
select
P.*,
ISNULL((sum([Q.Quantity])),0),
ISNULL((sum([Q.Price])),0)
From Table1 P
Left Outer Join Table2 Q on P.Product_ID = Q.Product_ID
Please help me with any suggestions that would work for me
How about this one:
select
P.Product_ID,
ISNULL(sum([Q.Quantity]),0),
ISNULL(sum([Q.Price]),0)
From Table1 P
Left Outer Join Table2 Q
on P.Product_ID = Q.Product_ID
group by
P.Product_Id