Selecting Data from Views with Aliases

Selecting Data from Views with Aliases

Question:
If I have created a view from two tables that happen to have a similar column name (e.g., name) :

CREATE VIEW team AS SELECT p.name, p.age, t.name, t.city 
FROM PLAYER p, TEAM t where ...

How do I then select this information from the view? Do I have to use the same aliases?

Answer:
The aliases of “p” and “t” are only for the table names. To create a view that includes columns of the same name, you need to create an alias for the column, like this:

CREATE VIEW team AS SELECT p.name AS PName, p.age AS PAge, 
t.name AS TName, t.city AS TCity FROM PLAYER p, TEAM t
where ....

Then you access the columns by their aliases, like this:

SELECT PName, PAge, TNameFROM Team

If you try to create a view with columns of the same name, it will return an error.

Share the Post:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular