SELF JOIN
In this page:
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?
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
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
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
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
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;
- बिना अलग aliases के same table को दो बार reference करना, जो एक
Not unique table/aliaserror देता है। a.id = b.idपर join करना, जो बस हर row को खुद से match कर देता है।- यह भूल जाना कि top-level rows (जैसे
NULLmanager_idवाला एक manager) एक inner join के साथ गायब हो जाती हैं, इसलिएLEFT JOINइस्तेमाल करें।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: