Database
A quick review of SQL Joins
Gumathi Geo Dev.to (EN Zone)
29 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
Postgres RLS in Symfony: three green isolation tests, and the UPDATE that moves tenant 1's invoices into tenant 2's books
Database
34
Dev.to (EN Zone)
Turning Mermaid ER diagrams into shareable 3D schema tours
Database
30
DEV Community
finally understand why redis blocks on one slow command, wish I knew this earlier
Database
27
Reddit r/webdev
Tracing Which Internal Consumer Read Each Webhook Event (One Registration, One Queue)
Database
29
DEV Community
Comments0
No comments yet — be the first