All Exams Test series for 1 year @ ₹349 only
Question

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?

Employee
 

EmpIDTeamID
18
28
38
47
57
69



Output
 

EmpIDTeamSize
13
23
33
42
52
61

The correct answer is
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

SQL Solution for Employee Team Size

The objective is to determine the size of the team each employee belongs to. This involves aggregating team membership counts and then associating these counts with individual employees.

1. Calculate Team Sizes

A subquery is used to calculate the number of employees per team:

SELECT TeamID, COUNT(TeamID) AS TeamSize FROM Employee GROUP BY TeamID
  • $GROUP BY TeamID$ aggregates records by team.
  • $COUNT(TeamID)$ counts employees in each team, defining $TeamSize$.
  • This creates a temporary result set (aliased as $B$) mapping $TeamID$ to $TeamSize$.

2. Associate Team Sizes with Employees

The main query joins the original $Employee$ table with the subquery results:

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
  • $Employee AS E$ represents the individual employee records.
  • The subquery result is aliased as $B$.
  • The join condition $E.TeamID = B.TeamID$ links each employee to their team's calculated size.
  • The final selection retrieves the employee's ID ($E.EmpID$) and their team's size ($B.TeamSize$).

Examination of Alternative Queries

  • Option 2: The self-join $WHERE A.EmpID = B.EmpID$ incorrectly restricts the count to 1 per employee, failing to measure team size.
  • Option 3: Grouping by $EmpID$ counts associated $TeamID$s per employee (always 1), not the total members in that employee's team.
  • Option 4: The subquery omits $TeamID$ from its output, making the join condition $WHERE A.TeamID = B.TeamID$ invalid due to a missing column in the subquery result $B$.
Was this answer helpful?

Important Questions from SQL

  1. Let Account be a relation as shown. 

                                                                                                             Account

    AccNoBalance
    A15000
    A25000
    A310000
    A415000
    A518000

    Consider the given SQL query.

    SELECT AccNo FROM Account AS A 

    WHERE (SELECT COUNT(*) FROM Account AS B  

    WHERE A.Balance < B.Balance) >= (SELECT COUNT(*)  

    FROM Account AS C WHERE A.Balance > C.Balance)

    The number of rows returned by the SQL query is __________ . (Answer in integer)

  2. Consider the following two tables named Raider and Team in a relational database maintained by a Kabaddi league. The attribute ID in table Team references the primary key of the Raider table, ID.

    Raider
    IDNameRaidsRaidPoints
    1Arjun200250
    2Ankush190219
    3Sunil150200
    4Reza150190
    5Pratham175220
    6Gopal193215
    Team
    CityIDBidPoints
    Jaipur2200
    Patna3195
    Hyderabad5175
    Jaipur1250
    Patna4200
    Jaipur6200

    The SQL query described below is executed on this database: 

    SELECT * 

    FROM Raider, Team 

    WHERE Raider.ID=Team.ID AND City="Jaipur" AND 

    RaidPoints > 200; 

    The number of rows returned by this query is ________.

  3. On a relation named Loan of a bank:
     

                             LOAN
    loan_numberbranch_nameamount
    L11Banjara Hills90000
    L14Kondapur50000
    L15SR Nagar40000
    L22SR Nagar25000
    L23Balanagar80000
    L25Kondapur70000
    L19SR Nagar65000


    the following SQL query is executed.
    SELECT L1.loan_number
    FROM Loan L1
    WHERE L1.amount > (SELECT MAX (L2.amount)
                                       FROM Loan L2
                                       WHERE L2.branch_name = 'SR Nagar');
    The number of rows returned by the query is ____________ (Answer in integer)

  4. Which of the following statements are correct? 

    A. The HAVING CLAUSE is an optional clause which tells Oracle to group rows based on distinct values that exist for specified columns.

     B. Unions can be used in subqueries. 

    C. The intersect clause outputs only rows produced by both the queries intersected. 

    D. The statement containing a subquery is called a parent statement. 

    E. If a select statement is defined as a subquery, the innermost select statement gets executed first.

     Choose the correct answer from the options given below:

  5. Consider the following two commands C1 and C2 on the relation R from an SQL database :
    C1 : drop table R;
    C2 : delete from R;
    Which of the following statements is TRUE ?
    I. Both C1 and C2 delete the schema for R.
    II. C2 retains relation R, but deletes all tuples in R.
    III. C1 deletes not only all tuples of R, but also the schema for R.
Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App