sql exercises – part i april-26-17
Post on 17-Jan-2018
214 Views
Preview:
DESCRIPTION
TRANSCRIPT
1
SQL Exercises – Part I
May 3, 2023
2
Some Simple SQL Commands CREATE TABLE test
( i int, s char(10) );
DESCRIBE test;
3
Inserting tuples into table INSERT INTO test VALUES(10, 'foobar1'); INSERT INTO test VALUES(11, 'foobar2'); INSERT INTO test VALUES(12, 'foobar3');
4
Querying the table SELECT * FROM test; SELECT * FROM test where i=10; SELECT * FROM test where s=‘foo’; SELECT * FROM test where s like ‘fo%’;
5
Deleting tuples from table Delete from test where i=10; Delete from test;
DROP TABLE test;
6
Exercise #1 MovieExec (name, address, cert#,
netWorth) Studio (name, address, precsC#)
Describe the tuples that would appear in the following expression:
Studio CROSS JOIN MovieExec
7
Solution #1 - 1Q: Studio CROSS JOIN 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
8
Solution #1 - 2 Instance Studio: Instance MovieExec:
200100
netWorth
1HG2FE
cert#address
namename
address
presC#
A B 1C D 2
200100200100
netWorth
1212
cert#
HG2DCFHF
MovieExec.addree
EGE
MovieExec.name
2DC1BA1BA
presC#
Studio.addres
s
Studio.name
Studio x MovieExec:
9
Exercise #2 - 1
Movie(title, year,length, inColor) StarsIn(movieTitle, movieYear, starName) MovieStar(name, address, gender,
birthdate, income)
Q-1: Find Harrison Ford’s birth date.
10
Solution #2 - 1Q-1: Find Harrison Ford’s birth date.
Select birthdate From MovieStarWhere name=‘Harrison Ford’
11
Exercise #2 - 2 Movie(title, year,length, inColor) StarsIn(movieTitle, movieYear, starName) MovieStar(name, address, gender,
birthdate, income)
Q-2: Find all the movie stars who earn at least $10,000,000
12
Solution #2 - 2Q-2: Find all the movie stars who earn at
least $10,000,000
Select name From MovieStar Where income>= 10,000,000
13
Exercise #2 - 3 Movie(title, year,length, inColor) StarsIn(movieTitle, movieYear, starName) MovieStar(name, address, gender,
birthdate, income)
Q-3: Find all the stars who either are male or live in Montreal (have string Montreal as part of their address)
14
Solution #2 - 3Q-3: 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%’
15
Exercise #2 - 4 Movie(title, year,length, inColor) StarsIn(movieTitle, movieYear, starName) MovieStar(name, address, gender,
birthdate, income)
Q-4: Find all the color movies (i.e. inColor=‘color’) that were made in 1980 and have length more than 80 minutes.
16
Solution #2 - 4Q-4: Find all the color movies (i.e.
inColor=“color”) that were made in 1980 and have length more than 80 minutes.
Select title From Movie Where inColor=‘color’ and year=1980 and length>80
17
Exercise #2 - 5 Movie(title, year,length, inColor) StarsIn(movieTitle, movieYear, starName) MovieStar(name, address, gender,
birthdate, income)
Q-5: Find all the color movies that Harrison Ford has played.
18
Solution #2 - 5Q-5: Find all the color movies that
Harrison Ford has played
Select title From Movie, StarsInWhere title=movieTitle and year=movieYear and starName =‘Harrison Ford’ and inColor =‘color’
19
Exercise #3 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.
20
Solution #3CREATE 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 ));
top related