JOINS

If you’re reading this, it’s likely that you’ve acquired an understanding of basic SELECT and FROM clauses. It’s also likely upon doing so, you have uncovered a hard truth: to solve many real-world problems, sourcing of data from multiple tables is required. This is where the JOIN clause comes into play.

The JOIN clause allows you to combine rows from two or more tables based on one or more related columns within the tables. Within this clause, instructions for combining the rows from the respective tables is required. These instructions will be specified within an ON clause by providing the table names, related column, and condition(s) required. It’s important to note there are six join types: CROSS, FULL, LEFT, INNER, RIGHT, and SELF. When the type is not explicitly mentioned, it is understood to be an INNER JOIN. For the purposes of this discussion and to first examine the most commonly used join type, we’ll consider the default join type, INNER and use the JOIN keyword alone. I’ve created separate pages dedicated to discussing each join type. To jump to those dedicated pages, use the links below or expand the JOINs section of the SQL Fundamentals sub-menu and choose the desired page.

CROSS JOIN
FULL JOIN
LEFT JOIN
INNER JOIN
RIGHT JOIN
SELF JOIN

JOINs can be confusing without a concrete example including data, visuals, and a real-world problem, so let’s introduce those before continuing further.

Example Data
In our example, there exists two tables, employees and jobs. The employees table contains one row per employee and within each row, data associated with the employee is stored. Similarly, the jobs table contains one row per job and within each row, data associated with the job is stored. For clarity, I’ve provided each table’s structure and example data below. The schema, corresponding tables, and data can be found at livesql.oracle.com.

Employees

