Lab 07 Sol
Lab 07 Sol
Lab 07 Sol
Class: BSCS-11AB
Lab 07: SQL Joins
Introduction
A SQL join clause combines records from two or more tables in a database. It creates a set that can be saved as a
table or used as it is. A JOIN is a means for combining fields from two tables by using values common to each.
Objectives
After performing this lab students should be able to: 1. Design SQL queries to retrieve data from multiple tables by
using JOIN operation. 2. Select among INNER, OUTER, NATURAL, CROSS join as and when required.
Tools/Software Requirement
• MySQL Community Server 5.6
• MySQL Workbench 6.1
• Sakila Database
Description
1. This lab assumes that MySQL Community Server is running, Sakila database has been
loaded using MySQL Workbench, and the query window is open.
2. Execute the JOIN queries on Sakila including cross join, inner join, natural join, outer
join (left outer join, right outer join, and full outer join) and observe how each join
operation differs from the other.
3. For your practice, create the following two tables and execute the following queries to better
understand how each join operation works.
Users
id name course_id
1 Alice 1
2 Bob 1
3 Caroline 2
4 David 5
5 Emma (NULL)
course_id name
1 HTML5
2 NULL
3 JavaScript
4 PHP
5 MySQL
4. Try running following SQL JOIN queries. You can try equivalent SQL queries for the
same result as well.
a. List rental and return date for the movie BREAKING HOME (notice one of the
join uses on clause where as the other uses using). 13 entries of renting this movie
should be found from the database.
use sakila;
b. List of movie titles that were never rented. There are 43 of them. Notice that there
are two outer joins as the movie might be missing altogether from the inventory
but may still be included in the result set. On the other hand movie might be
available in the inventory but never rented, the SQL query covers both cases.
Lab Task
Write SQL queries for the following information needs. You should execute your attempts
on SQL workbench and make necessary corrections if needed. .
c. Retrieve first and last name of actors who played in ALONE TRIP.
Query:
select actor.first_name,actor.last_name from film join (film_actor join actor on
film_actor.actor_id=actor.actor_id) on film.film_id=film_actor.film_id where
film.title like('ALONE TRIP')
Result:
e. Email addresses of customers who although rented a movie (include movie title in
the output) but didn’t pay anything.
Query:
select film.title,customer.email from payment,customer,rental,inventory,film
where payment.customer_id=customer.customer_id and
payment.rental_id=rental.rental_id and
f. List of unpaid rentals (film title, rental id, rental date and for how many days
rented). List should be sorted on film title and rental date.
Query:
select film.title,rental.rental_id,rental_date,film.rental_duration from
payment,customer,rental,inventory,film where
payment.customer_id=customer.customer_id and
payment.rental_id=rental.rental_id and
rental.inventory_id=inventory.inventory_id and inventory.film_id=film.film_id
and payment.amount='0.00';
Result: