1. Gyanastra
  2. Database Systems
  3. SQL Joins and Nested Queries
  4. Multi-Table SELECT Query with Join Condition: Employees in IT Department
Free lesson, no account needed 6 min read 20 practice questions after it
Asked in past papers16 times
  • GATE CSE202520222020201320092006200420031999199319911990
  • ISRO2018
  • UGC NET202220182012

Database Systems · SQL Joins and Nested Queries

Multi-Table SELECT Query with Join Condition: Employees in IT Department

Overview

A query may need attributes and a filtering condition that live in two different tables.
Here the employee names and IDs are in the employee table, but the word "IT" exists only in the department table, so the two tables must be joined before the IT filter can be applied.

Key concepts

  • Problem: retrieve the names and employee IDs of employees who work for the IT department.
  • Why a join is needed: the employee table stores only a department number (dept_no) for each employee; it contains nothing that says "IT". The word IT exists only in the department table (column dept_name).
    The attributes to display (emp_id, emp_name) live in the employee table.
  • Rule: whenever the required attributes and the filtering condition live in two different tables, the two tables must be joined to obtain the required attributes.
  • Cartesian (cross) product: writing several tables in the FROM clause is nothing but the Cartesian product of those tables — every employee row is paired with every department row, producing a large number of tuples.
  • Join condition: the equality condition between the foreign key and the primary key, dept_no = dept_id (the employee's department number equals the department table's department ID).
    It is applied as a selection after the Cartesian product and removes the meaningless tuples.
  • Equi-join result size: after joining employee with department on the department key, the number of tuples equals the number of tuples in the employee table — each employee is matched with exactly the one department he works for.
  • Projection: project exactly the attributes asked for — here only emp_id and emp_name.
Figure. The employee table holds dept_no while department holds dept_name IT. The Cartesian product is reduced by the join condition dept_no = dept_id, then filtered on IT and projected to emp_id and emp_name.

How it works

  1. Decide what to display (projection). Only two columns are needed: emp_id and emp_name. Nothing else is asked for, so nothing else is projected.
  2. Decide which tables to use (Cartesian product). Write both tables in the FROM clause:
SELECT emp_id, emp_name
FROM employee, department;

This pairs every employee row with every department row. 3. Remove the meaningless tuples with the join condition. In the Cartesian product, employee 1 appears paired with all departments; only the pairing in which the employee's own department number appears is meaningful (employee 1 must be paired only with department number 5).
Apply the selection condition:

SELECT emp_id, emp_name
FROM employee, department
WHERE employee.dept_no = department.dept_id;

After this condition, each employee is matched with exactly the one department he works for — the result is employee data with the corresponding department data adjacent to it. 4. Add the actual filtering condition (IT department). Out of the joined tuples, keep only those where the department name is IT:

SELECT emp_id, emp_name
FROM employee, department
WHERE employee.dept_no = department.dept_id
  AND department.dept_name = 'IT';
  1. Print only the projected columns. The final print is only emp_id and emp_name for the rows that survived Step 4.
  2. Optimization remark. The general execution strategy above can be optimized: first filter the department table on IT (make it small), then join the small result with the employee table.
    The optimal way is to filter first, make the relation small, and then join. Query optimization is not part of the GATE syllabus, so it is not discussed in detail.

Formulas

  • Relational algebra form of the whole query:

  • pi_emp_id, emp_name ( sigma_dept_name='IT' ( sigma_employee.dept_no = department.dept_id ( employee × department ) ) )

  • Reading it: Cartesian product → selection with the join condition → selection on dept_name = 'IT' → projection on emp_id, emp_name.

Worked examples

  • Employee 1 has dept_no = 5; the department table has a row with dept_id = 5. In the Cartesian product, employee 1 is paired with department 1, department 3, department 5, … — all of them.
  • After the condition dept_no = dept_id, employee 1 stays paired only with department 5 (department 1 matches with 1, department 3 matches with 3, and so on).
  • Because IT is one specific department row, employee 1 (paired with department 5) is discarded; only employees whose dept_no equals the dept_id of the IT row survive.
    The same employee rows that had also been paired with other department entries are dropped.
  • Final printed output after projecting (emp_id, emp_name):
emp_idemp_name
2[unclear]
3[unclear]
4Saumya

Employee IDs 2 and 3 (with their names) and employee ID 4 with name Saumya are printed — only ID and name, nothing else.

Revision summary

  • Listing multiple tables in FROM = Cartesian product, which creates many tuples;
    the useless ones must be filtered by a condition in WHERE.
  • The equality condition between the foreign key and the primary key (dept_no = dept_id) is the join condition.
  • After an equi-join of employee with department on the department key, the number of tuples equals the number of tuples in the employee table (one department match per employee).
  • Project exactly the attributes asked for — here only emp_id and emp_name.
  • Use the second table only when the attribute needed for display or for the condition is not present in the first table.
  • Optimization idea: filter (e.g. dept_name = 'IT') first to make the relation small, then join.
    Query optimization is not in the GATE syllabus.

Previous-year exam insights

  • A join written as FROM R, S WHERE R.a = S.b is an inner join: tuples with NULL or unmatched join keys are eliminated, so the result can have fewer tuples than either table;
    use LEFT/RIGHT OUTER JOIN to keep unmatched rows (with NULLs).
    Asked inGATE 2025GATE 2022GATE 2020
  • When the same table is used twice (self-join), alias it (e.g. FROM position p1, position p2) and qualify every column with the alias; unqualified or identical aliases make the condition trivially true and the query meaningless.
    Asked inUGC NET 2012
  • A multi-table query with a join condition and a filter is equivalent to a nested query using IN / EXISTS / = ANY on the key, and to the relational-algebra expression pi(...)(sigma_filter(R join S));
    exam questions ask which formulations are equivalent.
    Asked inGATE 2013GATE 2009UGC NET 2022
  • NOT IN with a subquery that returns NULL (or a NULL in the outer column) evaluates to UNKNOWN and returns no rows; NOT EXISTS does not have this problem, so NOT IN and NOT EXISTS are not always equivalent.
    Asked inGATE 2009
  • Without DISTINCT, SQL does not remove duplicate result tuples (bag/multiset semantics), whereas relational algebra projection removes duplicates; SELECT DISTINCT is needed to match pi.
    Asked inGATE 2013UGC NET 2012
  • Aggregate functions (SUM, COUNT, AVG) over a join are computed after the join, so a tuple duplicated by the join is counted multiple times; grouping is by the non-aggregated selected columns.
    Asked inUGC NET 2018
Pages 1–2 of 2