SQL Exercises [Part-1]
1
Exercise #1
MovieExec (name, address, cert#, netWorth)
Studio (name, address, precsC#)
Describe the tuples that would appear in the
following expression:
Select *
From Studio, MovieExec
2
Solution #1 - 1
MovieExec (name, address, cert#, netWorth)
Studio (name, address, precsC#)
Select *
From Studio, MovieExec
A: The result will be a 7-column relation with all
the attributes of Studio and MovieExec. Every
pair consisting of one tuple of Studio and one
tuple of MovieExec will be a tuple of the
resulting relation
3
Solution #1 - 2
Instance Studio: Instance MovieExec:
name address presC# name address cert# netWor
A B 1 th
C D 2 E F 2 100
G H 1 200
Studio x MovieExec:
Studio. Studio. presC# MovieE MovieE cert# netWor
name address xec.na xec.add th
me ree
A B 1 E F 2 100
A B 1 G H 1 200
C D 2 E F 2 100
C D 2 G H 1 200 4
Exercise #2 - 1
Movie(title, year,length, inColor)
StarsIn(movieTitle, movieYear, starName)
MovieStar(name, address, gender, birthdate,
income)
Q-1: Find all the stars who either are male or live in
Montreal (have string Montreal as part of their
address)
5
Solution #2 - 2
Q-1: Find all the stars who either are male or
live in Montreal (i.e. have string Montreal as
part of their address)
Select name
From MovieStar
Where gender=„M‟ or
address like „%Montreal%‟
More
6
Pattern match
% - any sequence of zero or more
characters (wildcard)
address like „%Montreal%‟
a sequence of characters of any length
containing Montreal
_ - any single character
address like „M_ _‟
there must be exactly three characters in the
string, the first of which must be a M.
7
Escape Character
To search for a % or a _ character in a
LIKE condition.
like „Montreal#%‟ escape „#‟
Check for the string „Montreal%‟
Back
8
Exercise #2 - 3
Movie(title, year,length, inColor)
StarsIn(movieTitle, movieYear, starName)
MovieStar(name, address, gender,
birthdate, income)
Q-2: Find all the color movies that Harrison
Ford has played.
9
Solution #2 - 4
Q-2: Find all the color movies that Harrison
Ford has played
Select title
From Movie, StarsIn
Where title=movieTitle and
year=movieYear and
starName =„Harrison Ford‟ and
inColor =„color‟
10
Exercise #3 - 1
Beers (name, manufacturer)
Bars (name, city, addr, license, phone)
Drinkers (name, city, addr, phone)
Likes (drinker, beer)
Sells (bar, beer, price)
What is the average price of „Molson Canadian‟
in Montreal?
Select avg(price)
From sells, bars
Where sells.bar = bars.name and
bars.city = „Montreal‟ and
sells.beer = „Molson Canadian‟ 11
Exercise #3 - 2
Beers (name, manufacturer)
Bars (name, city, addr, license, phone)
Drinkers (name, city, addr, phone)
Likes (drinker, beer)
Sells (bar, beer, price)
How many bars in Montreal sell a beer liked by
Richard for less than $5.00?
Select count(distinct bars.name)
From bars, sells, likes
Where bars.city = „Montreal' and bars.name = sells.bar
and sells.beer = likes.beer and
sells.price < 5.00 and likes.drinker = „Richard‟12
Exercise #3 - 3
Beers (name, manufacturer)
Bars (name, city, addr, license, phone)
Drinkers (name, city, addr, phone)
Likes (drinker, beer)
Sells (bar, beer, price)
Find all bars in Montreal for which we know either
the address or the phone number, but not both.
Select distinct name
From Bars
Where city = „Montreal‟ and
(((addr is NOT NULL) and (phone is NULL))
or ((addr is NULL) and (phone is NOT NULL))) 13
Exercise #4 - 1
The kings and queens of England are listed in:
Kings(name, dynasty, beginReign, endReign)
Parents(child, parent)
Both attributes are the names of kings or queens
Find all the pairs of kings or queens (A,B) such that A
was the grandchild of B.
SELECT p1.child, p2.parent
FROM Parents p1, Parents p2
WHERE p1.parent = p2.child
14
Exercise #5
Create an Employee table that can be
used to store information related to
employee‟s first name, last name, SIN,
employee number, birthdate, address,
gender, and salary.
15
Solution #5
CREATE TABLE EMPLOYEE
(FirstName VARCHAR(15) NOT NULL,
LastName VARCHAR(15) NOT NULL,
SIN CHAR(9) NOT NULL,
EmployeeNum CHAR(12) NOT NULL,
BirthDate DATE,
Address VARCHAR(30),
Gender CHAR,
Salary DECIMAL(10,2),
PRIMARY KEY (SIN),
UNIQUE (EmployeeNum ));
16
Primary Key
A primary key is an attribute (or combination
of attributes) that allows us to uniquely
identify each row in a table
It must be “unique” and “not null”
It can either be
A normal attribute that is guaranteed to be
unique (such as Social Security Number) or
Generated by the DBMS (such as a globally
unique identifier, or GUID, in Microsoft SQL
Server)
17
Example
Suppose we have a STUDENT table
A student's unique student ID would
be a good choice for a primary key
in the STUDENT table
The student's first name and last
name would not be a good choice
18
Foreign Key
A foreign key is an attribute or a
combination of attributes in a table
that match the primary key in another
table
19
Example
Supplier (supplier_id, supplier_name)
supplier_id is the primary key of
Supplier table
Product (product_id, product_name,
supplier_id)
supplier_id from Supplier table is the
foreign key in Product table
20
Example in SQL
CREATE TABLE Supplier
(supplier_id CHAR(10),
supplier_name VARCHAR(15) NOT NULL,
PRIMARY KEY (supplier_id));
CREATE TABLE Product
(product_id CHAR(10),
product_name VARCHAR(30) NOT NULL,
supplier_id CHAR(10),
PRIMARY KEY (product_id),
FOREIGN KEY (supplier_id) REFERENCES Supplier(supplier_id));
21
Referential Integrity
Supplier:
supplier_id supplier_name
-------------- -----------------
s1 ABC
s2 DEF
s3 ABC
Product:
product_id product_name supplier_id
------------- ------------------ -------------
p1 Shampoo s1
p2 Conditioner s1
p3 Soap s2
p4 Shampoo s2
Is it OK if we have some supplier_id in Supplier table that is not in
Products table (For example, s3)?
Yes!
22
Referential Integrity
Supplier:
supplier_id supplier_name
-------------- -----------------
s1 ABC
s2 DEF
s3 ABC
Product:
product_id product_name supplier_id
------------- ------------------ -------------
p1 Shampoo s1
p2 Conditioner s1
p3 Soap s2
p4 Shampoo s2
Is it OK if I add another row in Products table that contains a
supplier_id which does not appear in Supplier table. For example,
p5 Shampoo s4
No! 23