user2661125
user2661125

Reputation: 25

differences between two sql statements

I'm new to SQL and is doing some practice on sqlzoo (https://sqlzoo.net/wiki/More_JOIN_operations). Per one of its questions, my SQL statement is judged as wrong, but I think it is equivalent to the reference statement.

There are three tables. Details can be found on sqlzoo

This database features two entities (movies and actors) in a many-to-many relation. Each entity has its own table. A third table, casting , is used to link them. The relationship is many-to-many because each film features many actors and each actor has appeared in many films.

My statement

SELECT title, name
FROM casting JOIN movie ON movie.id=movieid
             JOIN actor ON actor.id=actorid
WHERE movieid in
(SELECT id FROM movie WHERE yr=1962)
AND ord=1

The reference statement that produces right result

SELECT title, name
FROM movie JOIN casting ON (id=movieid)
JOIN actor ON (actor.id = actorid)
WHERE ord=1 AND  yr = 1962

I cannot tell the difference between above two statements.

Upvotes: 0

Views: 50

Answers (1)

Joel Coehoorn
Joel Coehoorn

Reputation: 415705

They should both work and produce the same results. But the reference statement is much more efficient.

Upvotes: 1

Related Questions