sql movie database example

Example 1: movie database sql queries

select A.first_name
from stars A,
(
select star_id
from stars_in_movies
where movie_id = "X"
)B,
(   
select star_id
from stars_in_movies
where movie_id = "Y"
)C
where A.star_id = B.star_id
AND B.star_id = C.star_id

Example 2: 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 3: movie database sql queries

select director
  from movies inner join stars_in_movies
 where star_id=%s and movie_id = id;

Example 4: movie database sql queries

select director -- directors who have worked with star X
from 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 5: movie database sql queries

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

Example 6: 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"
    )
)

Tags:

Sql Example