CREATE TABLE customers (
customer_id INT PRIMARY KEY,
customer_name VARCHAR(50)
);
-- Create orders table
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE
);
-- Insert sample data
INSERT INTO customers (customer_id, customer_name)
VALUES
(1, 'Alice'),
(2, 'Bob'),
(3, 'Charlie');
INSERT INTO orders (order_id, customer_id, order_date)
VALUES
(101, 1, '2024-01-01'),
(102, 3, '2024-01-02');
SELECT * FROM customers;
-- Using LEFT JOIN
SELECT
c.customer_id,
c.customer_name
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
WHERE o.order_id IS NULL;
-- Using NOT EXISTS
SELECT c.customer_id, c.customer_name
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.order_id IS NULL;
-- The customers table contains a list of all customers.
-- The orders table keeps track of purchases made by customers.
-- A LEFT JOIN is used to link customers to their orders.
-- If a customer has no orders, their order_id will be NULL.
-- The WHERE condition filters out all customers who have at least one order.
-- The final result shows only customers who have never placed an order.