What is the 't' mean at end of the subquery in SQL Server

Viewed 964

I have following code used for query data by rows and start point:

SELECT InquiryId
FROM (select InquiryId,  ROW_NUMBER() over (order by InquiryId) as Seq 
from [InquiryTable] WITH(NOLOCK)
where InquiryId >= 100 and InquiryId <= 200)t
where Seq Between 1 and 20

What is character 't' means at the end of the forth row in SQL server?

Thanks

3 Answers

Here is a helpful visual showing how t is an alias:

SELECT *
FROM table t

Replace table with a subquery:

SELECT *
FROM (SELECT * FROM table) t

It is an alias for your subquery. Improved indentation helps you to understand better:

SELECT InquiryId
FROM (select InquiryId,  ROW_NUMBER() over (order by InquiryId) as Seq 
      from [InquiryTable] WITH(NOLOCK)
      where InquiryId >= 100 
        and InquiryId <= 200
     ) AS t
where Seq Between 1 and 20

doesn't mean matter, you are renaming the table in the subquery as t, but in from to rename you mustn't use as, but you must use directly the name in the table, and you can require the columns of the table or the subquery in you case using t.col

Related