employee_idfirst_namelast_nameemailphone_numberhire_datejob_idsalarycommission_pctmanager_iddepartment_id
100StevenKingSKING515.123.456717-Jun-03AD_PRES2400090
101NeenaKochharNKOCHHAR515.123.456821-Sep-05AD_VP1700010090
102LexDe HaanLDEHAAN515.123.456913-Jan-01AD_VP1700010090
103AlexanderHunoldAHUNOLD590.423.45673-Jan-06IT_PROG900010260
104BruceErnstBERNST590.423.456821-May-07IT_PROG600010360
105DavidAustinDAUSTIN590.423.456925-Jun-05IT_PROG480010360
106ValliPataballaVPATABAL590.423.45605-Feb-06IT_PROG480010360
107DianaLorentzDLORENTZ590.423.55677-Feb-07IT_PROG420010360
108NancyGreenbergNGREENBE515.124.456917-Aug-02FI_MGR12008101100
109DanielFavietDFAVIET515.124.416916-Aug-02FI_ACCOUNT9000108100
110JohnChenJCHEN515.124.426928-Sep-05FI_ACCOUNT8200108100
111IsmaelSciarraISCIARRA515.124.436930-Sep-05FI_ACCOUNT7700108100
112Jose ManuelUrmanJMURMAN515.124.44697-Mar-06FI_ACCOUNT7800108100
113LuisPoppLPOPP515.124.45677-Dec-07FI_ACCOUNT6900108100
114DenRaphaelyDRAPHEAL515.127.45617-Dec-02PU_MAN1100010030
115AlexanderKhooAKHOO515.127.456218-May-03PU_CLERK310011430
116ShelliBaidaSBAIDA515.127.456324-Dec-05PU_CLERK290011430
117SigalTobiasSTOBIAS515.127.456424-Jul-05PU_CLERK280011430
118GuyHimuroGHIMURO515.127.456515-Nov-06PU_CLERK260011430
119KarenColmenaresKCOLMENA515.127.456610-Aug-07PU_CLERK250011430
120MatthewWeissMWEISS650.123.123418-Jul-04ST_MAN800010050
121AdamFrippAFRIPP650.123.223410-Apr-05ST_MAN820010050
122PayamKauflingPKAUFLIN650.123.32341-May-03ST_MAN790010050
123ShantaVollmanSVOLLMAN650.123.423410-Oct-05ST_MAN650010050
124KevinMourgosKMOURGOS650.123.523416-Nov-07ST_MAN580010050
125JuliaNayerJNAYER650.124.121416-Jul-05ST_CLERK320012050
126IreneMikkilineniIMIKKILI650.124.122428-Sep-06ST_CLERK270012050
127JamesLandryJLANDRY650.124.133414-Jan-07ST_CLERK240012050
128StevenMarkleSMARKLE650.124.14348-Mar-08ST_CLERK220012050
129LauraBissotLBISSOT650.124.523420-Aug-05ST_CLERK330012150
130MozheAtkinsonMATKINSO650.124.623430-Oct-05ST_CLERK280012150
131JamesMarlowJAMRLOW650.124.723416-Feb-05ST_CLERK250012150
132TJOlsonTJOLSON650.124.823410-Apr-07ST_CLERK210012150
133JasonMallinJMALLIN650.127.193414-Jun-04ST_CLERK330012250
134MichaelRogersMROGERS650.127.183426-Aug-06ST_CLERK290012250
135KiGeeKGEE650.127.173412-Dec-07ST_CLERK240012250
136HazelPhiltankerHPHILTAN650.127.16346-Feb-08ST_CLERK220012250
137RenskeLadwigRLADWIG650.121.123414-Jul-03ST_CLERK360012350
138StephenStilesSSTILES650.121.203426-Oct-05ST_CLERK320012350
139JohnSeoJSEO650.121.201912-Feb-06ST_CLERK270012350
140JoshuaPatelJPATEL650.121.18346-Apr-06ST_CLERK250012350
141TrennaRajsTRAJS650.121.800917-Oct-03ST_CLERK350012450
142CurtisDaviesCDAVIES650.121.299429-Jan-05ST_CLERK310012450
143RandallMatosRMATOS650.121.287415-Mar-06ST_CLERK260012450
144PeterVargasPVARGAS650.121.20049-Jul-06ST_CLERK250012450
145JohnRussellJRUSSEL011.44.1344.4292681-Oct-04SA_MAN140000.410080
146KarenPartnersKPARTNER011.44.1344.4672685-Jan-05SA_MAN135000.310080
147AlbertoErrazurizAERRAZUR011.44.1344.42927810-Mar-05SA_MAN120000.310080
148GeraldCambraultGCAMBRAU011.44.1344.61926815-Oct-07SA_MAN110000.310080
149EleniZlotkeyEZLOTKEY011.44.1344.42901829-Jan-08SA_MAN105000.210080
150PeterTuckerPTUCKER011.44.1344.12926830-Jan-05SA_REP100000.314580
151DavidBernsteinDBERNSTE011.44.1344.34526824-Mar-05SA_REP95000.2514580
152PeterHallPHALL011.44.1344.47896820-Aug-05SA_REP90000.2514580
153ChristopherOlsenCOLSEN011.44.1344.49871830-Mar-06SA_REP80000.214580
154NanetteCambraultNCAMBRAU011.44.1344.9876689-Dec-06SA_REP75000.214580
155OliverTuvaultOTUVAULT011.44.1344.48650823-Nov-07SA_REP70000.1514580
156JanetteKingJKING011.44.1345.42926830-Jan-04SA_REP100000.3514680
157PatrickSullyPSULLY011.44.1345.9292684-Mar-04SA_REP95000.3514680
158AllanMcEwenAMCEWEN011.44.1345.8292681-Aug-04SA_REP90000.3514680
159LindseySmithLSMITH011.44.1345.72926810-Mar-05SA_REP80000.314680
160LouiseDoranLDORAN011.44.1345.62926815-Dec-05SA_REP75000.314680
161SarathSewallSSEWALL011.44.1345.5292683-Nov-06SA_REP70000.2514680
162ClaraVishneyCVISHNEY011.44.1346.12926811-Nov-05SA_REP105000.2514780
163DanielleGreeneDGREENE011.44.1346.22926819-Mar-07SA_REP95000.1514780
164MatteaMarvinsMMARVINS011.44.1346.32926824-Jan-08SA_REP72000.114780
165DavidLeeDLEE011.44.1346.52926823-Feb-08SA_REP68000.114780
166SundarAndeSANDE011.44.1346.62926824-Mar-08SA_REP64000.114780
167AmitBandaABANDA011.44.1346.72926821-Apr-08SA_REP62000.114780
168LisaOzerLOZER011.44.1343.92926811-Mar-05SA_REP115000.2514880
169HarrisonBloomHBLOOM011.44.1343.82926823-Mar-06SA_REP100000.214880
170TaylerFoxTFOX011.44.1343.72926824-Jan-06SA_REP96000.214880
171WilliamSmithWSMITH011.44.1343.62926823-Feb-07SA_REP74000.1514880
172ElizabethBatesEBATES011.44.1343.52926824-Mar-07SA_REP73000.1514880
173SunditaKumarSKUMAR011.44.1343.32926821-Apr-08SA_REP61000.114880
174EllenAbelEABEL011.44.1644.42926711-May-04SA_REP110000.314980
175AlyssaHuttonAHUTTON011.44.1644.42926619-Mar-05SA_REP88000.2514980
176JonathonTaylorJTAYLOR011.44.1644.42926524-Mar-06SA_REP86000.214980
177JackLivingstonJLIVINGS011.44.1644.42926423-Apr-06SA_REP84000.214980
178KimberelyGrantKGRANT011.44.1644.42926324-May-07SA_REP70000.15149
179CharlesJohnsonCJOHNSON011.44.1644.4292624-Jan-08SA_REP62000.114980
180WinstonTaylorWTAYLOR650.507.987624-Jan-06SH_CLERK320012050
181JeanFleaurJFLEAUR650.507.987723-Feb-06SH_CLERK310012050
182MarthaSullivanMSULLIVA650.507.987821-Jun-07SH_CLERK250012050
183GirardGeoniGGEONI650.507.98793-Feb-08SH_CLERK280012050
184NanditaSarchandNSARCHAN650.509.187627-Jan-04SH_CLERK420012150
185AlexisBullABULL650.509.287620-Feb-05SH_CLERK410012150
186JuliaDellingerJDELLING650.509.387624-Jun-06SH_CLERK340012150
187AnthonyCabrioACABRIO650.509.48767-Feb-07SH_CLERK300012150
188KellyChungKCHUNG650.505.187614-Jun-05SH_CLERK380012250
189JenniferDillyJDILLY650.505.287613-Aug-05SH_CLERK360012250
190TimothyGatesTGATES650.505.387611-Jul-06SH_CLERK290012250
191RandallPerkinsRPERKINS650.505.487619-Dec-07SH_CLERK250012250
192SarahBellSBELL650.501.18764-Feb-04SH_CLERK400012350
193BritneyEverettBEVERETT650.501.28763-Mar-05SH_CLERK390012350
194SamuelMcCainSMCCAIN650.501.38761-Jul-06SH_CLERK320012350
195VanceJonesVJONES650.501.487617-Mar-07SH_CLERK280012350
196AlanaWalshAWALSH650.507.981124-Apr-06SH_CLERK310012450
197KevinFeeneyKFEENEY650.507.982223-May-06SH_CLERK300012450
198DonaldOConnellDOCONNEL650.507.983321-Jun-07SH_CLERK260012450
199DouglasGrantDGRANT650.507.984413-Jan-08SH_CLERK260012450
200JenniferWhalenJWHALEN515.123.444417-Sep-03AD_ASST440010110
201MichaelHartsteinMHARTSTE515.123.555517-Feb-04MK_MAN1300010020
202PatFayPFAY603.123.666617-Aug-05MK_REP600020120
203SusanMavrisSMAVRIS515.123.77777-Jun-02HR_REP650010140
204HermannBaerHBAER515.123.88887-Jun-02PR_REP1000010170
205ShelleyHigginsSHIGGINS515.123.80807-Jun-02AC_MGR12008101110
206WilliamGietzWGIETZ515.123.81817-Jun-02AC_ACCOUNT8300205110

