relational table query movie code example

Example 1: movie database sql queries

select first_name,last_name
  from stars_in_movies inner join stars
 where movie_id=%s and star_id=id;

Example 2: movie database sql queries

select Title from Movie m  
inner join (select top 20 MovieID,sum(Rating) Rate from Ratings group by movieid having count(UserID)>39 order by sum(Rating) DESC) tbl
on m.MovieID=tbl.MovieID
order by tbl.Rate desc

Example 3: movie database sql queries

select first_name --All stars who have not acted with star X
from stars
where star_id not in
(
    select star_id --All stars who have acted with star X
    from stars_in_movies
    where movie_id in
    (
        select movie_id     --All movies in which star X has acted
        from stars_in_movies
        where star_id = "X"
    )
)

Example 4: movie database sql queries

select Title from Movie m  
    inner join (select  MovieID,sum(Rating) Rate from Ratings group by movieid having count(UserID)>39 order by sum(Rating) DESC limit 20) tbl
    on m.MovieID=tbl.MovieID
    order by tbl.Rate desc

Tags:

Sql Example