Timesgroup interview questions: Self JOIN with an example
A self-join is used when a table is joined (compared) to itself. It means when you need to compare values in a column with other values in the same column in the same table.
Use of self-joins: obtaining running counts and running totals in an SQL query.
Example: select emp1.name,emp1.salary,emp1.state from employee emp1,employee emp2
where emp1.state=emp2.state;
---------------------------------------------------
Name Salary State
---------------------------------------------------
Rajesh Singh 10250 Delhi
Kavita 10850 Delhi
Kamal Singh 15250 Delhi
Rajesh Singh 20250 Haryana
Manish Kumar 15100 Haryana
---------------------------------------------------
Use of self-joins: obtaining running counts and running totals in an SQL query.
Example: select emp1.name,emp1.salary,emp1.state from employee emp1,employee emp2
where emp1.state=emp2.state;
---------------------------------------------------
Name Salary State
---------------------------------------------------
Rajesh Singh 10250 Delhi
Kavita 10850 Delhi
Kamal Singh 15250 Delhi
Rajesh Singh 20250 Haryana
Manish Kumar 15100 Haryana
---------------------------------------------------
Comments
Post a Comment