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

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';

SQL IN operator

SQL IN

IN operator allows you to specify multiple values in where clause and retrieve data matching a list of values.

Syntax:

SELECT column_1,column_2..column n
FROM table_name
WHERE some_column IN (value1,value2,...);

Example :

SELECT  * FROM Categories WHERE CategoryID in(1,2,3) 

Example with sub-query :

SELECT  * FROM Products WHERE CategoryID in(SELECT CategoryID FROM Categories WHERE CategoryName='books')

Result set: