English 中文(简体)
SQL Tutorial

5. 图瓦卢

Selected Reading

SQL - Aliases
  • 时间:2024-09-17

SQL - Apas Syntax


Previous Page Next Page  

You can rename a table or a column in a database temporarily by giving them another pseudo name. This pseudo name is known as Apas. The use of apases is to address a specific table or a column in an SQL statement without changing their original name in the database. Apases are created with the AS keyword.

Apases can be especially useful when working with complex queries involving multiple tables or columns with similar names. By assigning temporary names to these tables or columns, you can make your SQL query more readable and easier to understand.

Apasing Table Names

Apases are used to address database tables with a shorter or more meaningful name within an SQL query. The basic syntax of a table apas is as follows.

SELECT column1, column2....
FROM table_name AS apas_name

Example

Assume we have created a table with name CUSTOMERS in SQL database using CREATE TABLE statement as shown below −

CREATE TABLE CUSTOMERS (
   ID INT NOT NULL,
   NAME VARCHAR (20) NOT NULL,
   AGE INT NOT NULL,
   ADDRESS CHAR (25),
   SALARY DECIMAL (18, 2),       
   PRIMARY KEY (ID)
);

Following query inserts values into this table using the INSERT statement −

insert INTO CUSTOMERS VALUES(1,  Ramesh , 32,  Ahmedabad , 2000.00);
insert INTO CUSTOMERS VALUES(2,  Khilan , 25,  Delhi , 1500.00);
insert INTO CUSTOMERS VALUES(3,  kaushik , 23,  Kota , 2000.00);
insert INTO CUSTOMERS VALUES(4,  Chaitap , 25,  Mumbai , 6500.00);
insert INTO CUSTOMERS VALUES(5,  Hardik , 27,  Bhopal , 8500.00);
insert INTO CUSTOMERS VALUES(6,  Komal , 22,  MP , 4500.00);
insert INTO CUSTOMERS VALUES(7,  Muffy , 24,  Indore , 10000.00);

If we verify the contents of the CUSTOMERS table using the SELECT statement, we can observe the inserted records as shown below −

Table 1 − CUSTOMERS Table is as follows.

SELECT * from CUSTOMERS;
+----+----------+-----+-----------+----------+
| ID | NAME     | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |
|  2 | Khilan   |  25 | Delhi     |  1500.00 |
|  3 | kaushik  |  23 | Kota      |  2000.00 |
|  4 | Chaitap |  25 | Mumbai    |  6500.00 |
|  5 | Hardik   |  27 | Bhopal    |  8500.00 |
|  6 | Komal    |  22 | MP        |  4500.00 |
|  7 | Muffy    |  24 | Indore    | 10000.00 |
+----+----------+-----+-----------+----------+

Now, creating the second table ORDERS using CREATE TABLE statement as shown below −

CREATE TABLE ORDERS (
   OID INT NOT NULL,
   DATES DATETIME NOT NULL,
   CUSTOMER_ID INT NOT NULL,
   AMOUNT INT NOT NULL,      
   PRIMARY KEY (OID)
);

Following query inserts values into this table using the INSERT statement −

insert INTO ORDERS VALUES(102,  2009-10-08 00:00:00 , 3, 3000);
insert INTO ORDERS VALUES(100,  2009-10-08 00:00:00 , 3, 1500);
insert INTO ORDERS VALUES(101,  2009-11-20 00:00:00 , 2, 1560);
insert INTO ORDERS VALUES(103,  2008-05-20 00:00:00 , 4, 2060);

If we verify the contents of the ORDERS table using the SELECT statement, we can observe the inserted records as shown below −

Table 2 − ORDERS Table is as follows.

SELECT * from ORDERS;
+-----+---------------------+-------------+--------+
|OID  | DATES               | CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 100 | 2009-10-08 00:00:00 |           3 |   1500 |
| 101 | 2009-11-20 00:00:00 |           2 |   1560 |
| 102 | 2009-10-08 00:00:00 |           3 |   3000 |
| 103 | 2008-05-20 00:00:00 |           4 |   2060 |
+-----+---------------------+-------------+--------+

