Wednesday, September 30, 2020

 

SQL for Beginners Series - Session 11

ALIAES in SQL


Please click here for you tube video Answer



--******************************************************************************************
-- Session 11 - ALIASES IN SQL (Microsoft SQL Server)
--******************************************************************************************

--> ALIAS -- used to give a short name (Temporary name)

--> Types of Aliases in SQL

--> Column Alias
--> Table Alias

--> Column Alias ---------------------------

SELECT Empid,EmpName,EmpLocation,DOJ FROM dbo.tblEmployee

SELECT Empid,EmpName,EmpLocation,DOJ AS DateOfJoining FROM dbo.tblEmployee

SELECT Empid,EmpName,EmpLocation,DOJ DateOfJoining FROM dbo.tblEmployee

The above 2nd SELECT statement DOJ column has been aliased (renamed) as DateOfJoining which is called Column Alias

We can use AS keyword to alias which is optional.. even though we don't use AS keyword Alias still works. You can run 2nd and 3rd SELECT statements and check

--> Table Alias

SELECT * FROM dbo.tblEmployee E


SELECT * FROM dbo.tblEmployee E 
JOIN dbo.tblDept D 
ON E.Dept_id = D.Deptid

In the above query tblEmployee table has been aliased as E which is called as Table Alias 
also tblDept table as D

In real time, we have to work with many tables, so it is advised to give proper naming convention.

SELECT * FROM dbo.tblEmployee Emp 
JOIN dbo.tblDept Dept 
ON Emp.Dept_id = Dept.Deptid

We can use Emp as short cut name which is easy to understand. Dept for tblDept table.

USES of Alias:
1. Shortens the table or Column name and compact SQL 
2. More readable code


SELECT * FROM dbo.tblEmployee
SELECT * FROM dbo.tblDept

No comments:

Post a Comment

  Printing Number Pattern using SQL Link for video answer: SQL: --********************* --PRINT Pattern --********************* -- 1 -- ...