lecture17_2

6
1 S Q L Q u e r y E x a m p l e s E x e r c i s e ( 1 ) Relations: Movie(title , year , length, inColor, studioName, producerC#) StarsIn(movieTitle , movieYear , starName ) MovieStar(name , address, gender, birthdate) MovieExec(name , address, cert#, netWorth) Studio(name , address, presC#) Queries: a) Find the address of MGM studios. b) Find Sandra Bullock’s birthdate. c) Find all the stars that appear either in a movie made in 1980 or a movie with “Love” in the title. d) Find all executives worth at least $10,000,000. e) Find all the stars who either are male or live in Miami ( have Miami as a part of their address).

Upload: enis-byci

Post on 26-Dec-2015

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: lecture17_2

1

�������������� ������������������������� ��"!$#�%&��%�#('()�*

SQL Query Examples

+�,�-�,�.�,/�0�1�,�2�,�3�0�4�0�2�-5�6�7�8 98":$;�<&5�<�;(=(>�?

Exercise (1)• Relations:Movie(title, year, length, inColor, studioName, producerC#)

StarsIn(movieTitle, movieYear, starName)

MovieStar(name, address, gender, birthdate)

MovieExec(name, address, cert#, netWorth)

Studio(name, address, presC#)

• Queries:a) Find the address of MGM studios.

b) Find Sandra Bullock’s birthdate.

c) Find all the stars that appear either in a movie made in 1980 or a moviewith “Love” in the title.

d) Find all executives worth at least $10,000,000.

e) Find all the stars who either are male or live in Miami ( have Miami as apart of their address).

Page 2: lecture17_2

2

@�A�B�A�C�AD�E�F�A�G�A�H�E�I�E�G�BJ�K�L�M NM"O$P�Q&J�Q�P(R(S�T

Answers (1)

a) SELECT address FROM studio WHERE name = ‘MGM’ ;

b) SELECT birthdate FROM moviestar WHERE name = ‘Sandra Bullock’ ;

c) SELECT starName FROM StarsIn

WHERE movieYear = 1980 OR movieTitle LIKE ‘%Love%’;

SELECT starName FROM StarsIn WHERE movieYear = 1980 OR movieTitle LIKE 'Love %’ OR movieTitle LIKE '% Love %' OR movieTitle LIKE '% Love'

OR movieTitle = 'Love';d) SELECT name FROM MovieExec WHERE netWorth >= 10,000,000;e) SELECT name FROM MovieStar WHERE gender = ‘M’ OR address LIKE ’Miami %’ OR address LIKE ‘% Miami %’ OR address LIKE ‘% Miami’

OR address = ‘Miami’ ;

