8

Im quite new to SQL and I'm just trying to find out what it means when you use t1, t2 or t3 etc... I cant get my head around it and would just like to learn about it. I've tried looking all over google but i've found nothing yet... Can you help?

Thanks Mw

3

1 Answer 1

10

t1/t2/t3 are common table aliases for "temp" tables (e.g. subqueries that are made of multiple tables and don't alias nicely).... call it a bit of laziness if that helps :)

SELECT * FROM MyTable t1 means from now on, I'm calling MyTable t1. Another way of writing it would be: SELECT t1.* FROM MyTable t1 or if you didn't use the alias, SELECT MyTable.* FROM MyTable

3
  • 1
    Not necessarily temp tables. It's just an alias. Sometimes used for temp tables, some others to resolve namespaces. Joining two tables with the same column name? You alias each table so you can choose whether you want t1.id or t2.id. Dec 4, 2012 at 13:22
  • Also at least in SQL Server temp tables and sub queries are very different concepts. Dec 4, 2012 at 13:23
  • The purpose of quoting "temp" was more making note of unnamed/unspecified tables in queries, which often relates to subqueries, table variables, and the likes. I agree with you, it has nothing to do with actual temp tables. I'm merely trying to reference when t1/t2/t3 (as opposed to more appropriately named aliases, such as u for a Users table) is most commonly used. Can you suggest how I might modify the answer to be more clear on this point? Dec 4, 2012 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.