Jobs

job_idjob_titlemin_salarymax_salary
AD_PRESPresident2008040000
AD_VPAdministration Vice President1500030000
AD_ASSTAdministration Assistant30006000
FI_MGRFinance Manager820016000
FI_ACCOUNTAccountant42009000
AC_MGRAccounting Manager820016000
AC_ACCOUNTPublic Accountant42009000
SA_MANSales Manager1000020080
SA_REPSales Representative600012008
PU_MANPurchasing Manager800015000
PU_CLERKPurchasing Clerk25005500
ST_MANStock Manager55008500
ST_CLERKStock Clerk20085000
SH_CLERKShipping Clerk25005500
IT_PROGProgrammer400010000
MK_MANMarketing Manager900015000
MK_REPMarketing Representative40009000
HR_REPHuman Resources Representative40009000
PR_REPPublic Relations Representative450010500

Real-world Problem
Build a report containing each employee’s ID, job ID, job title, salary, and the minimum and maximum salary associated with the employee’s job.

Let’s now return to developing an understanding on JOINs and how this concept will help you solve the real-world problem above.

JOINs Continued
Based on the example data above, the columns required in the report are spread out across two tables, employees and jobs. These tables have a related column, job ID. This is not a coincidence. The tables have been purposely designed this way to minimize redundancy and permit the lookup of each employee’s job ID and data associated with each job within the jobs table. This is known as a primary key–foreign key relationship. The job ID serves as a primary key in the jobs table and as a foreign key in the employees table. Additionally, each job ID within the jobs table can be found within zero or more rows in the employees table and each employee’s job ID can be found within exactly one row in the jobs table. The plain English translation is an employee is assigned one and only one job and a job is assigned to zero or more employees. We’ll take advantage of this relationship in solving the example problem.

Solving the Problem Using JOINs
We’ll combine the two tables’ rows using the JOIN and ON clauses. The basic syntax for the JOIN clause is below.

schema_1.table_1 JOIN schema_2.table_2

By replacing the tables on the left and right side of the JOIN keyword, we have the following:

hr.employees JOIN hr.jobs

