?? Day 30: Exploring Practical SQL Implementation - Part 2??
JIGNESH KUMAR
Data Science Intern at Alma Better || Electrical and Instrumentation Engineer at SIC
SQL Joins Commands:
SELECT t1.*, t2.*
FROM table1 t1
JOIN table2 t2 ON t1.column_name = t2.column_name;
Common Types of Joins:
1. INNER JOIN:
2. LEFT JOIN (or LEFT OUTER JOIN):
3. RIGHT JOIN (or RIGHT OUTER JOIN):
4. FULL JOIN (or FULL OUTER JOIN):
Joins in Real life Examples
Let's consider a scenario where we have two tables in a database - employees and departments.
*Table:employees*
+-------------+--------------+--------+
| employee_id | employee_name| dept_id|
+-------------+--------------+--------+
| 1 | Jignesh | 101 |
| 2 | Upendra | 102 |
| 3 | Rajan | 101 |
| 4 | Krishna | 103 |
+-------------+--------------+--------+
*Table:departments*
+--------+------------------+
| dept_id| department_name |
+--------+------------------+
| 101 | HR |
| 102 | IT |
| 103 | Finance |
+--------+------------------+
1. INNER JOIN:
SELECT employees.*, departments.*
FROM employees
INNER JOIN departments ON employees.dept_id = departments.dept_id;
领英推荐
+-------------+--------------+--------+--------+------------------+
| employee_id | employee_name| dept_id| dept_id| department_name |
+-------------+--------------+--------+--------+------------------+
| 1 | Jignesh | 101 | 101 | HR |
| 2 | Upendra | 102 | 102 | IT |
| 3 | Rajan | 101 | 101 | HR |
| 4 | Krishna | 103 | 103 | Finance |
+-------------+--------------+--------+--------+------------------+
2. LEFT JOIN:
SELECT employees.*, departments.*
FROM employees
LEFT JOIN departments ON employees.dept_id = departments.dept_id;
+-------------+--------------+--------+--------+------------------+
| employee_id | employee_name| dept_id| dept_id| department_name |
+-------------+--------------+--------+--------+------------------+
| 1 | Jignesh | 101 | 101 | HR |
| 2 | Upendra | 102 | 102 | IT |
| 3 | Rajan | 101 | 101 | HR |
| 4 | Krishna | 103 | 103 | Finance |
+-------------+--------------+--------+--------+------------------+
3. RIGHT JOIN:
SELECT employees.*, departments.*
FROM employees
RIGHT JOIN departments ON employees.dept_id = departments.dept_id;
+-------------+--------------+--------+--------+------------------+
| employee_id | employee_name| dept_id| dept_id| department_name |
+-------------+--------------+--------+--------+------------------+
| 1 | Jignesh | 101 | 101 | HR |
| 2 | Upendra | 102 | 102 | IT |
| 3 | Rajan | 101 | 101 | HR |
| NULL | NULL | NULL | 103 | Finance |
+-------------+--------------+--------+--------+------------------+
4. FULL JOIN:
SELECT employees.*, departments.*
FROM employees
FULL JOIN departments ON employees.dept_id = departments.dept_id;
+-------------+--------------+--------+--------+------------------+
| employee_id | employee_name| dept_id| dept_id| department_name |
+-------------+--------------+--------+--------+------------------+
| 1 | Jignesh | 101 | 101 | HR |
| 2 | Upendra | 102 | 102 | IT |
| 3 | Rajan | 101 | 101 | HR |
| 4 | Krishna | 103 | 103 | Finance |
| NULL | NULL | NULL | 103 | Finance |
+-------------+--------------+--------+--------+------------------+