GATE DA 2026 · SQL Queries

MSQ · 2 marks · Problem DBMS-SQL-0027

Consider a table Employee(EmpID, TeamID), where the column EmpID (ID of an employee) is the primary key. The column TeamID denotes the team ID of the team of which the employee is a member. TeamID is a NOT NULL column. We want to display the size of the team (denoted as TeamSize) in which each employee is a member by using SQL. As an example, the desired output for the given Employee table is also shown in tabular form. Which of the following is/are correct? \(\textbf{Employee}\)\[\begin{array}{|c|c|} \hline \text{EmpID} & \text{TeamID} \\ \hline 1 & 8 \\ \hline 2 & 8 \\ \hline 3 & 8 \\ \hline 4 & 7 \\ \hline 5 & 7 \\ \hline 6 & 9 \\ \hline \end{array}\]\(\textbf{Output}\)\[\begin{array}{|c|c|} \hline \text{EmpID} & \text{TeamSize} \\ \hline 1 & 3 \\ \hline 2 & 3 \\ \hline 3 & 3 \\ \hline 4 & 2 \\ \hline 5 & 2 \\ \hline 6 & 1 \\ \hline \end{array}\]
  1. A
    SELECT E.EmpID, B.TeamSize FROM Employee AS E, (SELECT TeamID, COUNT(TeamID) AS TeamSize FROM Employee GROUP BY TeamID) AS B WHERE E.TeamID = B.TeamID
  2. B
    SELECT A.EmpID, COUNT(B.TeamID) AS TeamSize FROM Employee AS A, Employee AS B WHERE A.TeamID = B.TeamID AND A.EmpID = B.EmpID GROUP BY A.EmpID
  3. C
    SELECT B.EmpID, B.TeamSize FROM (SELECT EmpID, COUNT(TeamID) AS TeamSize FROM Employee GROUP BY EmpID) AS B
  4. D
    SELECT A.EmpID, B.TeamSize FROM Employee AS A, (SELECT COUNT(TeamID) AS TeamSize FROM Employee GROUP BY TeamID) AS B WHERE A.TeamID = B.TeamID

Solve it on GATE DA practice →More Relational Model & SQL problems

Show the worked solutionWe need a subquery that computes team sizes (grouped by \(\text{TeamID}\)) and joins that back to every employee row via matching \(\text{TeamID}\).Correct query: \(\text{SELECT TeamID, COUNT(TeamID) AS TeamSize FROM Employee GROUP BY TeamID}\) (as subquery \(B\)) correctly computes one row per team with its size, retaining the \(\text{TeamID}\) column needed for the join; joining this to \(E\) on \(\text{E.TeamID = B.TeamID}\) correctly attaches each employee to their team's size.The other options fail: grouping/joining by \(\text{EmpID}\) instead of \(\text{TeamID}\) (options where the self-join or grouping matches each employee only to themselves) always gives a team size of \(1\) for everyone, which is wrong; and one option's subquery omits \(\text{TeamID}\) from its own \(\text{SELECT}\) list while still trying to join on it, which is invalid SQL.Answer: The query using "(SELECT TeamID, COUNT(TeamID) AS TeamSize FROM Employee GROUP BY TeamID)" as a subquery, joined on E.TeamID = B.TeamID, is correct.Click Here for Detail SolutionGateOverflow discussion: https://gateoverflow.in/523173/gate-da-2026-question-41

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com