Chapter 3: Introduction To SQL
Chapter 3: Introduction To SQL
Database System Concepts - 6th Edition 3.2 ©Silberschatz, Korth and Sudarshan
Joins
Find the course ID, semester, year and title of each course offered
by the Comp. Sci. department
Database System Concepts - 6th Edition 3.3 ©Silberschatz, Korth and Sudarshan
Natural Join
Natural join matches tuples with the same values for all
common attributes, and retains only one copy of each common
column.
The natural join operation operates on two relations and
produces a relation as the result.
select *
from instructor natural join teaches;
Database System Concepts - 6th Edition 3.4 ©Silberschatz, Korth and Sudarshan
Natural Join Example
Database System Concepts - 6th Edition 3.5 ©Silberschatz, Korth and Sudarshan
Natural Join (Cont.)
Danger in natural join: beware of unrelated attributes with same name which
get equated incorrectly
List the names of instructors along with the titles of courses that they teach
Incorrect version (makes course.dept_name = instructor.dept_name)
select name, title
`from instructor natural join teaches natural join course;
Correct version
select name, title
from instructor natural join teaches, course
where teaches.course_id = course.course_id;
Another correct version
select name, title
from (instructor natural join teaches)
join course using (course_id);
The Rename Operation
The SQL allows renaming relations and attributes using the
as clause:
old-name as new-name
The as clause can appear in both the select and from clauses.
Why using the as clause?
Database System Concepts - 6th Edition 3.7 ©Silberschatz, Korth and Sudarshan
The Rename Operation (cont.)
Exp:
Database System Concepts - 6th Edition 3.8 ©Silberschatz, Korth and Sudarshan
The LIKE Operation
SQL includes a string-matching operator for comparisons on
character strings. The operator “like” uses patterns that are
described using two special characters, percent (%), and
underscore (_).
s LIKE p: pattern matching on strings
p may contain two special characters:
% = any sequence of characters
_ = any single character
Exp: Find the names of all instructors whose name includes
the substring “dar”.
select name
from instructor
where name like '%dar%'
Database System Concepts - 6th Edition 3.9 ©Silberschatz, Korth and Sudarshan
The LIKE Operation
Patters are case sensitive.
Pattern matching examples:
‘Intro%’ matches any string beginning with “Intro”.
‘%Comp%’ matches any string containing “Comp” as a substring.
‘_ _ _’ matches any string of exactly three characters.
‘_ _ _ %’ matches any string of at least three characters.
Database System Concepts - 6th Edition 3.10 ©Silberschatz, Korth and Sudarshan
Other String Operations
SQL supports a variety of string operations such as
concatenation (using “||”)
converting from upper to lower case (and vice versa)
finding string length, extracting substrings, etc.
Database System Concepts - 6th Edition 3.11 ©Silberschatz, Korth and Sudarshan
Attribute Specification in Select Clause
Select all attributes of the instructor relation
select instructor.*
from instructor, teaches
where instructor.ID= teaches.ID;
Database System Concepts - 6th Edition 3.12 ©Silberschatz, Korth and Sudarshan
Ordering the Display of Tuples
List in alphabetic order the names of all instructors
Database System Concepts - 6th Edition 3.13 ©Silberschatz, Korth and Sudarshan
The Between Operator
SQL includes a between comparison operator
Example: Find the names of all instructors with salary between
$90,000 and $100,000 (that is, $90,000 and $100,000)
select name
from instructor
where salary between 90000 and 100000
Similarly, we can use the not between comparison operator.
Tuple comparison
Database System Concepts - 6th Edition 3.14 ©Silberschatz, Korth and Sudarshan
The Set Operations
(union, intersect, except)
Find courses that ran in Fall 2009 or in Spring 2010
(select course_id from section where sem = ‘Fall’ and year = 2009)
union
(select course_id from section where sem = ‘Spring’ and year = 2010)
Find courses that ran in Fall 2009 and in Spring 2010
(select course_id from section where sem = ‘Fall’ and year = 2009)
intersect
(select course_id from section where sem = ‘Spring’ and year = 2010)
Find courses that ran in Fall 2009 but not in Spring 2010
(select course_id from section where sem = ‘Fall’ and year = 2009)
except
(select course_id from section where sem = ‘Spring’ and year = 2010)
Database System Concepts - 6th Edition 3.15 ©Silberschatz, Korth and Sudarshan
The Set Operations
Database System Concepts - 6th Edition 3.16 ©Silberschatz, Korth and Sudarshan
The Set Operations
Set operations union, intersect, and except
Each of the above operations automatically eliminates
duplicates
To retain all duplicates use the corresponding multiset
versions union all, intersect all and except all.
Database System Concepts - 6th Edition 3.17 ©Silberschatz, Korth and Sudarshan
Null Values
It is possible for tuples to have a null value, denoted by null, for some of their attributes
null signifies an unknown value or that a value does not exist.
The predicate is null can be used to check for null values.
Exp: Find all instructors whose salary is null.
select name
from instructor
where salary is null
Database System Concepts - 6th Edition 3.18 ©Silberschatz, Korth and Sudarshan
Null Values and Three Valued Logic
Database System Concepts - 6th Edition 3.19 ©Silberschatz, Korth and Sudarshan