sql exercises – part i april-26-17

20
1 SQL Exercises – Part I August 29, 2022

Upload: jade-daniel

Post on 17-Jan-2018

214 views

Category:

Documents


0 download

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

1

SQL Exercises – Part I

May 3, 2023

Page 2: SQL Exercises – Part I April-26-17

2

Some Simple SQL Commands CREATE TABLE test

( i int,    s char(10)      );

DESCRIBE test;

Page 3: SQL Exercises – Part I April-26-17

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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