12/13/2023 6:45:11 PM

In SQL, table aliases allow you to "rename" a table in a query (give it an alias). Sometimes this is necessary if you are referencing the same table more than once. Usually, it is not necessary. If it is not necessary, only create an alias if that alias adds clarity. Never add an alias simply to make the code shorter or more concise. Again...

NEVER ADD AN ALIAS TO MAKE THE CODE SHORTER OR MORE CONCISE!
Adding aliases for shortness or conciseness may seem fine when you are coding but, inevitably, at some point in the future, you or someone else will need to review that code and that shortness will become ambiguity and confusion.

Remember, code for others and your future self.

Example 1

-- concise but unclear select P.PostId ,P.UserId ,P.Title ,U.FirstName ,U.LastName from Users U join Posts P on U.UserId = P.UserId
-- not concise but clear select Posts.PostId ,Posts.UserId ,Posts.Title ,Users.FirstName ,Users.LastName from Users join Posts on Users.UserId = Posts.UserId

Example 2

-- concise but unclear select U.UserId ,U.FirstName ,U.LastName ,FF.UserId ,FF.FirstName ,FF.LastName ,FP.PostId ,FP.Title from Users U left join Users FF on U.FavoriteFriendUserId = FF.UserId left join Posts FP on U.FavoritePostId = FP.PostId
-- not concise but clear select Users.UserId ,Users.FirstName ,Users.LastName ,FavoriteFriend.UserId ,FavoriteFriend.FirstName ,FavoriteFriend.LastName ,FavoritePost.PostId ,FavoritePost.Title from Users left join Users FavoriteFriend on Users.FavoriteFriendUserId = FavoriteFriend.UserId left join Posts FavoritePost on Users.FavoritePostId = FavoritePost.PostId
-- even less concise but more clear select Users.UserId ,Users.FirstName ,Users.LastName ,FavoriteFriend.UserId as FavoriteFriend_UserId ,FavoriteFriend.FirstName as FavoriteFriend_FirstName ,FavoriteFriend.LastName as FavoriteFriend_LastName ,FavoritePost.PostId as FavoritePost_PostId ,FavoritePost.Title as FavoritePost_Title from Users left join Users FavoriteFriend on Users.FavoriteFriendUserId = FavoriteFriend.UserId left join Posts FavoritePost on Users.FavoritePostId = FavoritePost.PostId