+ All Categories
Home > Documents > SQL Exercises [Part-1] - Concordia...

SQL Exercises [Part-1] - Concordia...

Date post: 30-Mar-2018
Category:
Upload: buitruc
View: 217 times
Download: 0 times
Share this document with a friend
23
1 SQL Exercises [Part-1]
Transcript
Page 1: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

1

SQL Exercises [Part-1]

Page 2: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

2

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

Page 3: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

3

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

Page 4: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

4

Solution #1 - 2

Instance Studio: Instance MovieExec:

200

100

netWorth

1 H G

2 F E

cert# address name name address presC#

A B 1

C D 2

200

100

200

100

netWorth

1

2

1

2

cert#

H G 2 D C

F

H

F

MovieExec.add

ree

E

G

E

MovieExec.na

me

2 D C

1 B A

1 B A

presC# Studio.address

Studio.name

Studio x MovieExec:

Page 5: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

5

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)

Page 6: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

6

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

Page 7: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

7

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.

Page 8: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

8

Escape Character

To search for a % or a _ character in a LIKE condition.

like „Montreal#%‟ escape „#‟

Check for the string „Montreal%‟

Back

Page 9: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

9

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.

Page 10: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

10

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‟

Page 11: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

11

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‟

Page 12: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

12

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‟

Page 13: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

13

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)))

Page 14: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

14

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

Page 15: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

15

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.

Page 16: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

16

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 ));

Page 17: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

17

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)

Page 18: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

18

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

Page 19: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

19

Foreign Key

A foreign key is an attribute or a combination of attributes in a table that match the primary key in another table

Page 20: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

20

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

Page 21: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

21

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));

Page 22: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

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 we have some supplier_id in Supplier table that is not in Products table (For example, s3)?

Yes!

Page 23: SQL Exercises [Part-1] - Concordia Universityusers.encs.concordia.ca/.../Tutorials/1.SQL_Exercises_Part_I.pdf · SQL Exercises [Part-1] 2 ... Solution #2 - 2 Q-1: Find all the stars

23

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!


Recommended