tags:

views:

126

answers:

2

Hi,

I have following tables.

genre:
  genre_id
  name

actors:
  actor_id
  name

movies:
  movie_id
  actor_id
  genre_id
  title

I have following query to select all the actors with genre_id 3.

select a.name, m.genre_id from 
actors as a
, movies as m
where
m.genre_id = 3;

is it possible to make a query without "movies as m" since I do not need m.genre_id. I just want to display actors name.

+4  A: 

Have to join to movies if the genre_id is the only info you have....

select a.name
from actors as a
inner join movies as m on a.actor_id = m.actor_id
where m.genre_id = 3;
CSharpAtl
+2  A: 

CSharpAtl has it, but as an alternative:

  select a.name
    from actors a
    where actor_id in (select m.actor_id 
                         from movies m 
                        where a.actor_id = m.actor_id
                          and genre_id = 3)
northpole
Where exists( select 1 from ... would work as well - and possibly be faster owing to not executing the 'in'
Kieveli
You are correct that would also work. However, I am not sure about performance differences. Do you have any info backing that up? I would love to find that out.
northpole
I just spoke to my DBA and he said that generally speaking if the sub query is returning more than one row (large data set) then EXISTS is almost always faster. If you are returning one (or very few) row in the sub query then IN is the way to go....nice catch Kieveli
northpole