|
| 1 | +# PostgreSQL Joins |
| 2 | +## Sample Tables |
| 3 | + |
| 4 | +Let's use these sample tables for illustration: |
| 5 | + |
| 6 | +```sql |
| 7 | +CREATE TABLE employees ( |
| 8 | + id SERIAL PRIMARY KEY, |
| 9 | + name TEXT, |
| 10 | + manager_id INTEGER |
| 11 | +); |
| 12 | + |
| 13 | +CREATE TABLE departments ( |
| 14 | + id SERIAL PRIMARY KEY, |
| 15 | + dept_name TEXT, |
| 16 | + manager_id INTEGER |
| 17 | +); |
| 18 | +``` |
| 19 | + |
| 20 | +Sample data: |
| 21 | + |
| 22 | +```sql |
| 23 | +INSERT INTO employees (name, manager_id) VALUES |
| 24 | +('Alice', NULL), |
| 25 | +('Bob', 1), |
| 26 | +('Charlie', 1), |
| 27 | +('Diana', 2); |
| 28 | + |
| 29 | +INSERT INTO departments (dept_name, manager_id) VALUES |
| 30 | +('HR', 1), |
| 31 | +('Engineering', 2), |
| 32 | +('Marketing', 3); |
| 33 | +``` |
| 34 | + |
| 35 | +## Examples |
| 36 | + |
| 37 | +**INNER JOIN:** List employees and their department if they are a manager. |
| 38 | + |
| 39 | +```sql |
| 40 | +SELECT e.name, d.dept_name |
| 41 | +FROM employees e |
| 42 | +INNER JOIN departments d ON e.id = d.manager_id; |
| 43 | +``` |
| 44 | + |
| 45 | +**LEFT JOIN:** List all employees and their department (if any). |
| 46 | + |
| 47 | +```sql |
| 48 | +SELECT e.name, d.dept_name |
| 49 | +FROM employees e |
| 50 | +LEFT JOIN departments d ON e.id = d.manager_id; |
| 51 | +``` |
| 52 | + |
| 53 | +**RIGHT JOIN:** List all departments and their manager (if any). |
| 54 | + |
| 55 | +```sql |
| 56 | +SELECT d.dept_name, e.name AS manager |
| 57 | +FROM departments d |
| 58 | +RIGHT JOIN employees e ON d.manager_id = e.id; |
| 59 | +``` |
| 60 | + |
| 61 | +**FULL JOIN:** Show all employees and departments, matching managers where possible. |
| 62 | + |
| 63 | +```sql |
| 64 | +SELECT e.name, d.dept_name |
| 65 | +FROM employees e |
| 66 | +FULL JOIN departments d ON e.id = d.manager_id; |
| 67 | +``` |
| 68 | + |
| 69 | +**CROSS JOIN:** Every employee with every department. |
| 70 | + |
| 71 | +```sql |
| 72 | +SELECT e.name, d.dept_name |
| 73 | +FROM employees e |
| 74 | +CROSS JOIN departments d; |
| 75 | +``` |
| 76 | + |
| 77 | +**SELF JOIN:** List employees and their managers. |
| 78 | + |
| 79 | +```sql |
| 80 | +SELECT e.name AS employee, m.name AS manager |
| 81 | +FROM employees e |
| 82 | +INNER JOIN employees m ON e.manager_id = m.id; |
| 83 | +``` |
| 84 | +Joins in PostgreSQL allow you to combine rows from two or more tables based on related columns. This is essential for querying relational data efficiently. |
| 85 | + |
| 86 | +## Types of Joins |
| 87 | + |
| 88 | +### 1. INNER JOIN |
| 89 | + |
| 90 | +Returns rows when there is a match in both tables. |
| 91 | + |
| 92 | +```sql |
| 93 | +SELECT a.*, b.* |
| 94 | +FROM table_a a |
| 95 | +INNER JOIN table_b b ON a.id = b.a_id; |
| 96 | +``` |
| 97 | + |
| 98 | +### 2. LEFT JOIN (LEFT OUTER JOIN) |
| 99 | + |
| 100 | +Returns all rows from the left table, and matched rows from the right table. Unmatched rows from the right table will have NULLs. |
| 101 | + |
| 102 | +```sql |
| 103 | +SELECT a.*, b.* |
| 104 | +FROM table_a a |
| 105 | +LEFT JOIN table_b b ON a.id = b.a_id; |
| 106 | +``` |
| 107 | + |
| 108 | +### 3. RIGHT JOIN (RIGHT OUTER JOIN) |
| 109 | + |
| 110 | +Returns all rows from the right table, and matched rows from the left table. Unmatched rows from the left table will have NULLs. |
| 111 | + |
| 112 | +```sql |
| 113 | +SELECT a.*, b.* |
| 114 | +FROM table_a a |
| 115 | +RIGHT JOIN table_b b ON a.id = b.a_id; |
| 116 | +``` |
| 117 | + |
| 118 | +### 4. FULL JOIN (FULL OUTER JOIN) |
| 119 | + |
| 120 | +Returns all rows when there is a match in one of the tables. Unmatched rows will have NULLs for columns from the other table. |
| 121 | + |
| 122 | +```sql |
| 123 | +SELECT a.*, b.* |
| 124 | +FROM table_a a |
| 125 | +FULL JOIN table_b b ON a.id = b.a_id; |
| 126 | +``` |
| 127 | + |
| 128 | +### 5. CROSS JOIN |
| 129 | + |
| 130 | +Returns the Cartesian product of both tables (every row of the first table with every row of the second table). |
| 131 | + |
| 132 | +```sql |
| 133 | +SELECT a.*, b.* |
| 134 | +FROM table_a a |
| 135 | +CROSS JOIN table_b b; |
| 136 | +``` |
| 137 | + |
| 138 | +### 6. SELF JOIN |
| 139 | + |
| 140 | +A table joined with itself. |
| 141 | + |
| 142 | +```sql |
| 143 | +SELECT a.*, b.* |
| 144 | +FROM employees a |
| 145 | +INNER JOIN employees b ON a.manager_id = b.id; |
| 146 | +``` |
| 147 | + |
| 148 | +## Join Conditions |
| 149 | + |
| 150 | +- Use `ON` to specify the join condition. |
| 151 | +- You can join on multiple columns: |
| 152 | + |
| 153 | +```sql |
| 154 | +SELECT * |
| 155 | +FROM orders o |
| 156 | +INNER JOIN customers c ON o.customer_id = c.id AND o.region = c.region; |
| 157 | +``` |
| 158 | + |
| 159 | +## Practical Example |
| 160 | + |
| 161 | +Suppose you have two tables: |
| 162 | + |
| 163 | +```sql |
| 164 | +CREATE TABLE authors ( |
| 165 | + id SERIAL PRIMARY KEY, |
| 166 | + name TEXT |
| 167 | +); |
| 168 | + |
| 169 | +CREATE TABLE books ( |
| 170 | + id SERIAL PRIMARY KEY, |
| 171 | + title TEXT, |
| 172 | + author_id INTEGER REFERENCES authors(id) |
| 173 | +); |
| 174 | +``` |
| 175 | + |
| 176 | +**Get all books with their authors:** |
| 177 | + |
| 178 | +```sql |
| 179 | +SELECT books.title, authors.name |
| 180 | +FROM books |
| 181 | +INNER JOIN authors ON books.author_id = authors.id; |
| 182 | +``` |
| 183 | + |
| 184 | +**Get all authors and their books (including authors with no books):** |
| 185 | + |
| 186 | +```sql |
| 187 | +SELECT authors.name, books.title |
| 188 | +FROM authors |
| 189 | +LEFT JOIN books ON authors.id = books.author_id; |
| 190 | +``` |
| 191 | + |
| 192 | +## Tips |
| 193 | + |
| 194 | +- Use table aliases for readability. |
| 195 | +- Always specify join conditions to avoid Cartesian products (except for CROSS JOIN). |
| 196 | +- Use `USING(column)` for joins on columns with the same name. |
| 197 | + |
| 198 | +```sql |
| 199 | +SELECT * |
| 200 | +FROM table_a |
| 201 | +INNER JOIN table_b USING (id); |
| 202 | +``` |
| 203 | + |
| 204 | +## References |
| 205 | + |
| 206 | +- [PostgreSQL Documentation: Joins](https://www.postgresql.org/docs/current/tutorial-inheritance.html) |
| 207 | +- [PostgreSQL SELECT](https://www.postgresql.org/docs/current/sql-select.html) |
0 commit comments