Inner Join
Some parts in the output code are omitted for clarity reasons.
Description
An inner join (sometimes called a simple join) is a join of two or more tables that returns only those rows that satisfy the join condition. (Oracle SQL Reference Inner Join Subsection).
{ [ INNER ] JOIN table_reference
{ ON condition
| USING (column [, column ]...)
}
| { CROSS
| NATURAL [ INNER ]
}
JOIN table_reference
}
Sample Source Patterns
Order by clause added because the result order may vary between Oracle and Snowflake.
Since the result set is too large, Row Limiting Clause was added. You can remove this clause to retrieve the entire result set.
Check this section to set up the sample database.
Basic Inner Join
In the Inner Join clause "INNER" is an optional keyword, the following queries have two selects that retrieve the same data set.
Oracle
IN -> Oracle_01.sql
SELECT
*
FROM
hr.employees
INNER JOIN hr.departments ON
hr.departments.department_id = hr.employees.department_id
ORDER BY employee_id
FETCH NEXT 10 ROWS ONLY;
SELECT
*
FROM
hr.employees
JOIN hr.departments ON
hr.departments.department_id = hr.employees.department_id
ORDER BY employee_id
FETCH NEXT 10 ROWS ONLY;
|EMPLOYEE_ID|FIRST_NAME|LAST_NAME|EMAIL |PHONE_NUMBER|HIRE_DATE |JOB_ID |SALARY|COMMISSION_PCT|MANAGER_ID|DEPARTMENT_ID|DEPARTMENT_ID|DEPARTMENT_NAME|MANAGER_ID|LOCATION_ID|
|-----------|----------|---------|--------|------------|-----------------------|----------|------|--------------|----------|-------------|-------------|---------------|----------|-----------|
|100 |Steven |King |SKING |515.123.4567|2003-06-17 00:00:00.000|AD_PRES |24000 | | |90 |90 |Executive |100 |1700 |
|101 |Neena |Kochhar |NKOCHHAR|515.123.4568|2005-09-21 00:00:00.000|AD_VP |17000 | |100 |90 |90 |Executive |100 |1700 |
|102 |Lex |De Haan |LDEHAAN |515.123.4569|2001-01-13 00:00:00.000|AD_VP |17000 | |100 |90 |90 |Executive |100 |1700 |
|103 |Alexander |Hunold |AHUNOLD |590.423.4567|2006-01-03 00:00:00.000|IT_PROG |9000 | |102 |60 |60 |IT |103 |1400 |
|104 |Bruce |Ernst |BERNST |590.423.4568|2007-05-21 00:00:00.000|IT_PROG |6000 | |103 |60 |60 |IT |103 |1400 |
|105 |David |Austin |DAUSTIN |590.423.4569|2005-06-25 00:00:00.000|IT_PROG |4800 | |103 |60 |60 |IT |103 |1400 |
|106 |Valli |Pataballa|VPATABAL|590.423.4560|2006-02-05 00:00:00.000|IT_PROG |4800 | |103 |60 |60 |IT |103 |1400 |
|107 |Diana |Lorentz |DLORENTZ|590.423.5567|2007-02-07 00:00:00.000|IT_PROG |4200 | |103 |60 |60 |IT |103 |1400 |
|108 |Nancy |Greenberg|NGREENBE|515.124.4569|2002-08-17 00:00:00.000|FI_MGR |12008 | |101 |100 |100 |Finance |108 |1700 |
|109 |Daniel |Faviet |DFAVIET |515.124.4169|2002-08-16 00:00:00.000|FI_ACCOUNT|9000 | |108 |100 |100 |Finance |108 |1700 |
Snowflake
OUT -> Oracle_01.sql
SELECT
*
FROM
hr.employees
INNER JOIN
hr.departments
ON
hr.departments.department_id = hr.employees.department_id
ORDER BY employee_id
FETCH NEXT 10 ROWS ONLY;
SELECT
*
FROM
hr.employees
JOIN
hr.departments
ON
hr.departments.department_id = hr.employees.department_id
ORDER BY employee_id
FETCH NEXT 10 ROWS ONLY;
|EMPLOYEE_ID|FIRST_NAME|LAST_NAME|EMAIL |PHONE_NUMBER|HIRE_DATE |JOB_ID |SALARY |COMMISSION_PCT|MANAGER_ID|DEPARTMENT_ID|DEPARTMENT_ID|DEPARTMENT_NAME|MANAGER_ID|LOCATION_ID|
|-----------|----------|---------|--------|------------|----------|----------|--------|--------------|----------|-------------|-------------|---------------|----------|-----------|
|100 |Steven |King |SKING |515.123.4567|2003-06-17|AD_PRES |24000.00| | |90 |90 |Executive |100 |1700 |
|101 |Neena |Kochhar |NKOCHHAR|515.123.4568|2005-09-21|AD_VP |17000.00| |100 |90 |90 |Executive |100 |1700 |
|102 |Lex |De Haan |LDEHAAN |515.123.4569|2001-01-13|AD_VP |17000.00| |100 |90 |90 |Executive |100 |1700 |
|103 |Alexander |Hunold |AHUNOLD |590.423.4567|2006-01-03|IT_PROG |9000.00 | |102 |60 |60 |IT |103 |1400 |
|104 |Bruce |Ernst |BERNST |590.423.4568|2007-05-21|IT_PROG |6000.00 | |103 |60 |60 |IT |103 |1400 |
|105 |David |Austin |DAUSTIN |590.423.4569|2005-06-25|IT_PROG |4800.00 | |103 |60 |60 |IT |103 |1400 |
|106 |Valli |Pataballa|VPATABAL|590.423.4560|2006-02-05|IT_PROG |4800.00 | |103 |60 |60 |IT |103 |1400 |
|107 |Diana |Lorentz |DLORENTZ|590.423.5567|2007-02-07|IT_PROG |4200.00 | |103 |60 |60 |IT |103 |1400 |
|108 |Nancy |Greenberg|NGREENBE|515.124.4569|2002-08-17|FI_MGR |12008.00| |101 |100 |100 |Finance |108 |1700 |
|109 |Daniel |Faviet |DFAVIET |515.124.4169|2002-08-16|FI_ACCOUNT|9000.00 | |108 |100 |100 |Finance |108 |1700 |
Inner Join with using clause
Oracle
IN -> Oracle_02.sql
SELECT
*
FROM
hr.employees
INNER JOIN hr.departments
USING(department_id)
ORDER BY employee_id
FETCH NEXT 10 ROWS ONLY;
|DEPARTMENT_ID|EMPLOYEE_ID|FIRST_NAME|LAST_NAME|EMAIL |PHONE_NUMBER|HIRE_DATE |JOB_ID |SALARY|COMMISSION_PCT|MANAGER_ID|DEPARTMENT_NAME|MANAGER_ID|LOCATION_ID|
|-------------|-----------|----------|---------|--------|------------|-----------------------|----------|------|--------------|----------|---------------|----------|-----------|
|90 |100 |Steven |King |SKING |515.123.4567|2003-06-17 00:00:00.000|AD_PRES |24000 | | |Executive |100 |1700 |
|90 |101 |Neena |Kochhar |NKOCHHAR|515.123.4568|2005-09-21 00:00:00.000|AD_VP |17000 | |100 |Executive |100 |1700 |
|90 |102 |Lex |De Haan |LDEHAAN |515.123.4569|2001-01-13 00:00:00.000|AD_VP |17000 | |100 |Executive |100 |1700 |
|60 |103 |Alexander |Hunold |AHUNOLD |590.423.4567|2006-01-03 00:00:00.000|IT_PROG |9000 | |102 |IT |103 |1400 |
|60 |104 |Bruce |Ernst |BERNST |590.423.4568|2007-05-21 00:00:00.000|IT_PROG |6000 | |103 |IT |103 |1400 |
|60 |105 |David |Austin |DAUSTIN |590.423.4569|2005-06-25 00:00:00.000|IT_PROG |4800 | |103 |IT |103 |1400 |
|60 |106 |Valli |Pataballa|VPATABAL|590.423.4560|2006-02-05 00:00:00.000|IT_PROG |4800 | |103 |IT |103 |1400 |
|60 |107 |Diana |Lorentz |DLORENTZ|590.423.5567|2007-02-07 00:00:00.000|IT_PROG |4200 | |103 |IT |103 |1400 |
|100 |108 |Nancy |Greenberg|NGREENBE|515.124.4569|2002-08-17 00:00:00.000|FI_MGR |12008 | |101 |Finance |108 |1700 |