|
| 1 | +### **SQL Joins** |
| 2 | +SQL **Joins** are used to **combine rows** from two or more tables based on a **related column**. Joins are essential for **retrieving data** spread across multiple tables. |
| 3 | + |
| 4 | +--- |
| 5 | + |
| 6 | +## **1. Types of Joins** |
| 7 | + |
| 8 | +### **1.1 INNER JOIN** |
| 9 | +Returns **only matching rows** from both tables. |
| 10 | + |
| 11 | +```sql |
| 12 | +SELECT e.id, e.name, d.department_name |
| 13 | +FROM employees e |
| 14 | +INNER JOIN departments d |
| 15 | +ON e.department_id = d.department_id; |
| 16 | +``` |
| 17 | +**Explanation:** |
| 18 | +- Matches rows where **employees.department_id = departments.department_id**. |
| 19 | +- Rows without matches are **excluded**. |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +### **1.2 LEFT JOIN (LEFT OUTER JOIN)** |
| 24 | +Returns **all rows from the left table** and **matching rows from the right table**. Non-matching rows in the right table are filled with **NULL**. |
| 25 | + |
| 26 | +```sql |
| 27 | +SELECT e.id, e.name, d.department_name |
| 28 | +FROM employees e |
| 29 | +LEFT JOIN departments d |
| 30 | +ON e.department_id = d.department_id; |
| 31 | +``` |
| 32 | +**Use Case:** |
| 33 | +- Retrieve all employees, even if they **don’t belong to any department**. |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +### **1.3 RIGHT JOIN (RIGHT OUTER JOIN)** |
| 38 | +Returns **all rows from the right table** and **matching rows from the left table**. Non-matching rows in the left table are filled with **NULL**. |
| 39 | + |
| 40 | +```sql |
| 41 | +SELECT e.id, e.name, d.department_name |
| 42 | +FROM employees e |
| 43 | +RIGHT JOIN departments d |
| 44 | +ON e.department_id = d.department_id; |
| 45 | +``` |
| 46 | +**Use Case:** |
| 47 | +- Retrieve **all departments**, even if they have **no employees**. |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +### **1.4 FULL OUTER JOIN** |
| 52 | +Returns **all rows from both tables**. Non-matching rows in either table are filled with **NULL**. |
| 53 | + |
| 54 | +**Note:** MySQL does not support **FULL OUTER JOIN** directly, so it can be simulated using **UNION**: |
| 55 | + |
| 56 | +```sql |
| 57 | +SELECT e.id, e.name, d.department_name |
| 58 | +FROM employees e |
| 59 | +LEFT JOIN departments d |
| 60 | +ON e.department_id = d.department_id |
| 61 | + |
| 62 | +UNION |
| 63 | + |
| 64 | +SELECT e.id, e.name, d.department_name |
| 65 | +FROM employees e |
| 66 | +RIGHT JOIN departments d |
| 67 | +ON e.department_id = d.department_id; |
| 68 | +``` |
| 69 | +**Use Case:** |
| 70 | +- Retrieve a **complete list of employees and departments**, showing unmatched records from both sides. |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +### **1.5 CROSS JOIN** |
| 75 | +Produces a **Cartesian Product** of rows, combining **all rows from both tables**. |
| 76 | + |
| 77 | +```sql |
| 78 | +SELECT e.name, d.department_name |
| 79 | +FROM employees e |
| 80 | +CROSS JOIN departments d; |
| 81 | +``` |
| 82 | +**Use Case:** |
| 83 | +- Generate all possible **combinations** between employees and departments. |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +### **1.6 SELF JOIN** |
| 88 | +Joins a table with **itself**. |
| 89 | + |
| 90 | +```sql |
| 91 | +SELECT e1.name AS employee, e2.name AS manager |
| 92 | +FROM employees e1 |
| 93 | +INNER JOIN employees e2 |
| 94 | +ON e1.manager_id = e2.id; |
| 95 | +``` |
| 96 | +**Use Case:** |
| 97 | +- Retrieve a **hierarchy** where employees are related to managers in the same table. |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +## **2. Practical Examples** |
| 102 | + |
| 103 | +### **2.1 Find Employees and Their Department Names** |
| 104 | +```sql |
| 105 | +SELECT e.name, e.salary, d.department_name |
| 106 | +FROM employees e |
| 107 | +INNER JOIN departments d |
| 108 | +ON e.department_id = d.department_id; |
| 109 | +``` |
| 110 | + |
| 111 | +--- |
| 112 | + |
| 113 | +### **2.2 Find Employees Without Departments** |
| 114 | +```sql |
| 115 | +SELECT e.name, e.salary |
| 116 | +FROM employees e |
| 117 | +LEFT JOIN departments d |
| 118 | +ON e.department_id = d.department_id |
| 119 | +WHERE d.department_id IS NULL; |
| 120 | +``` |
| 121 | + |
| 122 | +--- |
| 123 | + |
| 124 | +### **2.3 Find Departments Without Employees** |
| 125 | +```sql |
| 126 | +SELECT d.department_name |
| 127 | +FROM employees e |
| 128 | +RIGHT JOIN departments d |
| 129 | +ON e.department_id = d.department_id |
| 130 | +WHERE e.id IS NULL; |
| 131 | +``` |
| 132 | + |
| 133 | +--- |
| 134 | + |
| 135 | +### **2.4 Find Employees Working in Multiple Departments** |
| 136 | +```sql |
| 137 | +SELECT e.name, COUNT(d.department_id) AS department_count |
| 138 | +FROM employees e |
| 139 | +INNER JOIN departments d |
| 140 | +ON e.department_id = d.department_id |
| 141 | +GROUP BY e.name |
| 142 | +HAVING COUNT(d.department_id) > 1; |
| 143 | +``` |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +### **2.5 Calculate Total Salary by Department** |
| 148 | +```sql |
| 149 | +SELECT d.department_name, SUM(e.salary) AS total_salary |
| 150 | +FROM employees e |
| 151 | +INNER JOIN departments d |
| 152 | +ON e.department_id = d.department_id |
| 153 | +GROUP BY d.department_name; |
| 154 | +``` |
| 155 | + |
| 156 | +--- |
| 157 | + |
| 158 | +### **2.6 Find Employees with Their Managers (Self Join)** |
| 159 | +```sql |
| 160 | +SELECT e1.name AS employee, e2.name AS manager |
| 161 | +FROM employees e1 |
| 162 | +LEFT JOIN employees e2 |
| 163 | +ON e1.manager_id = e2.id; |
| 164 | +``` |
| 165 | + |
| 166 | +--- |
| 167 | + |
| 168 | +## **3. Joining More Than Two Tables** |
| 169 | + |
| 170 | +```sql |
| 171 | +SELECT e.id, e.name, d.department_name, l.location |
| 172 | +FROM employees e |
| 173 | +INNER JOIN departments d |
| 174 | +ON e.department_id = d.department_id |
| 175 | +INNER JOIN locations l |
| 176 | +ON d.location_id = l.location_id; |
| 177 | +``` |
| 178 | +**Use Case:** |
| 179 | +- Combines **employees, departments, and locations**. |
| 180 | + |
| 181 | +--- |
| 182 | + |
| 183 | +## **4. Nested Joins (Multiple JOIN Conditions)** |
| 184 | + |
| 185 | +```sql |
| 186 | +SELECT e.name, d.department_name, l.city |
| 187 | +FROM employees e |
| 188 | +LEFT JOIN departments d ON e.department_id = d.department_id |
| 189 | +LEFT JOIN locations l ON d.location_id = l.location_id |
| 190 | +WHERE l.city = 'Berlin'; |
| 191 | +``` |
| 192 | + |
| 193 | +**Use Case:** |
| 194 | +- Find employees based in **Berlin**. |
| 195 | + |
| 196 | +--- |
| 197 | + |
| 198 | +## **5. Tips for Joins** |
| 199 | + |
| 200 | +1. **Aliases:** Use aliases (**e, d**) to make queries more readable. |
| 201 | +2. **Indexes:** Create indexes on **foreign keys** to improve performance. |
| 202 | +3. **NULL Handling:** Always check for **NULL** values when using **OUTER JOINs**. |
| 203 | +4. **Check Results:** Test with a **LIMIT 10** query before joining large datasets. |
| 204 | +5. **Use Subqueries for Filtering:** Use subqueries to pre-filter data before joining, if performance slows down. |
| 205 | + |
| 206 | +--- |
| 207 | + |
| 208 | +## **6. Real-World Use Cases** |
| 209 | + |
| 210 | +1. **Customer Orders Analysis:** |
| 211 | +```sql |
| 212 | +SELECT c.customer_name, o.order_date, p.product_name |
| 213 | +FROM customers c |
| 214 | +INNER JOIN orders o ON c.customer_id = o.customer_id |
| 215 | +INNER JOIN products p ON o.product_id = p.product_id; |
| 216 | +``` |
| 217 | + |
| 218 | +2. **Top 3 Products by Region:** |
| 219 | +```sql |
| 220 | +SELECT r.region_name, p.product_name, SUM(s.sales) AS total_sales, |
| 221 | + RANK() OVER (PARTITION BY r.region_name ORDER BY SUM(s.sales) DESC) AS rank |
| 222 | +FROM sales s |
| 223 | +INNER JOIN products p ON s.product_id = p.product_id |
| 224 | +INNER JOIN regions r ON s.region_id = r.region_id |
| 225 | +GROUP BY r.region_name, p.product_name; |
| 226 | +``` |
| 227 | + |
| 228 | +3. **Employee Salary Growth Over Time:** |
| 229 | +```sql |
| 230 | +SELECT e.name, s.salary, s.salary - LAG(s.salary, 1) OVER (PARTITION BY e.id ORDER BY s.date) AS salary_growth |
| 231 | +FROM employees e |
| 232 | +INNER JOIN salaries s ON e.id = s.employee_id; |
| 233 | +``` |
| 234 | + |
| 235 | +--- |
| 236 | + |
| 237 | +CompliedByUdithaWick |
0 commit comments