Sitemap

SQL JOIN….

RIGHT JOIN

--

A RIGHT JOIN returns all rows from the right table, and matching rows from the left table. If no match exists, the left table’s columns become NULL.

CREATE TABLE customers (
customer_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(120),
city VARCHAR(60),
signup_at_utc DATETIME
);

CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
order_code VARCHAR(10) NOT NULL,
status VARCHAR(20) NOT NULL,
total_amount DECIMAL(10,2) NOT NULL,
placed_at_utc DATETIME NOT NULL,
INDEX idx_orders_customer_id (customer_id)
);

INSERT INTO customers (customer_id, name, email, city, signup_at_utc) VALUES
(101, ‘Aisha’, ‘aisha@demo.com’, ‘Delhi’, ‘2026–01–01 10:00:00’),
(102, ‘Rohan’, ‘rohan@demo.com’, ‘Mumbai’, ‘2026–01–02 11:00:00’),
(103, ‘Meera’, ‘meera@demo.com’, NULL, ‘2026–01–03 12:00:00’),
(104, ‘Arjun’, ‘arjun@demo.com’, ‘Delhi’, ‘2026–01–04 13:00:00’),
(106, ‘Neha’, ‘neha@demo.com’, ‘Pune’, ‘2026–01–05 09:30:00’),
(107, ‘Ishaan’, ‘ishaan@demo.com’, ‘Bengaluru’, ‘2026–01–06 18:10:00’),
(108, ‘Riya’, NULL, ‘Delhi’, ‘2026–01–07 07:45:00’),
(109, ‘Aisha’, ‘aisha2@demo.com’, ‘Jaipur’, ‘2026–01–08 20:00:00’);

Get Brokenversion’s stories in your inbox

Join Medium for free to get updates from this writer.

INSERT INTO orders (order_id, customer_id, order_code, status, total_amount, placed_at_utc) VALUES
(1, 101, ‘A’, ‘PAID’, 499.00, ‘2026–01–10 10:00:00’),
(2, 102, ‘B’, ‘PAID’, 299.00, ‘2026–01–10 11:00:00’),
(3, 105, ‘C’, ‘PAID’, 199.00, ‘2026–01–10 12:00:00’),
(4, 101, ‘D’, ‘CANCELLED’, 999.00, ‘2026–01–11 09:00:00’),
(5, 106, ‘E’, ‘PAID’, 799.00, ‘2026–01–11 10:30:00’),
(6, 106, ‘F’, ‘PENDING’, 149.00, ‘2026–01–12 08:20:00’),
(7, NULL,’G’, ‘PAID’, 249.00, ‘2026–01–12 09:10:00’),
(8, 999, ‘H’, ‘PAID’, 129.00, ‘2026–01–12 15:40:00’);

Press enter or click to view image in full size
EXAMPLE FOR UNDERSTANDING

LEFT JOIN

A LEFT JOIN returns all rows from the left table, and matching rows from the right table. If no match exists, the right table’s columns become NULL.

Show every customer, and the order they placed

SELECT
c.customer_id,
c.name,
o.order_id,
o.order_code
FROM customers c
LEFT JOIN orders o
ON o.customer_id = c.customer_id;

Press enter or click to view image in full size

INNER JOIN

INNER JOIN returns only those rows where there is a matching value in both tables based on a join condition.

Press enter or click to view image in full size

Count of orders per customer??

FULL OUTER JOIN

Retrieves all rows from both tables. If there is a match, it returns the matched rows.

SELECT
s.student_id,
s.name,
p.amount
FROM students s
LEFT JOIN payments p
ON p.student_id = s.student_id

UNION

SELECT
p.student_id,
s.name,
p.amount
FROM students s
RIGHT JOIN payments p
ON p.student_id = s.student_id
WHERE s.student_id IS NULL;

SELF JOIN

A SELF JOIN is when a table is joined with itself. It is commonly used to represent hierarchies such as: employee → manager

In an employee hierarchy, both the employee and the manager are stored in the same table, so we use two aliases to treat it like two different tables.

--

--