1. Gyanastra
  2. Database Systems
  3. SQL Basics
  4. SELECT vs Projection: SELECT DISTINCT Equivalence
Free lesson, no account needed 4 min read 11 practice questions after it
Asked in past papers4 times
  • GATE CSE20152000
  • ISRO2018
  • UGC NET2017

Database Systems · SQL Basics

SELECT vs Projection: SELECT DISTINCT Equivalence

Overview

SQL's SELECT clause corresponds to projection in relational algebra, not to the relational-algebra selection operation, despite the similar names.
The one real difference is duplicate handling: relational-algebra projection always removes duplicates, while SQL SELECT keeps them unless DISTINCT is added.

Key concepts

  • Projection on a base table: A projection can be applied directly to the base table instead of first forming an intermediate attribute list/result and then projecting again. Both ways give the same answer.
  • SQL SELECT = projection (PROJECT π\pi): In SQL, SELECT is the operation that chooses columns, so it matches relational-algebra projection.
  • SQL SELECT ≠ selection (σ\sigma): Relational-algebra selection is not the same as SQL SELECT just because the word "select" appears in both. That is wrong; SQL SELECT is projection.
  • Duplicate handling: Relational-algebra projection does not allow duplicates — duplicate tuples are eliminated. SQL SELECT without duplicate elimination may keep duplicate rows.
  • SELECT DISTINCT: Adding the keyword DISTINCT eliminates duplicates in SQL, so SELECT DISTINCT is the form that matches relational-algebra projection exactly.

How it works

  1. Given an employee table and a required attribute list, one way is to first find the required attribute list/result, then apply the projection on that result.
  2. A simpler equivalent way is to apply the projection directly on the Employee table.
  3. Both produce the same result, so the expression can be simplified.
  4. In SQL, choose columns with SELECT; add DISTINCT when duplicates must be removed to match relational-algebra projection.

Formulas

  • πA(πA(Employee))=πA(Employee)\pi_A(\pi_A(Employee)) = \pi_A(Employee) — projecting an attribute list AA twice gives the same result as projecting once; AA is the attribute list and EmployeeEmployee is the base table.

Worked examples

For an attribute list AA on the Employee table:

  • First form the required attribute list/result, then project on it: πA(πA(Employee))\pi_A(\pi_A(Employee)).
  • Or project directly on the base table: πA(Employee)\pi_A(Employee).
  • Both give the same answer, so the nested expression simplifies to πA(Employee)\pi_A(Employee).

Comparison

OperationRelational algebra equivalentDuplicate rows
SQL SELECTProjection (π\pi)May be allowed unless DISTINCT is used
SQL SELECT DISTINCTProjection (π\pi) with duplicate eliminationDuplicates removed
Relational-algebra selection (σ\sigma)Not SQL SELECTNot the same operation as SQL SELECT

Revision summary

  • SQL SELECT is projection (π\pi), not relational-algebra selection (σ\sigma); the shared word "select" is misleading.
  • Relational-algebra projection always eliminates duplicate tuples.
  • SQL SELECT may retain duplicate rows; only SELECT DISTINCT removes them.
  • SELECT DISTINCT is the exact SQL match for relational-algebra projection.
  • A projection of a projection can be simplified to a direct projection on the base table: πA(πA(Employee))=πA(Employee)\pi_A(\pi_A(Employee)) = \pi_A(Employee).

Previous-year exam insights

  • SQL SELECT without DISTINCT is bag (multiset) semantics: it retains duplicates, whereas relational algebra is set-based and projection always removes duplicates; SELECT DISTINCT restores set semantics.
    Asked inGATE 2015GATE 2000UGC NET 2017
  • SELECT DISTINCT A,B FROM R1,R2 (with A,B attributes of R1) returns the distinct (A,B) tuples of R1 whenever R2 is non-empty, because the cross product repeats each R1 tuple |R2| times and DISTINCT removes the repeats; if R2 is empty the result is empty.
    Asked inGATE 2000UGC NET 2017
  • The number of tuples in a cross product R × S is |R|·|S|; without DISTINCT each R tuple appears |S| times in a projection onto R's attributes, while SELECT DISTINCT over R × S with a non-empty S returns the distinct tuples of R (an empty S gives an empty result).
    Asked inGATE 2000UGC NET 2017
  • Relational algebra selection σ chooses rows (tuples) satisfying a predicate, while projection π chooses columns (attributes); SQL expresses selection with the WHERE clause, not with SELECT.
    Asked inGATE 2015
  • Projection is idempotent: π_A(π_A(R)) = π_A(R), and more generally π_A(π_B(R)) = π_A(R) whenever A ⊆ B;
    also π_A(σ_c(R)) = π_A(σ_c(π_{A∪attrs(c)}(R))) when the predicate's attributes are retained.
  • SQL's SELECT clause list corresponds to π, the FROM clause to the cross product/cartesian product, and WHERE to σ; a full SQL query SELECT A FROM R WHERE c is equivalent to π_A(σ_c(R)).
    Asked inGATE 2015
Pages 1–2 of 2