T01 L05 1a
T01 L05 1a
CSE 4308
To solve this task, I designed a relational database schema with the following tables:
B. **Queries**
The queries were following:
2. Find the movie titles that did not receive any ratings.
3. Show the count of movies that got released in each month along with the month.
4. Find the months between the release date of the first movie and the last movie directed by
‘James Cameron’.
5. Find the reviewer who gives the highest number of lowest rev_star.
6. Show the movie title and its average rating for each of the movies (if there is no rating then it
will show 0 in that place).
7. Show all the mov_id and mov_title with suffix (’old gold’ if the mov_year is less than 1980,
’trendy 90’s’ if the movie was from 1980 to 2000, and ’weird 20’s’ if the movie was from after
2000).
8. Find all the movies and the names of actors who acted in the movies and directors of movies
(no matter whether the name of the actor or director exists or not).
SELECT (MONTHS_BETWEEN(MIN(MOV_RELEASEDATE),MAX(MOV_RELEASEDATE))) AS
MONTHS_DIFFERENCE
FROM DIRECTOR D,MOVIE M,DIRECTION DN WHERE D.DIR_ID=DN.DIR_ID
AND M.MOV_ID=DN.MOV_ID AND D.DIR_FIRSTNAME='James' AND D.DIR_LASTNAME='Cameron';
WITH LowestRating AS (
SELECT MOV_ID, MIN(REV_STARS) AS LowestRating
FROM RATING
GROUP BY MOV_ID
)
SELECT MOV_ID,
MOV_TITLE,
CASE
WHEN MOV_YEAR < 1980 THEN ' (old gold)'
WHEN MOV_YEAR > 1980 AND MOV_YEAR<2000 THEN ' (trendy 90s)'
WHEN MOV_YEAR > 2000 THEN ' (weird 20s)'
ELSE ''
END AS MOVIE_TITLE_WITH_SUFFIX
FROM MOVIE;
D. **Continuing my queries**
10. In the Rating_directed_movie table insert only the rating of those movies that have director
information available.
12. For each rating if it is greater than the overall rating average+2 then set the Status ’Better’, if
less than the overall rating average-2 then set the Status ’Bad’ else ’So So’
UPDATE Rating_directed_movie
SET Status = CASE
WHEN REV_STARS > (SELECT AVG(REV_STARS) FROM Rating_directed_movie) + 2 THEN
'Better'
WHEN REV_STARS < (SELECT AVG(REV_STARS) FROM Rating_directed_movie) - 2 THEN
'Bad'
ELSE 'So So'
END;
COMMIT;
### Problems Faced
1. **Data Validation**: Ensuring data integrity and validation for input data to prevent incorrect or
invalid data from being added to the database.
2. **Query Complexity**: Designing SQL queries that efficiently retrieve and manipulate data from the
database while handling various search and update scenarios.
3. **Data Consistency**: Ensuring that data remains consistent and accurate, especially when
performing updates or deletions.
4. **Performance**: Optimizing the database schema and queries for improved performance, especially
when dealing with a large number of movie records.
5. **Security**: Implementing proper security measures to protect the database from unauthorized
access or SQL injection attacks.
Overall, these challenges were addressed through careful design of the database schema, validation
checks in the application layer, and by writing efficient SQL queries.