TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2351 4.54K
๐Ÿ”— SQL Fundamentals Part-4: JOINS

In real databases, data is stored in multiple tables, not one big table. JOINS allow you to combine data from different tables.

Example:
Customers Table
customer_id | name
1 | Rahul
2 | Priya

Orders Table
order_id | customer_id | amount
101 | 1 | 500
102 | 2 | 300

To see customer name + order amount, we must use JOIN.

Basic JOIN Syntax

SELECT columns
FROM table1
JOIN table2
ON table1.column = table2.column;

ON defines the relationship between tables.

1๏ธโƒฃ INNER JOIN
Returns only matching records from both tables.

SELECT customers.name, orders.amount
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id;

Result:
name | amount
Rahul | 500
Priya | 300

๐Ÿ‘‰ If a customer has no order, they will not appear.

2๏ธโƒฃ LEFT JOIN (Very Common โญ)

Returns: All rows from left table, Matching rows from right table, If no match โ†’ NULL

SELECT customers.name, orders.amount
FROM customers
LEFT JOIN orders
ON customers.customer_id = orders.customer_id;

Result:
name | amount
Rahul | 500
Priya | 300
Amit | NULL

๐Ÿ‘‰ Amit has no order.

3๏ธโƒฃ RIGHT JOIN

Opposite of LEFT JOIN. Returns: All rows from right table, Matching rows from left table

SELECT customers.name, orders.amount
FROM customers
RIGHT JOIN orders
ON customers.customer_id = orders.customer_id;

Used less frequently in analytics.

4๏ธโƒฃ FULL JOIN
Returns: All records from both tables, If no match โ†’ NULL

SELECT customers.name, orders.amount
FROM customers
FULL JOIN orders
ON customers.customer_id = orders.customer_id;

5๏ธโƒฃ SELF JOIN

A table joins with itself. Used when rows relate to other rows in the same table.

SELECT e.name AS employee, m.name AS manager
FROM employees e
LEFT JOIN employees m
ON e.manager_id = m.employee_id;

JOIN Visual Understanding

โ€ข INNER JOIN: Only matching rows
โ€ข LEFT JOIN: All left + matching right
โ€ข RIGHT JOIN: All right + matching left
โ€ข FULL JOIN: All rows from both
โ€ข SELF JOIN: Table joined with itself

Real Data Analyst Examples
-- Customer order report
SELECT c.name, o.amount
FROM customers c
JOIN orders o
ON c.customer_id = o.customer_id;

-- Products with category
SELECT p.product_name, c.category
FROM products p
JOIN categories c
ON p.category_id = c.category_id;

-- Sales by region
SELECT r.region_name, SUM(s.amount)
FROM sales s
JOIN regions r
ON s.region_id = r.region_id
GROUP BY r.region_name;

Used daily in Power BI dashboards, analytics queries, and reports.

Mini Practice Tasks
1. Show customer names with their order amount.
2. Show all customers even if they have no orders.
3. Show employees with their manager names.
4. Show products with their category name.

Common Interview Questions
โœ” Difference between INNER JOIN and LEFT JOIN
โœ” When to use SELF JOIN
โœ” Why LEFT JOIN is used in analytics
โœ” Difference between JOIN and UNION
โœ” Join execution order

Double Tap โ™ฅ๏ธ For More
  • โค 19
More from @sqlanalyst
  1. Oct 12, 2026SQL Interview Series โ€” Part 6 ๐Ÿ“Œ Question 6: Find the Top 2 Highest-Paid Employees in Eachโ€ฆ
  2. Oct 12, 2026๐Ÿš€ ๐—ช๐—ต๐—ถ๐—ฐ๐—ต ๐—ง๐—ฒ๐—ฐ๐—ต ๐—ฆ๐—ธ๐—ถ๐—น๐—น ๐—ช๐—ถ๐—น๐—น ๐—ฌ๐—ผ๐˜‚ ๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ? Want to builโ€ฆ
  3. Oct 9, 2026SQL Interview Series โ€” Part 5 ๐Ÿ“Œ Question 5: Find Employees Who Earn More Than Their Managโ€ฆ
  4. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  5. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  6. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
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 โ†’