SELECT CLAUSE

Common Use Case
Often, there is a need to include data at multiple levels of detail within a single result set. For example, a task may require a value within each row of a table to be compared to the result of some aggregate (e.g., average, sum, min, etc.) function computed across those rows belonging to the same group.

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.

oe.orders
For each order placed by a customer, various attributes are captured. There is one row per order.
order_idorder_dateorder_modecustomer_idorder_statusorder_totalsales_rep_idpromotion_id
245816-AUG-07 03.34.12.234359 PMdirect101078279.6153
239719-NOV-07 02.41.54.696211 PMdirect102142283.2154
245402-OCT-07 05.49.34.678340 PMdirect10316653.4154
235414-JUL-08 06.18.23.234567 PMdirect104046257155
235808-JAN-08 05.03.12.654278 PMdirect10527826155
238114-MAY-08 08.59.08.843679 PMdirect106323034.6156
244031-AUG-07 09.53.06.008765 PMdirect107370576.9156
235708-JAN-06 08.19.44.123456 PMdirect108559872.4158
239410-FEB-08 09.22.35.564789 PMdirect109521863158
243502-SEP-07 11.22.53.134567 PMdirect144662303159
245520-SEP-07 11.34.11.456789 AMdirect145714087.5160
237916-MAY-07 02.22.24.234567 AMdirect146817848.2161
239602-FEB-06 01.34.56.345678 AMdirect147834930161
240629-JUN-07 04.41.20.098765 AMdirect14882854.2161
243413-SEP-07 05.49.30.647893 AMdirect1498268651.8161
243602-SEP-07 06.18.04.378034 AMdirect11686394.8161
244627-JUL-07 07.03.08.302945 AMdirect1178103679.3161
244727-JUL-08 08.59.10.223344 AMdirect101833893.6161
243214-SEP-07 09.53.40.223345 AMdirect1021010523163
243313-SEP-07 10.19.00.654279 AMdirect1031078163
235526-JAN-06 09.22.51.962632 AMonline104894513.5
235626-JAN-08 09.22.41.934562 AMonline105529473.8
235908-JAN-06 09.34.13.112233 PMonline10695543.1
236014-NOV-07 12.22.31.223344 PMonline1074990.4
236113-NOV-07 01.34.21.986210 PMonline1088120131.3
236213-NOV-07 02.41.10.619477 PMonline109492829.4
236323-OCT-07 05.49.56.346122 PMonline144010082.3
236428-AUG-07 06.18.45.942399 PMonline14549500
236528-AUG-07 07.03.34.003399 PMonline146927455.3
236628-AUG-07 08.59.23.144778 PMonline147537319.4
236727-JUN-08 09.53.32.335522 PMonline14810144054.8
236826-JUN-08 10.19.43.190089 PMonline1491060065
236926-JUN-07 11.22.54.009932 PMonline116011097.4
237027-JUN-08 12.22.11.647398 AMonline1174126
237116-MAY-07 01.34.56.113356 AMonline118679405.6
237227-FEB-07 12.22.33.356789 AMonline119916447.2
237327-FEB-08 01.34.51.220065 AMonline1204416
237427-FEB-08 02.41.45.109654 AMonline12104797
237526-FEB-07 03.49.50.459233 AMonline1222103834.4
237607-JUN-07 06.18.08.883310 AMonline123611006.2
237707-JUN-07 07.03.01.001100 AMonline141538017.8
237824-MAY-07 08.59.10.010101 AMonline142525691.3
238016-MAY-07 09.53.02.909090 AMonline143327132.6
238214-MAY-08 10.19.03.828321 AMonline144871173
238312-MAY-08 11.22.30.545103 AMonline145836374.7
238412-MAY-08 12.22.34.525972 PMonline146329249.1
238508-DEC-07 11.34.11.331392 AMonline1474295892
238606-DEC-07 12.22.34.225609 PMonline1481021116.9
238711-MAR-07 03.34.56.536966 PMonline149552758.9
238804-JUN-07 04.41.12.554435 PMonline1504282694.3
238904-JUN-08 05.49.43.546954 PMonline151417620
239018-NOV-07 04.18.50.546851 PMonline15297616.8
239127-FEB-06 05.03.03.828330 PMdirect153248070.6156
239221-JUL-07 08.59.57.571057 PMdirect154926632161
239310-FEB-08 07.53.19.528202 PMdirect155423431.9161
239502-FEB-06 08.19.11.227550 PMdirect156368501163
239819-NOV-07 09.22.53.224175 PMdirect15797110.3163
239919-NOV-07 10.22.38.340990 PMdirect158025270.3161
240010-JUL-07 01.34.29.559387 AMdirect159269286.4161
240110-JUL-07 02.22.53.554822 AMdirect1603969.2163
240202-JUL-07 03.34.44.665170 AMdirect1618600154
240301-JUL-07 04.49.13.615512 PMdirect1620220154
240401-JUL-07 04.49.13.664085 PMdirect1636510158
240501-JUL-07 04.49.13.678123 PMdirect16451233159
240729-JUN-07 07.03.21.526005 AMdirect16592519155
240829-JUN-07 08.59.31.333617 AMdirect1661309158
240929-JUN-07 09.53.41.984501 AMdirect167248154
241024-MAY-08 10.19.51.985501 AMdirect168645175156
241124-MAY-07 11.22.10.548639 AMdirect169815760.5156
241229-MAR-06 10.22.09.509801 AMdirect170966816158
241329-MAR-08 01.34.04.525934 PMdirect101548552161
241429-MAR-07 02.22.40.536996 PMdirect102810794.6153
241529-MAR-06 01.34.50.545196 PMdirect1036310161
241629-MAR-07 04.41.20.945676 PMdirect1046384160
241720-MAR-07 05.49.10.974352 PMdirect10551926.6163
241820-MAR-04 04.18.21.862632 PMdirect10645546.6163
241920-MAR-07 07.03.32.764632 PMdirect107331574160
242013-MAR-07 08.59.43.666320 PMdirect108229750160
242112-MAR-07 09.53.54.562432 PMdirect109172836
242216-DEC-07 08.19.55.462332 PMdirect144211188.5153
242321-NOV-07 10.22.33.362632 AMdirect145310367.7160
242421-NOV-07 10.22.33.263332 AMdirect146413824153
242516-NOV-06 11.34.22.162552 PMdirect14751500.8163
242617-NOV-06 12.22.11.262552 AMdirect14867200
242710-NOV-07 01.34.22.362124 AMdirect14979055163
242810-NOV-07 02.41.34.463567 AMdirect116814685.8
242910-NOV-07 03.49.25.526321 AMdirect117950125154
243002-OCT-07 06.18.36.663332 AMdirect101829669.9159
243114-SEP-06 07.03.04.763452 AMdirect10215610.6163
243701-SEP-06 08.59.15.826132 AMdirect103413550163
243801-SEP-07 09.53.26.934626 AMdirect10405451154
243931-AUG-07 10.19.37.811132 AMdirect105122150.1159
244101-AUG-08 11.22.48.734526 AMdirect10652075.2160
244227-JUL-06 12.22.59.662632 PMdirect107952471.9154
244327-JUL-06 01.34.16.562632 PMdirect10803646154
244427-JUL-07 02.22.27.462632 PMdirect109177727.2155
244527-JUL-06 03.34.38.362632 PMdirect14485537.8158
244818-JUN-07 04.41.49.262632 PMdirect14551388158
244913-JUN-07 05.49.07.162632 PMdirect146686155
245011-APR-07 06.18.10.362632 PMdirect14731636159
245117-DEC-07 05.03.52.562632 PMdirect148710474.6154
245206-OCT-07 08.59.43.462632 PMdirect149512589159
245304-OCT-07 09.53.34.362632 PMdirect1160129153
245607-NOV-06 07.53.25.989889 PMdirect11703878.4163
245731-OCT-07 11.22.16.162632 PMdirect118521586.2159
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_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
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
Explore the following two real-world examples to obtain a better understanding on how this type of subquery can be used.

