Inner Joins or Equal Joins are joins which include only the rows where the values in the joined columns match.
You can code inner joins either by Implicit Syntax or Explicit Syntax.
SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name; |
SELECT column_name(s) FROM table1, table2 WHERE table1.column_name = table2.column_name; |
Let's look at the "Orders" table data below:
Orderid | Customernumber | Orderdate |
---|---|---|
1001 | 10 | 2020-09-08 |
1002 | 99 | 2020-09-01 |
1003 | 17 | 2020-08-25 |
1004 | 76 | 2020-09-19 |
1005 | 44 | 2020-09-25 |
Let's look at the "Customers" table data below:
Customernumber | Customername | Country |
---|---|---|
76 | Jack | America |
17 | Jancy | Germany |
20 | Carmen | Russia |
10 | Robert | India |
99 | Brian | China |
Notice that the "Customernumber" column in the "Orders" table refers to the "Customernumber" in the "Customers" table. The relationship between the two tables above is the "Customernumber" column.
Let us see how to create SQL statement that selects records that have matching values in both tables.
The following SQL statement selects all orders with customer information.
SELECT Orders.Orderid, Customers.Customername, Orders.Orderdate FROM Orders INNER JOIN Customers ON Orders.Customernumber=Customers.Customernumber; |
The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns. If there are records in the "Orders" table that do not have matches in "Customers", these orders will not be shown.
Orderid | Customername | Orderdate |
---|---|---|
1001 | Robert | 2020-09-08 |
1002 | Brian | 2020-09-01 |
1003 | Jancy | 2020-08-25 |
1004 | Jack | 2020-09-19 |
The following SQL statement is same as above example SQL statement.
SELECT Orders.Orderid, Customers.Customername, Orders.Orderdate FROM Orders, Customers Where Orders.Customernumber=Customers.Customernumber; |
If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!