1
join  Organisation o (NOLOCK) on o.OrganisationID = h.OrganisationId

I just want to know, what does the o before nolock mean? I have also seen people putting a, u and h in front of nolock - what is it referring to?

Sorry, if this is a stupid question, I am just a beginner in SQL.

andrewsi
  • 10,807
  • 132
  • 35
  • 51
Shiyu
  • 11
  • 1

2 Answers2

1

That o is merely an alias of Organization - a nickname, so that you can type less (and query seems to be cleaner). Otherwise you have to state the long name and say:

join Organisation (NOLOCK) on Organisation.OrganisationID = h.OrganisationId
Peter Pei Guo
  • 7,770
  • 18
  • 35
  • 54
1

Specifically that o is an example of a table alias which can optionally be used with AS also

join  Organisation AS o 

the h also contained in the join condition is also a table alias, something like this

FROM Hospitals AS h
JOIN Organisation AS o (NOLOCK) on o.OrganisationID = h.OrganisationId

In most cases aliases are used to simplify (or shorten) code, and many also view this as making the code more legible. Aliases can on occasion be necessary; for example if using a lookup table more then once in a query the table aliases (L1 & L2 below) becomes essential to differentiate the joined data.

FROM MyDataTable AS D
INNER JOIN MyLookupTable AS L1 D.code1 = L1.lookup_code
INNER JOIN MyLookupTable AS L2 D.code2 = L2.lookup_code

Attitudes regarding use of aliases differ markedly from strong proponents to strong detractors. See this stackoverflow question as an example.

Community
  • 1
  • 1
Paul Maxwell
  • 33,002
  • 3
  • 32
  • 51