TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2754 1.09K
๐Ÿš€ SQL Roadmap 2026 โ€” Part 10

SQL JOINs โ€” Combining Data from Multiple Tables

In real-world databases, information is rarely stored in one table.

For example: customers, orders, products, payments, employees, departments

A customer may exist in one table while their orders exist in another. JOINs allow us to combine related data from multiple tables. This is one of the most important SQL concepts for a Data Analyst.

๐Ÿง  1. Why Do We Need JOINs?

Suppose we have two tables:

customers

customer_id | customer_name
101 | Alice
102 | Bob
103 | Charlie


orders

order_id | customer_id | amount
1 | 101 | 500
2 | 101 | 800
3 | 102 | 300


The customer name is stored in "customers". The order amount is stored in "orders".

To answer:

ยซHow much did each customer spend?ยป

We need to combine the tables. That's where JOIN comes in.

๐Ÿ”— 2. Basic JOIN Structure

SELECT
c.customer_name,
o.order_id,
o.amount
FROM customers c
JOIN orders o
ON c.customer_id = o.customer_id;


Here: customers โ†’ c, orders โ†’ o. These are called table aliases.

The condition: ON c.customer_id = o.customer_id tells SQL how the tables are related.

๐Ÿ”‘ 3. The JOIN Key

A JOIN usually connects tables through a related column.

customers.customer_id โ†“ orders.customer_id


Often: one table contains a primary key, another table contains the corresponding foreign key.

Example: customers.customer_id โ†’ Primary Key, orders.customer_id โ†’ Foreign Key.

๐Ÿงฉ 4. INNER JOIN

"INNER JOIN" returns only rows that have a match in both tables.

SELECT c.customer_name, o.order_id, o.amount 
FROM customers c
INNER JOIN orders o ON c.customer_id = o.customer_id;


Result:

Alice | 1 | 500
Alice | 2 | 800
Bob | 3 | 300


Charlie is missing because Charlie has no matching order.

Customers โˆฉ Orders - Only matching records.

๐Ÿ‘ˆ 5. LEFT JOIN

"LEFT JOIN" returns: All rows from the left table + matching rows from the right table.

SELECT c.customer_name, o.order_id, o.amount 
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id;


Result includes:

Charlie | NULL | NULL


๐ŸŽฏ 6. Finding Customers Who Never Ordered

This is a very common interview and analytics problem.

SELECT c.customer_id, c.customer_name 
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.customer_id IS NULL;


This technique is often called an anti-join pattern.

๐Ÿ‘‰ 7. RIGHT JOIN

"RIGHT JOIN" returns: All rows from the right table + matching rows from the left table.

In practice, many analysts prefer rewriting a RIGHT JOIN as a LEFT JOIN by switching table order because it is often easier to read.

๐Ÿ”„ 8. FULL OUTER JOIN

"FULL OUTER JOIN" returns: All rows from both tables, whether they match or not.

Conceptually:

LEFT JOIN + RIGHT JOIN

It can reveal: matching records, customers without orders, orders without matching customers.

โš ๏ธ Not every database supports "FULL OUTER JOIN" directly.

๐Ÿ†š 9. INNER JOIN vs LEFT JOIN

โ€ข INNER JOIN = Returns only customers with matching orders.

โ€ข LEFT JOIN = Returns all customers, including those without orders.

Simple rule:

INNER JOIN = matching records,

LEFT JOIN = keep everything from the left table.

๐Ÿ“Š 10. JOIN + Aggregation

Question:

ยซHow much has each customer spent?ยป
  • โค 5
More from @sqlanalyst
  1. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  2. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  3. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  4. Sep 29, 2026SQL Interview Series โ€” Part 2 ๐Ÿ“Œ Question 2: Find Duplicate Records Suppose you have an Emโ€ฆ
  5. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  6. Sep 29, 2026SQL Interview Series โ€” Part 1 Hi guys, let's start a SQL interview series covering frequenโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’