The projection operator
This relational operator is a base for SQL (Structured Query Language) and is used to write queries. The projection operator is also known as vertical partitioning.
Note: The projection operator is always written in the last of a RA query. It encapsulates the query containing other operators.
A projection query looks like this:
The
Let's look at a few examples to make things clear:
Consider the following table:
StudentID | StudentName | CGPA |
1 | John | 3.45 |
2 | Harry | 3.50 |
3 | Elizabeth | 3.21 |
4 | Charlie | 2.94 |
The above table holds the data of a few students. Suppose we only want the names and CGPA of the students, so:
The result of the query above will be:
StudentName | CGPA |
John | 3.45 |
Harry | 3.50 |
Elizabeth | 3.21 |
Charlie | 2.94 |
Consider another table:
CustomerID | CustomerName | |
1 | John | jhwork80@gmail.com |
2 | Harry | harrypotter456@gmail.com |
3 | Harry | harryowens3@gmail.com |
4 | Charlie | charliechap@gmail.com |
We apply the following projection operation to the table above:
The result will be:
CustomerName |
John |
Harry |
Charlie |
We see that the projection operator gives distinct values, even though both the customers' named Harry are two different people with different IDs.
The following are some characteristics of the projection operator:
It extracts columns from the original relation.
It removes all the duplicate attributes while projecting the output table.
The projection operation is not commutative.
The cardinality (rows) of the projected table/relation may or may not be equal to the original table/relation.
The projection operator translates to the SELECT DISTINCT operation followed by FROM in SQL.
Free Resources