Aliases (Column Headings)

One way to make the result of a query more understandable is to change the column headings. For example, we have the table "books" with a field "quantity" (among others) in which the quantity of books in stock is stored; We want the text "stock" to appear as the header of the "quantity" field when displaying the information of said table, for this we place an alias as follows:


 select title, quantity as stock,  price

  from books;


To replace the name of one header field with another, place the keyword "as" followed by the header text.

If the alias consists of a single string, the quotes are not necessary, but if it contains more than one word, it is necessary to place it in double quotes:


 select title,

  quantity as "available stock",

  price

  from books;


You can also create an alias for calculated columns. For example:


 select title, price,

  price * 0.1 as discount,

  price- (price * 0.1) as "final price"

  from books;


The "as" keyword is optional, but it is convenient to use it.

So an "alias" is used as the name of a field or an expression. In these cases, they are optional, they serve to make the result more understandable.

Comments

Popular posts from this blog

Data Consistency and Inconsistency - What is Difference

Cardinality in DBMS – max/minimum cardinality & examples

Relational Model in DBMS