Friday, 29 November 2013

UNION Operator


The UNION operator is used to combine the result-set of two or more SELECT statements.

Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order.


SELECT column_name(s) FROM table1
 
UNION
 
SELECT column_name(s) FROM table2;


Note: The UNION operator selects only distinct values by default. To allow duplicate values, use the ALL keyword with UNION.


SELECT column_name(s) FROM table1
 
UNION ALL
 
SELECT column_name(s) FROM table2;











SQL Joins



Combine rows from two or more tables, based on a common field between them.



  • The most common type of join is: SQL INNER JOIN (simple join).
  • An SQL INNER JOIN return all rows from multiple tables where the join condition is met.

Ex:

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;




Different SQL JOINs

  • INNER JOIN: Returns all rows when there is at least one match in BOTH tables
  • LEFT JOIN: Return all rows from the left table, and the matched rows from the right table
  • RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table
  • FULL JOIN: Return all rows when there is a match in ONE of the tables





INNER JOIN Syntax

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;

SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;


SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;





SQL LEFT JOIN Syntax

The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.


SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;

SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;


SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;






RIGHT JOIN Keyword

The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match.

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;

SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name;


SELECT Orders.OrderID, Employees.FirstName
FROM Orders
RIGHT JOIN Employees
ON Orders.EmployeeID=Employees.EmployeeID
ORDER BY Orders.OrderID;






FULL OUTER JOIN Keyword


The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right table (table2).
The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.

SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;


SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName; 






















INSERT INTO SELECT Statement



selects data from one table and inserts it into an existing table.

We can copy all columns from one table to another, existing table:

INSERT INTO table2
SELECT * FROM table1;

INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Suppliers;

 

 We can copy only the columns we want to into another, existing table:
 
INSERT INTO table2
(column_name(s))
SELECT column_name(s)
FROM table1;

INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Suppliers
WHERE Country='Germany';





Thursday, 28 November 2013

SELECT INTO Statement



You can copy information from one table into another. 
 The SELECT INTO statement copies data from one table and inserts it into a new table.


SELECT *

INTO newtable [IN externaldb]
 
FROM table1;

- selects data from one table and inserts it into a new table. 


SELECT column_name(s)
 
INTO newtable [IN externaldb]
 
FROM table1; 

- copy only the columns we want into the new table. 




Qs:  Create a backup copy of Customers:

SELECT *
INTO CustomersBackup2013
FROM Customers; 

Qs: Use the IN clause to copy the table into another database: 

SELECT *
INTO CustomersBackup2013 IN 'Backup.mdb'
FROM Customers;

Qs: Copy only a few columns into the new table:

SELECT CustomerName, ContactName
INTO CustomersBackup2013
FROM Customers; 


Qs: Copy only the German customers into the new table:

SELECT *
INTO CustomersBackup2013
FROM Customers
WHERE Country='Germany';

Qs: Copy data from more than one table into the new table:

SELECT Customers.CustomerName, Orders.OrderID
INTO CustomersOrderBackup2013
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID;