Projection in SQL (Relational Algebra Concepts)
Notes: Projection in SQL and Relational Algebra
What is Projection?
In Relational Algebra, Projection is an operation that selects specific columns (attributes) from a relation
(table). It removes unwanted columns and keeps only those needed.
Symbol: pi
Example (Relational Algebra):
If we have a table STUDENT(id, name, age, course) and we want only name and course, then:
pi_name, course (STUDENT)
This means: Project the name and course columns from the STUDENT relation.
Equivalent in SQL:
SELECT name, course FROM STUDENT;
Key Features of Projection:
- Works on: Columns/Attributes only
- Removes: Duplicate rows (in Relational Algebra)
- SQL behavior: SQL allows duplicates unless DISTINCT is used
- Order of rows: Not preserved in relational algebra
- Output: New relation with selected columns
With Duplicate Removal:
In Relational Algebra, projection automatically removes duplicates.
To do this in SQL, use DISTINCT:
SELECT DISTINCT name FROM STUDENT;
Result Example:
STUDENT Table:
Projection in SQL (Relational Algebra Concepts)
| id | name | age | course |
|----|-------|-----|--------|
| 1 | Ali | 20 | CSE |
| 2 | Sara | 21 | EEE |
| 3 | Rani | 22 | ME |
| 4 | Sara | 21 | EEE |
Relational Algebra Query:
pi_name (STUDENT)
Result:
| name |
|-------|
| Ali |
| Sara |
| Rani |
(In Relational Algebra, duplicates are removed.)
In SQL:
SELECT name FROM STUDENT;
-> Shows all values, including duplicates.
SELECT DISTINCT name FROM STUDENT;
-> Removes duplicates, matches relational algebra behavior.