3. Given tables Employees (primary key EmployeeID) and Departments (foreign key ManagerID referencing EmployeeID), what will this query return?
SELECT Employees.Name, Departments.DepartmentName FROM Employees JOIN Departments ON Employees.EmployeeID = Departments.ManagerID;
medium
A. Syntax error due to wrong join condition
B. List of employee names who manage departments with their department names
C. List of departments without any employee names
D. List of all employees with all departments regardless of manager
Solution
Step 1: Understand join condition
The join matches Employees.EmployeeID to Departments.ManagerID, linking managers to their departments.
Step 2: Result of the join
The query returns names of employees who are managers and the names of the departments they manage.
Final Answer:
List of employee names who manage departments with their department names -> Option B
Quick Check:
Join on manager ID returns managers with departments [OK]
Hint: Join foreign key to primary key shows related records [OK]
Common Mistakes:
Thinking it returns all employees regardless of management
Assuming syntax error due to join condition
Expecting departments without managers
4. Consider these tables: Products(ProductID PK, Name) Sales(ProductID FK, Quantity) Why does this query cause an error?
SELECT * FROM Products JOIN Sales ON Products.ID = Sales.ProductID;
medium
A. Column Products.ID does not exist, causing an error
B. Foreign key cannot be used in JOIN condition
C. JOIN syntax is incorrect, missing JOIN type
D. Sales table must be listed first in FROM clause
Solution
Step 1: Check column names in JOIN condition
The Products table has ProductID as primary key, not ID.
Step 2: Identify cause of error
Using Products.ID causes an error because that column does not exist.
Final Answer:
Column Products.ID does not exist, causing an error -> Option A
Quick Check:
Wrong column name in JOIN = error [OK]
Hint: Verify column names exactly before joining [OK]
Common Mistakes:
Using wrong or misspelled column names
Thinking foreign keys can't be joined
Assuming JOIN type is mandatory
5. You have two tables: Authors(AuthorID PK, Name) Books(BookID PK, Title, AuthorID FK) Write a query to list each author with the count of books they wrote, including authors with zero books.
hard
A. SELECT Authors.Name, COUNT(Books.BookID) FROM Authors LEFT JOIN Books ON Authors.AuthorID = Books.AuthorID GROUP BY Authors.Name;
B. SELECT Authors.Name, COUNT(Books.BookID) FROM Authors JOIN Books ON Authors.AuthorID = Books.AuthorID GROUP BY Authors.Name;
C. SELECT Authors.Name, COUNT(*) FROM Books JOIN Authors ON Books.AuthorID = Authors.AuthorID GROUP BY Authors.Name;
D. SELECT Authors.Name, COUNT(Books.BookID) FROM Books LEFT JOIN Authors ON Books.AuthorID = Authors.AuthorID GROUP BY Authors.Name;
Solution
Step 1: Use LEFT JOIN to include all authors
LEFT JOIN keeps all authors even if they have no matching books.
Step 2: Count books per author
COUNT(Books.BookID) counts books; NULLs for authors without books count as zero.
Final Answer:
SELECT Authors.Name, COUNT(Books.BookID) FROM Authors LEFT JOIN Books ON Authors.AuthorID = Books.AuthorID GROUP BY Authors.Name; -> Option A
Quick Check:
LEFT JOIN + COUNT on foreign key = authors with book counts [OK]
Hint: Use LEFT JOIN to include all from primary key table [OK]