SQL JOINs are used to combine rows from two or more tables based on a related column between them. Let's look at each type of JOIN and provide a visual explanation:
INNER JOIN: This is the most common type of JOIN. It returns the rows where there is a match in both tables.Example:Table ApgsqlCopy code| ID
|----|-------||1| |
|2| Bob |
|3| |Table BjavascriptCopy code|ID|Course|
|----|---------|
|2|Math|
|3|Science|
|4|English|Inner Join on IDpgsqlCopy code| ID |Name| Course |
|----|-------|---------||2| | Math |
|3| Carol | Science |Visual representation:cssCopy code[ ][ ][ Carol ]\ / [ Math ][ Science ]
LEFT (OUTER) JOIN: This returns all the rows from the left table and the matched rows from the right table. If there is no match, the result is NULL on the right side.Left Join on IDpgsqlCopy code| ID |Name| Course |
|----|-------|---------||1| |NULL|
|2| Bob | Math |
|3| Carol | Science |Visual representation:cssCopy code[ ][ ][ Carol ]\ / [ Math ][ Science ]
RIGHT (OUTER) JOIN: This returns all the rows from the right table, and the matched rows from the left table. If there is no match, the result is NULL on the left side.Right Join on IDpgsqlCopy code| ID |Name| Course |
|----|-------|---------||2| | Math |
|3| Carol | Science |
|4|NULL| |Visual representation:cssCopy code[ ][ Carol ]/ \ /[ Math ][ Science ][ ]
FULL (OUTER) JOIN: It returns all rows when there is a match in either left or right table. If there is no match, the result is NULL on either side.Full Join on IDpgsqlCopy code| ID |Name| Course |
|----|-------|---------||1| |NULL|
|2| Bob | Math |
|3| Carol | Science |
|4|NULL| |Visual representation:cssCopy code[ ][ ][ Carol ]\ / \ /[ Math ][ Science ][ ]
INNER JOIN: This is the most common type of JOIN. It returns the rows where there is a match in both tables.
Example:
Table A
────────────────────────────────────────────────────────────────────
pgsqlCopy code| ID | Name |
|----|-------|
| 1 | |
| 2 | |
| 3 | Carol |
Table B
────────────────────────────────────────────────────────────────────
javascriptCopy code| ID | Course |
|----|---------|
| 2 | Math |
| 3 | Science |
| 4 | English |
Inner Join on ID
────────────────────────────────────────────────────────────────────
pgsqlCopy code| ID | Name | Course |
|----|-------|---------|
| 2 | | Math |
| 3 | Carol | Science |
Visual representation:
────────────────────────────────────────────────────────────────────
cssCopy code[ ] [ ] [ Carol ]
\ / \
[ Math ] [ Science ]
LEFT (OUTER) JOIN: This returns all the rows from the left table and the matched rows from the right table. If there is no match, the result is NULL on the right side.
Left Join on ID
────────────────────────────────────────────────────────────────────
pgsqlCopy code| ID | Name | Course |
|----|-------|---------|
| 1 | | NULL |
| 2 | | Math |
| 3 | Carol | Science |
Visual representation:
────────────────────────────────────────────────────────────────────
cssCopy code[ ] [ ] [ Carol ]
\ / \
[ Math ] [ Science ]
RIGHT (OUTER) JOIN: This returns all the rows from the right table, and the matched rows from the left table. If there is no match, the result is NULL on the left side.
Right Join on ID
────────────────────────────────────────────────────────────────────
pgsqlCopy code| ID | Name | Course |
|----|-------|---------|
| 2 | | Math |
| 3 | Carol | Science |
| 4 | NULL | English |
Visual representation:
────────────────────────────────────────────────────────────────────
cssCopy code [ ] [ Carol ]
/ \ /
[ Math ] [ Science ] [ ]
FULL (OUTER) JOIN: It returns all rows when there is a match in either left or right table. If there is no match, the result is NULL on either side.
Full Join on ID
────────────────────────────────────────────────────────────────────
pgsqlCopy code| ID | Name | Course |
|----|-------|---------|
| 1 | | NULL |
| 2 | | Math |
| 3 | Carol | Science |
| 4 | NULL | English |
Visual representation:
────────────────────────────────────────────────────────────────────
cssCopy code[ ] [ ] [ Carol ]
\ / \ /
[ Math ] [ Science ] [ ]
These visual representations are simplified and are meant to illustrate the principles of the various joins. In reality, SQL tables can have many more rows and columns, and joins can be more complex.