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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

_agx_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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pawan KumarDatabase 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.