WHERE CLAUSE

Common Use Case
Often, there is a need to filter the results of a query based on the results of another query, dynamically. For example, consider the task of producing a report on a daily basis containing the most recent day’s business transactions. A user could update the query on a daily basis to contain the hardcoded value associated with the most recent date value. However, the more reliable and convenient option would be to use a subquery to return this date value at runtime.

Example Data
The schemas, corresponding tables, and data used in the example problems can be found at livesql.oracle.com. To more easily follow the examples covered on this page, consider first reading the table descriptions below and reviewing each table’s contents. To review the data maintained in each table, click on the icon next to the table name. To hide the data, click the icon again.

hr.employees
Contains information related to each employee and a hierarchal employee-manager relationship. There is one row per employee.
employee_idfirst_namelast_nameemailphone_numberhire_datejob_idsalarycommission_pctmanager_iddepartment_id
100StevenKingSKING515.123.456717-Jun-03AD_PRES24000 – – 90
101NeenaKochharNKOCHHAR515.123.456821-Sep-05AD_VP17000 – 10090
102LexDe HaanLDEHAAN515.123.456913-Jan-01AD_VP17000 – 10090
103AlexanderHunoldAHUNOLD590.423.45673-Jan-06IT_PROG9000 – 10260
104BruceErnstBERNST590.423.456821-May-07IT_PROG6000 – 10360
105DavidAustinDAUSTIN590.423.456925-Jun-05IT_PROG4800 – 10360
106ValliPataballaVPATABAL590.423.45605-Feb-06IT_PROG4800 – 10360
107DianaLorentzDLORENTZ590.423.55677-Feb-07IT_PROG4200 – 10360
108NancyGreenbergNGREENBE515.124.456917-Aug-02FI_MGR12008 – 101100
109DanielFavietDFAVIET515.124.416916-Aug-02FI_ACCOUNT9000 – 108100
110JohnChenJCHEN515.124.426928-Sep-05FI_ACCOUNT8200 – 108100
111IsmaelSciarraISCIARRA515.124.436930-Sep-05FI_ACCOUNT7700 – 108100
112Jose ManuelUrmanJMURMAN515.124.44697-Mar-06FI_ACCOUNT7800 – 108100
113LuisPoppLPOPP515.124.45677-Dec-07FI_ACCOUNT6900 – 108100
114DenRaphaelyDRAPHEAL515.127.45617-Dec-02PU_MAN11000 – 10030
115AlexanderKhooAKHOO515.127.456218-May-03PU_CLERK3100 – 11430
116ShelliBaidaSBAIDA515.127.456324-Dec-05PU_CLERK2900 – 11430
117SigalTobiasSTOBIAS515.127.456424-Jul-05PU_CLERK2800 – 11430
118GuyHimuroGHIMURO515.127.456515-Nov-06PU_CLERK2600 – 11430
119KarenColmenaresKCOLMENA515.127.456610-Aug-07PU_CLERK2500 – 11430
120MatthewWeissMWEISS650.123.123418-Jul-04ST_MAN8000 – 10050
121AdamFrippAFRIPP650.123.223410-Apr-05ST_MAN8200 – 10050
122PayamKauflingPKAUFLIN650.123.32341-May-03ST_MAN7900 – 10050
123ShantaVollmanSVOLLMAN650.123.423410-Oct-05ST_MAN6500 – 10050
124KevinMourgosKMOURGOS650.123.523416-Nov-07ST_MAN5800 – 10050
125JuliaNayerJNAYER650.124.121416-Jul-05ST_CLERK3200 – 12050
126IreneMikkilineniIMIKKILI650.124.122428-Sep-06ST_CLERK2700 – 12050
127JamesLandryJLANDRY650.124.133414-Jan-07ST_CLERK2400 – 12050
128StevenMarkleSMARKLE650.124.14348-Mar-08ST_CLERK2200 – 12050
129LauraBissotLBISSOT650.124.523420-Aug-05ST_CLERK3300 – 12150
130MozheAtkinsonMATKINSO650.124.623430-Oct-05ST_CLERK2800 – 12150
131JamesMarlowJAMRLOW650.124.723416-Feb-05ST_CLERK2500 – 12150
132TJOlsonTJOLSON650.124.823410-Apr-07ST_CLERK2100 – 12150
133JasonMallinJMALLIN650.127.193414-Jun-04ST_CLERK3300 – 12250
134MichaelRogersMROGERS650.127.183426-Aug-06ST_CLERK2900 – 12250
135KiGeeKGEE650.127.173412-Dec-07ST_CLERK2400 – 12250
136HazelPhiltankerHPHILTAN650.127.16346-Feb-08ST_CLERK2200 – 12250
137RenskeLadwigRLADWIG650.121.123414-Jul-03ST_CLERK3600 – 12350
138StephenStilesSSTILES650.121.203426-Oct-05ST_CLERK3200 – 12350
139JohnSeoJSEO650.121.201912-Feb-06ST_CLERK2700 – 12350
140JoshuaPatelJPATEL650.121.18346-Apr-06ST_CLERK2500 – 12350
141TrennaRajsTRAJS650.121.800917-Oct-03ST_CLERK3500 – 12450
142CurtisDaviesCDAVIES650.121.299429-Jan-05ST_CLERK3100 – 12450
143RandallMatosRMATOS650.121.287415-Mar-06ST_CLERK2600 – 12450
144PeterVargasPVARGAS650.121.20049-Jul-06ST_CLERK2500 – 12450
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_CLERK3200 – 12050
181JeanFleaurJFLEAUR650.507.987723-Feb-06SH_CLERK3100 – 12050
182MarthaSullivanMSULLIVA650.507.987821-Jun-07SH_CLERK2500 – 12050
183GirardGeoniGGEONI650.507.98793-Feb-08SH_CLERK2800 – 12050
184NanditaSarchandNSARCHAN650.509.187627-Jan-04SH_CLERK4200 – 12150
185AlexisBullABULL650.509.287620-Feb-05SH_CLERK4100 – 12150
186JuliaDellingerJDELLING650.509.387624-Jun-06SH_CLERK3400 – 12150
187AnthonyCabrioACABRIO650.509.48767-Feb-07SH_CLERK3000 – 12150
188KellyChungKCHUNG650.505.187614-Jun-05SH_CLERK3800 – 12250
189JenniferDillyJDILLY650.505.287613-Aug-05SH_CLERK3600 – 12250
190TimothyGatesTGATES650.505.387611-Jul-06SH_CLERK2900 – 12250
191RandallPerkinsRPERKINS650.505.487619-Dec-07SH_CLERK2500 – 12250
192SarahBellSBELL650.501.18764-Feb-04SH_CLERK4000 – 12350
193BritneyEverettBEVERETT650.501.28763-Mar-05SH_CLERK3900 – 12350
194SamuelMcCainSMCCAIN650.501.38761-Jul-06SH_CLERK3200 – 12350
195VanceJonesVJONES650.501.487617-Mar-07SH_CLERK2800 – 12350
196AlanaWalshAWALSH650.507.981124-Apr-06SH_CLERK3100 – 12450
197KevinFeeneyKFEENEY650.507.982223-May-06SH_CLERK3000 – 12450
198DonaldOConnellDOCONNEL650.507.983321-Jun-07SH_CLERK2600 – 12450
199DouglasGrantDGRANT650.507.984413-Jan-08SH_CLERK2600 – 12450
200JenniferWhalenJWHALEN515.123.444417-Sep-03AD_ASST4400 – 10110
201MichaelHartsteinMHARTSTE515.123.555517-Feb-04MK_MAN13000 – 10020
202PatFayPFAY603.123.666617-Aug-05MK_REP6000 – 20120
203SusanMavrisSMAVRIS515.123.77777-Jun-02HR_REP6500 – 10140
204HermannBaerHBAER515.123.88887-Jun-02PR_REP10000 – 10170
205ShelleyHigginsSHIGGINS515.123.80807-Jun-02AC_MGR12008 – 101110
206WilliamGietzWGIETZ515.123.81817-Jun-02AC_ACCOUNT8300 – 205110
hr.jobs
Contains information related to each job. It’s possible for a job to exist without being assigned to any employee. There is one row per job.
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 Examples
In practice, subqueries in the WHERE clause are typically used to implement dynamic filter criteria. Explore the following two real-world examples to obtain a better understanding on how this type of subquery can be used.

