Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

UNION ALL

The UNION ALL clause/operator is used to combine the results of two or more select statements
Including duplicate rows/records.

Syntax:

SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]
UNION ALL
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]

Example:

SELECT * FROM [UserData]
UNION ALL
SELECT * FROM Employee


Union

The SQL UNION clause/operator is used to combine the results of two or more SELECT statements.

SQL UNION clause/operator removes all duplicate rows and returns only distinct rows.

Syntax:

SELECT column1 [, column2 ] ..
FROM table1 [, table2 ]
[WHERE condition]
UNION
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]


Example:

SELECT * FROM [UserData]
UNIO
SELECT * FROM [Employee]



SQL Aliases

Definition:

You can rename table name or table columns names temporarily known as alias.

Renaming is temporary name change there is not impact of table or column name changes on database.

Syntax:

SELECT columnName AS aliasName
FROM tableName as TableAliasName;


Example with right outer join:

SELECT ProductName AS [Product Name]
,UnitPrice AS [Price]
,CategoryName AS [Product Category],ImagePath AS [Image]
 FROM Products P
RIGHT OUTER JOIN Categories C ON P.CategoryID=C.CategoryID


Example with self join:

SELECT e1.*
FROM employee e1, employee e2
WHERE e1.Country = e2.Country
AND e2.EmployeeName='Nagnath';