2

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

Terry Zhang
  • 4,541
  • 8
  • 23
  • 29
  • 3
    `t` is an alias for the sub-query. it can be anything other than a reserved keyword – Vamsi Prabhala Nov 15 '18 at 16:05
  • I can't find a canonical Q&A for SQL Server aliases, but this should help https://stackoverflow.com/questions/4981481/how-to-write-update-sql-with-table-alias-in-sql-server-2008 or https://stackoverflow.com/questions/198196/when-to-use-sql-table-alias – DavidG Nov 15 '18 at 16:07

3 Answers3

6

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
Aaron Dietz
  • 10,137
  • 1
  • 14
  • 26
5

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
Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82
-1

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