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 ): In SQL,SELECTis the operation that chooses columns, so it matches relational-algebra projection. - SQL
SELECT≠ selection (): Relational-algebra selection is not the same as SQLSELECTjust because the word "select" appears in both. That is wrong; SQLSELECTis projection. - Duplicate handling: Relational-algebra projection does not allow duplicates — duplicate tuples are eliminated. SQL
SELECTwithout duplicate elimination may keep duplicate rows. SELECT DISTINCT: Adding the keywordDISTINCTeliminates duplicates in SQL, soSELECT DISTINCTis the form that matches relational-algebra projection exactly.
How it works
- 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.
- A simpler equivalent way is to apply the projection directly on the
Employeetable. - Both produce the same result, so the expression can be simplified.
- In SQL, choose columns with
SELECT; addDISTINCTwhen duplicates must be removed to match relational-algebra projection.
Formulas
- — projecting an attribute list twice gives the same result as projecting once; is the attribute list and is the base table.
Worked examples
For an attribute list on the Employee table:
- First form the required attribute list/result, then project on it: .
- Or project directly on the base table: .
- Both give the same answer, so the nested expression simplifies to .
Comparison
| Operation | Relational algebra equivalent | Duplicate rows |
|---|---|---|
SQL SELECT | Projection () | May be allowed unless DISTINCT is used |
SQL SELECT DISTINCT | Projection () with duplicate elimination | Duplicates removed |
| Relational-algebra selection () | Not SQL SELECT | Not the same operation as SQL SELECT |
Revision summary
- SQL
SELECTis projection (), not relational-algebra selection (); the shared word "select" is misleading. - Relational-algebra projection always eliminates duplicate tuples.
- SQL
SELECTmay retain duplicate rows; onlySELECT DISTINCTremoves them. SELECT DISTINCTis the exact SQL match for relational-algebra projection.- A projection of a projection can be simplified to a direct projection on the base table: .
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