MySQL LEFT JOIN Sales Report Exercise

This exercise uses products, sales and customers to build practical sales reports with aggregation and LEFT JOIN. The queries also show the anti-join pattern for finding products or customers with no matching sales.

When aggregate queries select nonaggregated columns, group by the columns that define the row. This keeps the examples compatible with MySQL's default ONLY_FULL_GROUP_BY behavior.
There are three tables in total
  • customers
  • c_id ,
    customer
    ( 8 records)
  • products
  • p_id,
    product and price
    ( 8 records )
  • sales
  • sale_id,
    c_id ( customer id ), p_id (product_id), qty ( quantity sold) ,store ( name )
LEFT JOIN ( Basic Query) You can download SQL Dump of these three tables here.
From the above data we will produce following reports.
  1. Exercise on LEFT JOIN
  2. List of products sold
  3. List of quantity sold against each product.
  4. List of quantity and total sales against each product
  5. List of quantity sold against each product and against each store.
  6. List of quantity sold against each Store with total turnover of the store.
  7. List of products which are not sold
  8. List of customers who have not purchased any product.


Joining tables Using SQL LEFT Join, RIGHT Join and Inner Join

1.List of products sold

SELECT p_id, product FROM sales GROUP BY p_id, product ORDER BY product;
productp_id
CPU4
Monitor3
RAM2

2.List of quantity sold against each product.

SELECT p_id, product, SUM(qty) AS total_qty FROM sales GROUP BY p_id, product ORDER BY product;
productp_idsum(qty)
CPU41
Monitor312
RAM27

3. List of quantity and total sales against each product

SELECT a.p_id, a.product, SUM(a.qty) AS total_qty,
       SUM(a.qty * b.price) AS total_sales
FROM sales AS a
LEFT JOIN products AS b ON a.p_id = b.p_id
GROUP BY a.p_id, a.product
ORDER BY a.product;
productp_idsum(qty)sum(qty*price)
CPU4155
Monitor312900
RAM27630

4. List of quantity sold against each product and against each store.

SELECT product, store, SUM(qty) AS total_qty FROM sales GROUP BY product, store ORDER BY product, store;
productstoresum(qty)
CPUDEF1
MonitorABC10
MonitorDEF2
RAMABC3
RAMDEF4

5.List of quantity sold against each Store with total turnover of the store.

SELECT a.store, SUM(a.qty) AS total_qty,
       SUM(b.price * a.qty) AS total_price
FROM sales AS a
LEFT JOIN products AS b ON a.p_id = b.p_id
GROUP BY a.store
ORDER BY a.store;
storetotal_qtytotal_price
ABC131020
DEF7565
Using MIN() with JOIN and ROW_NUMBER() to select one deterministic row per store
WITH ranked_sales AS (
  SELECT s.store, s.sale_id, s.qty, p.product, p.price,
         ROW_NUMBER() OVER (
           PARTITION BY s.store
           ORDER BY s.qty, s.sale_id
         ) AS rn
  FROM sales AS s
  INNER JOIN products AS p ON p.p_id = s.p_id
)
SELECT store, qty AS min_qty, product, price * qty AS total_price
FROM ranked_sales
WHERE rn = 1
ORDER BY store;
storemin_qtyproducttotal_price
ABC2Monitor150
DEF1CPU55
Using MAX() with JOIN and deterministic ranking
WITH ranked_sales AS (
  SELECT s.store, s.sale_id, s.qty, p.product, p.price,
         ROW_NUMBER() OVER (
           PARTITION BY s.store
           ORDER BY s.qty DESC, s.sale_id
         ) AS rn
  FROM sales AS s
  INNER JOIN products AS p ON p.p_id = s.p_id
)
SELECT store, qty AS max_qty, product, price * qty AS total_price
FROM ranked_sales
WHERE rn = 1
ORDER BY store;
storemax_qtyproducttotal_price
ABC3Monitor225
DEF2RAM180

6. List of products which are not sold

SELECT a.product, a.p_id
FROM products AS a
LEFT JOIN sales AS b ON a.p_id = b.p_id
WHERE b.sale_id IS NULL
ORDER BY a.p_id;
productp_id
Hard Disk1
Keyboard5
Mouse6
Motherboard7
Power supply8

7. List of customers who have not purchased any product.

SELECT a.customer, a.c_id
FROM customers AS a
LEFT JOIN sales AS b ON a.c_id = b.c_id
WHERE b.sale_id IS NULL
ORDER BY a.c_id;
customerc_id
King5
Ronn7
Jem8
Tom9

Master Relational Data with SQLite and Tkinter

Dive into the world of relational databases with this in-depth tutorial. Learn how to create and manage interconnected tables for products, customers, and sales using SQLite. Enhance your Python applications by leveraging Tkinter to design user-friendly interfaces and generate dynamic reports. A perfect guide for mastering data relationships and crafting interactive database-driven applications.

Learn More

LEFT JOIN ( Basic Query) Download SQL Dump of these three tables here. Exercise II : Stock report from Purchase and sales tables .
SELECT Query LEFT JOIN using Multiple Tables
Exercise : Sales - Agent using table JOIN and Date functions

All LEFT JOIN examples · Stock report exercise · Sales-agent reporting · GROUP BY · SUM()




Subscribe to our YouTube Channel here



plus2net.com




SQL Video Tutorials










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer