Home > Blockchain >  What does the 'a' and 'ar' stand for?
What does the 'a' and 'ar' stand for?

Time:03-18

I am working on the question, and after checking the answer posted by others on the internet, I can't understand why is there 'From albums a, artists ar' instead of 'From albums, artists'?

What does the 'a' and 'ar' stand for?

Select Count(Distinct a.AlbumId) AS album_number, a.ArtistId, ar.Name
**From albums a, artists ar**
Where a.ArtistId in (Select ar.ArtistId
                     From artists ar
                     Where ar.Name = 'Led Zeppelin')

The question is:enter image description here

CodePudding user response:

It is an alias you can use an alias when you join to access its corresponding columns. It could be anything. If you do not specify the alias and tries to access the column name present in both the table with same name give you ambiguity error

Like this

Select Count(Distinct albs.AlbumId) AS album_number, albs.ArtistId, arts.Name
From albums albs, artists arts
Where a.ArtistId in (Select ar.ArtistId
                     From artists ar
                     Where ar.Name = 'Led Zeppelin')

CodePudding user response:

These are aliases for datasets or tables named before. So a is albums table and ar is artists in the shown context. SQL allows to skip AS before such aliases. Technically the complete statement sounds like this: FROM albums AS a, artists AS ar. By the way comma is also shugar for JOIN (see here). Aliases can also be used to substitute names of columns in the dataset.

  •  Tags:  
  • sql
  • Related