3. …
• Additional operations
– Set intersection ∩
– Natural join
– Assignment operation
– Outer join
• Left outer join
• Right outer join
• Full outer join
3
4. Relational Algebra
• The relational algebra defines a set of algebraic
operations that operate on tables, and output
tables as their results.
• These operations can be combined to get
expressions that express desired queries.
• The algebra defines the basic operations used
with in relational query languages.
4
5. …
• The operations in relational algebra can be
divided into:
– Basic operations
– Additional operations that can be expressed in
terms of the basic operations
– Extended operations, some of which add further
expressive power to relational algebra
5
6. Relational Query Languages
• Query languages: Allow manipulation and
retrieval of data from a database.
• Relational model supports simple, powerful QLs:
– Strong formal foundation based on logic.
– Allows for much optimization.
• Query Languages != programming languages!
– QLs not expected to be “Turing complete”.
– QLs not intended to be used for complex calculations.
– QLs support easy, efficient access to large data sets.
6
7. Formal Relational Query Languages
• Two mathematical Query Languages form
the basis for “real” languages (e.g. SQL),
and for implementation:
– Relational Algebra: More
operational(procedural), very useful for
representing execution plans.
– Relational Calculus: Lets users describe what
they want, rather than how to compute it.
(Non-operational, declarative.)
7
8. Preliminaries
• A query is applied to relation instances, and
the result of a query is also a relation instance.
– Schemas of input relations for a query are fixed
(but query will run regardless of instance!)
– The schema for the result of a given query is also
fixed! Determined by definition of query language
constructs.
8
10. Relational Algebra
• Basic operations:
– Selection ( ) Selects a subset of rows from relation.
– Projection ( ) Deletes unwanted columns from relation.
– Cross-product ( ) Allows us to combine two relations.
– Set-difference ( ) Tuples in reln. 1, but not in reln. 2.
– Union ( ) Tuples in reln. 1 and in reln. 2.
• Additional operations:
– Intersection, join, division, renaming: Not essential, but (very!)
useful.
• Since each operation returns a relation, operations can be
composed! (Algebra is “closed”.)
σ
π
−
×
10
11. Selection
σrating
S
>8
2( )
sid sname rating age
28 yuppy 9 35.0
58 rusty 10 35.0
• Selects rows that satisfy
selection condition.
• Schema of result identical
to schema of (only) input
relation.
• Result relation can be the
input for another
relational algebra
operation! (Operator
composition.)
12. Projection sname rating
yuppy 9
lubber 8
guppy 5
rusty 10
πsname rating
S
,
( )2
age
35.0
55.5
πage S( )2
• Deletes attributes that are not in
projection list.
• Schema of result contains exactly
the fields in the projection list, with
the same names that they had in the
(only) input relation.
• Projection operator has to eliminate
duplicates! (Why??, what are the
consequences?)
– Note: real systems typically
don’t do duplicate
elimination unless the user
explicitly asks for it. (Why
not?)
13. Union, Intersection, Set-
Difference
• All of these operations take
two input relations, which
must be union-compatible:
– Same number of fields.
– `Corresponding’ fields
have the same type.
• What is the schema of result?
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
44 guppy 5 35.0
28 yuppy 9 35.0
sid sname rating age
31 lubber 8 55.5
58 rusty 10 35.0
S S1 2∪
S S1 2∩
sid sname rating age
22 dustin 7 45.0
S S1 2−
14. Cross-Product
• Each row of S1 is paired with each row of R1.
• Result schema has one field per field of S1 and
R1, with field names `inherited’ if possible.
– Conflict: Both S1 and R1 have a field called sid.
(sid) sname rating age (sid) bid day
22 dustin 7 45.0 22 101 10/10/96
22 dustin 7 45.0 58 103 11/12/96
31 lubber 8 55.5 22 101 10/10/96
31 lubber 8 55.5 58 103 11/12/96
58 rusty 10 35.0 22 101 10/10/96
58 rusty 10 35.0 58 103 11/12/96
14
15. Division
• Not supported as a primitive operator, but useful
for expressing queries like:
Find sailors
who have reserved all boats.
• Precondition: in A/B, the attributes in B must be
included in the schema for A. Also, the result has
attributes A-B.
– SALES(supId, prodId);
– PRODUCTS(prodId);
– Relations SALES and PRODUCTS must be built using
projections.
– SALES/PRODUCTS: the ids of the suppliers supplying15
17. SET OPERATORS
SET operators are mainly used to combine the same
type of data from two or more tables. Although
more than one select statement will then be present,
only one result set is returned.
Four Set Operators:
Operator Returns
UNION
Combine two or more result sets into a single set,
without duplicates.
UNION ALL
Combine two or more result sets into a single set,
including all duplicates.
INTERSECT
Takes the data from both result sets which are in
common.
EXCEPT
Takes the data from first result set, but not the second
(i.e. no matching to each other)
17
18. Syntax:
SELECT [Column_Name, . . . ] FROM [table1]
[set operator]
SELECT [Column_Namse, . . .] FROM [table2]
Example
Create two tables with same column name and data
type.
CREATE TABLE Student1(
Name VARCHAR( 15),
TotalMark INT)
CREATE TABLE Stundent2(
Name VARCHAR(15),
TotalMark INT) 18
19. Let us insert a few values into the tables.
INSERT INTO Student1 VALUES('Robert',1063);
INSERT INTO Student1 VALUES('John',1070);
INSERT INTO Student1 VALUES('Rose',1032);
INSERT INTO Student1 VALUES('Abel',1002);
INSERT INTO Student2 VALUES('Robert',1063);
INSERT INTO Student2 VALUES('Rose',1032);
INSERT INTO Student2 VALUES('Boss',1086);
INSERT INTO Student2 VALUES('Marry',1034);
19
20. .Result Set for Students1 table
Result Set for Students2 table
20
21. UNION ALL
The SQL UNION ALL Operator is used to list all
records from two or more select statements.
All the records from both tables must be in
the same order.
SELECT * FROM student1 UNION ALL
SELECT * FROM student2
N.B you can use specific columns
21
22. Here Robert and Rose are stored in both tables. UNION ALL
retuns all records (including duplicate records).
22
23. Union
Union works like Distinct. Union all DOES NOT
do distinct.
SELECT * FROM student1 UNION
SELECT * FROM student2
The Robert and Rose records are duplicate
records. Thus, these are returned only once.
23
24. INTERSECT
INTERSECT returns any distinct values that are
returned by both the query on the left and
right sides of the INTERSECT operand.
SELECT * FROM student1INTERSECT
SELECT * FROM student2
Only the Robert and Rose records are returned,
because they are found in both tables. 24
25. .
EXCEPT
EXCEPT clause in SQL Server is working as like MINUS
operation in Oracle. EXCEPT query returns all rows
which are in the first query but those are not
returned in the second query.
Example 1
SELECT * FROM student1 EXCEPT
SELECT * FROM student2
25
26. .Example 2
EXCEPT returns any distinct values from the left select
query that are not also found on the right select
query.
SELECT * FROM student2 EXCEPT
SELECT * FROM student1
From the two results we understand that if any records
are found in both tables, they are removed from the
first table's record set.
26
27. Joins and join-like operators
• A JOIN puts the information from two tables together
into one result set. We can think of a result set as
being a “virtual” table. It has both columns and rows,
and the columns have data types.
• When data from more than one table in the database
is required, a join condition is used. Rows in one table
can be joined to rows on another table according to
common values existing in corresponding columns that
is usually primary and foreign keys.
27
28. Joins
• Condition Join:
Example
• Result schema same as that of cross-
product.
• Fewer tuples than cross-product. Filters
tuples not satisfying the join condition.
• Sometimes called a theta-join.
R c S c R S = ×σ ( )
(sid) sname rating age (sid) bid day
22 dustin 7 45.0 58 103 11/12/96
31 lubber 8 55.5 58 103 11/12/96
S R
S sid R sid
1 1
1 1
. .<
28
29. Cont..
Types of join.
1. Inner Join
– Equi-join
– Natural Join
2. Outer Join
– Left outer Join
– Right outer join
– Full outer join
3. Cross Join
4. Self Join
29
30. 1. Inner join
A table join that involves selecting rows from the table
based on the equality of values in columns from each
table.
An inner join in SQL returns rows where there is at least
one match on both tables.
30
31. Cont…
• Equi-Join: A special case of condition join where
the condition c contains only equalities.
• Result schema similar to cross-product, but only
one copy of fields for which equality is specified.
• Natural Join: Equijoin on all common fields.
sid sname rating age bid day
22 dustin 7 45.0 101 10/10/96
58 rusty 10 35.0 103 11/12/96
)11(,..,,..,
RS
sidbidagesid
π
31
32. .Example Let's assume that we have the following two
tables.
Create the following two tables and relate them
Create table student( Id int, Name char(20), Depid
varchar(20))
Create table dept( ddepid varchar(20) primary key,
Dname char(30))
The two tables are created then relate them
Alter table student add constraint foreign key (depid)
references dept (ddepid)
Now to create inner join
Select id,name,dname from student inner join dept where
depid=ddepid. id name dname
32
33. 2. Outer join
Outer joins make use of the JOIN keyword between the
table names to let SQL Server know to mark a table
as “preserved”. There are three different types of
outer joins: LEFT, RIGHT, and FULL.
33
34. To work with joins, we need two tables. One table will
be called Department with columns of DepartmentId
and DepartmentName. The other table will be called
Employees with columns of LastName and
DepartmentId. The DepartmentId in the Department
table will be the primary key while the DepartmentId
in the Employee table will be the foreign key and
relate them.
Table Employee Department Table
34
35. .
2.1 LEFT outer JOIN : it displays all the rows from
first table and matched rows from second table .
(left table are persevered).
35
36. Cont….
Like other joins we must first use the SELECT statement
to declare which columns to select from the table, in
this case all of them. Next we declare what table we
want the columns selected from with the FROM
statement. We next use the LEFT JOIN keyword to
preserve the rows in the left table and then use the
ON keyword to output the same instances of
DepartmentId in both tables.
select * from employee left join dept on
employee.DeptId=dept.DeptId
36
37. .
Execute the query and make sure it completes
successfully. You should see a table with columns of
LastName, two DeptIds, and DeptName.
As you can see, all the rows in the Employee table are
displayed while we are missing the Marketing
department on the right side. This is because we Left
Joined the table, so all the rows on the left side
were preserved.
37
38. 2.2 RIGHT outer Join :- Right outer join displays all the
rows of second table and matched rows from first table(right
table are persevered).
38
39. Cont…
In the next example we will RIGHT join the table and a
Marketing row will appear. The syntax is exactly the
same as the previous join, the only difference is we
change LEFT to RIGHT.
select * from employee right join dept on
employee.DeptId=dept.DeptId
Now you can see the marketing row
39
40. .2.3 FULL OUTER JOIN
A FULL join combines the LEFT and RIGHT joins
to get all rows from both tables. So all the
rows that appeared with the LEFT and RIGHT
join queries will appear together regardless of
null values.
40
41. Cont…
select * from employee left join dept on
employee.DeptId=dept.DeptId union
select * from employee right join dept on
employee.DeptId=dept.DeptId
41
42. 3. Cross join
Cross join is the Cartesian product of tables and
its size is
Let m is the size of the record of the first table
n is the size of the record of the second table
Size=m*n
select * from employee cross join dept
42
44. 4) Self Join
• Joining the table itself called self join. Self join
is used to retrieve the records having some
relation or similarity with other records in the
same table. Here, we need to use aliases for
the same table to set a self join between
single table and retrieve records satisfying the
condition in where clause.
Example
SELECT e1.Username,e1.FirstName,e1.LastName
from Employee e1 _inner join Employee e2 on
e1.id=e2.DepartID
44
45. Assignment
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
bid bname color
101 Interlake Blue
102 Interlake Red
103 Clipper Green
104 Marine Red
sid bid day
22 101 10/10/96
58 103 11/12/96
Reserves
Sailors
Boats
45
46. Construct relational algebra and SQL queries for the
following statements based on the above tables .
1. Find names of sailors who’ve reserved boat #103
2. Find names of sailors who’ve reserved a red boat
3. Find sailors who’ve reserved a red boat or a green
boat
4. Find sailors who’ve reserved a red and a green boat
46
Editor's Notes
The slides for this text are organized into chapters. This lecture covers relational algebra from Chapter 4. The relational calculus part can be found in Chapter 4, Part B.
Chapter 1: Introduction to Database Systems
Chapter 2: The Entity-Relationship Model
Chapter 3: The Relational Model
Chapter 4 (Part A): Relational Algebra
Chapter 4 (Part B): Relational Calculus
Chapter 5: SQL: Queries, Programming, Triggers
Chapter 6: Query-by-Example (QBE)
Chapter 7: Storing Data: Disks and Files
Chapter 8: File Organizations and Indexing
Chapter 9: Tree-Structured Indexing
Chapter 10: Hash-Based Indexing
Chapter 11: External Sorting
Chapter 12 (Part A): Evaluation of Relational Operators
Chapter 12 (Part B): Evaluation of Relational Operators: Other Techniques
Chapter 13: Introduction to Query Optimization
Chapter 14: A Typical Relational Optimizer
Chapter 15: Schema Refinement and Normal Forms
Chapter 16 (Part A): Physical Database Design
Chapter 16 (Part B): Database Tuning
Chapter 17: Security
Chapter 18: Transaction Management Overview
Chapter 19: Concurrency Control
Chapter 20: Crash Recovery
Chapter 21: Parallel and Distributed Databases
Chapter 22: Internet Databases
Chapter 23: Decision Support
Chapter 24: Data Mining
Chapter 25: Object-Database Systems
Chapter 26: Spatial Data Management
Chapter 27: Deductive Databases
Chapter 28: Additional Topics