TGTGInsighttelegram intelligenceLIVE / telegram public index
← Data Analytics
Data Analytics avatar

TGINSIGHT POST

Post #2737

@sqlspecialist

Data Analytics

Views6,030Post view count
PostedApr 2504/25/2026, 07:22 PM
Post content

Post content

🔥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