Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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)
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

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)
Avatar of Murray Brown

ASKER

Thanks but I am talking more specifically about the other brackets eg "("
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]
ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Thanks Pawan
welcome. Glad to help !
My mistake.I will request a reallocation
How do I request that the points are shared?
@_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 ?
Thanks
@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.
@agx
This is very different what i have asked. But anyways I shall open a new thread. Thanks
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.
>> 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.
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
I have reallocated the points in no particular order. Thanks
Assisted really ?
That is why I said in no particular order