Relational Projection

Say you have a table:

 Addresses
 First-Name   Last-Name    Address
 _______________________________________
 William      Underwood    Somewhere
 John         Smith        Somewhere-Else
 Jane         Smith        Somewhere-Else
Projection lets you answer questions such as "How many unique names are there in the database?" and "What addresses are there in the database?"

For example, if we projected the above table to {First-Name Last-Name} we would be left with

 Addresses.projectTo({First-Name Last-Name})
 First-Name   Last-Name 
 ______________________
 William      Underwood
 John         Smith
 Jane         Smith
Likewise, if we projected down to {Address} we would have
 Addresses.projectTo({Address})
 Address
 ______________
 Somewhere
 Somewhere-Else
Note that the duplicates are eliminated from the result: in relational theory, you can't have two tuples (rows) in a table which are identical; by removing the First-Name from the table, the rows "John, Smith, Somewhere-Else" and "Jane, Smith, Somewhere-Else" become indistinguishable, and so they are collapsed into one row.



I have not tested it, but one may be able to achieve this in SQL using something like:

 select first, last, combined 
 from (select first, last, concat(first,' ',last) as combined
       from names
       )
 group by first, last, combined

It's simpler than that actually, you'd just do
 SELECT DISTINCT First-Name, Last-Name FROM Addresses
Not all dialects support Distinct (at least they did not used to). Then again they don't all support nested queries either.

Note though, that I'm deliberately ignoring standard SQL syntax for exactly this reason. You know SQL, but yet you don't know relational algebra, which SQL is a broken implementation of. The alternate syntax makes sure that misconceptions brought from experience in query languages are exposed.

I am just covering the practical side of things because for good or bad, we are stuck with SQL for a while.


One of the operators in RelationalAlgebra


EditText of this page (last edited April 23, 2006) or FindPage with title or text search