SQL

Chap 5. The DB Language (SQL)
Contents
2

Simple Queries in SQL

Queries Involving More Than One Relation

Subqueries

Full-Relation Operations

Database Modifications
The Development of SQL

Evolution of SQL standards
– Sequel: System R project (mid-1970’s)
» later on called SQL
– SQL-86, SQL-89
– SQL-92 (also, called SQL2)
[Standardization of SQL]
- adopted by ANSI in 1986 and ISO in 1987
» SQL-86, SQL-89, SQL-92 published by ANSI
- nowadays improvement by ISO/IEC JTC1
– SQL:1999 (also, called SQL3)
» object-relational features
– SQL:2003
» XML-related features
– SQL:2006
» XQuery
3
– SQL:2008
» INSTEAD OF trigger
– SQL:2011
» temporal data
Example Schema
Movie Database Schema
Movies(title, year, length, genre, studioName, producerC#)
MovieStar(name, address, gender, birthdate)
StarsIn(movieTitle, movieYear, starName)
MovieExec(name, address, cert#, netWorth)
Studio(name, address, presC#)
4
Simple Queries in SQL

Basic form of query in SQL
» retrieve tuples of one relation that satisfy a condition
– basic keywords: SELECT, FROM, WHERE
» FROM clause: relation or relations to which the query refers
» WHERE clause: condition that tuples must satisfy
» SELECT clause: attributes of the tuples as part of the answer
(ex) Find movies produced by Disney studios in 1990.
the entire attributes of the tuple
SELECT *
FROM Movies
WHERE studioName = ‘Disney’ AND year = 1990;
5
Simple Queries in SQL

(cont’d)
Projection in SQL
– project the relation onto some of its attributes
(ex) Find the titles and lengths for all movies produced by Disney
Studios in 1990.
SELECT title, length
FROM Movies
WHERE studioName = ‘Disney’ AND year = 1990;
6
title
length
Pretty Woman
119
p title, year (s studioName=‘Disney’ AND
year=1990
(Movies))
Simple Queries in SQL (cont’d)

optional
Rename column headers
SELECT title AS name, length AS duration
FROM Movie
WHERE studioName = ‘Disney’ AND year = 1990;
7
name
duration
Pretty Woman
119
Note: Case sensitivity
SQL is case-insensitive
» about keywords, names of attributes, relations, aliases
 e.g., in keywords
 FROM, from, FrOm: all OK
» upper and lower case letters are only distinct inside the quoted string
 e.g., ‘FROM’ and ‘from’ are different character strings
8
Simple Queries in SQL (cont’d)

p title,
SELECT list
length*0.0167lengthInHours (Movies)
– can function like the lists in an extended projection
» arithmetic expression, constant, string expression,  operator, etc
(ex) Output with the length in hours and an additional column
having constant ‘hrs.’
SELECT title, length*0.0167 AS length, ‘hrs.’ AS inHours
FROM Movies
WHERE studioName = ‘Disney’ AND year = 1990;
9
title
length
Pretty Woman
1.98334
inHours
hrs.
Note: Reading and Writing SQL
 Reading SELECT-FROM-WHERE query
» FROM clause
 relation(s) used
» WHERE clause
 conditions a tuple satisfy
» SELECT clause
 the output form of the query
 Writing SELECT-FROM-WHERE query
 its sequence is equal to reading
» FROM clause, WHERE clause, SELECT clause
10
Simple Queries in SQL (cont’d)

Selection in SQL
comparison expressions
– expression for conditions in the WHERE clause
» the six common comparison operators: =, <>, <, >, <=, >=
» arithmetic operators: +, -, *, etc
» concatenation operator || to strings
 strings are denoted by single quotes
» numeric constants
» boolean values may be combined by the logical operators AND, OR,
and NOT
11
Simple Queries in SQL (cont’d)
(Ex) Retrieve the titles of movies made by MGM Studios that
either were made after 1970 or were less than 90 minutes long.
SELECT title
FROM Movies
WHERE (year > 1970 OR length < 90)
AND studioName = ‘MGM’
12
Note: SQL Queries and Relational Algebra
 Relational algebra for basic SQL queries
SELECT L
FROM R
WHERE C

p L (s C (R))
p title, length
s studioName = ‘Disney’ AND year = 1990
(Ex) Previous example
SELECT title, length
Movies
FROM Movies
WHERE studioName = ‘Disney’ AND year = 1990;
p title, year (s studioName=‘Disney’
13
AND year=1990
(Movies))
Simple Queries in SQL (cont’d)

Comparisons of Strings
– string comparison is based on the lexicographic order
(ex) ‘at’ < ‘bar’ , ‘fodder’ < ‘foo’
Bit string
» represented by B followed by a quoted string of 0’s and 1’s.
(ex) B’011’ is the string of three bits, 011
» hexadecimal notation: X instead of B
(ex) X’7ff’
14
Simple Queries in SQL (cont’d)

Pattern Matching
– s LIKE p
 string comparison based on a simple pattern match
» s is a string type attribute, and p is a pattern
» pattern: a string with the optional use of special characters % and _
 % : any sequence of 0 or more characters
 _ : any one character
15
Simple Queries in SQL (cont’d)
(ex) Retrieve titles of movies whose names start with “Star “ and
are nine characters long.
SELECT title
FROM Movies
WHERE title LIKE ‘Star _ _ _ _’
/* Result: {Star Wars, Star Trek, . . .} */
(ex) Retrieve all movies with possessive (‘s) in their titles
SELECT title
two consecutive apostrophes
represent a single apostrophe
FROM Movies
WHERE title LIKE ‘%’’s%’
/* Result : {Rogan’s Run, . . .} */
16
Simple Queries in SQL (cont’d)

Escape characters in LIKE expression
 What if does LIKE expression involve % or _ ?
» keyword ESCAPE and the chosen escape character, in quotes
(ex) s LIKE ‘x%%x%’ ESCAPE ‘x’
/* any string that begins and ends with % */
17
Simple Queries in SQL (cont’d)

Dates and Times
» are generally supported as special data types
– date type: DATE
» (ex) date constant: DATE ‘1948-05-14’
– time type: TIME
» (ex) time constant : TIME ‘15:00:02.5’
– date+time type: TIMESTAMP
» (ex) date+time constant : TIMESTAMP ‘1945-05-14 12:00:00’
 dates or times can be compared by using the same
comparison operators
18
Simple Queries in SQL (cont’d)

Null Values
» used when the value is not defined
Two NULL values
are not the same
– value unknown
» there is some value, but we do not know
 e.g., the unknown birthday of a movie star
– value inapplicable
» there is no value that makes sense here
 e.g., the spouse of an unmarried movie star
– value withheld
» we are not entitled to know the value
 e.g., an unlisted phone number
19
Simple Queries in SQL (cont’d)

Operations on NULL values
– NULL in arithmetic operators like ×, +
» the result is NULL
(ex) if the value of x is NULL, x + 3 is NULL
– NULL in comparison operators like =, >
» a result is UNKNOWN
(ex) if the value of x is NULL, x > 3 is UNKNOWN
 Predicates related with NULL: IS NULL, IS NOT NULL
(ex) Check whether the value of x is NULL.
 x IS NULL
the result is
TRUE or FALSE
20
Predicate: operator
producing a boolean result
Simple Queries in SQL (cont’d)
NULL is not a constant
» cannot use NULL explicitly as an operand
(ex) NULL + 3, NULL = 5 : not correct SQL

NULL in some commercial DBMSs
e.g., Oracle
 can be used as if it were a constant
(ex) NULL + 3 : the result is NULL
(ex) INSERT INTO R(A1, A2) VALUES(NULL, NULL)
 a tuple where the value of each attribute is null is inserted
(NULL, NULL)
21
Simple Queries in SQL (cont’d)

Truth table for three-valued logic
X
TRUE
FALSE
22
y
UNKNOWN
UNKNOWN
x AND y
x OR y
NOT x
UNKNOWN
FALSE
TRUE
UNKNOWN
FALSE
TRUE
UNKNOWN TRUE
UNKNOWN
TRUE
UNKNOWN
UNKNOWN
UNKNOWN
FALSE
UNKNOWN
UNKNOWN
UNKNOWN
UNKNOWN
UNKNOWN
FALSE
UNKNOWN
Note: Pitfalls regarding NULLs

Query’s result including tuples with NULL values
» include only tuples for which the condition has the value TRUE
(ex) Consider the following query:
SELECT *
FROM Movies
WHERE length <= 120
OR length > 120
» a tuple with a NULL in length attribute
 WHERE clause evaluates to UNKNOWN
 such a tuple is not returned as part of the answer
 “Find all movies tuples with non-NULL lengths”
23
Simple Queries in SQL (cont’d)

Ordering the Output
» the order may be based on the value of any attribute
– output in sorted order
ORDER BY <list of attribute>
» the default order: ASC (ascending)
» output highest first: DESC (descending)
24
Simple Queries in SQL (cont’d)
(Ex) Retrieve the movies produced by Disney in 1990. Order
the output by length (shortest first), and among movies of
equal length, alphabetically.
SELECT *
FORM Movies
WHERE studioName = ‘Disney’ AND year = 1990
ORDER BY length, title;
25
Queries Involving More Than One Relation

Cartesian Products and Joins in SQL
– simple way to couple relations in one query
» list relations in the FROM clause
Theta Join: sC (R  S)
(ex) Movies (title, year, length, genre, studioName, producerC#),
MovieExec (name, address, cert#, netWorth)
– Find the name and address of the producer of Star Wars.
Two relations in FROM clause:
SELECT name, address
Cartesian product
FROM Movies, MovieExec
WHERE title = ‘Star Wars’ AND producerC# = cert#
26
Queries Involving More Than One Relation (cont’d)

Disambiguating Attributes
– a query involving several relations
 may have two or more attributes with the same name.
– R.A : attribute A of relation R
(ex) Find pairs of names for a star and an executive who have
the same address.
SELECT MovieStar.name, MovieExec.name
FROM MovieStar, MovieExec
WHERE MovieStar.address = MovieExec.address
R.A notation can also be used in situations where no ambiguity
27
Queries Involving More Than One Relation (cont’d)

Tuple Variables
– a query may involve two or more tuples in the same relation
» use tuple variable that is an alias for the relation
(ex) Retrieve names of two stars who have the same address.
SELECT Star1.name, Star2.name
FROM MovieStar AS Star1, MovieStar AS Star2
WHERE Star1.address = Star2.address AND Star1.name < Star2.name
 Star1.name < Star2.name
 avoid the case that each star name is paired with itself
 produce each pair of stars with a common address only once
28
Not in the textbook
Note: Join & Tuple Variables
Star1, Star2: tuple variables
address
address
*
address
Star1
*
Star2
*
*
MovieStar
29
MovieExec
MovieStar
Select MovieStar.name, MovieExec.name
Select Star1.name, Star2.name
From MovieStar, MovieExec
From MovieStar AS Star1, MovieStar AS Star2
Where MovieStar.address = MovieExec.address
Where Star1.address = Star2.address
Queries Involving More Than One Relation

(cont’d)
Conversion to Relational Algebra
– Cartesian product of all relations in the FROM clause
– Selection using the condition in the WHERE clause
– Projection using the attributes in the SELECT clause
SELECT name, address
FROM Movies, MovieExec
WHERE title = ‘Star Wars’ AND producerC# = cert#
p name, address (s title = ‘Star Wars’ AND
30
producerC# = cert#
(Movies × MovieExec))
Queries Involving More Than One Relation
(cont’d)
MovieStar(name, address, gender, birthdate)
MovieExec(name, address, cert#, netWorth)

Union, Intersection, and Difference
– keywords: UNION, INTERSECT, EXCEPT
(ex) Retrieve the names and addresses of all female movie stars
who are also movie executives with a net worth over 10,000,000.
(SELECT name, address
FROM MovieStar
WHERE gender = ‘F’ )
INTERSECT
(SELECT name, address
FROM MovieExec
Select name, address
From MovieStar, MovieExec
Where MovieStar.name = MovieExec.name AND
MovieStar.address = MovieExec.address AND
gender = ‘F’ AND netWorth > 10000000
WHERE netWorth > 10000000);
31
Subqueries
 Subqueries
» a query that is part of the entire query
(ex) two subqueries in UNION, INTERSECT, EXCEPT
– a subquery returns a relation
» can appear in FROM clauses
» can be used in various ways in WHERE clauses
32
Subqueries
(cont’d)
(Ex) Retrieve the titles and years of movies whose producers
are presidents of studios located in Los Angeles.
Movies (title, year, length, genre, studioName, producerC#)
Studio (name, address, presC#)
SELECT title, year
SELECT title, year
FROM Movies, Studio
FROM Movies
WHERE producerC# = presC# AND
WHERE producerC# IN
address LIKE ‘%Los Angeles’
(SELECT presC#
FROM Studio
WHERE address LIKE ‘%Los Angeles%’)
For each tuple in Movies,
check if it satisfies the condition
in the WHERE clause
33
Subqueries

(cont’d)
Subqueries that produce scalar values
– when the result of a subquery is a single value from a single attribute,
» this subquery can be used as if it were a constant
(Ex) Movies(title, year, length, genre, studioName, producerC#)
MovieExec(name, address, cert#, netWorth)
Find the producer’s name of Star Wars.
SELECT name
FROM MovieExec
WHERE cert# =
(SELECT producerC#
FROM Movies
WHERE title = ‘Star Wars’);
34
Subqueries

(cont’d)
Conditions involving relations
» a relation can be used in conditions, but
» it must be expressed as a subquery

Predicates that involve one-column relation
 R: relation, s: scalar value
EXISTS R
» true iff R is not empty
s IN R
» true iff s is equal to one of the values in R
35
Select name
From MovieExec
Where EXISTS
(Select producerC#
From Movies
Where producerC# = cert#
AND title = ‘Star Wars’ )
Subqueries
s > ALL R
“>” could be
replaced by
any of
comparison
operators
(cont’d)
(Ex) Find the titles of movies made by Disney whose length is
greater than the length of every movie made by MGM.
» true iff s is greater than every value in unary relation R
may need the difference operator and the join operator
in relational algebra expressions
s > ANY R
» true iff s is greater than at least one value in unary relation R
the join operator may work
in relational algebra expressions
 EXIST and IN are predicates while ALL and ANY(or SOME) are
quantifiers
36
Subqueries

(cont’d)
Conditions Involving Tuples
– a tuple in SQL
» represented by a parenthesized list of scalar values
(ex) (123, ‘foo’), (name, address, networth)
– if a tuple t has the same number of components as a relation R,
» t and R can be compared
 t IN R
 t <> ANY R
37
/* There is some tuple in R other than t */
Subqueries
(cont’d)
(Ex) Movies(title, year, length, genre, studioName, producerC#)
StarsIn(movieTitle, movieYear, starName)
MovieExec(name, address, cert#, netWorth)
Find the names of producers of movies where Harrison Ford stars.
SELECT name
FROM MovieExec
No duplicates
WHERE cert# IN
(SELECT producerC#
FROM Movies
WHERE (title, year) IN
(SELECT movieTitle, movieYear
FROM StarsIn
WHERE starName = ‘Harrison Ford’));
38
Subqueries
(cont’d)
(example – cont’d)
 A nested query can be written as a single Select-From-Where
SELECT name
FROM MovieExec, Movies, StarsIn
WHERE cert# = producerC# AND
title = movieTitle AND
year = movieYear AND
starName = ‘Harrison Ford’;
duplicates may occur
– FROM clause: relations mentioned in the main query or subqueries
– WHERE clause: IN are replaced by equi-join
39
Subqueries
Not in the textbook
(cont’d)
(ex – cont’d) duplicates
<starName>
<title>
<producer-name>
Harrison Ford - - - Star Wars
- - - Steven Spielberg
Harrison Ford - - - Indiana Jones - - - Steven Spielberg
(H.F., t1, y1)
(t1, y1, c1)
(H.F., t2, y2)
(t2, y2, c1)
StarsIn
40
Movies
(c1, Steven Spielberg)
MovieExec
Subqueries

(cont’d)
Correlated Subquery
A subquery needs to be evaluated for each value of
the tuple variable outside the subquery.
– a tuple variable declared outside the subquery
» is used inside the subquery
The cost for processing a correlated
subquery can be very expensive
(ex) Find the titles that have been used for two or more movies.
SELECT title
FROM Movies AS Old
WHERE year < ANY
(SELECT year
Select title
FROM Movies
From Movies M1, Movies M2
WHERE title = Old.title)
Where M1.title = M2.title
AND M1.year < M2.year
41
Movies with the same title
made in different years
title
year
King Kong 1933 ...
King Kong 1976 ...
King Kong 2005 ...
Subqueries

(cont’d)
Subqueries in From Clauses
– a parenthesized subquery can be used in the FROM clause
» we must give this subquery a tuple-variable alias
(ex) Find the names of producers of Harrison ford’s movies.
SELECT Name
FROM MovieExec,(SELECT producerC#
FROM Movies, StarsIn
WHERE title = movieTitle AND
year = movieYear AND
starname = ‘Harrison Ford’) Prod
WHERE cert# = Prod.producerC#;
42
Subqueries

(cont’d)
 ,  : relations
SQL Join Expressions
» the join expression can be used as a query by itself, or
» can be used as a subquery in the FROM clause
–  CROSS JOIN 
/* cartesian product */
Movies CROSS JOIN StarsIn
–  JOIN  ON <condition>
SELECT *
FROM Movies, StarsIn
/* theta join */
Movies JOIN StarsIn ON
title = movieTitle AND year = movieYear;
–  NATURAL JOIN 
MovieStar NATURAL JOIN MovieExec
43
Subqueries
(cont’d)
 Join expression in the FROM clause
SELECT title, year, starName
FROM Movies JOIN StarsIn ON
title = movieTitle AND year = movieYear;
44
Subqueries

(cont’d)
Outerjoin
– add to the output dangling tuples, padded with Null values
 dangling tuples: tuples fail to be joined

Natural Outerjoin
 NATURAL [LEFT | RIGHT | FULL] OUTER JOIN 

Theta-Outerjoin : use “JOIN ON...” syntax
 [LEFT | RIGHT | FULL] OUTER JOIN  ON ...
45
Subqueries
(cont’d)
(Ex) MovieStar(name, address, gender, birthdate)
MovieExec(name, address, cert#, netWorth)
MovieStar NATURAL FULL OUTER JOIN MovieExec;
name
address
gender
birthdate
cert#
networth
Mary T. Moore
Tom Hanks
Carrie Fisher
George Lucas
Maple St.
Cherry Ln.
Locust Ln.
Oak Rd.
‘F’
‘M’
‘F’
NULL
9/9/77
8/8/88
5/5/55
NULL
12345
NULL
NULL
23456
$100…
NULL
NULL
$200…
A star, but not an executive(e.g., Tom Hanks), or
An executive, but not a star(e.g., George Lucas)
46
dangling tuples, but
are added to the output
Subqueries
(cont’d)
(ex) Movies NATURAL LEFT OUTER JOIN MovieExec;
» the first two tuples are in the output, but the 3rd tuple is not
(ex) Movies NATURAL RIGHT OUTER JOIN MovieExec;
» the first and 3rd tuples are in the output, but the 2nd tuple is not
(ex) Movies FULL OUTER JOIN StarsIN ON
title = movieTitle AND year = movieYear
(ex) Movies LEFT OUTER JOIN StarsIN ON
title = movieTitle AND year = movieYear
47
Note: Predicates in Boolean Expressions

Basic comparison predicates: =, <>, <, >, <=, >=

LIKE predicate
(ex) title LIKE ‘%love%’

IN predicate

IS NULL, IS TRUE, IS FALSE, IS UNKNOWN unary predicates
(ex) studioName IS NULL,

cost = 100 IS TRUE
EXISTS and UNIQUE unary predicates
» EXISTS subquery,
UNIQUE subquery
 ANY(or SOME) and ALL
» quantifiers used with comparison predicates
48
“true” if the subquery
contains no duplicates
Full-Relation Operations

Eliminating duplicates
Operations that act on relations
as a whole,
rather than on tuples individually
» relations in SQL are bags, rather than sets
» the SQL system does not ordinarily eliminate duplicates
– keyword DISTINCT
(ex) SELECT DISTINCT genre, studioName
From Movies
» produce only one copy of any tuple
The cost of duplicate elimination is expensive
» thus, duplicate elimination should be used judiciously
49
Full-Relation Operations (cont’d)

Duplicates in UNION, INTERSECT and EXCEPT
» union, intersection, and difference operations
the result is a set
 normally eliminate duplicates
– prevent duplicate elimination: keyword ALL
the result is a bag
» R UNION ALL S
(SELECT title, year FROM Movie)
UNION ALL
(SELECT movieTitle AS title, movieYear AS year
FROM StarsIn);
» R INTERSECT ALL S
» R EXCEPT ALL S
50
Full-Relation Operations (cont’d)

Aggregation Operators
» five aggregation operators
– COUNT: the number of values
Aggregation operators are used
to a scalar valued expression,
typically a column name.
COUNT(*) is an exception,
which counts all the tuples
– SUM: the sum of the values in this column
– AVG: the average of the values in this column
– MIN: the least value in this column
– MAX: the greatest value in this column
Duplicates elimination
» e.g., COUNT(DISTINCT x): eliminate duplicates from the column x
before applying the aggregation operator
51
Full-Relation Operations (cont’d)
(ex)
SELECT COUNT(*)
FROM StarsIn;
(ex)
SELECT COUNT(starName)
FROM StarsIn;
Counts the number of values
in the starName column.
Almost same as COUNT(*)
(ex)
SELECT COUNT(DISTINCT starName)
FROM StarsIn;
Do not count duplicate values
more than once
(ex)
SELECT AVG(netWorth)
FROM MovieExec;
“*”: denotes an entire tuple
COUNT(starName)
- does not count NULL, i.e.,
count rows excluding the null values
Full-Relation Operations (cont’d)

Grouping: GROUP BY clause
» grouping attributes follows GROUP BY
» tuples are grouped according to their values in the grouping attributes
The total length
of films produced
by each studio
aggregated value
SELECT studioName, SUM(length)
FROM Movies
grouping
GROUP BY studioName;
p
studioName, sumLength
 studioName, SUM(length)sumLength
attributes
Movies
 In SELECT clauses, only grouping attributes may appear in unaggregated form
SELECT studioName, genre, SUM(length)
FROM Movies
GROUP BY studioName
53
make sense ???
Full-Relation Operations (cont’d)

Grouping, Aggregation, and Nulls
» there are a few rules when tuples have nulls
COUNT(*): a number
of tuples in a relation
– NULL is ignored in any aggregation
» COUNT(A): number of tuples with non-NULL values for attribute A
– NULL can form a group
» there can be a group where the value of the grouping attribute is NULL
– empty bag
» the result is NULL for any aggregation, except COUNT
Let R be a relation with no tuple.
Select COUNT(*) From R
54
 The result is 0.
Full-Relation Operations (cont’d)
(Ex) Suppose we have a relation R(A, B).
» R has one tuple (NULL, NULL)
The result of
COUNT(*) is 1
SELECT A, COUNT(B)
FROM R
GROUP A;
Insert Into R
Values (NULL, NULL)
(NULL, 0)
may or may not be
displayed in the output
SELECT A, SUM(B)
FROM R
GROUP A;
55
(NULL, NULL)
Full-Relation Operations (cont’d)

Condition on the groups: HAVING clause
– express a condition on aggregated properties of the group
(ex) Find the producer names and the total film lengths for only
those producers who made at least one film prior to 1930.
SELECT name, SUM(length)
FROM MovieExec, Movie
WHERE producerC# = cert#
GROUP BY name
HAVING MIN(year) < 1930;
condition about a group
56
As in SELECT clause,
only grouping attributes
may appear unaggregated
in the HAVING clause.
(ex) name LIKE ‘John%’ : OK
genre = ‘Drama’ : No
Full-Relation Operations (cont’d)

Order of clauses in SQL
– SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY
 WHERE clause: specifies the condition about tuples
 HAVING clause: specifies the condition about groups

Evaluation sequence
– tuples satisfying conditions in the WHERE clause are selected
– these tuples are grouped by grouping attributes in the GROUP BY clause
– groups satisfying conditions in the HAVING clause are selected
57
Database Modifications

Database modification statements
– INSERT, DELETE, UPDATE

Insertion
INSERT INTO R(A1, … , An) VALUES (v1, … , vn);
» the keywords INSERT INTO,
» the name of a relation R, a parenthesized list of attribute names
» the keyword VALUES, and a tuple expression
(Ex) INSERT INTO StarsIn(movieTitle, movieYear, starName)
VALUES(‘The Maltese Falcon’, 1942,‘Sydney Greenstreet’);
58
Database Modifications (cont’d)
(ex) INSERT INTO StarsIn
may omit the list of attributes
VALUES(‘The Maltese Falcon’,1942,‘Sydney Greenstreet’);
(ex) Add to relation Studio all movie studios that are mentioned in
Movies, but do not appear in Studio.
INSERT INTO Studio(name)
SELECT DISTINCT studioName
FROM Movies
WHERE studioName NOT IN(SELECT name FROM Studio);
 address and presC# attributes in the inserted Studio tuples :
» NULL values are used
59
Database Modifications (cont’d)

Deletion
DELETE FROM R WHERE <condition>;
» the keywords DELETE FROM
» the name of a relation R,
» the keyword WHERE, and a condition
(ex) DELETE FROM StarsIn
WHERE movieTitle = ‘The Maltese Falcon’ AND
movieYear = 1942 AND starName = ‘Sydney Greenstreet’;
60
Database Modifications (cont’d)

Update
UPDATE R SET <new-value assignments> WHERE <condition>;
» the keyword UPDATE, a relation name R
» the keyword SET, a list of assignments
If more than one assignments,
they are separated by commas
» the keyword WHERE, and a condition
(ex) Attach the title Pres. in front of the name of every movie
executive who is the president of a studio.
the condition for identifying
tuples to be updated requires
information in other tables
UPDATE MovieExec
SET name = ‘Pres. ‘ || name
WHERE cert# IN (SELECT presC# FROM Studio);
column1 = value1, column2 = value2, . . .
61