Amazon Interview Question
Software Engineer / DevelopersTeam: Amazon Instant Video
Country: UK
Interview Type: Phone Interview
Why are you making it complex...it is just
select g.id from gener g where g.genre in ("Action", "Comedy");
This is correct query 'select g.id from gener g where g.genre in ("Action", "Comedy");'as there is only 1 column if you put 'and' result will be 0
I think Aayush is correct. Query above will give id,s where either action or comedy (or action&comedy) was present. This query does not answer the question correctly, if you want to use this particular query, I would suggest using something like this
select g.id,count(*) from gener g where g.genre in ("Action", "Comedy") group by g.id having count(*) > 1
For clarity on why a simple where with or wont work.
20 Action
21 Action
22 Comedy
21 Comedy
Supposed to return 21 as it is both Action and Comedy.
If the nested query in the loop sounds confusing, albeit something longer here
select g.id
from genre g, genre x
where g.id = x.id and
((g.Genre = 'Action' and x.Genre='Comedy') or (g.Genre='Comedy' and x.Genre='Action'))
- San June 20, 2014