0% found this document useful (0 votes)
20 views19 pages

Chapter 3: Introduction To SQL

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views19 pages

Chapter 3: Introduction To SQL

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

Chapter 3: Introduction to SQL

Database System Concepts, 6th Ed.


©Silberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
Joins
 For all instructors who have taught some course, find their
names and the course ID of the courses they taught.

select name, course_id


from instructor, teaches
where instructor.ID = teaches.ID

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

select section.course_id, semester, year, title


from section, course
where section.course_id = course.course_id and
dept_name = ‘Comp. Sci.'

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

 List the names of instructors along with the course ID of the


courses that they taught.

select name, course_id


from instructor, teaches
where instructor.ID = teaches.ID;

select name, course_id


from instructor natural join teaches;

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?

1. Two relations in the from clause may have attributes with


the same name.
2. If we used an arithmetic expression in the select clause, the
resultant attribute does not have a name.
3. If we want to change the attribute name in the result

Database System Concepts - 6th Edition 3.7 ©Silberschatz, Korth and Sudarshan
The Rename Operation (cont.)
 Exp:

select ID, name, salary/12 as monthly_salary


from instructor

 Exp: Find the names of all instructors who have a higher


salary than some instructor in ‘Comp. Sci’.
select distinct T. name
from instructor as T, instructor as S
where T.salary > S.salary and S.dept_name = ‘Comp. Sci.’
 Keyword as is optional and may be omitted
instructor as T ≡ instructor T
Keyword as must be omitted in Oracle

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.

 For patterns to include the special pattern characters (that is,%and


_)
 like ’ab\%cd%’ escape ’\’ matches all strings beginning with “ab%cd”.
 like ’ab\\cd%’ escape ’\’ matches all strings beginning with “ab\cd”.
 like ‘100 \%' escape '\' match the string “100 %”`

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

select distinct name


from instructor
order by name
 We may specify desc for descending order or asc for
ascending order, for each attribute; ascending order is the
default.
 Exp: order by name desc
 Can sort on multiple attributes
 Exp: order by dept_name, name

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

select name, course_id


from instructor, teaches
where (instructor.ID, dept_name) = (teaches.ID, ’Biology’);

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.

Suppose a tuple occurs m times in r and n times in s,


then, it occurs:
 m + n times in r union all s
 min(m,n) times in r intersect all s
 max(0, m – n) times in r except all s

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

 Exp: 5 + null returns null

The result of any arithmetic expression involving null is null.

Database System Concepts - 6th Edition 3.18 ©Silberschatz, Korth and Sudarshan
Null Values and Three Valued Logic

The result of any comparison with null returns unknown


 Example: 5 < null or null <> null or null = null
 Three-valued logic using the truth value unknown:
 OR: (unknown or true) = true,
(unknown or false) = unknown
(unknown or unknown) = unknown
 AND: (true and unknown) = unknown,
(false and unknown) = false,
(unknown and unknown) = unknown
 NOT: (not unknown) = unknown
 Result of where clause predicate is treated as false if it
evaluates to unknown

Database System Concepts - 6th Edition 3.19 ©Silberschatz, Korth and Sudarshan

You might also like