Most Recent Employee(s) Hired
Imagine you work within a human resource department and a recurring task is to generate a report to review the information related to the most recent employee(s) hired. Instead of retrieving the entire employee listing and associated details, sorting by hire date in descending order, and then excluding all employees with a hire date less than the most recent hire date value, a subquery in the WHERE clause can be used to first determine the hire date associated with the most recent employee(s) hired and subsequently compare the value to each employee’s hire date. It’s important to note this date value is computed at query runtime and reflects the most recent hire date based on the data available at that point in time. By using the subquery, many steps and the hardcoding of date values can be avoided. See the query and corresponding result set below.

SELECT
	hr.employees.employee_id,
    hr.employees.first_name,
    hr.employees.last_name,
    hr.employees.job_id,
    hr.employees.hire_date,
    hr.employees.salary
FROM
	hr.employees
WHERE
	hr.employees.hire_date = (
        SELECT
        	MAX(hr.employees.hire_date)
        FROM
        	hr.employees
    );
employee_idfirst_namelast_namejob_idhire_datesalary
167AmitBandaSA_REP2008-04-216200
173SunditaKumarSA_REP2008-04-216100

Maxed Out Salary
For managers of employees, it’s important to understand where each employee sits in their current job ID’s salary range. Does the employee have room to grow or has the employee reached the maximum salary possible for the specific job ID? Imagine you’re a manager and your task is to answer this question for all employees with the ultimate goal being to generate a report containing the employee ID, job ID, and salary for each employee that has a salary equal to the maximum salary for the job ID. There are many ways to solve this problem, including the arguably more straightforward method utilizing an INNER JOIN; however, I’ve used a correlated subquery in the WHERE clause below to maintain focus on the current topic of subqueries. See the query and corresponding result set below. A brief explanation is provided following the result set.

SELECT
	hr.employees.employee_id,
    hr.employees.job_id,
    hr.employees.salary
FROM
    hr.employees
WHERE
    hr.employees.salary - (
    	SELECT
    		hr.jobs.max_salary
    	FROM
    		hr.jobs
    	WHERE
    		hr.employees.job_id = hr.jobs.job_id
    ) = 0
employee_idjob_idsalary
109FI_ACCOUNT9000

In the query above, the difference between each employee’s salary and the maximum salary for their corresponding job ID is computed and compared to the value of 0 (i.e., no difference). Computing the difference is made possible by the subquery in the WHERE clause. This subquery executes one time per row in the outer query (i.e., employee) and returns the maximum salary for the specific employee’s job ID. It’s important to note the WHERE clause within the subquery ensures the correct maximum salary value from the jobs table is used to compute each difference.

Knowledge Check
If you’d like to confirm your understanding on the use of subqueries in the WHERE clause and other fundamental concepts, consider solving a handful of my practice problems here.