The next step is to specify the condition or criteria for combining the rows from each table in the ON clause. The basic syntax for the ON clause is below.

ON schema_1.table_1.column_1 = schema_2.table_2.column_2

For our problem, we are interested in retrieving the details associated with each employee’s job ID value. Therefore, we’ll combine the rows from the employees and jobs tables based on matching job ID values by specifying so in the ON clause.

ON hr.employees.job_id = hr.jobs.job_id

As a result of the joining operation, each employee’s associated job details from the jobs table are accessible within the rows. The code and corresponding output is below.

SELECT
    hr.employees.*,
    hr.jobs.*
FROM
    hr.employees
    JOIN
    hr.jobs
    	ON hr.employees.job_id = hr.jobs.job_id;
employee_idfirst_namelast_nameemailphone_numberhire_datejob_idsalarycommission_pctmanager_iddepartment_idjob_idjob_titlemin_salarymax_salary
206WilliamGietzWGIETZ515.123.81817-Jun-02AC_ACCOUNT8300205110AC_ACCOUNTPublic Accountant42009000
205ShelleyHigginsSHIGGINS515.123.80807-Jun-02AC_MGR12008101110AC_MGRAccounting Manager820016000
200JenniferWhalenJWHALEN515.123.444417-Sep-03AD_ASST440010110AD_ASSTAdministration Assistant30006000
100StevenKingSKING515.123.456717-Jun-03AD_PRES2400090AD_PRESPresident2008040000
102LexDe HaanLDEHAAN515.123.456913-Jan-01AD_VP1700010090AD_VPAdministration Vice President1500030000
101NeenaKochharNKOCHHAR515.123.456821-Sep-05AD_VP1700010090AD_VPAdministration Vice President1500030000
110JohnChenJCHEN515.124.426928-Sep-05FI_ACCOUNT8200108100FI_ACCOUNTAccountant42009000
109DanielFavietDFAVIET515.124.416916-Aug-02FI_ACCOUNT9000108100FI_ACCOUNTAccountant42009000
113LuisPoppLPOPP515.124.45677-Dec-07FI_ACCOUNT6900108100FI_ACCOUNTAccountant42009000
111IsmaelSciarraISCIARRA515.124.436930-Sep-05FI_ACCOUNT7700108100FI_ACCOUNTAccountant42009000
112Jose ManuelUrmanJMURMAN515.124.44697-Mar-06FI_ACCOUNT7800108100FI_ACCOUNTAccountant42009000
108NancyGreenbergNGREENBE515.124.456917-Aug-02FI_MGR12008101100FI_MGRFinance Manager820016000
203SusanMavrisSMAVRIS515.123.77777-Jun-02HR_REP650010140HR_REPHuman Resources Representative40009000
103AlexanderHunoldAHUNOLD590.423.45673-Jan-06IT_PROG900010260IT_PROGProgrammer400010000
104BruceErnstBERNST590.423.456821-May-07IT_PROG600010360IT_PROGProgrammer400010000
107DianaLorentzDLORENTZ590.423.55677-Feb-07IT_PROG420010360IT_PROGProgrammer400010000
106ValliPataballaVPATABAL590.423.45605-Feb-06IT_PROG480010360IT_PROGProgrammer400010000
105DavidAustinDAUSTIN590.423.456925-Jun-05IT_PROG480010360IT_PROGProgrammer400010000
201MichaelHartsteinMHARTSTE515.123.555517-Feb-04MK_MAN1300010020MK_MANMarketing Manager900015000
202PatFayPFAY603.123.666617-Aug-05MK_REP600020120MK_REPMarketing Representative40009000
204HermannBaerHBAER515.123.88887-Jun-02PR_REP1000010170PR_REPPublic Relations Representative450010500
116ShelliBaidaSBAIDA515.127.456324-Dec-05PU_CLERK290011430PU_CLERKPurchasing Clerk25005500
115AlexanderKhooAKHOO515.127.456218-May-03PU_CLERK310011430PU_CLERKPurchasing Clerk25005500
117SigalTobiasSTOBIAS515.127.456424-Jul-05PU_CLERK280011430PU_CLERKPurchasing Clerk25005500
118GuyHimuroGHIMURO515.127.456515-Nov-06PU_CLERK260011430PU_CLERKPurchasing Clerk25005500
119KarenColmenaresKCOLMENA515.127.456610-Aug-07PU_CLERK250011430PU_CLERKPurchasing Clerk25005500
114DenRaphaelyDRAPHEAL515.127.45617-Dec-02PU_MAN1100010030PU_MANPurchasing Manager800015000
148GeraldCambraultGCAMBRAU011.44.1344.61926815-Oct-07SA_MAN110000.310080SA_MANSales Manager1000020080
147AlbertoErrazurizAERRAZUR011.44.1344.42927810-Mar-05SA_MAN120000.310080SA_MANSales Manager1000020080
146KarenPartnersKPARTNER011.44.1344.4672685-Jan-05SA_MAN135000.310080SA_MANSales Manager1000020080
145JohnRussellJRUSSEL011.44.1344.4292681-Oct-04SA_MAN140000.410080SA_MANSales Manager1000020080
149EleniZlotkeyEZLOTKEY011.44.1344.42901829-Jan-08SA_MAN105000.210080SA_MANSales Manager1000020080
155OliverTuvaultOTUVAULT011.44.1344.48650823-Nov-07SA_REP70000.1514580SA_REPSales Representative600012008
156JanetteKingJKING011.44.1345.42926830-Jan-04SA_REP100000.3514680SA_REPSales Representative600012008
157PatrickSullyPSULLY011.44.1345.9292684-Mar-04SA_REP95000.3514680SA_REPSales Representative600012008
158AllanMcEwenAMCEWEN011.44.1345.8292681-Aug-04SA_REP90000.3514680SA_REPSales Representative600012008
159LindseySmithLSMITH011.44.1345.72926810-Mar-05SA_REP80000.314680SA_REPSales Representative600012008
160LouiseDoranLDORAN011.44.1345.62926815-Dec-05SA_REP75000.314680SA_REPSales Representative600012008
161SarathSewallSSEWALL011.44.1345.5292683-Nov-06SA_REP70000.2514680SA_REPSales Representative600012008
179CharlesJohnsonCJOHNSON011.44.1644.4292624-Jan-08SA_REP62000.114980SA_REPSales Representative600012008
163DanielleGreeneDGREENE011.44.1346.22926819-Mar-07SA_REP95000.1514780SA_REPSales Representative600012008
164MatteaMarvinsMMARVINS011.44.1346.32926824-Jan-08SA_REP72000.114780SA_REPSales Representative600012008
165DavidLeeDLEE011.44.1346.52926823-Feb-08SA_REP68000.114780SA_REPSales Representative600012008
166SundarAndeSANDE011.44.1346.62926824-Mar-08SA_REP64000.114780SA_REPSales Representative600012008
167AmitBandaABANDA011.44.1346.72926821-Apr-08SA_REP62000.114780SA_REPSales Representative600012008
168LisaOzerLOZER011.44.1343.92926811-Mar-05SA_REP115000.2514880SA_REPSales Representative600012008
169HarrisonBloomHBLOOM011.44.1343.82926823-Mar-06SA_REP100000.214880SA_REPSales Representative600012008
170TaylerFoxTFOX011.44.1343.72926824-Jan-06SA_REP96000.214880SA_REPSales Representative600012008
171WilliamSmithWSMITH011.44.1343.62926823-Feb-07SA_REP74000.1514880SA_REPSales Representative600012008
172ElizabethBatesEBATES011.44.1343.52926824-Mar-07SA_REP73000.1514880SA_REPSales Representative600012008
173SunditaKumarSKUMAR011.44.1343.32926821-Apr-08SA_REP61000.114880SA_REPSales Representative600012008
174EllenAbelEABEL011.44.1644.42926711-May-04SA_REP110000.314980SA_REPSales Representative600012008
175AlyssaHuttonAHUTTON011.44.1644.42926619-Mar-05SA_REP88000.2514980SA_REPSales Representative600012008
176JonathonTaylorJTAYLOR011.44.1644.42926524-Mar-06SA_REP86000.214980SA_REPSales Representative600012008
177JackLivingstonJLIVINGS011.44.1644.42926423-Apr-06SA_REP84000.214980SA_REPSales Representative600012008
178KimberelyGrantKGRANT011.44.1644.42926324-May-07SA_REP70000.15149SA_REPSales Representative600012008
154NanetteCambraultNCAMBRAU011.44.1344.9876689-Dec-06SA_REP75000.214580SA_REPSales Representative600012008
153ChristopherOlsenCOLSEN011.44.1344.49871830-Mar-06SA_REP80000.214580SA_REPSales Representative600012008
152PeterHallPHALL011.44.1344.47896820-Aug-05SA_REP90000.2514580SA_REPSales Representative600012008
151DavidBernsteinDBERNSTE011.44.1344.34526824-Mar-05SA_REP95000.2514580SA_REPSales Representative600012008
150PeterTuckerPTUCKER011.44.1344.12926830-Jan-05SA_REP100000.314580SA_REPSales Representative600012008
162ClaraVishneyCVISHNEY011.44.1346.12926811-Nov-05SA_REP105000.2514780SA_REPSales Representative600012008
184NanditaSarchandNSARCHAN650.509.187627-Jan-04SH_CLERK420012150SH_CLERKShipping Clerk25005500
185AlexisBullABULL650.509.287620-Feb-05SH_CLERK410012150SH_CLERKShipping Clerk25005500
186JuliaDellingerJDELLING650.509.387624-Jun-06SH_CLERK340012150SH_CLERKShipping Clerk25005500
187AnthonyCabrioACABRIO650.509.48767-Feb-07SH_CLERK300012150SH_CLERKShipping Clerk25005500
188KellyChungKCHUNG650.505.187614-Jun-05SH_CLERK380012250SH_CLERKShipping Clerk25005500
189JenniferDillyJDILLY650.505.287613-Aug-05SH_CLERK360012250SH_CLERKShipping Clerk25005500
190TimothyGatesTGATES650.505.387611-Jul-06SH_CLERK290012250SH_CLERKShipping Clerk25005500
191RandallPerkinsRPERKINS650.505.487619-Dec-07SH_CLERK250012250SH_CLERKShipping Clerk25005500
192SarahBellSBELL650.501.18764-Feb-04SH_CLERK400012350SH_CLERKShipping Clerk25005500
193BritneyEverettBEVERETT650.501.28763-Mar-05SH_CLERK390012350SH_CLERKShipping Clerk25005500
194SamuelMcCainSMCCAIN650.501.38761-Jul-06SH_CLERK320012350SH_CLERKShipping Clerk25005500
195VanceJonesVJONES650.501.487617-Mar-07SH_CLERK280012350SH_CLERKShipping Clerk25005500
196AlanaWalshAWALSH650.507.981124-Apr-06SH_CLERK310012450SH_CLERKShipping Clerk25005500
197KevinFeeneyKFEENEY650.507.982223-May-06SH_CLERK300012450SH_CLERKShipping Clerk25005500
198DonaldOConnellDOCONNEL650.507.983321-Jun-07SH_CLERK260012450SH_CLERKShipping Clerk25005500
199DouglasGrantDGRANT650.507.984413-Jan-08SH_CLERK260012450SH_CLERKShipping Clerk25005500
180WinstonTaylorWTAYLOR650.507.987624-Jan-06SH_CLERK320012050SH_CLERKShipping Clerk25005500
181JeanFleaurJFLEAUR650.507.987723-Feb-06SH_CLERK310012050SH_CLERKShipping Clerk25005500
182MarthaSullivanMSULLIVA650.507.987821-Jun-07SH_CLERK250012050SH_CLERKShipping Clerk25005500
183GirardGeoniGGEONI650.507.98793-Feb-08SH_CLERK280012050SH_CLERKShipping Clerk25005500
132TJOlsonTJOLSON650.124.823410-Apr-07ST_CLERK210012150ST_CLERKStock Clerk20085000
143RandallMatosRMATOS650.121.287415-Mar-06ST_CLERK260012450ST_CLERKStock Clerk20085000
142CurtisDaviesCDAVIES650.121.299429-Jan-05ST_CLERK310012450ST_CLERKStock Clerk20085000
141TrennaRajsTRAJS650.121.800917-Oct-03ST_CLERK350012450ST_CLERKStock Clerk20085000
140JoshuaPatelJPATEL650.121.18346-Apr-06ST_CLERK250012350ST_CLERKStock Clerk20085000
125JuliaNayerJNAYER650.124.121416-Jul-05ST_CLERK320012050ST_CLERKStock Clerk20085000
126IreneMikkilineniIMIKKILI650.124.122428-Sep-06ST_CLERK270012050ST_CLERKStock Clerk20085000
127JamesLandryJLANDRY650.124.133414-Jan-07ST_CLERK240012050ST_CLERKStock Clerk20085000
128StevenMarkleSMARKLE650.124.14348-Mar-08ST_CLERK220012050ST_CLERKStock Clerk20085000
129LauraBissotLBISSOT650.124.523420-Aug-05ST_CLERK330012150ST_CLERKStock Clerk20085000
130MozheAtkinsonMATKINSO650.124.623430-Oct-05ST_CLERK280012150ST_CLERKStock Clerk20085000
131JamesMarlowJAMRLOW650.124.723416-Feb-05ST_CLERK250012150ST_CLERKStock Clerk20085000
144PeterVargasPVARGAS650.121.20049-Jul-06ST_CLERK250012450ST_CLERKStock Clerk20085000
133JasonMallinJMALLIN650.127.193414-Jun-04ST_CLERK330012250ST_CLERKStock Clerk20085000
134MichaelRogersMROGERS650.127.183426-Aug-06ST_CLERK290012250ST_CLERKStock Clerk20085000
135KiGeeKGEE650.127.173412-Dec-07ST_CLERK240012250ST_CLERKStock Clerk20085000
136HazelPhiltankerHPHILTAN650.127.16346-Feb-08ST_CLERK220012250ST_CLERKStock Clerk20085000
137RenskeLadwigRLADWIG650.121.123414-Jul-03ST_CLERK360012350ST_CLERKStock Clerk20085000
138StephenStilesSSTILES650.121.203426-Oct-05ST_CLERK320012350ST_CLERKStock Clerk20085000
139JohnSeoJSEO650.121.201912-Feb-06ST_CLERK270012350ST_CLERKStock Clerk20085000
124KevinMourgosKMOURGOS650.123.523416-Nov-07ST_MAN580010050ST_MANStock Manager55008500
123ShantaVollmanSVOLLMAN650.123.423410-Oct-05ST_MAN650010050ST_MANStock Manager55008500
122PayamKauflingPKAUFLIN650.123.32341-May-03ST_MAN790010050ST_MANStock Manager55008500
121AdamFrippAFRIPP650.123.223410-Apr-05ST_MAN820010050ST_MANStock Manager55008500
120MatthewWeissMWEISS650.123.123418-Jul-04ST_MAN800010050ST_MANStock Manager55008500

