# Table Aliasing
# What is Table Aliasing?
Table aliasing involves giving a table in your SQL query a temporary name. This temporary name (aka alias) is often an abbreviation of the full table name. This is particularly useful in queries involving multiple tables because it simplifies the query syntax and makes it easier to read and write.
# Example Using Authors and Books
Let’s revisit the Authors and Books tables and see how table aliasing can be applied.
# Authors Table
AuthorID | AuthorName |
---|---|
1 | J.K. Rowling |
2 | George Orwell |
3 | Leo Tolstoy |
# Books Table
BookID | Title | AuthorID |
---|---|---|
101 | Harry Potter | 1 |
102 | 1984 | 2 |
103 | Animal Farm | 2 |
104 | War and Peace | 3 |
105 | Anna Karenina | 3 |
Here’s how we might perform a join to get the Titles and Authors using aliasing:
|
|
# Explanation:
Books b
: Here,Books
is aliased asb
. This means that in the rest of the query, we can refer to theBooks
table asb
.Authors a
: Similarly,Authors
is aliased asa
. Any reference to theAuthors
table can now be made usinga
.b.Title, a.AuthorName
: Instead of writingBooks.Title
andAuthors.AuthorName
, we use the aliases, making the query more concise.b.AuthorID = a.AuthorID
: The JOIN condition uses the aliases as a stand in for the full table names as well.
# Lessons
- SQL - W7 Many-to-Many Relationships
- SQL - W7 JOIN Tables with Many-to-Many Relationships
- SQL - W7 Table Aliasing
- Next: SQL - W7 Practice Assignment