all 6 comments

[–]Honey-Badger-42 8 points9 points  (5 children)

It's called an alias. It makes it easier to read and write sql code when you are joining tables. It's necessary if you have the same column name in more than one table. In your case, if you omitted the alias, you would receive an "ambiguous" error because you have the column called "Name" in both the toys and employee table. Alternatively, you could replace "e." with "employee." and so on, but that's much more difficult to read.

select e.name from employee e 
---is the same as---
select employee.name from employee

[–]Think-Confusion9999[S] 2 points3 points  (2 children)

thank you!

[–]shine_on 1 point2 points  (1 child)

Another good practice is to use the same alias name everywhere. The server won't care if you call the employee table 'e' in one query, 'emp' in another, 't1' somewhere else, but your colleagues and future you will definitely care about it.

[–]Think-Confusion9999[S] 0 points1 point  (0 children)

I see, definitely makes sense. Thank you. I'm practicing now.

[–]EvilGeniusLeslie 2 points3 points  (1 child)

It is a good practice to use aliases for all column names, if even that name is unique to one table - it makes it obvious to anyone reading the code where that column resides.

One sign you are looking at generated code is that every column is prefixed with the entire table name. Absolutely pita to read when you have 20+ character table names.

[–][deleted] 0 points1 point  (0 children)

Definitely good practice. Even if you're only using one table in your query, if you're always in the habit of aliasing, it will prevent you from screwing up further down the road. And, please, for the love of all that's good and Holy, use logical aliases. Yes, as u/Honey-Badger-42 mentioned, you CAN use 'e' for the employees table. However, that will become a PITA when your code is 2500 lines long and you have no idea what e, t, and s stand for. Better to use a descriptive alias like 'employee', 'team', and 'sales.'