You’ll notice that the output includes more columns than are required to meet the reporting requirements. To adhere to the best practice of retrieving the minimal amount of data to solve a problem or address a task, I’ve adjusted the code slightly to retrieve only the data we need. The updated code and output follow.

SELECT
    hr.employees.employee_id,
    hr.employees.job_id,
    hr.jobs.job_title,
    hr.employees.salary,
    hr.jobs.min_salary,
    hr.jobs.max_salary
FROM
    hr.employees
    JOIN
    hr.jobs
    	ON hr.employees.job_id = hr.jobs.job_id;
employee_idjob_idjob_titlesalarymin_salarymax_salary
206AC_ACCOUNTPublic Accountant830042009000
205AC_MGRAccounting Manager12008820016000
200AD_ASSTAdministration Assistant440030006000
100AD_PRESPresident240002008040000
102AD_VPAdministration Vice President170001500030000
101AD_VPAdministration Vice President170001500030000
110FI_ACCOUNTAccountant820042009000
109FI_ACCOUNTAccountant900042009000
113FI_ACCOUNTAccountant690042009000
111FI_ACCOUNTAccountant770042009000
112FI_ACCOUNTAccountant780042009000
108FI_MGRFinance Manager12008820016000
203HR_REPHuman Resources Representative650040009000
103IT_PROGProgrammer9000400010000
104IT_PROGProgrammer6000400010000
107IT_PROGProgrammer4200400010000
106IT_PROGProgrammer4800400010000
105IT_PROGProgrammer4800400010000
201MK_MANMarketing Manager13000900015000
202MK_REPMarketing Representative600040009000
204PR_REPPublic Relations Representative10000450010500
116PU_CLERKPurchasing Clerk290025005500
115PU_CLERKPurchasing Clerk310025005500
117PU_CLERKPurchasing Clerk280025005500
118PU_CLERKPurchasing Clerk260025005500
119PU_CLERKPurchasing Clerk250025005500
114PU_MANPurchasing Manager11000800015000
148SA_MANSales Manager110001000020080
147SA_MANSales Manager120001000020080
146SA_MANSales Manager135001000020080
145SA_MANSales Manager140001000020080
149SA_MANSales Manager105001000020080
155SA_REPSales Representative7000600012008
156SA_REPSales Representative10000600012008
157SA_REPSales Representative9500600012008
158SA_REPSales Representative9000600012008
159SA_REPSales Representative8000600012008
160SA_REPSales Representative7500600012008
161SA_REPSales Representative7000600012008
179SA_REPSales Representative6200600012008
163SA_REPSales Representative9500600012008
164SA_REPSales Representative7200600012008
165SA_REPSales Representative6800600012008
166SA_REPSales Representative6400600012008
167SA_REPSales Representative6200600012008
168SA_REPSales Representative11500600012008
169SA_REPSales Representative10000600012008
170SA_REPSales Representative9600600012008
171SA_REPSales Representative7400600012008
172SA_REPSales Representative7300600012008
173SA_REPSales Representative6100600012008
174SA_REPSales Representative11000600012008
175SA_REPSales Representative8800600012008
176SA_REPSales Representative8600600012008
177SA_REPSales Representative8400600012008
178SA_REPSales Representative7000600012008
154SA_REPSales Representative7500600012008
153SA_REPSales Representative8000600012008
152SA_REPSales Representative9000600012008
151SA_REPSales Representative9500600012008
150SA_REPSales Representative10000600012008
162SA_REPSales Representative10500600012008
184SH_CLERKShipping Clerk420025005500
185SH_CLERKShipping Clerk410025005500
186SH_CLERKShipping Clerk340025005500
187SH_CLERKShipping Clerk300025005500
188SH_CLERKShipping Clerk380025005500
189SH_CLERKShipping Clerk360025005500
190SH_CLERKShipping Clerk290025005500
191SH_CLERKShipping Clerk250025005500
192SH_CLERKShipping Clerk400025005500
193SH_CLERKShipping Clerk390025005500
194SH_CLERKShipping Clerk320025005500
195SH_CLERKShipping Clerk280025005500
196SH_CLERKShipping Clerk310025005500
197SH_CLERKShipping Clerk300025005500
198SH_CLERKShipping Clerk260025005500
199SH_CLERKShipping Clerk260025005500
180SH_CLERKShipping Clerk320025005500
181SH_CLERKShipping Clerk310025005500
182SH_CLERKShipping Clerk250025005500
183SH_CLERKShipping Clerk280025005500
132ST_CLERKStock Clerk210020085000
143ST_CLERKStock Clerk260020085000
142ST_CLERKStock Clerk310020085000
141ST_CLERKStock Clerk350020085000
140ST_CLERKStock Clerk250020085000
125ST_CLERKStock Clerk320020085000
126ST_CLERKStock Clerk270020085000
127ST_CLERKStock Clerk240020085000
128ST_CLERKStock Clerk220020085000
129ST_CLERKStock Clerk330020085000
130ST_CLERKStock Clerk280020085000
131ST_CLERKStock Clerk250020085000
144ST_CLERKStock Clerk250020085000
133ST_CLERKStock Clerk330020085000
134ST_CLERKStock Clerk290020085000
135ST_CLERKStock Clerk240020085000
136ST_CLERKStock Clerk220020085000
137ST_CLERKStock Clerk360020085000
138ST_CLERKStock Clerk320020085000
139ST_CLERKStock Clerk270020085000
124ST_MANStock Manager580055008500
123ST_MANStock Manager650055008500
122ST_MANStock Manager790055008500
121ST_MANStock Manager820055008500
120ST_MANStock Manager800055008500

