🔗 SQL Joins & Meta Information
Returns only the matching records from both left and right tables. If there's no match, the record is excluded.
INNER JOIN TableB
ON TableA.ID = TableB.ID
Returns all records from the left table and matching records from the right table. Non-matching right records show NULL.
LEFT JOIN TableB
ON TableA.ID = TableB.ID
Returns all records from the right table and matching records from the left table. Non-matching left records show NULL.
RIGHT JOIN TableB
ON TableA.ID = TableB.ID
Returns all records when there's a match in either left or right table. Non-matching records show NULL for missing side.
FULL OUTER JOIN TableB
ON TableA.ID = TableB.ID
Returns records from the left table that have no matching records in the right table. Excludes all common records.
LEFT JOIN TableB
ON TableA.ID = TableB.ID
WHERE TableB.ID IS NULL
Returns records from the right table that have no matching records in the left table. Excludes all common records.
RIGHT JOIN TableB
ON TableA.ID = TableB.ID
WHERE TableA.ID IS NULL
Returns all records from both tables except the matching ones. Shows only non-matching records from both sides.
FULL OUTER JOIN TableB
ON TableA.ID = TableB.ID
WHERE TableA.ID IS NULL
OR TableB.ID IS NULL
Returns records from both tables where there are no matching keys. Identifies completely separate datasets with no overlap.
WHERE NOT EXISTS
(SELECT 1 WHERE
TableA.ID = TableB.ID)
A table is joined with itself to compare rows within the same table. Useful for hierarchical data or finding duplicates.
FROM TableA AS A
INNER JOIN TableA AS B
ON A.ManagerID = B.EmployeeID
Left Join
Left Outer Join
Both return the same results
Right Join
Right Outer Join
Both return the same results
In SQL and database operations, NULL represents the absence of a value. It's not zero, not an empty string, but literally "no data". When joins don't find matching records, the corresponding columns are filled with NULL values, indicating that no matching data exists in that table.
| # | Join Type | Returns | Use Case |
|---|---|---|---|
| 1 | Inner Join | Only matching records | Find exact matches between tables |
| 2 | Left Join | All left + matching right | Keep all left records, match if possible |
| 3 | Right Join | All right + matching left | Keep all right records, match if possible |
| 4 | Full Join | All records from both | Combine everything, show all data |
| 5 | Left Anti Join | Left only (no match in right) | Find records unique to left table |
| 6 | Right Anti Join | Right only (no match in left) | Find records unique to right table |
| 7 | Inner Except Join | All except matching | Find all non-matching records |
| 8 | Disjoint Join | No common records | Identify completely separate datasets |
| 9 | Self Join | Table with itself | Compare rows within same table |
Comments
Post a Comment