Mastering SQL Joins: Visual Explanation
SQL Joins Overview
A JOIN clause is used to combine rows from two or more tables, based on a related column between them.
Table A Table B
+----+----------+ +----+----------+
| id | name | | id | order_val|
+----+----------+ +----+----------+
| 1 | Alice | | 1 | $150 |
| 2 | Bob | | 3 | $400 |
+----+----------+ +----+----------+
- INNER JOIN: Returns records that have matching values in both tables.
sql SELECT A.name, B.order_val FROM TableA A INNER JOIN TableB B ON A.id = B.id; -- Result: Alice | $150 - LEFT JOIN (OUTER): Returns all records from the left table, and the matched records from the right table.
sql SELECT A.name, B.order_val FROM TableA A LEFT JOIN TableB B ON A.id = B.id; -- Result: -- Alice | $150 -- Bob | NULL - RIGHT JOIN (OUTER): Returns all records from the right table, and the matched records from the left table.
- FULL OUTER JOIN: Returns all records when there is a match in either left or right table.