Self joins & UNION
A self join treats the same table as two roles, such as employee and manager. UNION combines compatible result sets and removes duplicates; UNION ALL keeps them. Both sides need the same column count.
हिन्दी में समझें
सेल्फ जॉइन में एक टेबल दो भूमिकाओं में आती है, जैसे कर्मचारी और मैनेजर। UNION दो परिणाम जोड़कर दोहराव हटाता है; UNION ALL रखता है।
अभ्यास: customers और employees के अनोखे name जोड़ें, name क्रम में।
Hinglish mein samjhein
Self join same table ko do roles mein use karta hai, employee aur manager jaisi. UNION result sets jodkar duplicates hatata hai; UNION ALL unhe rakhta hai.
Practice: customers aur employees ke unique names combine karke sort karo.
A small SQL example
SELECT e.name, m.name AS manager FROM employees e LEFT JOIN employees m ON e.manager_id=m.id ORDER BY e.id;Examples use the fictional shop database bundled with the SQL playground: customers, products, orders, order_items and employees. The engine is SQLite; other SQL dialects may differ.
Try this example in the SQL playground →Practise this idea
Show unique names from customers and employees combined, sorted by name.
Check answer & explanation
SELECT name FROM customers UNION SELECT name FROM employees ORDER BY name;UNION combines compatible single-column results and removes duplicate names. The final ORDER BY sorts the combined set.
Stuck in the course? Choose Check answer for either the challenge or quiz. Read the explanation and continue to the next lesson at your own pace.
Check your understanding
Which keeps duplicate rows?
Check the quiz answer
UNION ALL
A self join treats the same table as two roles, such as employee and manager. UNION combines compatible result sets and removes duplicates; UNION ALL keeps them. Both sides need the same column count.
References & corrections
Use the official SQLite documentation for full language rules. Read how these lessons are made and checked. Found a mistake? Report it to Excel Keys with this lesson URL and a reproducible example.