SQL Using brackets in a Join

Hi

In the following SQL Join I am trying to get the same result as if I had created a view that joined the Orders and Customers table and then joined that view to the Shippers table. I am using brackets around the first join. Is this the right way to go about this?

(Orders Left Join Customers On (Orders.[Customer ID] = Customers.ID)) Left Join Shippers On (Orders.[Shipper Id] = Shippers.ID)
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
 
_agx_Connect With a Mentor Commented:
Do you mean square brackets "[ ]" or parenthesis "( )"? Also which DBMS?

Since the column names contain invalid characters, like spaces, they must be escaped. The syntax is DBMS specific. Square brackets are the correct way to that if you are using SQL Server (or Access).

As far as placing parenthesis around the JOIN's, again it depends on the DBMS, and type of JOIN.  They are usually not required for SQL Server, but again it depends on the exact JOIN.  In this specific case, they should NOT be required for SQL Server. In other words, this:

FROM
    (Orders LEFT JOIN Customers ON (Orders.[Customer ID] = Customers.ID))
    LEFT JOIN Shippers ON (Orders.[Shipper Id] = Shippers.ID)

should yield the same result as this:

FROM
    Orders LEFT JOIN Customers ON Orders.[Customer ID] = Customers.ID
                  LEFT JOIN Shippers ON Orders.[Shipper Id] = Shippers.ID
0
 
Pawan KumarDatabase ExpertCommented:
Yes that the right way.

if you have space in the column name or database name or any object name then you need to use brackets.

(Orders Left Join Customers On (Orders.[Customer ID] = Customers.ID)) Left Join Shippers On (Orders.[Shipper Id] = Shippers.ID)
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks but I am talking more specifically about the other brackets eg "("
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Pawan KumarDatabase ExpertCommented:
Even when we create scripts using SQL Server Management Studio , sql will enclose all names with square brackets.

Also note that you need to use square brackets when the name contains a space or a reserved word e.g. [System]
0
 
Pawan KumarConnect With a Mentor Database ExpertCommented:
Those ( brackets does not matter in this case.

Sequence of join only matters. You can use directly like below.

Orders
Left Join Customers On Orders.[Customer ID] = Customers.ID
Left Join Shippers On Orders.[Shipper Id] = Shippers.ID
0
 
_agx_Commented:
In this specific case, they should NOT be required for SQL Server.

Generally, parenthesis are optional unless using them changes the order of evaluation. In this case - it doesn't.
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks Pawan
0
 
Pawan KumarDatabase ExpertCommented:
welcome. Glad to help !
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
My mistake.I will request a reallocation
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
How do I request that the points are shared?
0
 
Pawan KumarDatabase ExpertCommented:
@_agx

>> Since the column names contain invalid characters
Which are the other invalid characters you are talking about in SQL Server other than space? can you please give an example?

>> As far as placing parenthesis around the JOIN's, again it depends on the DBMS, and type of JOIN.  They are usually not required for SQL Server, but again it depends on the exact JOIN.
Which exact join it depends on - this is not clear to me ? can you please give an example ?
0
 
_agx_Commented:
Thanks
0
 
_agx_Commented:
@Pawan Kumar - Generally it is best to open a new thread, to make it easier for others with the same question to find the answers.

If you haven't encountered any, you must be lucky enough not work with legacy applications, which usually contain all kinds of wacky column names. (When possible, I prefer to rename the columns and avoid square brackets altogether.)  Search the BOL, for more details.
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/154c19c4-95ba-4b6f-b6ca-479288feabfb/characters-that-are-not-allowed-in-table-name-column-name-in-sql-server-?forum=databasedesign

Parenthesis are used to enforce order of operations, same as when mixing AND / OR operators.  (They're also required for derived tables).  So if you have a series of JOIN's that need to take place first, to ensure the right results, parenthesis are required. It's generally only seen in far in more complex queries than this one. Search for SQL Server JOIN's parenthesis and you should find some examples.
0
 
Pawan KumarDatabase ExpertCommented:
@agx
This is very different what i have asked. But anyways I shall open a new thread. Thanks
0
 
_agx_Commented:
Edit:
@Pawan Kumar - Sorry, then I don't understand what you are asking, and a new thread would probably be best.  The above answered "what characters other than spaces are invalid for column names" and "when would parenthesis be required in a JOIN".  Edit: I don't have time to put together an example of the latter, but essentially it is required if parenthesis will change the meaning of the JOINs. Again, the same as when using a mix of AND / OR operators.
0
 
_agx_Commented:
>> How do I request that the points are shared?

Sorry, I didn't see your question amidst the flurry of other posts.  There should be some sort of "Request Attention" link at the bottom of the question.  You can ask a moderator to reopen the question for reallocation.
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi. I looked for the Request Attention option during the time of the question and now but couldn't find it, I did however find a "Report Question" option which I clicked. I then asked for the points to be evenly allocated
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
I have reallocated the points in no particular order. Thanks
0
 
Pawan KumarDatabase ExpertCommented:
Assisted really ?
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
That is why I said in no particular order
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.