SQL Server BasicsWhat is SQL Server database?What is RDBMS?What is Normalization?Why we use Denormalization?What_is_SQL?What is PL/SQL?Difference between SQL and PL/SQLDatabase TableOne to One RelationshipOne to Many RelationshipMany to Many RelationshipMany to One RelationshipString Data TypesNumber Data TypesDate Data TypesOther Data TypesCreate DatabaseDrop DatabaseCreating and Managing Users in SQL ServerCreate TableAlter TableDrop TableConstraints in SQL serverPrimary KeyForeign KeyUnique KeyCandidate KeyComposite KeyDifference between primary key and candidate keyPrimary key and foreign key relationshipSurrogate KeyCascading Referential Integrity ConstraintsSelf Referential Integrity ConstraintsInsert into statementInsert multiple recordsUpdate statementDelete statementTruncate statementDifference between Delete and TruncateAlias in SQL ServerSelect statementSelect DistinctSelect TopSelect IntoNull Functions(ISNULL(),NULLIF(),COALESCE())Sub QueryIdentity ColumnSequence objectDifference between sequence and identity columnSQL Server ClausesWHERE ClauseOrder By ClauseTop N ClauseGroup By ClauseHaving ClauseDifference between Where and HavingSQL Server OperatorsArithmetic OperatorsComparison OperatorsLogical OperatorsBitwise OperatorsAny OperatorsAll OperatorsUnion OperatorsUnion All OperatorsDifference between Union and Union AllIntersect OperatorExcept OperatorDifference between Except and IntersectJoinsInner JoinLeft JoinRight JoinFull JoinSelf JoinCross JoinViewsWhat are views?Create views using SSMSIndexed ViewsComplex ViewsCheck Option in ViewCheck Encryption in ViewSchema Binding Option in ViewRead-only ViewsUpdatable ViewsAdvantages and disadvantages of viewsCreate multiple views on one tableCan we implement index on views?Can we Perform Insert, update, delete operation on views?Stored Procedure and FunctionsWhat are Stored Procedures?Why we use stored procedures?Passing parameters to Stored procedureUser-Defined FunctionsDifference between UDF and Stored procedurePre-Defined Functions@@Indentity and Scope_IndentityNULLIF, ISNULL and COALESCE

Alias in SQL Server

In SQL Server, an alias is a temporary name assigned to a table, column, or expression in a query to make the query more readable or to differentiate between multiple instances of the same object. Aliases are commonly used in SELECT, FROM, and JOIN clauses. Here are examples of using aliases in SQL Server:

1- Table Alias:


SELECT e.FirstName, e.LastName
FROM Employees AS e
WHERE e.Department = 'Sales';

In this example, "e" is an alias assigned to the "Employees" table. It allows us to refer to the table using the shorter alias "e" instead of the full table name.

2- Column Alias:


SELECT SUM(Quantity) AS TotalQuantity
FROM Orders;

Here, "TotalQuantity" is an alias assigned to the result of the SUM() function. It provides a meaningful name for the calculated column in the query result.

3- Alias in JOIN:


SELECT e.FirstName, d.DepartmentName
FROM Employees AS e
JOIN Departments AS d ON e.DepartmentID = d.DepartmentID;

In this example, "e" is an alias for the "Employees" table, and "d" is an alias for the "Departments" table. The aliases are used to specify the join condition between the two tables.

4- Alias in Subquery:


SELECT e.FirstName, e.LastName
FROM (
    SELECT EmployeeID, FirstName, LastName
    FROM Employees
    WHERE Department = 'Sales'
) AS e;

Here, "e" is an alias for the subquery. It allows us to treat the subquery as a temporary table and refer to it in the main query.

Aliases are often assigned using the "AS" keyword, but it is optional in SQL Server. For example, you can write FROM Employees e instead of FROM Employees AS e for a table alias.

Using aliases in SQL Server can improve the readability and conciseness of your queries, especially when dealing with complex joins, subqueries, or long table/column names. They provide a way to assign temporary names to objects within the query, making it easier to understand and maintain the code.