• SQLSACT (12/19/2012)

    Why does the 1st query return duplicates and the 2nd query doesn't?

    ... the 2nd query having a stream aggregate. Why is this being added?

    Thanks

    The first query does a JOIN; joins return all matching rows, whether duplicate matches or not.

    In the second query, you used EXISTS() rather than join. EXISTS() just verifies whether or not a row exists. Since SQL has chosen to use a JOIN to generate the final results, SQL uses the stream aggregate to eliminate duplicate join values, thus insuring that only a single match occurs for each join to the other table.

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".