SQL
Part 2
Database System Concepts, 7th Ed.
©Silberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
Joined Relations
▪ Join operations take two relations and return as a result another
relation.
▪ A join operation is a Cartesian product which requires that tuples in the
two relations match (under some condition). It also specifies the
attributes that are present in the result of the join.
▪ The join operations are typically used as subquery expressions in the
from clause.
▪ Three types of joins:
• Natural join
• Inner join
• Outer join
Database System Concepts - 7th Edition 4.2 ©Silberschatz, Korth and Sudarshan
Natural Join in SQL
▪ Natural join matches tuples with the same values for all common
attributes, and retains only one copy of each common column.
▪ List the names of instructors along with the course ID of the courses that
they taught
• select name, course_id
from students, takes
where student.ID = takes.ID;
Database System Concepts - 7th Edition 4.3 ©Silberschatz, Korth and Sudarshan
Natural Join in SQL
▪ Natural join matches tuples with the same values for all common
attributes, and retains only one copy of each common column.
▪ List the names of instructors along with the course ID of the courses that
they taught
• select name, course_id
from students, takes
where student.ID = takes.ID;
▪ Same query in SQL with “natural join” construct
• select name, course_id
from student natural join takes;
Database System Concepts - 7th Edition 4.4 ©Silberschatz, Korth and Sudarshan
Natural Join in SQL (Cont.)
▪ The from clause can have multiple relations combined using natural join:
select A1, A2, … An
from r1 natural join r2 natural join .. natural join rn
where P ;
Database System Concepts - 7th Edition 4.5 ©Silberschatz, Korth and Sudarshan
Student Relation
Database System Concepts - 7th Edition 4.6 ©Silberschatz, Korth and Sudarshan
Takes Relation
Database System Concepts - 7th Edition 4.7 ©Silberschatz, Korth and Sudarshan
student natural join takes
Database System Concepts - 7th Edition 4.8 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 7th Edition 4.9 ©Silberschatz, Korth and Sudarshan
Dangerous in Natural Join
▪ Beware of unrelated attributes with same name which get equated
incorrectly
▪ Example -- List the names of students instructors along with the titles of
courses that they have taken
• Correct version
select name, title
from student natural join takes, course
where takes.course_id = course.course_id;
• Incorrect version
select name, title
from student natural join takes natural join course;
▪ This query omits all (student name, course title) pairs where the
student takes a course in a department other than the student's
own department.
▪ The correct version (above), correctly outputs such pairs.
Database System Concepts - 7th Edition 4.10 ©Silberschatz, Korth and Sudarshan
Natural Join with Using Clause
▪ To avoid the danger of equating attributes erroneously, we can use the
“using” construct that allows us to specify exactly which columns should be
equated.
▪ Query example
select name, title
from (student natural join takes) join course using (course_id) ;
Database System Concepts - 7th Edition 4.11 ©Silberschatz, Korth and Sudarshan
Join
▪ The on condition allows a general predicate over the relations being
joined.
▪ This predicate is written like a where clause predicate except for the use
of the keyword on.
▪ Query example
select *
from student join takes on student_ID = takes_ID;
• The on condition above specifies that a tuple from student matches a
tuple from takes if their ID values are equal.
▪ Equivalent to:
select *
from student , takes
where student_ID = takes_ID;
Database System Concepts - 7th Edition 4.12 ©Silberschatz, Korth and Sudarshan
Join
▪ The on condition allows a general predicate over the relations being
joined.
▪ This predicate is written like a where clause predicate except for the use
of the keyword on.
▪ Query example
select *
from student join takes on student_ID = takes_ID ;
• The on condition above specifies that a tuple from student matches a
tuple from takes if their ID values are equal.
There is one significant difference between INNER JOIN and NATURAL
JOIN is the number of columns returned.
Database System Concepts - 7th Edition 4.13 ©Silberschatz, Korth and Sudarshan
Outer Join
▪ An extension of the join operation that avoids loss of information.
▪ Uses null values.
▪ Three forms of outer join:
• left outer join
• right outer join
• full outer join
Database System Concepts - 7th Edition 4.14 ©Silberschatz, Korth and Sudarshan
Outer Join Examples
▪ Relation course
▪ Relation prereq
▪ Observe that
course information is missing CS-347
prereq information is missing CS-315
Database System Concepts - 7th Edition 4.15 ©Silberschatz, Korth and Sudarshan
Left Outer Join
▪ course natural left outer join prereq;
▪ In relational algebra: course ⟕ prereq
Relation course Relation prereq
Database System Concepts - 7th Edition 4.16 ©Silberschatz, Korth and Sudarshan
Right Outer Join
▪ course natural right outer join prereq;
▪ In relational algebra: course ⟖ prereq
Relation course Relation prereq
Database System Concepts - 7th Edition 4.17 ©Silberschatz, Korth and Sudarshan
Full Outer Join
▪ course natural full outer join prereq;
▪ In relational algebra: course ⟗ prereq
Relation course Relation prereq
Database System Concepts - 7th Edition 4.18 ©Silberschatz, Korth and Sudarshan
Joined Types and Conditions
▪ Join operations take two relations and return as a result another
relation.
▪ These additional operations are typically used as subquery expressions
in the from clause
▪ Join condition – defines which tuples in the two relations match.
▪ Join type – defines how tuples in each relation that do not match any
tuple in the other relation (based on the join condition) are treated.
Database System Concepts - 7th Edition 4.19 ©Silberschatz, Korth and Sudarshan
Joined Relations – Examples
▪ course natural right outer join prereq;
▪ course full outer join prereq using (course_id);
Relation course Relation prereq
Database System Concepts - 7th Edition 4.20 ©Silberschatz, Korth and Sudarshan
Joined Relations – Examples
▪ course inner join prereq on
course.course_id = prereq.course_id;
▪ course left outer join prereq on
course.course_id = prereq.course_id;
Relation course Relation prereq
Database System Concepts - 7th Edition 4.21 ©Silberschatz, Korth and Sudarshan
Joined Relations – Examples
▪ course natural right outer join prereq;
▪ course full outer join prereq using (course_id);
Relation course Relation prereq
Database System Concepts - 7th Edition 4.22 ©Silberschatz, Korth and Sudarshan
Assertions
▪ An assertion is a predicate expressing a condition that we wish the
database always to satisfy.
▪ The following constraints, can be expressed using assertions:
▪ For each tuple in the student relation, the value of the attribute tot_cred
must equal the sum of credits of courses that the student has completed
successfully.
▪ An instructor cannot teach in two different classrooms in a semester in the
same time slot.
▪ An assertion in SQL takes the form:
create assertion <assertion-name> check (<predicate>);
Database System Concepts - 7th Edition 4.23 ©Silberschatz, Korth and Sudarshan
Built-in Data Types in SQL
▪ date: Dates, containing a (4 digit) year, month and date
• Example: date '2005-7-27'
▪ time: Time of day, in hours, minutes and seconds.
• Example: time '09:00:30' time '09:00:30.75'
▪ timestamp: date plus time of day
• Example: timestamp '2005-7-27 09:00:30.75'
Database System Concepts - 7th Edition 4.24 ©Silberschatz, Korth and Sudarshan
Index Creation
▪ Many queries reference only a small proportion of the records in a table.
▪ It is inefficient for the system to read every record to find a record with
particular value.
▪ An index on an attribute of a relation is a data structure that allows the
database system to find those tuples in the relation that have a specified
value for that attribute efficiently, without scanning through all the tuples of
the relation.
▪ We create an index with the create index command
create index <name> on <relation-name> (attribute);
Database System Concepts - 7th Edition 4.25 ©Silberschatz, Korth and Sudarshan
Index Creation Example
▪ create table student
(ID varchar (5),
name varchar (20) not null,
dept_name varchar (20),
tot_cred numeric (3,0) default 0,
primary key (ID)) ;
▪ create index studentID_index on student(ID) ;
▪ The query:
select *
from student
where ID = '12345’;
can be executed by using the index to find the required record, without
looking at all records of student.
Database System Concepts - 7th Edition 4.26 ©Silberschatz, Korth and Sudarshan