Above, Equal to, or Below Average Order Total?
Consider the task of computing a difference involving two values at different levels of detail, the order total for a specific order and the average order total for the corresponding customer. In the example below, there exists one row per order in the orders table, and within each row are attributes describing the order, such as the customer having placed the order and the total.

To compute the desired row-level difference, a subquery can be used. For each row in the outer query (i.e., order), the average order total is computed for the customer referenced in the row. It’s important to note the WHERE clause within the subquery ensures the orders corresponding to the customer referenced in the row are used to compute the average. This computation is made one time for each row produced by the outer query. So, if there are 105 orders, then the subquery is executed 105 times. See the query and corresponding result set below.

SELECT
	oe.orders.order_id,
    oe.orders.customer_id,
    oe.orders.order_total,
    (
        SELECT
        	AVG(average_order.order_total)
        FROM
        	oe.orders average_order
        WHERE
			oe.orders.customer_id = average_order.customer_id
    ) AS average_order_total,
    oe.orders.order_total - (
        SELECT
        	AVG(average_order.order_total)
        FROM
        	oe.orders average_order
        WHERE
    		oe.orders.customer_id = average_order.customer_id
    ) AS difference 
FROM
	oe.orders;
order_idcustomer_idorder_totalaverage_order_totaldifference
244010770576.938903.331673.6
2360107990.438903.3-37912.9
24191073157438903.3-7329.3
244210752471.938903.313568.6
235710859872.453349.9256522.475
2361108120131.353349.92566781.375
24201082975053349.925-23599.925
2443108364653349.925-49703.925
239915825270.325270.30
24021616006000
24081663093090
2358105782615344.125-7518.125
235610529473.815344.12514129.675
24171051926.615344.125-13417.525
243910522150.115344.1256805.975
23941092186366313.9-44450.9
236210992829.466313.926515.5
24211097283666313.96522.1
244410977727.266313.911413.3
238014327132.627132.60
240015969286.469286.40
24031622202200
24041635105100
241016845175451750
23541044625736651.3759605.625
235510494513.536651.37557862.125
241610438436651.375-36267.375
2438104545136651.375-31200.375
237111879405.650495.928909.7
245711821586.250495.9-28909.7
245514514087.514343.58-256.08
2364145950014343.58-4843.58
238314536374.714343.5822031.12
242314510367.714343.58-3975.88
2448145138814343.58-12955.58
2374121479747970
237714138017.838017.80
239315523431.923431.90
245810178279.647598.77530680.825
244710133893.647598.775-13705.175
24131014855247598.775953.225
243010129669.947598.775-17928.875
24541036653.45147.851505.55
2433103785147.85-5069.85
24151033105147.85-4837.85
2437103135505147.858402.15
24361166394.88076.75-1681.95
236911611097.48076.753020.65
242811614685.88076.756609.05
24531161298076.75-7947.75
23731204164160
237814225691.325691.30
238915117620176200
239515668501685010
23981577110.37110.30
241116915760.515760.50
237914617848.217692.52155.68
236514627455.317692.529762.78
238414629249.117692.5211556.58
24241461382417692.52-3868.52
24491468617692.52-17606.52
24061482854.237140.1-34285.9
2367148144054.837140.1106914.7
238614821116.937140.1-16023.2
2426148720037140.1-29940.1
245114810474.637140.1-26665.5
2434149268651.880623.94188027.86
23681496006580623.94-20558.94
238714952758.980623.94-27865.04
2427149905580623.94-71568.94
24521491258980623.94-68034.94
2375122103834.4103834.40
23901527616.87616.80
241217066816668160
24351446230332056.9230246.08
236314410082.332056.92-21974.62
23821447117332056.9239116.08
242214411188.532056.92-20868.42
24451445537.832056.92-26519.12
237211916447.216447.20
239115348070.648070.60
2405164123312330
240916748480
239710242283.217302.8524980.35
24321021052317302.85-6779.85
241410210794.617302.85-6508.25
24311025610.617302.85-11692.25
238110623034.69049.87513984.725
23591065543.19049.875-3506.775
24181065546.69049.875-3503.275
24411062075.29049.875-6974.675
23961473493074255.64-39325.64
236614737319.474255.64-36936.24
238514729589274255.64221636.36
24251471500.874255.64-72754.84
2450147163674255.64-72619.64
2446117103679.339452.17564227.125
237011712639452.175-39326.175
24291175012539452.17510672.825
24561173878.439452.175-35573.775
237612311006.211006.20
2388150282694.3282694.30
239215426632266320
2401160969.2969.20
2407165251925190

