sql exercises – part i april-26-17
DESCRIPTION
Some Simple SQL Commands CREATE TABLE test ( i int, s char(10) ); DESCRIBE test;TRANSCRIPT
![Page 1: SQL Exercises – Part I April-26-17](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b1b7f8b9ab0599938f8/html5/thumbnails/1.jpg)
1
SQL Exercises – Part I
May 3, 2023
![Page 2: SQL Exercises – Part I April-26-17](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b1b7f8b9ab0599938f8/html5/thumbnails/2.jpg)
2
Some Simple SQL Commands CREATE TABLE test
( i int, s char(10) );
DESCRIBE test;
![Page 3: SQL Exercises – Part I April-26-17](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b1b7f8b9ab0599938f8/html5/thumbnails/3.jpg)
3
Inserting tuples into table INSERT INTO test VALUES(10, 'foobar1'); INSERT INTO test VALUES(11, 'foobar2'); INSERT INTO test VALUES(12, 'foobar3');
![Page 4: SQL Exercises – Part I April-26-17](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b1b7f8b9ab0599938f8/html5/thumbnails/4.jpg)
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%’;
![Page 5: SQL Exercises – Part I April-26-17](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b1b7f8b9ab0599938f8/html5/thumbnails/5.jpg)
5
Deleting tuples from table Delete from test where i=10; Delete from test;
DROP TABLE test;
![Page 6: SQL Exercises – Part I April-26-17](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b1b7f8b9ab0599938f8/html5/thumbnails/6.jpg)
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
![Page 7: SQL Exercises – Part I April-26-17](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b1b7f8b9ab0599938f8/html5/thumbnails/7.jpg)
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
![Page 8: SQL Exercises – Part I April-26-17](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b1b7f8b9ab0599938f8/html5/thumbnails/8.jpg)
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:
![Page 9: SQL Exercises – Part I April-26-17](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b1b7f8b9ab0599938f8/html5/thumbnails/9.jpg)
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.
![Page 10: SQL Exercises – Part I April-26-17](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b1b7f8b9ab0599938f8/html5/thumbnails/10.jpg)
10
Solution #2 - 1Q-1: Find Harrison Ford’s birth date.
Select birthdate From MovieStarWhere name=‘Harrison Ford’
![Page 11: SQL Exercises – Part I April-26-17](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b1b7f8b9ab0599938f8/html5/thumbnails/11.jpg)
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
![Page 12: SQL Exercises – Part I April-26-17](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b1b7f8b9ab0599938f8/html5/thumbnails/12.jpg)
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
![Page 13: SQL Exercises – Part I April-26-17](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b1b7f8b9ab0599938f8/html5/thumbnails/13.jpg)
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)
![Page 14: SQL Exercises – Part I April-26-17](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b1b7f8b9ab0599938f8/html5/thumbnails/14.jpg)
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%’
![Page 15: SQL Exercises – Part I April-26-17](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b1b7f8b9ab0599938f8/html5/thumbnails/15.jpg)
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.
![Page 16: SQL Exercises – Part I April-26-17](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b1b7f8b9ab0599938f8/html5/thumbnails/16.jpg)
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
![Page 17: SQL Exercises – Part I April-26-17](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b1b7f8b9ab0599938f8/html5/thumbnails/17.jpg)
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.
![Page 18: SQL Exercises – Part I April-26-17](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b1b7f8b9ab0599938f8/html5/thumbnails/18.jpg)
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’
![Page 19: SQL Exercises – Part I April-26-17](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b1b7f8b9ab0599938f8/html5/thumbnails/19.jpg)
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.
![Page 20: SQL Exercises – Part I April-26-17](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b1b7f8b9ab0599938f8/html5/thumbnails/20.jpg)
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 ));