Empowering CS learners, aspiring programmers, and startups with AI, Data Science & Programming insights — scaling skills from learning foundations to enterprise-grade solutions.
SQL> SELECT a.ID, b.NAME, a.SALARY
FROM CUSTOMERS a, CUSTOMERS b
WHERE a.SALARY < b.SALARY;
I understand self join is creating two temporary tables out of the same table.
In the above query, two temporary tables (with aliases a and b) created out of same table CUSTOMERS. While ID and SALARY are retrieved from a, NAME from b.
Beyond that, an explanation of the above query will be helpful. Thanks in advance.
The join creating one giant table out of the table. It is comparing all the rows with all the rows, and any time b.salary is more than a.salary, it’s returning the id and salary of the lower paid individual and the name of the higher paid individual.
All the other rows where the salaries are the same or b lower get tossed from the results.