Above, Equal to, or Below Average Salary?
The example above likely rings a bell. It shares similar characteristics and falls into the same theme or type of problem as the one previously solved where there was a need to compute the difference between an employee’s salary and the average salary of those employees within the same job role. In that example, a subquery in the FROM clause was used. Subqueries in the SELECT clause provide another way to solve the same problem. I believe we can all agree it’s nice to have options when solving problems.

Below is another solution to that same problem using a subquery in the SELECT clause as opposed to the FROM clause. While the topic of performance is not within the scope of this page, it’s wise to compare the performance of candidate solutions and choose the most optimal one given the environment you’re operating within. Context matters!

SELECT
	hr.employees.employee_id,
    hr.employees.job_id,
    hr.employees.salary,
	(      
        SELECT
            AVG(job_salary.salary) AS average_salary
        FROM
            hr.employees job_salary
        GROUP BY
            job_salary.job_id
        HAVING
            hr.employees.job_id = job_salary.job_id
    ) AS average_salary,
    hr.employees.salary - (
        SELECT
    		AVG(job_salary.salary) AS average_salary
        FROM
            hr.employees job_salary
        GROUP BY
            job_salary.job_id
        HAVING
            hr.employees.job_id = job_salary.job_id
    ) AS difference
FROM
	hr.employees
