SQL INNER JOIN
INNER JOIN combines rows from two or more tables only when matching values exist in both.
๐น Basic Syntax
SELECT columns
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;๐น Example: Employees and Departments
SELECT
e.employee_name,
d.department_name
FROM employees e
INNER JOIN departments d
ON e.department_id = d.department_id;โ This returns only those employees who are assigned to a department.
๐น Join on Multiple Conditions
SELECT *
FROM orders o
INNER JOIN customers c
ON o.customer_id = c.customer_id
AND o.region = c.region;๐น Using Table Aliases (Best Practice)
Shortens query and improves readability:
SELECT e.name, d.name
FROM employees e
INNER JOIN departments d
ON e.dept_id = d.id;๐ง Quick Recap
| Key Point | Explanation |
|---|---|
INNER JOIN | Returns only rows with matching values in both tables |
ON clause | Defines the join condition |
| Table aliases | Improve clarity and simplify column references |
| Filters | Can be combined with WHERE, GROUP BY, etc. |
๐ Use INNER JOIN to extract meaningful data from related tables