Now, the following query shows the usage of a table apas. The customers table is apased as C and the orders table is apased as O −

SELECT C.ID, C.NAME, C.AGE, O.AMOUNT 
FROM CUSTOMERS AS C, ORDERS AS O
WHERE  C.ID = O.CUSTOMER_ID;

Output

This would produce the following result.

+----+----------+-----+--------+
| ID | NAME     | AGE | AMOUNT |
+----+----------+-----+--------+
|  3 | kaushik  |  23 |   3000 |
|  2 | Khilan   |  25 |   1560 |
|  3 | kaushik  |  23 |   1500 |
|  4 | Chaitap |  25 |   2060 |
+----+----------+-----+--------+

Apasing Column Names

We can also use an apas with a column name in SQL to give it a different name in the result set of a query. The basic syntax of a column apas is as follows.

SELECT column_name AS apas_name
FROM table_name

Example

Following is the usage of a column apas. Here, the NAME column is apased as CUSTOMER_NAME −

SELECT ID AS CUSTOMER_ID, NAME AS CUSTOMER_NAME
FROM CUSTOMERS

Output

This would produce the following result.

+-------------+---------------+
| CUSTOMER_ID | CUSTOMER_NAME |
+-------------+---------------+
|           1 | Ramesh        |
|           2 | Khilan        |
|           3 | kaushik       |
|           4 | Chaitap      |
|           5 | Hardik        |
|           6 | Komal         |
|           7 | Muffy         |
+-------------+---------------+

Apasing with Self Join

The SQL Self Join is used to join a table to itself as if the table were two tables. To carry this out, at least one table is temporarily renamed in the SQL statement. During this process, the join renames the second table with a temporary name to avoid misunderstandings. This renaming is done using apases.

Syntax

Following is the syntax for performing a self-join with apases −

SELECT column_name(s)
FROM my_table a, my_table b
ON a.join_column = b.join_column

Example

Now, let us join the CUSTOMERS table to itself using the following Self Join query. Our aim is to estabpsh a relationship among customers on the basis of their earnings. In here, we are using apases with column names and also table names to provide a more meaningful resultant table.

SELECT a.ID, b.NAME as EARNS_HIGHER, a.NAME as EARNS_LESS, a.SALARY as LOWER_SALARY
FROM CUSTOMERS a, CUSTOMERS b
WHERE a.SALARY < b.SALARY;

Output

Output of the above query is as follows −

+----+--------------+------------+--------------+
| ID | EARNS_HIGHER | EARNS_LESS | LOWER_SALARY |
+----+--------------+------------+--------------+
|  2 | Ramesh       | Khilan     |      1500.00 |
|  2 | Kaushik      | Khilan     |      1500.00 |
|  6 | Chaitap     | Komal      |      4500.00 |
|  3 | Chaitap     | Kaushik    |      2000.00 |
|  2 | Chaitap     | Khilan     |      1500.00 |
|  1 | Chaitap     | Ramesh     |      2000.00 |
|  6 | Hardik       | Komal      |      4500.00 |
|  4 | Hardik       | Chaitap   |      6500.00 |
|  3 | Hardik       | Kaushik    |      2000.00 |
|  2 | Hardik       | Khilan     |      1500.00 |
|  1 | Hardik       | Ramesh     |      2000.00 |
|  3 | Komal        | Kaushik    |      2000.00 |
|  2 | Komal        | Khilan     |      1500.00 |
|  1 | Komal        | Ramesh     |      2000.00 |
|  6 | Muffy        | Komal      |      4500.00 |
|  5 | Muffy        | Hardik     |      8500.00 |
|  4 | Muffy        | Chaitap   |      6500.00 |
|  3 | Muffy        | Kaushik    |      2000.00 |
|  2 | Muffy        | Khilan     |      1500.00 |
|  1 | Muffy        | Ramesh     |      2000.00 |
+----+--------------+------------+--------------+
Advertisements