Joins on E-Commerce

Setup: Create Tables

We have three tables:

  1. Customers: Stores customer details.

  2. Orders: Stores order details.

  3. Products: Stores product details.

CREATE TABLE customers (
    customer_id SERIAL PRIMARY KEY,
    customer_name VARCHAR(50)
);

CREATE TABLE orders (
    order_id SERIAL PRIMARY KEY,
    customer_id INT REFERENCES customers(customer_id),
    product_id INT,
    quantity INT
);

CREATE TABLE products (
    product_id SERIAL PRIMARY KEY,
    product_name VARCHAR(50),
    price DECIMAL(10, 2)
);

Populate some data,

1. INNER JOIN

Question: List all customers who placed orders with the product name and quantity.

2. LEFT JOIN

Question: Show all customers and their orders, including orders without a product.

3. FULL OUTER JOIN

Question: Show all customers, orders, and products, including those with no matches.

Last updated