, , .
1. LEFT JOIN
SELECT DISTINCT
tblCountry.countryID,
tblCountry.tblCountry
FROM
tblCountry
LEFT JOIN
tblProjectCountry
ON
tblProjectCountry.countryID = tblCountry.countryID
WHERE
tblProjectCountry.ProjectID IS NULL
ORDER BY
tblCountry.countryID
erikkallen , .
2. NOT EXISTS
NOT EXISTS IN rohancragg :
SELECT
tblCountry.countryID,
tblCountry.tblCountry
FROM
tblCountry
WHERE
NOT EXISTS (SELECT TOP 1 1 FROM tblProjectCountry WHERE tblProjectCountry.countryID = tblCountry.countryID)
ORDER BY
tblCountry.countryID
.
In my test for MS SQL 2005, there was no significant difference between the first and second query for a table with ~ 250 countries and ~ 5000 projects. However, on a table with more than 3 megabyte projects, the second version (using NOT EXISTS) performed much, much better.
So, as always, it's worth checking both versions.
source
share