Given a table Employees with columns EmployeeID, Name, and ManagerID (which references EmployeeID), what is the output of the following query?
SELECT e.Name AS Employee, m.Name AS Manager FROM Employees e LEFT JOIN Employees m ON e.ManagerID = m.EmployeeID ORDER BY e.EmployeeID;
SELECT e.Name AS Employee, m.Name AS Manager FROM Employees e LEFT JOIN Employees m ON e.ManagerID = m.EmployeeID ORDER BY e.EmployeeID;
Think about how the ManagerID links to EmployeeID in the same table.
The query joins the Employees table to itself to find each employee's manager by matching ManagerID to EmployeeID. Employees without a manager have ManagerID as null, so their manager name is null.
Which option contains a syntax error in this self join query to find employees and their managers?
SELECT e.Name, m.Name FROM Employees e JOIN Employees m ON e.ManagerID = m.EmployeeID;
Look for incomplete clauses or misplaced keywords.
Option B ends with WHERE; which is incomplete and causes a syntax error. The WHERE clause must be followed by a condition.
In a self join used to represent hierarchical data, what is the main purpose of joining the table to itself?
Think about how hierarchical relationships are stored in one table.
Self join allows comparing rows within the same table to find hierarchical relationships, such as which employee reports to which manager.
Given the table Categories with columns CategoryID, Name, and ParentID, what is the output of this query?
SELECT c.Name AS Child, p.Name AS Parent FROM Categories c LEFT JOIN Categories p ON c.ParentID = p.CategoryID WHERE p.Name IS NOT NULL ORDER BY c.Name;
SELECT c.Name AS Child, p.Name AS Parent FROM Categories c LEFT JOIN Categories p ON c.ParentID = p.CategoryID WHERE p.Name IS NOT NULL ORDER BY c.Name;
Remember that ParentID links to CategoryID of the parent category.
The query finds child categories with their parent names, excluding those without parents by filtering p.Name IS NOT NULL.
Consider this query on Employees table:
SELECT e.Name, m.Name FROM Employees e INNER JOIN Employees m ON e.ManagerID = m.EmployeeID WHERE e.ManagerID IS NULL;
Why does it return no rows?
Think about how INNER JOIN and WHERE conditions interact.
The WHERE e.ManagerID IS NULL filters employees without managers, but the join requires matching managers, so no rows satisfy both conditions.