



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




This worksheet has three parts: tutorial Questions, followed by some Examples and their Solutions.
You must bring your answers to the main questions along to your tutorial. You will need to be able to show these to your tutor, and may be exchanging them with other students, so it is best to have them printed out on paper. If you cannot do some questions, write down what it is that you find challenging and use this to ask your tutor in the meeting. Tutorials will not usually cover the Examples, but if you have any questions about those then write them down and ask your tutor, or go along to InfBASE during the week. It’s important both for your learning and other students in the group that you come to tutorials properly prepared. If you have not attempted the main tutorial questions, then you may be sent away from the tutorial to do them elsewhere. Data & Analysis tutorials are not formally assessed, but they are a compulsory and important part of the course. If you do not do the exercises then you are unlikely to pass the exam. Attendance at tutorials is obligatory: if you are ill or otherwise unable to attend one week then email your tutor, and if possible attend another tutorial group in the same week. Optional Reading: Chapter 5 (SQL) of Ramakrishnan and Gehrke, Database Management Systems; or the equivalent in any other database textbook (see Lecture 4 for six of these). Please send any corrections and suggestions to [email protected]
In the previous tutorial you constructed some queries around a set of relational tables dealing with air travel. In this tutorial, you will formulate queries in SQL for the same application domain, with similar (but not identical) tables. You will also interact with a database using LibreOffice Base to execute these queries.
In this tutorial, you will be using LibreOffice Base. To set up the program on a DICE machine, you need to follow these steps:
(a) Download travel.odb from the course web page: http://www.inf.ed.ac.uk/teaching/courses/ inf1/da (b) From the desktop, choose Applications -> Office -> LibreOffice Base
(c) When the Database Wizard window appears, choose Open an existing database file.
(d) Click
(e) Locate the travel.odb file you downloaded earlier and click
(f ) In the Database column on the left-hand side, click Tables.
You can now select individual tables to inspect their content. To start from the command line instead, enter libreoffice travel.odb in a terminal window at the appropriate directory. The air travel data is now loaded and you are ready to start the tutorial. Make sure you regularly save any data you wish to keep, and print out results to bring to tutorials. If you wish to try the tutorial on a non-DICE machine then you may want to install your own copy of LibreOffice from http://www.libreoffice.org. Versions are available for Linux, Windows and Mac. You can also try the very similar OpenOffice Base on Windows machines in the open access computer labs (not the ones in the AT caf´e area, though).
Recall the following SQL syntax points from lectures:
The tables in this database have the same design as the last tutorial. There are four tables — Airport, Booking, Flight and Seat. Below you will find a series of English-language queries about this data; for each you should do the following:
To run the query on the travel database, proceed as follows.
Examples
This section contains some examples of SQL queries designed to answer English-language questions. The examples are very similar to the main tutorial questions, but using a database of information about films rather than air travel. Download the database file film.odb from the course web page: http://www.inf.ed.ac.uk/ teaching/courses/inf1/da. You will notice that film.odb contains similar (but not identical) tables to those presented in the Examples section of Tutorial 3. There are four tables — Actor, Film, PerformsIn and Director. For each of the English-language queries below, questions do the following:
See the instructions from Question 1 for more detail on how to enter and edit SQL queries in Libre- Office.
Queries
(a) Retrieve details of all films that were released in 2010. The output schema should be the same as that of the Film table.
(b) Retrieve details of all actors that are not in their thirties. The output schema should be the same as that of the Actor table.
(c) Retrieve the names of all directors.
(d) Retrieve the names of all American directors.
(e) Find out the names of all British actors above the age of 40.
(f ) List all performances by an actor, giving for each one the actor’s name and the film in which he or she appeared.
(g) Find out the names of all actors that have played the character of Bruce Wayne (Batman).
(h) Retrieve the names of all actors that have played the character of Bruce Wayne, together with the year the corresponding films were released.
(i) Retrieve all actors from the film Inception. The output schema should be the same as that of the Actor table.
(j) Find out the names of all actors that have performed in a film directed by Christopher Nolan.
(k) Retrieve the titles of all films in which Leonardo Di Caprio and Kate Winslet have co-acted.
(l) Assuming that the ids of actors and directors are used consistently across the tables, retrieve details of all actors that have directed a film.
Solutions to Examples
These are not entirely “model” answers; instead, they indicate a possible solution. Remember that not all of these questions will have a single “right” answer. There can be multiple appropriate ways to formulate a query. If you have difficulties with a particular example, or have trouble following through the solution, please raise this as a question in your tutorial.
(a) Retrieve details of all films that were released in 2010. The output schema should be the same as that of the Film table. select ∗ from Film where year = 2010
filmId title year directorId DSP10 Despicable Me 2010 PCF INC10 Inception 2010 CN SHI10 Shutter Island 2010 SCO
(b) Retrieve details of all actors that are not in their thirties. The output schema should be the same as that of the Actor table. select ∗ from Actor where age< 30 or age> 39
actorId name nationality age CB379 Christian Bale British 40 DP423 Dev Patel British 24 ELP87 Ellen Page American 27 EMG32 Ewan McGregor British 43 HBC54 Helena Bonham Carter British 48 JD801 Judi Dench British 80 LDC21 Leonardo DiCaprio American 40 MKE12 Michael Keaton American 63
(c) Retrieve the names of all directors. select name from Director
name Darren Aronofsky Tim Burton Christopher Nolan Danny Boyle James Cameron Michael Keaton Pierre Coffin Martin Scorsese Sam Mendes
(d) Retrieve the names of all American directors. select name from Director where nationality = ’American’
name Darren Aronofsky Tim Burton Michael Keaton Martin Scorsese
select A.name, F.year from Actor A join PerformsIn P on A.actorId = P.actorId join Film F on P.filmId = F.filmId where P.character = ’Bruce Wayne’
name year Christian Bale 2008 Christian Bale 2012 Michael Keaton 1992
(i) Retrieve all actors from the film Inception. The output schema should be the same as that of the Actor table. select A.∗ from Actor A, PerformsIn P, Film F where A.actorId = P.actorId and P.filmId = F.filmId and F.title = ’Inception’
actorId name nationality age ELP87 Ellen Page American 27 JGL81 Joseph Gordon-Levitt American 33 LDC21 Leonardo DiCaprio American 40
(j) Find out the names of all actors that have performed in a film directed by Christopher Nolan. select distinct A.name from Actor A, PerformsIn P, Film F, Director D where A.actorId = P.actorId and P.filmId = F.filmId and F.directorId = D.directorId and D.name = ’Christopher Nolan’
name Christian Bale Ellen Page Joseph Gordon-Levitt Leonardo DiCaprio
(k) Retrieve the titles of all films in which Leonardo Di Caprio and Kate Winslet have co-acted.
select distinct F. title from Film F, Actor A1, Actor A2, PerformsIn P1, PerformsIn P where A1.name = ’Leonardo DiCaprio’ and A2.name = ’Kate Winslet’ and A1.actorId = P1.actorId and P1.filmId = F.filmId and A2.actorId = P2.actorId and P2.filmId = F.filmId
title Revolutionary Road Titanic
(l) Assuming that the ids of actors and directors are used consistently across the tables, retrieve details of all actors that are also in the database as having directed a film. select A.∗ from Actor A, Director D where A.actorId = D.directorId
actorId name nationality age MKE12 Michael Keaton American 63