1

I have to write sql query with two different way, both are giving same output. But Still not understand

  1. what is different between both query?

  2. which one is fast in access?

  3. which is more recommend?

Query # 1

SELECT PageControlDet.PageId, PageMaster.PageName, PageMaster.PageURL, 
       PageMaster.PageTitle, PageMaster.PageDescription
FROM AgentRoleAccessDet 
INNER JOIN PageControlDet ON AgentRoleAccessDet.ControlId = PageControlDet.ControlId
INNER JOIN PageMaster ON PageControlDet.PageId = PageMaster.PageId 

and

Query #2

SELECT PageControlDet.PageId, PageMaster.PageName, PageMaster.PageURL, 
       PageMaster.PageTitle, PageMaster.PageDescription
FROM   AgentRoleAccessDet,  PageControlDet, PageMaster
WHERE  AgentRoleAccessDet.ControlId = PageControlDet.ControlId and
    PageControlDet.PageId = PageMaster.PageId 
ORDER BY PageControlDet.PageId
Pavi
  • 335
  • 2
  • 3
  • 20
Saroop Trivedi
  • 2,245
  • 6
  • 31
  • 49

3 Answers3

1
  1. what is different between both query?

One uses INNER JOIN, the other doesn't. Just listing all of the tables in the FROM clause, separated by commas is an older style/syntax, that's mostly deprecated these days. It's from before the JOIN syntax was introduced into the standard.

  1. which one is fast in access?

They should perform equally well. You'd have to generate execution plans to know for sure, but the optimizer should treat them both equally.

  1. which is more recommend?

The first. Making the joins explicit and distinct from any filtering can help others to understand the query in the future. Also, as soon as you introduce OUTER joins, you'd need to use deprecated syntax to use the second style.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
0

You should definitely use the inner join.

Specifying multiple tables in the From section is deprecated and can lead to ambiguous query interpretation. See this answer for more details.

Community
  • 1
  • 1
Yaakov Ellis
  • 40,752
  • 27
  • 129
  • 174
0

In second example your query is equivalent to this:

SELECT PageControlDet.PageId, PageMaster.PageName, PageMaster.PageURL, 
       PageMaster.PageTitle, PageMaster.PageDescription
FROM   AgentRoleAccessDet
CROSS JOIN PageControlDet 
CROSS JOIN PageMaster
WHERE  AgentRoleAccessDet.ControlId = PageControlDet.ControlId and
       PageControlDet.PageId = PageMaster.PageId 
ORDER BY PageControlDet.PageId

Using cross join without this keyword (comma separated list of tables) is old syntax, TSQL support both (old and new with CROSS Join keyword). Cross join will produce Cartesian product. Due to your example I'll prefer first query - because it's more readable, understandable and well-formed according to the query logic. About performance: I think that MSSQL Optimizer will change your second query to first.

Roman Badiornyi
  • 1,509
  • 14
  • 28