JOIN operator in SQL is used to combine rows from two or more tables based on a related column between them. It allows you to retrieve data from multiple tables in a single query, making it a powerful tool for working with relational databases.
Key Points
- Combining Tables:
JOINcombines rows from two or more tables based on a common column. - Types of JOINs: The most common types of
JOINare:INNER JOIN: Returns only matching rows.LEFT JOIN(orLEFT OUTER JOIN): Returns all rows from the left table and matching rows from the right table.RIGHT JOIN(orRIGHT OUTER JOIN): Returns all rows from the right table and matching rows from the left table.FULL JOIN(orFULL OUTER JOIN): Returns all rows when there is a match in either table.CROSS JOIN: Returns the Cartesian product of the two tables (all possible combinations).
- Alias Support: You can use table aliases to simplify queries.
Syntax
column1, column2, ...: The columns you want to retrieve.table1, table2: The tables you want to join.common_column: The column that relates the two tables.
Examples
INNER JOIN
Suppose you have two tables:Employees and Departments.
Table: Employees
| EmployeeID | Name | City | DepartmentID |
|---|---|---|---|
| 1 | Anand | Chennai | 101 |
| 2 | Bala | Coimbatore | 102 |
| 3 | Kavitha | Karaikal | 103 |
| 4 | Raj | Madurai | 104 |
| 5 | Kumar | Trichy | 105 |
| DepartmentID | DepartmentName |
|---|---|
| 101 | HR |
| 102 | Finance |
| 103 | IT |
| 104 | Marketing |
| 105 | Sales |
| Name | DepartmentName |
|---|---|
| Anand | HR |
| Bala | Finance |
| Kavitha | IT |
| Raj | Marketing |
| Kumar | Sales |
LEFT JOIN
ALEFT JOIN returns all rows from the left table (Employees) and matching rows from the right table (Departments). If there is no match, NULL values are returned for columns from the right table.
| Name | DepartmentName |
|---|---|
| Anand | HR |
| Bala | Finance |
| Kavitha | IT |
| Raj | Marketing |
| Kumar | Sales |
DepartmentName would appear as NULL.
RIGHT JOIN
ARIGHT JOIN returns all rows from the right table (Departments) and matching rows from the left table (Employees). If there is no match, NULL values are returned for columns from the left table.
| Name | DepartmentName |
|---|---|
| Anand | HR |
| Bala | Finance |
| Kavitha | IT |
| Raj | Marketing |
| Kumar | Sales |
Name column would appear as NULL.
FULL JOIN
AFULL JOIN returns all rows when there is a match in either table. If there is no match, NULL values are returned for columns from the table without a match.
Query:
| Name | DepartmentName |
|---|---|
| Anand | HR |
| Bala | Finance |
| Kavitha | IT |
| Raj | Marketing |
| Kumar | Sales |
NULL.
CROSS JOIN
ACROSS JOIN returns the Cartesian product of the two tables, meaning it combines each row of the first table with each row of the second table.
| Name | DepartmentName |
|---|---|
| Anand | HR |
| Anand | Finance |
| Anand | IT |
| Anand | Marketing |
| Anand | Sales |
| Bala | HR |
| Bala | Finance |
| Bala | IT |
| Bala | Marketing |
| Bala | Sales |
| Kavitha | HR |
| Kavitha | Finance |
| Kavitha | IT |
| Kavitha | Marketing |
| Kavitha | Sales |
| Raj | HR |
| Raj | Finance |
| Raj | IT |
| Raj | Marketing |
| Raj | Sales |
| Kumar | HR |
| Kumar | Finance |
| Kumar | IT |
| Kumar | Marketing |
| Kumar | Sales |
Practical Use Case
Suppose you have a table namedStudents and another table named Courses.
Table: Students
| StudentID | Name | City |
|---|---|---|
| 1 | Ram | Chennai |
| 2 | Karthik | Coimbatore |
| 3 | David | Bangalore |
| CourseID | CourseName |
|---|---|
| 101 | Mathematics |
| 102 | Science |
| 103 | English |
| Name | CourseName |
|---|---|
| Ram | Mathematics |
| Ram | Science |
| Ram | English |
| Karthik | Mathematics |
| Karthik | Science |
| Karthik | English |
| David | Mathematics |
| David | Science |
| David | English |
Key Takeaways
JOINcombines rows from two or more tables based on a related column.- Common types of
JOINincludeINNER JOIN,LEFT JOIN,RIGHT JOIN,FULL JOIN, andCROSS JOIN. INNER JOINreturns only matching rows, whileLEFT JOINandRIGHT JOINinclude non-matching rows withNULLvalues.CROSS JOINreturns the Cartesian product of the two tables.- Using
JOINallows you to retrieve and analyze data from multiple tables in a single query.