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;
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;
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
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):
We have another table called orders with three fields (order_id, supplier_id, and order_date). It contains the following data:
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;
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.
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
|
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;
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_id | supplier_name |
---|---|
10000 | IBM |
10001 | Hewlett Packard |
10002 | Microsoft |
10003 | NVIDIA |
order_id | supplier_id | order_date |
---|---|---|
500125 | 10000 | 2003/05/12 |
500126 | 10001 | 2003/05/13 |
500127 | 10004 | 2003/05/14 |
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_id | name | order_date |
---|---|---|
10000 | IBM | 2003/05/12 |
10001 | Hewlett Packard | 2003/05/13 |
Hi Robin
ReplyDeleteThe example of Inner join u hv given is not correct as per below link
http://www.techonthenet.com/sql/joins.php
please check.
As per w3 schools, this is correct. Please follow this link http://www.w3schools.com/sql/sql_join.asp
ReplyDeleteI 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