Given the following schema:
employees(emp-id, first-name, last-name, hire-date, dept-id, salary)
departments(dept-id, dept-name, manager-id, location-id)
You want to display the last names and hire dates of all latest hires in their respective departments in the location You issue the following query:
SQL>SELECT last-name, hire-date
FROM employees
WHERE (dept-id, hire-date) IN
(SELECT dept-id, MAX(hire-date)
FROM employees JOIN departments USING(dept-id)
WHERE location-id =1700
GROUP BY dept-id);
What is the outcome?
View worked solution
Answer: B
The SQL query executes successfully and produces the correct result. Let us analyze the query step-by-step:
- Subquery:
This subquery joins theSELECT dept-id, MAX(hire-date) FROM employees JOIN departments USING(dept-id) WHERE location-id = 1700 GROUP BY dept-idemployeesanddepartmentstables ondept-id, filters for departments located atlocation-id = 1700, groups the records bydept-id, and returns a set of pairs containing each department's ID along with its latest hire date (usingMAX(hire-date)). - Outer Query:
The outer query uses pairwise comparisonSELECT last-name, hire-date FROM employees WHERE (dept-id, hire-date) IN (...)(dept-id, hire-date) IN (...)to retrieve the last name and hire date of employees whose department and hire date match any of the(dept-id, MAX(hire-date))pairs returned by the subquery. This correctly identifies the latest hires in their respective departments for the specified location.
Since pairwise comparison and GROUP BY with joins are standard, valid SQL constructs, the query executes without errors and achieves the intended goal.
Free practice · Your answers on this page are not saved.
