Tuesday, January 21, 2014

23. Difference between Union and Joins?



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



Note 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.

The column names in the result-set of a UNION are usually equal to the column names in the first SELECT statement in the UNION.


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



An SQL JOIN clause is used to 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.

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



Orders Table

OrderID
CustomerID
OrderDate
10308
2
1996-09-18
10309
37
1996-09-19
10310
77
1996-09-20

Customers Table

CustomerID
CustomerName
ContactName
Country
1
Alfreds Futterkiste
Maria Anders
Germany
2
Ana Trujillo Emparedados y helados
Ana Trujillo
Mexico
3
Antonio Moreno Taquería
Antonio Moreno
Mexico


Notice that the "CustomerID" column in the "Orders" table refers to the customer in the "Customers" table. The relationship between the two tables above is the "CustomerID" column.

Then, if we run the following SQL statement (that contains an INNER JOIN):

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

OrderID
CustomerName
OrderDate
10308
Ana Trujillo Emparedados y helados
9/18/1996
10365
Antonio Moreno Taquería
11/27/1996
10383
Around the Horn
12/16/1996
10355
Around the Horn
11/15/1996
10278
Berglunds snabbköp
8/12/1996


Let's look at some data to explain how the INNER JOINS work:

We have a table called suppliers with two fields (supplier_id and supplier_ name). It contains the following data:
supplier_idsupplier_name
10000IBM
10001Hewlett Packard
10002Microsoft
10003NVIDIA
We have another table called orders with three fields (order_id, supplier_id, and order_date). It contains the following data:
order_idsupplier_idorder_date
500125100002003/05/12
500126100012003/05/13
500127100042003/05/14
If we run the SQL statement (that contains an INNER JOIN) below:
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date FROM suppliers INNER JOIN orders ON suppliers.supplier_id = orders.supplier_id;
Our result set would look like this:
supplier_idnameorder_date
10000IBM2003/05/12
10001Hewlett Packard2003/05/13
The rows for Microsoft and NVIDIA from the supplier table would be omitted, since the supplier_id's 10002 and 10003 do not exist in both tables. The row for 500127 (order_id) from the orders table would be omitted, since the supplier_id 10004 does not exist in the suppliers table.

3 comments:

  1. Hi Robin
    The example of Inner join u hv given is not correct as per below link
    http://www.techonthenet.com/sql/joins.php
    please check.

    ReplyDelete
  2. As per w3 schools, this is correct. Please follow this link http://www.w3schools.com/sql/sql_join.asp

    ReplyDelete
  3. I got your point, actually, the example mentioned above doesn't contain all the records of the table that's why after running the SQL, we got some different records which were earlier not there in Order and Customers table. The first example is quoted to show the relationship between the two tables and how the inner join is applied to return all rows from them where the join condition is met. I have added the second example to it which you mentioned. Thanks for your feedback.

    ReplyDelete