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

user1847961
  • 117
  • 2
  • 2
  • 8

1 Answers1

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

Eli Gassert
  • 9,745
  • 3
  • 30
  • 39
  • 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. – Fermin Silva Dec 04 '12 at 13:22
  • Also at least in SQL Server temp tables and sub queries are very different concepts. – Martin Smith Dec 04 '12 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? – Eli Gassert Dec 04 '12 at 13:27