Breaking Down the Solution
In the SELECT clause, the columns required to meet the reporting requirements are specified. Each of these columns are fully qualified by including the schema and table names (e.g., hr.employees.job_id). This is important as the job ID column is available in both tables. An error will result if the job ID column is included in the SELECT clause without including the name of the table it’s being sourced from. When parsing the query, the RDBMS doesn’t know which job ID column is being referenced without additional information such as the table name and possibly even the schema name in instances where this table and column exist across multiple schemas. The JOIN clause includes the tables of interest. The ON clause specifies to only combine rows from each table where the job ID value in the employee table row matches the job ID value in the jobs table row. In other words, if the employee’s job ID value can be found in the jobs table, then combine the employee’s row with the row in the jobs table where the matching job ID is found.

If you are having trouble following the explanation above and are familiar with the VLOOKUP (vertical lookup) function in excel, then please see the “Microsoft Excel Equivalent Operation” section at the end of this page.

Non-matching Job ID Values
Previously, I mentioned each job ID value in the employees table matches exactly one value in the jobs table, resulting in a guaranteed successful match and combining of employee and job rows. However, if this was not the case and it was possible for an employee to have a job ID value not existing in the jobs table, then the result of the join operation would effectively filter out those employees with non-matching job ID values. The same goes for those jobs stored in rows in the jobs table where the job ID value doesn’t exist in the employees table. That is, no employees are assigned that specific job. This is the intended result of the default join type, INNER JOIN. To modify the behavior of the join operation, you’ll need to become familiar with the other join types. Those join types and links to a discussion on each of them can be found at the beginning of this page.

Microsoft Excel Equivalent Operation
In my experience, teaching users to combine rows from multiple tables using SQL is easier when they’ve performed a similar operation using other programming languages or software. This has been especially true for users familiar with the Microsoft Excel function, VLOOKUP. Learners familiar with this function can largely focus on how to perform the task rather than on understanding the task itself.

In our example task, the employees and jobs tables can be thought of as merely separate worksheets within an excel workbook. To combine the rows in the employees worksheet with those found in the jobs worksheet, a vertical lookup can be performed on the common column, job ID. The table array used in the lookup will be a data range in the jobs worksheet, starting with the job ID and extending to the column of interest, say job title. Provided the correct column index is provided for the job title column in the jobs table, the value returned in each row will be the job title corresponding to the employee’s job ID value. Like the JOIN operation covered above, the job ID value in each row within the employees worksheet is searched for within the jobs worksheet. If the job ID is found, then the values associated with each job ID can be accessed. Otherwise, #N/A is returned, indicating a match could not be found.