In-class Exercise for Week 5: Getting Information Out of a Database Objective



Download 82.4 Kb.
Date31.03.2018
Size82.4 Kb.
#45032
In-class Exercise for Week 5: Getting Information Out of a Database

Objective: Learn to retrieve information from a relational database using the SQL SELECT statement.

Learning Outcomes:

  • Understand and apply SQL syntax

  • Transform an English-language business question into a SQL statement.

  • Identify data necessary to include in a query based on a schema.

Step 1: Individual (30 minutes)

To do this exercise, you will be working with a movie rental database. The schema for this database is provided on the accompanying document. All of the tables are in a schema called “moviedb.” You can’t write to any of the tables – you can only use SELECT statements to read from them (so don’t worry about causing any damage).



Spend some time looking at the schema carefully. The field names are pretty self-explanatory. For example, here are three tables:

You can see that a film has a title, description, rating, and length (among other things). You can also see that an actor has a first name and a last name. The film_actor table implements the many-to-many relationship between actor and film (i.e., a film can have more than one actor, and an actor can be in more than one film). You’ll also notice that data types are listed for each field, but they should be pretty obvious – for example, first_name is a VARCHAR because it is a string value.

In MySQL Workbench, open the connection to the class1 server using your username and password. Click on the moviedb schema to see the list of tables.

Now try a simple query. In the Query 1 pane, type the following:



m

And then click the Execute SQL Script button (the lightning bolt):



And you’ll see a list of all movie titles (this is just the first few):



On the following page you’re going to create a series of SQL SELECT queries to answer questions about the information in this database. Some of the questions can be answered by querying one table; others will require joining multiple tables to get the answer. Make sure you return only the information being requested!



For each question you’ll write down the SQL query (which you can copy and paste it from SQL Workbench) and the answer you get as a result of the query (which you can copy and paste from the results).

You should try to do as much of this on your own as you can, but you should feel free to consult with your colleagues (or me) in writing your queries.

PART 1: SINGLE TABLE QUERIES

  1. Which actors have the first name of “Ed”?

    1. ED CHASE, ED MANSFIELD, ED GUINESS

    2. select * from actor where first_name = 'Ed'



  1. How many PG-13 movies are in the database?

    1. 223

    2. select count(*) from film where rating = 'PG-13'



  1. What is the length of the film “Confessions Maguire”

    1. 65

    2. select length from film where title = 'Confessions Maguire'



  1. What movie has the longest running time?

    1. MUSCLE BRIGHT, 185 mins

    2. select length, title from film order by length desc

Notes: If you use the following query: “select max(length), title from film” the result returned will be ACADEMY DINOSAUR, 185. However, ACADEMY DINOSAUR is just the first movie title in the film table; it is not the name of the longest movie! Be aware that MySQL treats the field results independently. It first looks up the max(length), and when that is done, it looks up a movie title. Since there is only one length value returned, it will return one movie name (the first one)!

  1. Are PG movies, on average, longer than R movies? Prove it!

    1. No, R-rated movies are longer (average length = 118.66 mins, compared to 112.01 for PG)

    2. select avg(length), rating from film where rating = 'PG' Or rating = 'R' group by rating order by avg(length) desc;

PART 2: MULTI-TABLE QUERIES (JOINS)

  1. How many movies are in French?

    1. 36

    2. select count(*) from film, language where film.language_id = language.language_id And language.name = 'French'



  1. What were the titles of the films starring Julianne Dench? (create a query to get them all, but only list the first five)

    1. ADAPTATION HOLES, ATLANTIS CAUSE, BERETS AGENT, BULL SHAWSHANK, CHOCOLATE DUCK

    2. select title, first_name, last_name from film, actor, film_actor where film_actor.film_id = film.film_id And film_actor.actor_id = actor.actor_id And actor.first_name = 'Julianne' And actor.last_name = 'Dench' limit 0, 5



  1. How many films star Julianne Dench?

    1. 32

    2. select count(*) from film, actor, film_actor where film_actor.film_id = film.film_id And film_actor.actor_id = actor.actor_id And actor.first_name = 'Julianne' And actor.last_name = 'Dench'

FOR AN EXTRA CHALLENGE!

How many actors have a last name that starts with ‘W’?

Notes: You can use LIKE with a WHERE clause. LIKE looks for close matches, not exact ones. If you include a wildcard, it will search for values that match the same format. You would use the LIKE option in this manner… SELECT fieldname FROM tablename WHERE fieldname LIKE value (e.g., ‘%xyz%’)

For example, using the wildcard, LIKE ‘ap%’ would match with “apple,” “application,” “apex,” etc. Thus, SELECT fieldname FROM tablename WHERE fieldname LIKE ‘ap%’ will give you results for fieldname where values start with the letters ‘ap’. You can also allow any number of preceding characters by doing this instead LIKE ‘%ap%’. This would match with “cap”, “lap” , “lapse”, “caption”, etc. As long as ap is in there somewhere.



    1. 19

    2. select count(*) from actor where last_name like 'W%'




Download 82.4 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2024
send message

    Main page