Learn Programming, Tech & Coding · Free Online Tools

IT Question Answer
Back to MySQL
MySQL Joins Explained with Examples

MySQL Joins Explained with Examples

MySQL1,661 viewsBy Admin
mysqljoinsexamples

Advertisement

What are JOINs?

JOINs combine rows from two or more tables based on a related column. They're how relational databases connect data.

Sample Tables

users:  id | name        orders: id | user_id | total
        1  | Sara                1  | 1       | 100
        2  | Ali                 2  | 1       | 50

INNER JOIN — Matching Rows Only

SELECT users.name, orders.total
FROM users
INNER JOIN orders ON users.id = orders.user_id;
-- Sara 100, Sara 50 (Ali has no orders, excluded)

LEFT JOIN — All Left + Matches

SELECT users.name, orders.total
FROM users
LEFT JOIN orders ON users.id = orders.user_id;
-- Sara 100, Sara 50, Ali NULL

Types of JOINs

JOINReturns
INNEROnly matching rows
LEFTAll left + matches
RIGHTAll right + matches
FULL (via UNION)All rows both sides

FAQs

INNER vs LEFT JOIN?

INNER drops non-matching rows; LEFT keeps all left-table rows. More in our MySQL section.

Can I join more than two tables?

Yes — chain multiple JOINs.

Advertisement