Database
A quick review of SQL Joins
Gumathi Geo Dev.to (EN Zone)
1 views
SQL Joins, Simply
Join combines rows from two tables, matched on shared column, usually an id.
Example tables:
customers orders
customer_id | name order_id | customer_id | item
1 | Amina 101 | 1 | Bag
2 | Brian 102 | 1 | Shoes
3 | Carla 103 | 5 | Hat
Note: customer_id 5 has no match in customers. Carla has no orders. These gaps show what each join does differently.
_Inner Join _— only matching rows on both sides.
sql
SELECT customers.name, orders.item
FROM customers
INNER JOIN orders ON customers.customer_id = orders.customer_id;
Result: Amina's two orders only. Carla and order 103 drop out, no match.
Left Join — keeps all rows from left table, matched or not.
sql
SELECT customers.name, orders.item
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id;
Result: Amina's orders plus Carla with NULL item.
Right Join — keeps all rows from right table, matched or not.
sql
SELECT customers.name, orders.item
FROM customers
RIGHT JOIN orders ON customers.customer_id = orders.customer_id;
Result: Amina's orders plus order 103 with NULL name.
Full Join — keeps all rows from both tables.
sql
SELECT customers.name, orders.item
FROM customers
FULL JOIN orders ON customers.customer_id = orders.customer_id;
Result: Amina's orders, Carla with NULL item, order 103 with NULL name. Nothing dropped.
Quick rule
: Inner join = strict match only. Left/right = pick which side to keep fully. Full join = keep everything. Default to inner join unless missing rows matter to your question.
Read original: https://dev.to/mysticg/a-quick-review-of-sql-joins-1ic7
Related
The bug where every check passed and the data was still wrong
Database
0
DEV Community
I Round-Tripped 2,249 Test Fixtures Through sqlfluff's Auto-Fixer. Eight Came Back Unparsable.
Database
4
DEV Community
[$] PostgreSQL 19's "scary patch contest"
Database
4
LWN.net
Overview of caching in PostgreSQL
Database
7
Reddit r/programming
Comments0
No comments yet — be the first