That's right! Aliases are also convenient in other situations. Let's analyze the following situation:
We want to put information about children and their mothers into a database. At some point, we would also like to show children together with their mothers using a
Let's say we store both children and mothers in the same table
person. Each row has a column named
mother_id. This column contains the ID of another row – the mother's row.
The question is: can we join the table
person with the table
person? The answer is simple: yes, we can! But you can't simply write this in your SQL query:
person JOIN person
You need to provide two different aliases for the same table:
FROM person AS child
JOIN person AS mother
ON child.mother_id = mother.id;
Thanks to the aliases, the database engine will use the same table person twice – the first time to look for children and the second time to look for their mothers.