Projection (π) is another fundamental operation in relational algebra. It is used to select specific columns (attributes) from a relation (table) while eliminating duplicate rows. Unlike selection, which works on rows based on a condition, projection works on the columns, reducing the set of attributes in the result.
π_column1, column2, ... (R)
Consider the following relation Students:
| StudentID | Name | Age | Major |
|---|---|---|---|
| 1 | Alice | 20 | CS |
| 2 | Bob | 22 | Math |
| 3 | Charlie | 21 | Physics |
| 4 | Dave | 23 | CS |
| 5 | Eve | 19 | Math |
If we want to project only the Name of all students from the Students relation, we would use the following projection:
π(Name)(Students)
Result:
| Name |
|---|
| Alice |
| Bob |
| Charlie |
| Dave |
| Eve |
Name column, and there are no duplicates.If we want to project the Name and Age columns from the Students relation, we would use the following projection:
π(Name, Age)(Students)
Result:
| Name | Age |
|---|---|
| Alice | 20 |
| Bob | 22 |
| Charlie | 21 |
| Dave | 23 |
| Eve | 19 |
Name and Age columns.Name-Age combinations in the original relation, they would be removed in the result. However, in this case, each combination is unique.Suppose we modify the Students relation, where there are duplicate rows for some students (same Name and Age):
| StudentID | Name | Age | Major |
|---|---|---|---|
| 1 | Alice | 20 | CS |
| 2 | Bob | 22 | Math |
| 3 | Alice | 20 | CS |
| 4 | Dave | 23 | CS |
| 5 | Bob | 22 | Math |
If we now project on Name and Age, the result would eliminate the duplicate rows:
π(Name, Age)(Students)
Result:
| Name | Age |
|---|---|
| Alice | 20 |
| Bob | 22 |
| Dave | 23 |
(Name, Age) pair because duplicates are removed in projection.Let's say we want to find the distinct Majors in the Students table, ignoring other attributes. The query would look like this:
π(Major)(Students)
Result:
| Major |
|---|
| CS |
| Math |
| Physics |
Major column.You can combine projection and selection to refine your queries. For example, suppose you want to find the Names of all students majoring in "CS" and who are older than 20. You would first use selection to filter the rows, and then use projection to retrieve the Name column.
π(Name)(σ(Major = 'CS' AND Age > 20)(Students))
Explanation:
σ(Major = 'CS' AND Age > 20)(Students) selects the students who major in "CS" and are older than 20.π(Name) projects only the Name column of the selected rows.Result:
| Name |
|---|
| Dave |
Dave satisfies both conditions, so the result only contains his name.π_column1, column2, ... (R) where column1, column2, ... are the attributes you want in the result and R is the relation.π(Name)(Students) selects only the Name column.π(Name, Age)(Students) selects the Name and Age columns, removing duplicates if any.Open this section to load past papers