SQL INNER JOIN Example

SQL INNER JOIN is used to fetch records from more than one table that too when both the table participating in join have matching records. SQL INNER JOIN is very useful and is widely used in any SQL environment. Let us see an example of inner join:

SELECT  CUST_ID,CUST_NAME, CUST_CITY, CUST_OPERATOR FROM CUSTOMERS;
SQL INNER JOIN - Table1
SQL INNER JOIN – Table1
SELECT ZONE_ID, CITY, STATE FROM OPRT_ZONE;
SQL INNER JOIN - Table2
SQL INNER JOIN – Table2

We need to fetch all the details of customers along with their STATE. We see that in table CUSTOMERS we don’t have STATE details however we have CITY details in the table. In second table OPRT_ZONE we have CITY as well as STATE details. So we can fetch all the details ; CUSTOMER and CITY details from CUSTOMERS table and CITY and STATE details from OPRT_ZONE table. We have CITY as common column in both the tables , so we can use this column in our INNER JOIN condition and retrieve all the required information. Remember, INNER JOIN will only fetch matching records and will discard those records which do not have any match in both the tables. If we need not discard such records, we should apply OUTER JOINs and not INNER JOIN.

SELECT CUST.CUST_ID, CUST.CUST_NAME, CUST.CUST_CITY, OZON.STATE, CUST.CUST_OPERATOR
FROM CUSTOMERS CUST
INNER JOIN OPRT_ZONE OZON
ON CUST.CUST_CITY = OZON.CITY;
SQL INNER JOIN - EXAMPLE
SQL INNER JOIN – EXAMPLE

Leave a Reply

Your email address will not be published. Required fields are marked *