TGViewer
Data Analytics Data Analytics @sqlspecialist ยท 111K subscribers
Post #2737 7.75K
๐Ÿ”ฅNow, letโ€™s move to the most important SQL topic โ€” JOINS ๐Ÿ’ฏ๐Ÿ”ฅ

๐Ÿง  1. What is a JOIN?

JOIN is used to combine data from multiple tables

๐Ÿ‘‰ If data is stored in different tables โ†’ JOIN helps you connect them

๐Ÿ“Š Example Tables

๐Ÿ‘จโ€๐Ÿ’ผ employees

emp_id name dept_id

1 Amit 101
2 Neha 102
3 Ravi 101

๐Ÿข departments

dept_id dept_name

101 IT
102 HR

๐Ÿ”— 2. INNER JOIN (Most Used ๐Ÿ”ฅ)

๐Ÿ‘‰ Returns only matching records

SELECT e.name, d.dept_name
FROM employees e
INNER JOIN departments d
ON e.dept_id = d.dept_id;

โœ” Only employees with valid department

โฌ…๏ธ 3. LEFT JOIN

๐Ÿ‘‰ Returns all records from left table + matched from right

SELECT e.name, d.dept_name
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.dept_id;

โœ” Even if department is missing โ†’ employee will still show

โžก๏ธ 4. RIGHT JOIN

๐Ÿ‘‰ Returns all records from right table + matched from left

SELECT e.name, d.dept_name
FROM employees e
RIGHT JOIN departments d
ON e.dept_id = d.dept_id;

๐Ÿ”„ 5. FULL JOIN

๐Ÿ‘‰ Returns all records from both tables

SELECT e.name, d.dept_name
FROM employees e
FULL JOIN departments d
ON e.dept_id = d.dept_id;

(Note: Not supported in MySQL directly โ€” use UNION instead)

โšก 6. Quick Summary

โ€ข INNER -> Only matching rows
โ€ข LEFT -> All left + matched right
โ€ข RIGHT -> All right + matched left
โ€ข FULL -> Everything

๐ŸŽฏ 7. Practice Tasks

1. Get employee name with department name

2. Show all employees even if no department

3. Show all departments even if no employees

4. Find employees without department

5. Count employees per department (using JOIN)

๐Ÿ”ฅ Practice Task Solutions ๐Ÿ‘‡

โœ… 1. Get employee name with department name (INNER JOIN)

SELECT e.name, d.dept_name
FROM employees e
INNER JOIN departments d
ON e.dept_id = d.dept_id;

โœ… 2. Show all employees even if no department (LEFT JOIN)

SELECT e.name, d.dept_name
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.dept_id;

โœ… 3. Show all departments even if no employees (RIGHT JOIN)

SELECT e.name, d.dept_name
FROM employees e
RIGHT JOIN departments d
ON e.dept_id = d.dept_id;

๐Ÿ‘‰ (Alternative using LEFT JOIN โ€” interview friendly)

SELECT e.name, d.dept_name
FROM departments d
LEFT JOIN employees e
ON e.dept_id = d.dept_id;

โœ… 4. Find employees without department

SELECT e.name
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.dept_id
WHERE d.dept_id IS NULL;

โœ… 5. Count employees per department (using JOIN)

SELECT d.dept_name, COUNT(e.emp_id) AS total_emp
FROM departments d
LEFT JOIN employees e
ON d.dept_id = e.dept_id
GROUP BY d.dept_name;

โšก Mini Challenge ๐Ÿ”ฅ

๐Ÿ‘‰ Find departments with no employees

โšก Mini Challenge Solution ๐Ÿ‘‡

SELECT d.dept_name  
FROM departments d
LEFT JOIN employees e
ON d.dept_id = e.dept_id
WHERE e.emp_id IS NULL;


Double Tap โค๏ธ For More
  • โค 24
  • ๐Ÿ‘ 1
More from @sqlspecialist
  1. Oct 9, 2026โ€œHere, the data is sorted by the second column in descending order and the first five rowsโ€ฆ
  2. Oct 9, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 6 Guys, let's continue our Data Analyst Interviewโ€ฆ
  3. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  4. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  5. Oct 7, 2026๐Ÿ“Š Kandinsky 6.0 Video: AI-Powered Content Creation for Analysts The new Kandinsky 6.0 Vidโ€ฆ
  6. Oct 7, 2026Alternatively, depending on the Excel version and requirement, I could use functions suchโ€ฆ
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 โ†’