Page 268 - PYTHON-12
P. 268

12.15 SQL ALIASES


               SQL aliases are used to give an alternate name, i.e., a temporary name, to a database table or a
               column in a table. We can rename a table or a column temporarily by giving another name called
               alias name which leads to a temporary change (renaming) and does not change the actual name in
               the database.
               Aliases can be used when

               •  more than one table is involved in a query.
               •  functions are used in the query.

               •  column names are big or not very readable.
               •  two or more columns are combined together.

               Using column alias name, we can give different name(s) to column(s) for display (output) purpose
               only. They are created to make column names more readable. SQL aliases can be used both for
               tables as well as columns.
               •  COLUMN ALIASES are used to make column headings in the query result set easier to read.

               •  TABLE ALIASES are used to shorten a table name by giving an easy alternate name, making it
                   easier to read or when performing a self-join (i.e., listing the same table more than once in the
                   FROM clause).
               Syntax for table alias:

                       SELECT <columnname1>, <columnname2>.....

                       FROM <table_name> AS <alias_name>;

                       WHERE [<condition>];
               Syntax for column alias:

                       SELECT <column-name> AS <“alias_name”>
                       FROM <table_name>

                       WHERE [<condition>];
               For example,
                      Table: Student

                 Student_name   Date-of-birth            SELECT Name AS “Student_name”, DOB AS “Date_of_birth”
                Raj Kumar       17-Nov-2000
                Deep Singh      22-Aug-1996                       FROM student;
           Computer Science with Python–XII  12.38 Diksha Sharma  17-Dec-1999
                                02-Feb-2000
                Ankit Sharma
                                                                      Alias name for fields, Name and DOB
                Radhika Gupta
                                03-Dec-1999
                                21-April-1998
                Payal Goel
                Gurpreet Kaur
                                04-Jan-2000
                                05-May-1997
                Akshay Dureja
                                08-Oct-1999
                Shreya Anand
                                25-Dec-2000
                Prateek Mittal
                10 rows in a set (0.02 sec)
   263   264   265   266   267   268   269   270   271   272   273