Use this space to put some text. Update this text in HTML

468x60 banner ad

Advertise with Us

Powered by Blogger.

Wednesday 2 March 2016

SQL Alias

sql Alias is used to give an alias name to a column or a table. This is used for giving short name of a column and a table. you have to use alias, when column name is large or you can combine two column in a single column name or more readable.


SELECT * FROM Employee


 Aliases for columns:

SELECT
column-name as alias-name
from
table-name

Example1:

SELECT
  First_name AS [Employee Name]
FROM Employee

Result:


In the above query, the column First_name is given a alias as "Employee Name". So when the result is displayed the column name appears as "Employee Name" instead of  'First_name'.

Example2:


SELECT
  First_name + ' '+ Last_name AS [Employee Name]
FROM Employee


Result:




In the above query, the column First_name and Last_name combine together to display a single column as alias as "Employee Name". So when the result is displayed the column name appears as "Employee Name" instead of  "First_name" and "Last_Name". 

Table Name Alias

Syntax:
 
SELECT column-name
from table-name as alias-name


Example1:

Consider Two tables, First is Employee and Second is tblDepartment. 

SELECT
  emp.First_name, emp.Last_name,emp.Salary, emp.Joining_date, Department_ID
FROM Employee emp
 


SELECT dept.Id, dept.Name as [Department Name]
FROM tblDepartment dept





Below is the Query to fetch data from both the tables using SQL Alias,
 

SELECT 
  emp.First_name, emp.Last_name,emp.Salary, emp.Joining_date,
  dept.Name as [Department Name] 
from Employee as emp
INNER JOIN tblDepartment as dept
ON emp.Department_ID=dept.Id
 
Result

 

 

 


 



















 

 

 
  
  

No comments :

Post a Comment

Ask a Question?