ایک self-join ایک join ہے جہاں ایک table اپنے آپ سے join ہوتا ہے — یہ اس وقت مفید ہوتا ہے جب table کی rows ایسی ہی table میں دوسری rows سے متعلق ہوں، جیسے hierarchies (ملازمین اور ان کے منیجرز) یا table میں rows کا موازنہ۔
کلاسیکی مثال: ملازمین اور منیجرز
employees table — manager_id references another employee's id (in the SAME table):
id | name | manager_id
1 | Ann | NULL (Ann is the boss)
2 | Bob | 1 (Bob's manager is Ann)
3 | Carol | 1 (Carol's manager is Ann)
e.name employee, m.name manager
employees e
employees m e.manager_id m.id;