ORDER BY
    difference DESC;
employee_idjob_idsalaryaverage_salarydifference
103IT_PROG900057603240
168SA_REP1150083503150
174SA_REP1100083502650
162SA_REP1050083502150
145SA_MAN14000122001800
150SA_REP1000083501650
156SA_REP1000083501650
169SA_REP1000083501650
146SA_MAN13500122001300
170SA_REP960083501250
163SA_REP950083501150
157SA_REP950083501150
151SA_REP950083501150
109FI_ACCOUNT900079201080
184SH_CLERK42003215985
121ST_MAN82007280920
185SH_CLERK41003215885
137ST_CLERK36002785815
192SH_CLERK40003215785
120ST_MAN80007280720
141ST_CLERK35002785715
193SH_CLERK39003215685
152SA_REP90008350650
158SA_REP90008350650
122ST_MAN79007280620
188SH_CLERK38003215585
129ST_CLERK33002785515
133ST_CLERK33002785515
175SA_REP88008350450
125ST_CLERK32002785415
138ST_CLERK32002785415
189SH_CLERK36003215385
115PU_CLERK31002780320
142ST_CLERK31002785315
110FI_ACCOUNT82007920280
176SA_REP86008350250
104IT_PROG60005760240
186SH_CLERK34003215185
116PU_CLERK29002780120
134ST_CLERK29002785115
177SA_REP8400835050
117PU_CLERK2800278020
130ST_CLERK2800278515
100AD_PRES24000240000
101AD_VP17000170000
102AD_VP17000170000
108FI_MGR12008120080
114PU_MAN11000110000
200AD_ASST440044000
201MK_MAN13000130000
202MK_REP600060000
203HR_REP650065000
204PR_REP10000100000
205AC_MGR12008120080
206AC_ACCOUNT830083000
180SH_CLERK32003215-15
194SH_CLERK32003215-15
126ST_CLERK27002785-85
139ST_CLERK27002785-85
196SH_CLERK31003215-115
181SH_CLERK31003215-115
112FI_ACCOUNT78007920-120
118PU_CLERK26002780-180
143ST_CLERK26002785-185
147SA_MAN1200012200-200
187SH_CLERK30003215-215
197SH_CLERK30003215-215
111FI_ACCOUNT77007920-220
119PU_CLERK25002780-280
131ST_CLERK25002785-285
144ST_CLERK25002785-285
140ST_CLERK25002785-285
190SH_CLERK29003215-315
153SA_REP80008350-350
159SA_REP80008350-350
127ST_CLERK24002785-385
135ST_CLERK24002785-385
183SH_CLERK28003215-415
195SH_CLERK28003215-415
128ST_CLERK22002785-585
136ST_CLERK22002785-585
198SH_CLERK26003215-615
199SH_CLERK26003215-615
132ST_CLERK21002785-685
182SH_CLERK25003215-715
191SH_CLERK25003215-715
123ST_MAN65007280-780
160SA_REP75008350-850
154SA_REP75008350-850
171SA_REP74008350-950
105IT_PROG48005760-960
106IT_PROG48005760-960
113FI_ACCOUNT69007920-1020
172SA_REP73008350-1050
164SA_REP72008350-1150
148SA_MAN1100012200-1200
178SA_REP70008350-1350
161SA_REP70008350-1350
155SA_REP70008350-1350
124ST_MAN58007280-1480
165SA_REP68008350-1550
107IT_PROG42005760-1560
149SA_MAN1050012200-1700
166SA_REP64008350-1950
179SA_REP62008350-2150
167SA_REP62008350-2150
173SA_REP61008350-2250

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