开发者

SQL join syntax, when do you use "AS"?

开发者 https://www.devze.com 2023-02-05 10:52 出处:网络
I am working on an SQL statement and just wanted to clarify my understanding of join syntax in MS SQL.

I am working on an SQL statement and just wanted to clarify my understanding of join syntax in MS SQL.

Say I have the SQL...

select t.year from HOUSE AS h 
LEFT OUTER JOIN SUBJECT K 
    ON H.Name = K.Name 
INNER JOIN TESTCASE AS T 
    ON k.year IS NO开发者_JAVA技巧T NULL

Sorry for the confusing example but basically - why am I able to use LEFT OUTER JOIN SUBJECT K without the keyword AS whereas with an INNER JOIN, I need to use a keyword of AS for INNER JOIN TESTCASE AS T?


'AS' is not required in either of these cases, but I prefer it personally from the point of view of readability, as it conveys exactly what you were meaning to do.


As far as I know the AS is only for easy coding. You can create a smaller or more clear name for the table. So House as H and further in the query you can use H.Name instead of typing House.Name


I believe 'AS' used to be a required keyword, but is no longer needed, only for readability. I find 'As' is often used when creating a name for a data item in the SELECT, but not when giving a name to a table in a JOIN. For example:

SELECT t.ID as TestID FROM [House] h JOIN [TestCase] t ON t.ID=h.TestID

I think this is probably because a set of data items displayed with no 'As' could become confusing as to how many items there are, and how many are just names applied to them.


interesting though where the AS keyword is useful... lets say two tables have name and you get this back from a query in code then it is cool to change the column name to something unique.

0

精彩评论

暂无评论...
验证码 换一张
取 消