What Are Projection and Selection

What is the difference between Select and Project Operations

Select Operation : This operation is used to select rows from a table (relation) that specifies a given logic, which is called as a predicate. The predicate is a user defined condition to select rows of user's choice.

Project Operation : If the user is interested in selecting the values of a few attributes, rather than selection all attributes of the Table (Relation), then one should go for PROJECT Operation.

See more : Relational Algebra and its operations

Syntax for projection in SQL

The projection corresponds to the columns you select, the selection to the filter you define in your where clause:

SELECT ID, NAME
FROM PRODUCTS
WHERE PRICE > 100;

Here the projection corresponds to the ID and NAME columns whereas the selection corresponds to the price filter

Relational Algebra, when to use projection and selection in these queries?

Answer A) is correct (also I do not know what is the meaning of ^ in your notation, so I've just ignored it).

For B you should keep in mind that the companies might be recorded not only in the company table but also in the works table. Hence, your answer should be Π companyName (company) U Π companyName (works).

For C you should also be a bit more careful with identifying the employees. One could argue that former employees still listed in employee but not in works should also be considered. Depending on your (or your teacher's) point of view you might need to use Π personName (works) U Π personName (employee) instead of Π personName (works).

A more serious issue in C is the identification of managers. ΠpersonName(manages) are individuals being managed, not managers. Managers are Π managerName(manages).

Hence, for C you might prefer (Π personName (works) U Π personName (employee)) - Π managerName(manages).

In general, your understanding of projection and selection is correct, but you need to be slightly more careful when formulating the answers.

Are selection and projection associative?

If think if the selection is on subset of columns that are used in projections then YES,

but if not, there can be a situation where you are making a selection on columns that not exists.

what is a projection in LINQ, as in .Select()

.Select() is from method syntax for LINQ, select in your code from a in list select a is for query syntax. Both are same, query syntax compiles into method syntax.

You may see: Query Syntax and Method Syntax in LINQ (C#)

Projection:

Projection Operations - MSDN

Projection refers to the operation of transforming an object into a
new form that often consists only of those properties that will be
subsequently used. By using projection, you can construct a new type
that is built from each object. You can project a property and perform
a mathematical function on it. You can also project the original
object without changing it.

You may also see:
LINQ Projection

The process of transforming the results of a query is called
projection. You can project the results of a query after any filters
have been applied to change the type of the collection that is
returned.

Example from MSDN

List<string> words = new List<string>() { "an", "apple", "a", "day" };
var query = from word in words
select word.Substring(0, 1);

In the above example only first character from each string instance is selected / projected.

You can also select some fields from your collection and create an anonymous type or an instance of existing class, that process is called projection.

from a in list select new { ID = a.Id}

In the above code field Id is projected into an anonymous type ignoring other fields. Consider that your list has an object of type MyClass defined like:

class MyClass
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}

Now you can project the Id and Name to an anonymous type like:

Query Syntax:

var result = from a in list
select new
{
ID = a.Id,
Name = a.Name,
};

Method Syntax

var result = list.Select(r => new { ID = r.Id, Name = r.Name });

You can also project result to a new class. Consider you have a class like:

    class TemporaryHolderClass
{
public int Id { get; set; }
public string Name { get; set; }
}

Then you can do:

Query Syntax:

 var result = from a in list
select new TemporaryHolderClass
{
Id = a.Id,
Name = a.Name,
};

Method Syntax:

var result = list.Select(r => new TemporaryHolderClass  
{
Id = r.Id,
Name = r.Name
});

You can also project to the same class, provided you are not trying to project to classes generated/created for LINQ to SQL or Entity Framework.

Projection in Relational Algebra

If I remember relational algebra correctly it should be something like this:

π name (σ age = 34 (Person)) 

where π is projection and σ is selection. This would be equal to select name from Person where age = 34in SQL and can be read as from Person select relations where age is 34 and show name.

The formatting options here are rather limited so it doesn't quite look like it should...



Related Topics



Leave a reply



Submit