Joins let you combine rows from two or more tables based on related columns. They control which rows match and which are included when tables are combined.
-- Syntax examples for common JOIN types
SELECT columns
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;
-- LEFT JOIN returns all rows from table1 and matched rows from table2
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;
-- RIGHT JOIN returns all rows
from table2 and matched rows from table1
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column;
-- FULL OUTER JOIN returns all rows from both sides
(MySQL may not support directly)
SELECT columns
FROM table1
FULL OUTER JOIN table2
ON table1.common_column = table2.common_column;
-- INNER JOIN example: students enrolled in courses (only matches)
SELECT students.name, courses.course_name
FROM students
INNER JOIN courses
ON students.course_id = courses.course_id;
-- LEFT JOIN example: employees with their departments,
include employees without departments
SELECT employees.name, departments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;
-- RIGHT JOIN example: include all customers even if they have no orders (orders may be NULL)
SELECT orders.order_id, customers.customer_name
FROM orders
RIGHT JOIN customers
ON orders.customer_id = customers.customer_id;
-- FULL JOIN example (if supported): show all orders and customers, matched where possible
SELECT orders.order_id, customers.customer_name
FROM orders
FULL OUTER JOIN customers
ON orders.customer_id = customers.customer_id;
students table
-- id | name | course_id
1 | Alice | 10
2 | Bob | 20
3 | Carol | 10
courses table
-- course_id | course_name
10 | Math
20 | Physics
30 | History
INNER JOIN result (students + courses where course_id matches):
-- name | course_name
Alice | Math
Bob | Physics
Carol | Math
INNER JOIN when you require matched rows only.LEFT JOIN to preserve primary table rows while adding related data.UNION of LEFT and RIGHT joins where needed.INNER JOIN.LEFT JOIN).FULL OUTER JOIN in MySQL using UNION of LEFT JOIN and RIGHT JOIN.