GATE / CS ARCHIVE

GATE CSE 2014 SET 1 / QUESTION 54

GATE CSE 2014 Set 1 Q54

Database Management Systems · SQL & Queries · Full GATE CSE 2014 Set 1 paper

GATE 2014 Set 1, Q54 2 marks · MCQ
Report issue

SQL & Queries

Question 54, GATE 2014: Schemas: employees(emp-id,first-name,last-name,hire-date,dept-id,salary) and departments(dept-id,dept-name,manager-id,location-id). To list last names and hire dates of all latest hires in each department at location 1700, the printed query is 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). Outcome? A: Executes but incorrect. B: Executes and correct. C: Error because of pairwise comparison. D: Error because GROUP BY cannot accompany joins in a subquery. Hyphens are printed unquoted.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Our answer: B after identifier syntax repair

1. Method and setup. Read the subquery from its FROM clause onward: join employees to departments, restrict the departments by location, and group to obtain each department’s latest date. The outer predicate must match both department and date, preventing a latest date from one department accidentally selecting a person in another. Separately audit the literal identifier syntax.

2. Work through the result and its assumptions. Treating the hyphenated labels as properly quoted identifiers (or replacing hyphens with underscores), the inner query groups employees by department at location 1700 and returns each latest hire date. The outer row-value IN comparison selects all employees tied at those dates in their departments. Thus the intended result is correct, B; GROUP BY with a join and pairwise IN are valid. Literally, unquoted dept-id, last-name and other hyphenated names are not ordinary SQL identifiers: subtraction is parsed or syntax fails, especially USING(dept-id). The printed query needs identifier quoting/renaming.

Original paper, page 21 · Answer key