Tutorial 1 SQL Exercises Part I
Tutorial 1 SQL Exercises Part I
1
Exercise #1
MovieExec (name, address, cert#, netWorth)
Studio (name, address, precsC#)
Select *
From Studio, MovieExec
2
Solution #1 - 1
MovieExec (name, address, cert#, netWorth)
Studio (name, address, precsC#)
Select *
From Studio, MovieExec
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)
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
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
20
Example in SQL
CREATE TABLE Supplier
(supplier_id CHAR(10),
supplier_name VARCHAR(15) NOT NULL,
PRIMARY KEY (supplier_id));