← Back to MySQL Course | Chapter 9: Joins | Lesson 6 of 10

SELF JOIN

एक SELF JOIN एक class list जैसा है जहाँ हर student उस same list पर मौजूद अपने buddy का नाम भी बताता है। आप same table को दो बार इस्तेमाल करते हैं, हर इस्तेमाल को अपना nickname देते हुए।
Syntax
sql
SELECT a.column, b.column
FROM table_name AS a
JOIN table_name AS b
  ON a.column = b.column;

एक SELF JOIN क्या है?

एक SELF JOIN किसी table को अलग aliases के साथ इसे दो बार reference करके खुद से join करता है। यह same table के अंदर rows compare करने के लिए उपयोगी है, जैसे same table में एक employee की salary को एक colleague की से compare करना।

उदाहरण: What is a SELF JOIN?

sql
CREATE TABLE employees (id INT, name TEXT, salary INT);
INSERT INTO employees VALUES (1, 'Amit', 50000), (2, 'Priya', 60000);
SELECT a.name AS employee, b.name AS colleague
FROM employees a JOIN employees b ON a.id <> b.id;

Hierarchy Manage करना

आप hierarchical relationships दिखाने के लिए एक self join इस्तेमाल कर सकते हैं, जैसे एक organization chart में employees को उनके managers से link करना, जहाँ employee और manager दोनों rows same employees table में रहती हैं।

उदाहरण: Managing Hierarchy

sql
CREATE TABLE employees (id INT, name TEXT, manager_id INT);
INSERT INTO employees VALUES (1, 'Rahul', NULL), (2, 'Amit', 1), (3, 'Priya', 1);
SELECT emp.name AS employee, mgr.name AS manager
FROM employees emp LEFT JOIN employees mgr ON emp.manager_id = mgr.id;

Consecutive Records ढूँढना

आप अपने data में consecutive records ढूँढने या events की sequences detect करने के लिए एक self join इस्तेमाल कर सकते हैं, जैसे same user से एक-दूसरे के थोड़े समय के अंदर दो log entries ढूँढना।

उदाहरण: Finding Consecutive Records

sql
CREATE TABLE logins (id INT, user_id INT, login_time TEXT);
INSERT INTO logins VALUES (1, 1, '10:00'), (2, 1, '10:05'), (3, 1, '11:00');
SELECT a.id, b.id
FROM logins a JOIN logins b ON a.user_id = b.user_id AND b.id = a.id + 1;

Rows को दूसरी Rows से Compare करना

Self joins products compare करना या same characteristics share करने वाले items ढूँढना आसान बनाते हैं, जैसे same price range के अंदर products, products table को एक matching category या price bracket पर खुद से join करके।

उदाहरण: Comparing Rows with Other Rows

sql
CREATE TABLE products (id INT, name TEXT, category TEXT, price INT);
INSERT INTO products VALUES (1, 'Pen', 'Stationery', 10), (2, 'Pencil', 'Stationery', 12);
SELECT a.name, b.name
FROM products a JOIN products b ON a.category = b.category AND a.id < b.id;

Advanced Path Exploration

आप multi-step paths trace करने के लिए किसी table पर कई self joins perform कर सकते हैं, जैसे flight layovers या manufacturing steps, जहाँ हर join एक single table में stored chain में अगले step पर hop करता है।

उदाहरण: Advanced Path Exploration

sql
CREATE TABLE flights (id INT, origin TEXT, destination TEXT);
INSERT INTO flights VALUES (1, 'Delhi', 'Mumbai'), (2, 'Mumbai', 'Goa');
SELECT f1.origin, f1.destination, f2.destination AS final_stop
FROM flights f1 JOIN flights f2 ON f1.destination = f2.origin;
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. बिना अलग aliases के same table को दो बार reference करना, जो एक Not unique table/alias error देता है।
  2. a.id = b.id पर join करना, जो बस हर row को खुद से match कर देता है।
  3. यह भूल जाना कि top-level rows (जैसे NULL manager_id वाला एक manager) एक inner join के साथ गायब हो जाती हैं, इसलिए LEFT JOIN इस्तेमाल करें।
🔒

Chapter Quiz — Complete all 10 topics to unlock

0/10 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.