GATE / CS ARCHIVE

GATE CSE 1999 / QUESTION 22(b)

Count high-paid staff in every department

Database Management Systems · SQL & Queries · Full GATE CSE 1999 paper

GATE 1999, Q22(b) 3 marks · Descriptive
Report issue

SQL & Queries

Question 22(b), GATE 1999: EMP(Employee-no, Dept-no, Employee-name, Salary), DEPT(Dept-no, Dept-name, Location). For each department number, count employees whose salary exceeds Rs.1,00,000.
Question 22(b), GATE 1999: EMP(Employee-no, Dept-no, Employee-name, Salary), DEPT(Dept-no, Dept-name, Location). For each department number, count employees whose salary exceeds Rs.1,00,000.
Tap to enlarge
Answer & solution

Answer: LEFT JOIN all departments to qualifying employees and count the matching employee department values, including zeros. (no official key was published)

To interpret “for each department” literally, start from DEPT so that departments with zero qualifying employees are retained:

SELECT d."Dept-no", COUNT(e."Dept-no") AS high_salary_count
FROM DEPT AS d
LEFT JOIN EMP AS e
  ON e."Dept-no" = d."Dept-no"
 AND e.Salary > 100000
GROUP BY d."Dept-no";

The salary condition belongs in ON: placing it in WHERE would discard the null-extended rows and remove zero-count departments. COUNT(e."Dept-no") counts only actual matches, since a matched equality implies a non-null employee department value; COUNT(*) would incorrectly count the null-extended row as one. Salaries equal to 100000 do not qualify. This assumes the conventional unique department number in DEPT.
If the expected interpretation asks only for department numbers represented by qualifying employees, the shorter traditional query is:
SELECT "Dept-no", COUNT(*) AS high_salary_count
FROM EMP
WHERE Salary > 100000
GROUP BY "Dept-no";
That version omits zero-count departments. It is a different coverage choice, so the all-department query is preferable for the literal wording. Attribute names are quoted to preserve the printed hyphens.

Original paper, page 9