SQL Tutorial
SQL Database
SQL Table
SQL Select
SQL Order By
SQL Insert
SQL Update
SQL Delete
Difference
SQL Injection
SQL String Functions
Miscl
- SQL Formatter
- SQL group by
- SQL add/drop/update column operation
- SQL CAST Function
- SQL Comments
- SQL CONCAT Function
- CTE (Common Table Expression)SQL
- How to use distinct in SQL?
- Joining Three or More Tables in SQL
- What is Web SQL?
- How to create functions in SQL?
- How to run SQL Script?
- How to Delete Duplicate Rows in SQL?
- Nth Highest salary
- 12 Codd's Rules
- SQL EXCEPT
- Types of SQL JOIN
- Change datatype of column in SQL
- SQL Auto Increment
- SQL Like
- Commit and Rollback in SQL
- SQL Concatenate
- SQL get month from the date
- Savepoint in SQL
- SQL ORDER BY DATE
- TIME Datatype in SQL
- SQL BETWEEN
- CRUD Operations in SQL
- SQL INDEX
- Scalar Functions in SQL
- SET Operators in SQL
- Types of SQL Commands
- TCL Commands in SQL
- SQL Subquery
- SQL View
- Constraints in SQL
- Pattern Matching in SQL
- SQL Date Functions
- DDL Commands in SQL
- DML Commands in SQL
- SQL CASE
- SQL Inner Join
- SQL IN Operator
- Check Constraint in SQL
- SQL CLAUSES
- SQL LOGICAL OPERATORS
- Delete Column from Table
- Add Column in the Table
- Delete one row in SQL
- Change the Column Value
- How to Add Foreign Key in SQL
- Add a Primary Key
- Insert One or More rows
- How to Use LIKE in SQL
- Cursor in SQL
- Difference Between DROP and Truncate
- SQL Comparison Operators
- SQL COUNT WHERE
- SQL SELECT MIN
- SQL Stored Procedure
- SQL SELECT AVG
- SQL SELECT MAX
- SQL ADD COLUMN
- How to use Auto-Increment in SQL
- SQL Languages
- SQL Arithmetic Operators
- How to Use GROUP BY in SQL
- How to Use ORDER BY in SQL
- Trigger in SQL
- What is Race Condition
- SQL COUNT DISTINCT
PL/SQL Tutorial
Sql Interview Question
SQl Quiz
SQL SELECT AS
- SQL 'AS' is used to assign a new name temporarily to a table column or even a table.
- It makes an easy presentation of query results and allows the developer to label results more accurately without permanently renaming table columns or even the table itself.
- Let's see the syntax of select as:
Here, the Column_Name is the name of a column in the original table, and the New_Column_Name is the name assigned to a particular column only for that specific query. This means that New_Column_Name is a temporary name that will be assigned to a query.
Assigning a temporary name to the column of a table:
Let us take a table named orders, and it contains the following data:
Day_of_order | Customer | Product | Quantity |
---|---|---|---|
11-09-2001 | Ajeet | Mobile | 2 |
13-12-2001 | Mayank | Laptop | 20 |
26-12-2004 | Balaswamy | Water cannon | 35 |
Example:
Suppose you want to rename the 'day_of_order' column and the 'customer' column as 'Date' and 'Client', respectively.
Query:
The result will be shown as this table:
Day_of_order | Customer | Product | Quantity |
---|---|---|---|
11-09-2001 | Ajeet | Mobile | 2 |
13-12-2001 | Mayank | Laptop | 20 |
26-12-2004 | Balaswamy | Water cannon | 35 |
From the above results, we can see that temporarily the 'Day_of_order' is renamed as 'date' and 'customer' is renamed as 'client'.
Let us take another example. Consider we have a students table with the following data.
Student_RollNo | Student_Name | Student_Gender | Student_MobileNumber | Student_HomeTown | Student_Age | Student_Percentage |
---|---|---|---|---|---|---|
1 | Rohit More | Male | 9890786123 | Lucknow | 23 | 75 |
2 | Kunal Shah | Male | 7789056784 | Chandigarh | 20 | 92 |
3 | Kartik Goenka | Male | 9908743576 | Ahemdabad | 22 | 89 |
4 | Anupama Shah | Female | 8890907656 | Chennai | 24 | 92 |
5 | Snehal Jain | Female | 8657983476 | Surat | 21 | 94 |
Example 1:
Write a query to get the student name and the average of the percentage of the student under the temporary column name 'Student' and 'Student_Percentage', respectively.
Query:
Here, to calculate the average, we have used AVG () function. Further, the calculated average value of the percentage will be stored under the temporary name 'Average_Percentage'.
The result will be shown as this table:
Student | Average_Percentage |
---|---|
Rohit More | 88.4000 |
Example 2:
Write a query to get the student roll number and the student mobile number under the temporary column name 'Roll No' and 'Mobile Number', respectively.
Query:
The result will be shown as this table:
Roll No | Mobile Number |
---|---|
1 | 9890786123 |
2 | 7789056784 |
3 | 9908743576 |
4 | 8890907656 |
5 | 8657983476 |
Example 3:
Write a query to get the student roll number and the student phone number, home town under the temporary column name 'Roll No' and 'Student_Info', respectively.
Query:
Here, the CONCAT () function combines two different columns, student phone number and the home town, together in a single column. Further, the combined values of both these columns are stored under the temporarily assigned name 'Student_Info'.
The result will be shown as this table:
Roll No | Mobile Number |
---|---|
1 | 9890786123, Lucknow |
2 | 7789056784, Chandigarh |
3 | 9908743576, Ahemdabad |
4 | 8890907656, Chennai |
5 | 8657983476, Surat |
Assigning a temporary name to a table
Instead of remembering the table names, we can create an alias of them. We can assign a temporary name to the columns of a table; similarly, we can create an alias of a table.
Let's understand it with the help of an example.
Write a query to create an alias of a table named 'students'.
Query:
Here, 's' is the alias, i.e., the temporary name assigned to the 'students' table.
The result will be shown as this table:
Student_RollNo | Student_Name | Student_Gender | Student_MobileNumber | Student_HomeTown |
---|---|---|---|---|
3 | Kartik Goenka | Male | 9908743576 | Ahemdabad |