U�V�W�V�X�VY�Z�[�V�\�V�]�Z�^�Z�\�W_�`�a�b cb"d$e�f&_�f�e(g(h�i

Exercise (2)

• Relations:Movie(title, year, length, inColor, studioName, producerC#)

StarsIn(movieTitle, movieYear, starName)

MovieStar(name, address, gender, birthdate)

MovieExec(name, address, cert#, netWorth)

Studio(name, address, presC#)

• Queries:a) Who were the male stars in Terms of Endearment.

b) Which stars appeared in movies produced by MGM in 1995?

c) Which movies are longer than Gone With the Wind?

d) Which executives are worth more than Merv Griffin?

Page 3: lecture17_2

3

j�k�l�k�m�kn�o�p�k�q�k�r�o�s�o�q�lt�u�v�w xw"y$z�{&t�{�z(|(}�~

Answer (2)

a) SELECT name FROM MovieStar, StarsInWHERE gender = ‘M’ AND name = starName

AND movieTitle = ‘Terms of Endearment’ ;b) SELECT starName FROM MovieStar, Movie

WHERE title = movieTitle AND year = movieYear AND year = 1995 AND studioName = ‘MGM’;

c) SELECT M1.title FROM Movie AS M1, Movie AS M2WHERE M2.title = ‘Gone With the Wind’ AND M1.length > M2.length;

SELECT title FROM MovieWHERE length > ANY ( SELECT length FROM Movie

WHERE title = ‘Gone With the Wind’ );

d) SELECT M1.name FROM MovieExec AS M1, MovieExec AS M2WHERE M2.name = ‘Mery Griffin’ AND M1.networth > M2.networth;

����������������������������������������� ��"�$���&�����(�(���

Exercise 3• Relations:Classes(class, type, country, numGuns, bore, displacement)

Ships(name, class, launched)

Battles(name, date)

Outcomes(ship, battle, result)

• Queries:a) Find the countries whose ships had the largest number of guns.

b) Find the classes of ships at least one of which was sunk in a battle.

c) Find the names of the ships with a 16-inch bore.

d) Find the battles in which ships of the Kongo class participated.

e) Find the names of the ships whose number of guns was the largest forthose ships of the same bore.

Page 4: lecture17_2

4

�������������������������������������� �¡ ¢¡"£$¤�¥&��¥�¤(¦(§�¨

Answer (3-1)

a) SELECT country FROM classes

WHERE numGuns = (SELECT MAX(numGuns) from classes);

SELECT country FROM classes

WHERE numGuns >= ALL (SELECT numGuns from classes);

b) SELECT DISTINCT class FROM Ships

WHERE name IN (SELECT ship FROM Outcomes WHERE result = ‘sunk’ );

SELECT class FROM ShipsWHERE EXISTS (SELECT * FROM Outcomes

WHERE Ships.name = Outcomes.ship AND result = ‘sunk’ );

©�ª�«�ª�¬�ª­�®�¯�ª�°�ª�±�®�²�®�°�«³�´�µ�¶ ·¶"¸$¹�º&³�º�¹(»(¼�½

Answer (3-2)

c) SELECT name FROM ships

WHERE class IN (SELECT class from classes where bore = 16);

SELECT name FROM ships, classes

WHERE ships.class = classes.class AND bore = 16;

d) SELECT DISTINCT battle FROM ships, outcomes

WHERE name = ship AND class = ‘Kongo’ ;

SELECT DISTINCT battle FROM outcomes

WHERE ship = ANY (SELECT name FROM ships WHERE class = ‘Kongo’ );

Page 5: lecture17_2

5

¾�¿�À�¿�Á�¿Â�Ã�Ä�¿�Å�¿�Æ�Ã�Ç�Ã�Å�ÀÈ�É�Ê�Ë ÌË"Í$Î�Ï&È�Ï�Î(Ð(Ñ�Ò

Answer (3-3)

e) SELECT name FROM ships, classes AS C1

WHERE ships.class = C1.class

AND numGuns = (SELECT MAX(numGuns)

FROM classes AS C2

WHERE C1.bore = C2.bore);

Ó�Ô�Õ�Ô�Ö�Ô×�Ø�Ù�Ô�Ú�Ô�Û�Ø�Ü�Ø�Ú�ÕÝ�Þ�ß�à áà"â$ã�ä&Ý�ä�ã(å(æ�ç

Exercise 4• Relations:Classes(class, type, country, numGuns, bore, displacement)

Ships(name, class, launched) Battles(name, date) Outcomes(ship, battle, result)

• Queries:a) Find the number of battleship classes.

b) Find the average number of guns of battleship classes.

c) Find the average of guns of battleships. Note the difference between (b) and (c); dowe weight a class by the number of ships of that class or not.

d) Find for each class the year in which the first ship of that class was launched.

e) Find for each class the number of ships of that class sunk in battle.

Page 6: lecture17_2

6

è�é�ê�é�ë�éì�í�î�é�ï�é�ð�í�ñ�í�ï�êò�ó�ô�õ öõ"÷$ø�ù&ò�ù�ø(ú(û�ü

Answer (4-1)a) SELECT count(* ) FROM classes

WHERE type = ‘bc’ ;

b) SELECT avg(numGuns) FROM classes

WHERE type = ‘bc’ ;

c) SELECT avg(numGuns) FROM ships, classes

WHERE ships.class =classes.class AND type = ‘bc’ ;

d) SELECT class, launched FROM ships AS S1

WHRE launched <= ALL ( SELECT year

FROM ships AS S2

WHERE S2.class = S1.class);

ý�þ�ÿ�þ���þ�������þ��þ���� �����ÿ�����������������������! !"#

Answer (4-2)e) SELECT classes.class, count(*) FROM classes, ships, outcomes

WHERE classes.class = ships.class AND ship = name AND result = ‘sunk’

GROUP BY classes.class;