1

I would like to know what is the difference between

INNER JOIN TB_PACKAGE AS B
    ON (A.CdPackage = B.CdPackage)

and

INNER JOIN TB_PACKAGE AS B
    ON A.CdPackage = B.CdPackage

When I should use the parenthesis and when not, I need some sort of explanation.

Here is the entire query, it is a stored procedure:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [DB].[SP_UP_NOTES]
  @COD_ROTE  NUMERIC(10)
 ,@COD_STATS   NUMERIC(10)
AS

BEGIN

  UPDATE C
  SET C.IdStatus = @COD_STATS
  FROM DB.TB_NOTES AS A
  INNER JOIN OPMDM.TB_PACKAGE AS B
    ON (A.CdPackage= B.CdPackage)
  INNER JOIN OPMDM.TB_NOTES AS C
    ON (A.CdNotes = c.CdNotes)
  WHERE B.STATS = @COD_ROTE;

END
csforkids
  • 29
  • 1
  • 3
  • Unless the JOIN condition has a mix of logical operators, e.g. `a.X = b.X AND (a.Y = b.Y OR a.Foo = 'Bar')` you won't need parenthesis. Some however use parenthesis for readability, but this is a subjective preference. – StuartLC Jan 10 '18 at 09:47
  • 1
    Thank's for the explanation, really helpfull ! – csforkids Jan 10 '18 at 12:08

2 Answers2

2

No difference at all. The only time you need parentheses is if you need to do a complex join that requires enforcing of AND/OR evaluation order.

fauxmosapien
  • 525
  • 3
  • 6
0

In Your Above Code, there is no difference between the Code with and Without parenthesis. But In some cases, Use of parenthesis can make a diffrece

For Example If you are using AND and OR operators combined

SELECT
*
FROM YourTable
WHERE 
(
id > 10 and Name Like 'A%'
)
or
(
Id < 10 and name Like '%A'
)

In the above case Use of parenthesis Makes some effect since I want to get the records that Match the Criteria id > 10 and Name Like 'A%' or Id < 10 and name Like '%A'

here I want to group the 2 AND conditions and Generate the List of records that satisfy either of the conditions, So I am using parenthesis

Jayasurya Satheesh
  • 7,826
  • 3
  • 22
  • 39
  • In your example, `AND` has precedence over `OR` anyway, so the parenthesis are still unnecessary. Also, be careful - Applying a filter in `OUTER JOINs` is [NOT the same](https://stackoverflow.com/a/10297312/314291) as applying a filter in a `WHERE` clause. – StuartLC Jan 10 '18 at 16:35