Advertisement

Latest Post

Sunday, 18 September 2016

How to use AS Keyword in Sql

The keyword AS is used to assign an alias to the column or a table.
Syntax:
Basic syntax of table alias is below
SELECT column1, column2.... FROM your_table_name AS alias_name WHERE [condition];
Basic syntax of column alias is below
SELECT column_name AS alias_name FROM your_table_name WHERE [condition];
Example:
Following are two tables first is “Employee” table and second is “Salary” table.
EMPLOYEE table
Id
Name
Age
Address
Department
Gender
1
Alok Kumar Singh
30
IN
IT
Male
2
Tomas Paul
45
NZ
IT
Male
3
Cristomfer Dee
85
AU
HR
Female
4
Niel Macengi
55
US
BPO
Female

SALARY table
ID
EmployeeId
Amount
Date
1
1
50000
12-05-2016
2
2
54000
12-05-2016
3
3
456463
12-05-2016
4
4
150021
12-05-2016
5
1
450000
12-06-2016

Following is the example of table alias
USE SQLCLR
GO

SELECT
Emp.Id , Emp.Name, Sal.Amount, Sal.Date FROM EMPLOYEE AS Emp
,Salary Sal WHERE Emp.Id=Sal.EmployeeId ORDER BY Emp.Id

GO
                                             or
USE SQLCLR
GO

SELECT Emp.Id , Emp.Name, Sal.Amount, Sal.Date FROM EMPLOYEE AS Emp
INNER JOIN Salary Sal ON Emp.Id=Sal.EmployeeId ORDER BY Emp.Id

GO

Id
Name
Amount
Date
1
Alok Kumar Singh
50000
12-05-2016
1
Alok Kumar Singh
450000
12-06-2016
2
Tomas Paul
54000
12-05-2016
3
Cristomfer Dee
456463
12-05-2016
4
Niel Macengi
150021
12-05-2016
Following is the usage of column alias
USE SQLCLR
  GO
  SELECT Id AS EmployeeID, Name AS EmployeeName FROM EMPLOYEE

  GO

EmployeeID
EmployeeName
1
Alok Kumar Singh
2
Tomas Paul
3
Cristomfer Dee
4
Niel Macengi


Government Jobs