JOIN in SQL is used to combine data from multiple tables through a common column. This column is usually an id or a foreign key. The purpose is to display related data in a single result set.
đ Example: Suppose there is a Customers table and an Orders table. To get the order information of a customer, we combine these two tables with a JOIN.
INNER JOIN đ
Brings only the records that match in both tables.
If there is no match, that row will not appear in the result.
Logic: Intersection set
Example: The list of customers who have placed an order.
LEFT JOIN âď¸
Brings all records from the left table. If there is no match in the right table, those columns will be NULL.
Logic: Entire left table + if right table exists, add extra data
Example: The list of all customers, with empty order information for those who have not placed any orders.
â Difference:
INNER JOIN â Only the matching ones â
LEFT JOIN â Entire left, add right if exists, otherwise empty đď¸
đĄ Tip: Unnecessary LEFT JOIN decreases performance. To find unmatched records, the technique
LEFT JOIN ... WHERE right_table.column IS NULL
is commonly used. On large datasets, using indexes speeds up the query.
Top comments (0)