SQL Self Join
1.What is a SELF JOIN?
-
A. Joining with another table
-
B. Joining a table to itself
-
C. Creating backup
-
D. INNER JOIN with different keys
Joining a table to itself
Joining a table to itself
2.Why use a SELF JOIN?
-
A. Find duplicate rows
-
B. Hierarchical data (e.g., employees & managers)
-
C. Remove NULLs
-
D. Combine unrelated tables
Hierarchical data (e.g., employees & managers)
Hierarchical data (e.g., employees & managers)
3.Which SQL shows a SELF JOIN?
SELECT A.name, B.name
FROM employees A, employees B
WHERE A.manager_id = B.emp_id;
-
A. INNER JOIN
-
B. SELF JOIN
-
C. CROSS JOIN
-
D. FULL JOIN
SELF JOIN
SELF JOIN
4.What will be output of this query?
Table: employees
id | name | manager_id
1 | Ravi | NULL
2 | Sita | 1
3 | Aman | 1
SELECT A.name, B.name AS manager
FROM employees A
JOIN employees B ON A.manager_id = B.id;
-
A. 2 rows – Sita and Aman with manager Ravi
-
B. All employees
-
C. 1 row only
-
D. Error
2 rows – Sita and Aman with manager Ravi
2 rows – Sita and Aman with manager Ravi
5.Which type of JOIN is used internally for SELF JOIN?
-
A. FULL OUTER
-
B. INNER or LEFT JOIN
-
C. RIGHT JOIN only
-
D. NATURAL JOIN
INNER or LEFT JOIN
INNER or LEFT JOIN