Skip to main content
Google Classroom
Classroom
Java Theta 2024
MySQL:Case Study(Online Movie Rental)-Assignment
Case Study: Online Movie Rental Database Analysis
You are working as a database administrator for an online movie rental system. The
system tracks customers, movies, movie rentals, and payments. The database schema
involves tables for Customers, Movies, Rentals, and Payments.
# Database Schema:
1. Customers Table:
| customer_id | first_name | last_name | email | registration_date
| country |
|-------------|------------|-----------|---------------------|-------------------|-
---------|
| 1 | Alice | Johnson | [email protected] | 2021-05-14
| USA |
| 2 | Bob | Smith | [email protected] | 2022-01-25
| UK |
| 3 | Charlie | Brown | [email protected] | 2023-07-10
| Canada |
| 4 | David | Wilson | [email protected] | 2023-02-19
| Australia|
2. Movies Table:
| movie_id | movie_title | genre | price | stock_quantity |
|----------|-----------------------|-------------|--------|----------------|
| 1 | The Shawshank Redemption | Drama | 5.99 | 30 |
| 2 | The Dark Knight | Action | 7.99 | 50 |
| 3 | Inception | Sci-Fi | 6.99 | 40 |
| 4 | The Matrix | Sci-Fi | 8.99 | 20 |
| 5 | Gladiator | Action | 9.99 | 15 |
3. Rentals Table:
| rental_id | customer_id | movie_id | rental_date | return_date | status |
|-----------|-------------|----------|--------------|--------------|-----------|
| 101 | 1 | 1 | 2023-06-10 | 2023-06-15 | Returned |
| 102 | 2 | 2 | 2023-06-12 | NULL | On Rent |
| 103 | 3 | 3 | 2023-07-01 | NULL | On Rent |
| 104 | 4 | 4 | 2023-08-01 | NULL | On Rent |
4. Payments Table:
| payment_id | rental_id | payment_date | amount | payment_method |
|------------|-----------|--------------|--------|----------------|
| 201 | 101 | 2023-06-12 | 5.99 | Credit Card |
| 202 | 102 | 2023-06-15 | 7.99 | PayPal |
| 203 | 103 | 2023-07-03 | 6.99 | Debit Card |
| 204 | 104 | 2023-08-02 | 8.99 | Credit Card |
Database tables queries:
Questions:
1. DDL (Data Definition Language)
Write a query to add a new column phone_number (VARCHAR type) to the Customers
table.
___________________________________________________________________________________
___________________________________________________________________________________
__________
mysql> alter table customers add column phone_number varchar(200);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc customers;
+-------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+-------+
| customer_id | int | NO | PRI | NULL | |
| first_name | varchar(50) | YES | | NULL | |
| last_name | varchar(50) | YES | | NULL | |
| email | varchar(100) | YES | | NULL | |
| registration_date | date | YES | | NULL | |
| country | varchar(50) | YES | | NULL | |
| phone_number | varchar(200) | YES | | NULL | |
+-------------------+--------------+------+-----+---------+-------+
7 rows in set (0.02 sec)
***********************************************************************************
***********************************************************************************
**********
2. DML (Data Manipulation Language)
Write a query to update the status column in the Rentals table to "Returned"
where the rental_date is more than 7 days ago and the status is "On Rent".
___________________________________________________________________________________
___________________________________________________________________________________
__________
mysql> select * from rentals;
+-----------+-------------+----------+-------------+-------------+----------+
| rental_id | customer_id | movie_id | rental_date | return_date | status |
+-----------+-------------+----------+-------------+-------------+----------+
| 101 | 1 | 1 | 2023-06-10 | 2023-06-15 | Returned |
| 102 | 2 | 2 | 2023-06-12 | NULL | On Rent |
| 103 | 3 | 3 | 2023-07-01 | NULL | On Rent |
| 104 | 4 | 4 | 2023-08-01 | NULL | On Rent |
+-----------+-------------+----------+-------------+-------------+----------+
mysql> update rentals set status='Returned' where
day(rental_date)>day(rental_date)-7 and status='On Rent';
Query OK, 3 rows affected (0.02 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> select * from rentals;
+-----------+-------------+----------+-------------+-------------+----------+
| rental_id | customer_id | movie_id | rental_date | return_date | status |
+-----------+-------------+----------+-------------+-------------+----------+
| 101 | 1 | 1 | 2023-06-10 | 2023-06-15 | Returned |
| 102 | 2 | 2 | 2023-06-12 | NULL | Returned |
| 103 | 3 | 3 | 2023-07-01 | NULL | Returned |
| 104 | 4 | 4 | 2023-08-01 | NULL | Returned |
+-----------+-------------+----------+-------------+-------------+----------+
4 rows in set (0.00 sec)
***********************************************************************************
***********************************************************************************
**********
3. DISTINCT
Write a query to find the distinct genres of movies available in the Movies
table.
___________________________________________________________________________________
___________________________________________________________________________________
_______
mysql> select Distinct genre from movies;
+--------+
| genre |
+--------+
| Drama |
| Action |
| Sci-Fi |
+--------+
3 rows in set (0.00 sec)
***********************************************************************************
***********************************************************************************
**********
4. Joins (LEFT JOIN)
Write a query to list all customers and the movies they have rented, including
customers who have not rented any movies (use a LEFT JOIN).
___________________________________________________________________________________
___________________________________________________________________________________
____________
mysql> select c.first_name,c.last_name,c.email,c.registration_date,c.country from
customers c Left Join rentals r ON c.customer_id=r.customer_id;
+------------+-----------+---------------------+-------------------+-----------+
| first_name | last_name | email | registration_date | country |
+------------+-----------+---------------------+-------------------+-----------+
| Alice | Johnson |
[email protected] | 2021-05-14 | USA |
| Bob | Smith |
[email protected] | 2022-01-25 | UK |
| Charlie | Brown |
[email protected] | 2023-07-10 | Canada |
| David | Wilson |
[email protected] | 2023-02-19 | Australia |
| Smith | Sharma |
[email protected] | 2022-04-12 | USA |
+------------+-----------+---------------------+-------------------+-----------+
5 rows in set (0.00 sec)
***********************************************************************************
***********************************************************************************
**********
5. LIMIT and OFFSET
Write a query to retrieve the next 3 movies ordered by price in ascending order,
skipping the first 5 movies (use LIMIT and OFFSET).
___________________________________________________________________________________
___________________________________________________________________________________
____________
mysql> select * from movies order by price limit 5,3;
+----------+-----------------+--------+-------+----------------+
| movie_id | movie_title | genre | price | stock_quantity |
+----------+-----------------+--------+-------+----------------+
| 2 | The Dark Knight | Action | 7.99 | 50 |
| 4 | The Matrix | Sci-Fi | 8.99 | 20 |
| 5 | Gladiator | Action | 9.99 | 15 |
+----------+-----------------+--------+-------+----------------+
3 rows in set (0.00 sec)
***********************************************************************************
***********************************************************************************
**********
7. WHERE, IN, and BETWEEN
Write a query to list all movies that have a price between 5 and 8 and are in
the genres "Drama" or "Action".
___________________________________________________________________________________
___________________________________________________________________________________
_______________
mysql> select * from movies where price between 5 and 8 and genre
IN('Drama','Action');
+----------+--------------------------+--------+-------+----------------+
| movie_id | movie_title | genre | price | stock_quantity |
+----------+--------------------------+--------+-------+----------------+
| 1 | The Shawshank Redemption | Drama | 5.99 | 30 |
| 2 | The Dark Knight | Action | 7.99 | 50 |
+----------+--------------------------+--------+-------+----------------+
2 rows in set (0.00 sec)
***********************************************************************************
***********************************************************************************
**********
8. LIKE
Write a query to find all customers whose email contains "example.com". Include
the rental_id, customer_id, and rental_date for all their rentals.
___________________________________________________________________________________
___________________________________________________________________________________
____________
mysql> select r.rental_id,c.customer_id,r.rental_date from customers c Inner Join
rentals r ON r.customer_id=c.customer_id;'
+-----------+-------------+-------------+
| rental_id | customer_id | rental_date |
+-----------+-------------+-------------+
| 101 | 1 | 2023-06-10 |
| 102 | 2 | 2023-06-12 |
| 103 | 3 | 2023-07-01 |
| 104 | 4 | 2023-08-01 |
+-----------+-------------+-------------+
4 rows in set (0.00 sec)
***********************************************************************************
***********************************************************************************
**********