July 13, 2020 at 6:17 am
I am using SQL server 2008.
Is JOIN always faster than sub query ?
July 13, 2020 at 7:59 am
The second one: "it depends".
Overall, you cannot say this or that construct is faster. SQL is a declarative language. This means that you state what result you want, and the optimizer figures out the best way to compute that result. Ideally, you should be able to express you query in the clumsiest of ways, and the optimizer may still find the fast and simple plan. Reality is different, and there many things you can do in a query to make it go slow. But that is more subtle than JOIN vs. subquery.
When you write your query, focus on expressing what result you want. That may or may not give the best performance, but it helps you or someone else who is looking at the query two months later.
[font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]
July 13, 2020 at 4:12 pm
No.
In particular, for example, often an EXISTS() check is more efficient that an INNER JOIN when you just to need to verify that a matching row exists in another 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".
July 13, 2020 at 5:27 pm
Scott has given an example, and Erland is right. It depends. Test both ways for your situation.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply