1
votes

I seen some queries including table name with t suffix. like

select * 
from MyTable t

or

select Col1, Col2, Col3 
from MyTable t with (nolock) 
where ....

I searched, but I was not able to understand what is the purpose of including the t after inserting table name.

2
It's called an alias. The real question you should be asking yourself is, why are you using NOLOCK when you don't understand the implications and consequences of its use.Larnu
NOLOCK means read dirty and duplicate data while taking excessive locks and randomly throwing errors. It doesn't mean go fast, it means the query has a serious performance problem that someone tried to cover upPanagiotis Kanavos
I searched aliases are fundamental SQL concepts, used in all databases. They're mentioned in all articles, tutorials and courses for the simple reason that nobody wants to write the full table or column names all the time. In many cases they're necessary, eg when you want to join a table to itself, or when a query uses multiple tables with one or more identical namesPanagiotis Kanavos
Some people ise AS some don't, it's more preference. It's not really that much shorter. What is way short is SELECT t1.Col1, T2.Col1 over SELECT Table1.Col1, Table2.Col2.Larnu
You should never use it. If you have to ask what a table alias is, I suspect this came from legacy code with performance issues. NOLOCK is essentially a tornado siren warning that the queries have performance or concurrency issues that were covered up. Quite often the coverup results in other, hard to diagnose errors. Beware, there are other dragons in that systemPanagiotis Kanavos

2 Answers

3
votes

So, It is alias. if you have just one select without join it does not give benefits. if you have select like it:

select t1.Col1,t2.Col1,t1.Col3,t2.Col4
from MyTable t1
inner join MyTable2 t2 on t1.Col1 = t2.Col2

Then alias is a big comfort

3
votes

In that case this is an alias to the table name. So select * from MyTable t is equivalent to select * from MyTable as t. You can use any permitted literal instead of t. For example: select tbl.Col1 from MyTable tbl.