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_id | order_date | order_mode | customer_id | order_status | order_total | sales_rep_id | promotion_id |
|---|---|---|---|---|---|---|---|
| 2458 | 16-AUG-07 03.34.12.234359 PM | direct | 101 | 0 | 78279.6 | 153 | – |
| 2397 | 19-NOV-07 02.41.54.696211 PM | direct | 102 | 1 | 42283.2 | 154 | – |
| 2454 | 02-OCT-07 05.49.34.678340 PM | direct | 103 | 1 | 6653.4 | 154 | – |
| 2354 | 14-JUL-08 06.18.23.234567 PM | direct | 104 | 0 | 46257 | 155 | – |
| 2358 | 08-JAN-08 05.03.12.654278 PM | direct | 105 | 2 | 7826 | 155 | – |
| 2381 | 14-MAY-08 08.59.08.843679 PM | direct | 106 | 3 | 23034.6 | 156 | – |
| 2440 | 31-AUG-07 09.53.06.008765 PM | direct | 107 | 3 | 70576.9 | 156 | – |
| 2357 | 08-JAN-06 08.19.44.123456 PM | direct | 108 | 5 | 59872.4 | 158 | – |
| 2394 | 10-FEB-08 09.22.35.564789 PM | direct | 109 | 5 | 21863 | 158 | – |
| 2435 | 02-SEP-07 11.22.53.134567 PM | direct | 144 | 6 | 62303 | 159 | – |
| 2455 | 20-SEP-07 11.34.11.456789 AM | direct | 145 | 7 | 14087.5 | 160 | – |
| 2379 | 16-MAY-07 02.22.24.234567 AM | direct | 146 | 8 | 17848.2 | 161 | – |
| 2396 | 02-FEB-06 01.34.56.345678 AM | direct | 147 | 8 | 34930 | 161 | – |
| 2406 | 29-JUN-07 04.41.20.098765 AM | direct | 148 | 8 | 2854.2 | 161 | – |
| 2434 | 13-SEP-07 05.49.30.647893 AM | direct | 149 | 8 | 268651.8 | 161 | – |
| 2436 | 02-SEP-07 06.18.04.378034 AM | direct | 116 | 8 | 6394.8 | 161 | – |
| 2446 | 27-JUL-07 07.03.08.302945 AM | direct | 117 | 8 | 103679.3 | 161 | – |
| 2447 | 27-JUL-08 08.59.10.223344 AM | direct | 101 | 8 | 33893.6 | 161 | – |
| 2432 | 14-SEP-07 09.53.40.223345 AM | direct | 102 | 10 | 10523 | 163 | – |
| 2433 | 13-SEP-07 10.19.00.654279 AM | direct | 103 | 10 | 78 | 163 | – |
| 2355 | 26-JAN-06 09.22.51.962632 AM | online | 104 | 8 | 94513.5 | – | – |
| 2356 | 26-JAN-08 09.22.41.934562 AM | online | 105 | 5 | 29473.8 | – | – |
| 2359 | 08-JAN-06 09.34.13.112233 PM | online | 106 | 9 | 5543.1 | – | – |
| 2360 | 14-NOV-07 12.22.31.223344 PM | online | 107 | 4 | 990.4 | – | – |
| 2361 | 13-NOV-07 01.34.21.986210 PM | online | 108 | 8 | 120131.3 | – | – |
| 2362 | 13-NOV-07 02.41.10.619477 PM | online | 109 | 4 | 92829.4 | – | – |
| 2363 | 23-OCT-07 05.49.56.346122 PM | online | 144 | 0 | 10082.3 | – | – |
| 2364 | 28-AUG-07 06.18.45.942399 PM | online | 145 | 4 | 9500 | – | – |
| 2365 | 28-AUG-07 07.03.34.003399 PM | online | 146 | 9 | 27455.3 | – | – |
| 2366 | 28-AUG-07 08.59.23.144778 PM | online | 147 | 5 | 37319.4 | – | – |
| 2367 | 27-JUN-08 09.53.32.335522 PM | online | 148 | 10 | 144054.8 | – | – |
| 2368 | 26-JUN-08 10.19.43.190089 PM | online | 149 | 10 | 60065 | – | – |
| 2369 | 26-JUN-07 11.22.54.009932 PM | online | 116 | 0 | 11097.4 | – | – |
| 2370 | 27-JUN-08 12.22.11.647398 AM | online | 117 | 4 | 126 | – | – |
| 2371 | 16-MAY-07 01.34.56.113356 AM | online | 118 | 6 | 79405.6 | – | – |
| 2372 | 27-FEB-07 12.22.33.356789 AM | online | 119 | 9 | 16447.2 | – | – |
| 2373 | 27-FEB-08 01.34.51.220065 AM | online | 120 | 4 | 416 | – | – |
| 2374 | 27-FEB-08 02.41.45.109654 AM | online | 121 | 0 | 4797 | – | – |
| 2375 | 26-FEB-07 03.49.50.459233 AM | online | 122 | 2 | 103834.4 | – | – |
| 2376 | 07-JUN-07 06.18.08.883310 AM | online | 123 | 6 | 11006.2 | – | – |
| 2377 | 07-JUN-07 07.03.01.001100 AM | online | 141 | 5 | 38017.8 | – | – |
| 2378 | 24-MAY-07 08.59.10.010101 AM | online | 142 | 5 | 25691.3 | – | – |
| 2380 | 16-MAY-07 09.53.02.909090 AM | online | 143 | 3 | 27132.6 | – | – |
| 2382 | 14-MAY-08 10.19.03.828321 AM | online | 144 | 8 | 71173 | – | – |
| 2383 | 12-MAY-08 11.22.30.545103 AM | online | 145 | 8 | 36374.7 | – | – |
| 2384 | 12-MAY-08 12.22.34.525972 PM | online | 146 | 3 | 29249.1 | – | – |
| 2385 | 08-DEC-07 11.34.11.331392 AM | online | 147 | 4 | 295892 | – | – |
| 2386 | 06-DEC-07 12.22.34.225609 PM | online | 148 | 10 | 21116.9 | – | – |
| 2387 | 11-MAR-07 03.34.56.536966 PM | online | 149 | 5 | 52758.9 | – | – |
| 2388 | 04-JUN-07 04.41.12.554435 PM | online | 150 | 4 | 282694.3 | – | – |
| 2389 | 04-JUN-08 05.49.43.546954 PM | online | 151 | 4 | 17620 | – | – |
| 2390 | 18-NOV-07 04.18.50.546851 PM | online | 152 | 9 | 7616.8 | – | – |
| 2391 | 27-FEB-06 05.03.03.828330 PM | direct | 153 | 2 | 48070.6 | 156 | – |
| 2392 | 21-JUL-07 08.59.57.571057 PM | direct | 154 | 9 | 26632 | 161 | – |
| 2393 | 10-FEB-08 07.53.19.528202 PM | direct | 155 | 4 | 23431.9 | 161 | – |
| 2395 | 02-FEB-06 08.19.11.227550 PM | direct | 156 | 3 | 68501 | 163 | – |
| 2398 | 19-NOV-07 09.22.53.224175 PM | direct | 157 | 9 | 7110.3 | 163 | – |
| 2399 | 19-NOV-07 10.22.38.340990 PM | direct | 158 | 0 | 25270.3 | 161 | – |
| 2400 | 10-JUL-07 01.34.29.559387 AM | direct | 159 | 2 | 69286.4 | 161 | – |
| 2401 | 10-JUL-07 02.22.53.554822 AM | direct | 160 | 3 | 969.2 | 163 | – |
| 2402 | 02-JUL-07 03.34.44.665170 AM | direct | 161 | 8 | 600 | 154 | – |
| 2403 | 01-JUL-07 04.49.13.615512 PM | direct | 162 | 0 | 220 | 154 | – |
| 2404 | 01-JUL-07 04.49.13.664085 PM | direct | 163 | 6 | 510 | 158 | – |
| 2405 | 01-JUL-07 04.49.13.678123 PM | direct | 164 | 5 | 1233 | 159 | – |
| 2407 | 29-JUN-07 07.03.21.526005 AM | direct | 165 | 9 | 2519 | 155 | – |
| 2408 | 29-JUN-07 08.59.31.333617 AM | direct | 166 | 1 | 309 | 158 | – |
| 2409 | 29-JUN-07 09.53.41.984501 AM | direct | 167 | 2 | 48 | 154 | – |
| 2410 | 24-MAY-08 10.19.51.985501 AM | direct | 168 | 6 | 45175 | 156 | – |
| 2411 | 24-MAY-07 11.22.10.548639 AM | direct | 169 | 8 | 15760.5 | 156 | – |
| 2412 | 29-MAR-06 10.22.09.509801 AM | direct | 170 | 9 | 66816 | 158 | – |
| 2413 | 29-MAR-08 01.34.04.525934 PM | direct | 101 | 5 | 48552 | 161 | – |
| 2414 | 29-MAR-07 02.22.40.536996 PM | direct | 102 | 8 | 10794.6 | 153 | – |
| 2415 | 29-MAR-06 01.34.50.545196 PM | direct | 103 | 6 | 310 | 161 | – |
| 2416 | 29-MAR-07 04.41.20.945676 PM | direct | 104 | 6 | 384 | 160 | – |
| 2417 | 20-MAR-07 05.49.10.974352 PM | direct | 105 | 5 | 1926.6 | 163 | – |
| 2418 | 20-MAR-04 04.18.21.862632 PM | direct | 106 | 4 | 5546.6 | 163 | – |
| 2419 | 20-MAR-07 07.03.32.764632 PM | direct | 107 | 3 | 31574 | 160 | – |
| 2420 | 13-MAR-07 08.59.43.666320 PM | direct | 108 | 2 | 29750 | 160 | – |
| 2421 | 12-MAR-07 09.53.54.562432 PM | direct | 109 | 1 | 72836 | – | – |
| 2422 | 16-DEC-07 08.19.55.462332 PM | direct | 144 | 2 | 11188.5 | 153 | – |
| 2423 | 21-NOV-07 10.22.33.362632 AM | direct | 145 | 3 | 10367.7 | 160 | – |
| 2424 | 21-NOV-07 10.22.33.263332 AM | direct | 146 | 4 | 13824 | 153 | – |
| 2425 | 16-NOV-06 11.34.22.162552 PM | direct | 147 | 5 | 1500.8 | 163 | – |
| 2426 | 17-NOV-06 12.22.11.262552 AM | direct | 148 | 6 | 7200 | – | – |
| 2427 | 10-NOV-07 01.34.22.362124 AM | direct | 149 | 7 | 9055 | 163 | – |
| 2428 | 10-NOV-07 02.41.34.463567 AM | direct | 116 | 8 | 14685.8 | – | – |
| 2429 | 10-NOV-07 03.49.25.526321 AM | direct | 117 | 9 | 50125 | 154 | – |
| 2430 | 02-OCT-07 06.18.36.663332 AM | direct | 101 | 8 | 29669.9 | 159 | – |
| 2431 | 14-SEP-06 07.03.04.763452 AM | direct | 102 | 1 | 5610.6 | 163 | – |
| 2437 | 01-SEP-06 08.59.15.826132 AM | direct | 103 | 4 | 13550 | 163 | – |
| 2438 | 01-SEP-07 09.53.26.934626 AM | direct | 104 | 0 | 5451 | 154 | – |
| 2439 | 31-AUG-07 10.19.37.811132 AM | direct | 105 | 1 | 22150.1 | 159 | – |
| 2441 | 01-AUG-08 11.22.48.734526 AM | direct | 106 | 5 | 2075.2 | 160 | – |
| 2442 | 27-JUL-06 12.22.59.662632 PM | direct | 107 | 9 | 52471.9 | 154 | – |
| 2443 | 27-JUL-06 01.34.16.562632 PM | direct | 108 | 0 | 3646 | 154 | – |
| 2444 | 27-JUL-07 02.22.27.462632 PM | direct | 109 | 1 | 77727.2 | 155 | – |
| 2445 | 27-JUL-06 03.34.38.362632 PM | direct | 144 | 8 | 5537.8 | 158 | – |
| 2448 | 18-JUN-07 04.41.49.262632 PM | direct | 145 | 5 | 1388 | 158 | – |
| 2449 | 13-JUN-07 05.49.07.162632 PM | direct | 146 | 6 | 86 | 155 | – |
| 2450 | 11-APR-07 06.18.10.362632 PM | direct | 147 | 3 | 1636 | 159 | – |
| 2451 | 17-DEC-07 05.03.52.562632 PM | direct | 148 | 7 | 10474.6 | 154 | – |
| 2452 | 06-OCT-07 08.59.43.462632 PM | direct | 149 | 5 | 12589 | 159 | – |
| 2453 | 04-OCT-07 09.53.34.362632 PM | direct | 116 | 0 | 129 | 153 | – |
| 2456 | 07-NOV-06 07.53.25.989889 PM | direct | 117 | 0 | 3878.4 | 163 | – |
| 2457 | 31-OCT-07 11.22.16.162632 PM | direct | 118 | 5 | 21586.2 | 159 | – |
hr.employees
Contains information related to each employee and a hierarchal employee-manager relationship. There is one row per employee.
| employee_id | first_name | last_name | phone_number | hire_date | job_id | salary | commission_pct | manager_id | department_id | |
|---|---|---|---|---|---|---|---|---|---|---|
| 100 | Steven | King | SKING | 515.123.4567 | 17-Jun-03 | AD_PRES | 24000 | – | – | 90 |
| 101 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | 21-Sep-05 | AD_VP | 17000 | – | 100 | 90 |
| 102 | Lex | De Haan | LDEHAAN | 515.123.4569 | 13-Jan-01 | AD_VP | 17000 | – | 100 | 90 |
| 103 | Alexander | Hunold | AHUNOLD | 590.423.4567 | 3-Jan-06 | IT_PROG | 9000 | – | 102 | 60 |
| 104 | Bruce | Ernst | BERNST | 590.423.4568 | 21-May-07 | IT_PROG | 6000 | – | 103 | 60 |
| 105 | David | Austin | DAUSTIN | 590.423.4569 | 25-Jun-05 | IT_PROG | 4800 | – | 103 | 60 |
| 106 | Valli | Pataballa | VPATABAL | 590.423.4560 | 5-Feb-06 | IT_PROG | 4800 | – | 103 | 60 |
| 107 | Diana | Lorentz | DLORENTZ | 590.423.5567 | 7-Feb-07 | IT_PROG | 4200 | – | 103 | 60 |
| 108 | Nancy | Greenberg | NGREENBE | 515.124.4569 | 17-Aug-02 | FI_MGR | 12008 | – | 101 | 100 |
| 109 | Daniel | Faviet | DFAVIET | 515.124.4169 | 16-Aug-02 | FI_ACCOUNT | 9000 | – | 108 | 100 |
| 110 | John | Chen | JCHEN | 515.124.4269 | 28-Sep-05 | FI_ACCOUNT | 8200 | – | 108 | 100 |
| 111 | Ismael | Sciarra | ISCIARRA | 515.124.4369 | 30-Sep-05 | FI_ACCOUNT | 7700 | – | 108 | 100 |
| 112 | Jose Manuel | Urman | JMURMAN | 515.124.4469 | 7-Mar-06 | FI_ACCOUNT | 7800 | – | 108 | 100 |
| 113 | Luis | Popp | LPOPP | 515.124.4567 | 7-Dec-07 | FI_ACCOUNT | 6900 | – | 108 | 100 |
| 114 | Den | Raphaely | DRAPHEAL | 515.127.4561 | 7-Dec-02 | PU_MAN | 11000 | – | 100 | 30 |
| 115 | Alexander | Khoo | AKHOO | 515.127.4562 | 18-May-03 | PU_CLERK | 3100 | – | 114 | 30 |
| 116 | Shelli | Baida | SBAIDA | 515.127.4563 | 24-Dec-05 | PU_CLERK | 2900 | – | 114 | 30 |
| 117 | Sigal | Tobias | STOBIAS | 515.127.4564 | 24-Jul-05 | PU_CLERK | 2800 | – | 114 | 30 |
| 118 | Guy | Himuro | GHIMURO | 515.127.4565 | 15-Nov-06 | PU_CLERK | 2600 | – | 114 | 30 |
| 119 | Karen | Colmenares | KCOLMENA | 515.127.4566 | 10-Aug-07 | PU_CLERK | 2500 | – | 114 | 30 |
| 120 | Matthew | Weiss | MWEISS | 650.123.1234 | 18-Jul-04 | ST_MAN | 8000 | – | 100 | 50 |
| 121 | Adam | Fripp | AFRIPP | 650.123.2234 | 10-Apr-05 | ST_MAN | 8200 | – | 100 | 50 |
| 122 | Payam | Kaufling | PKAUFLIN | 650.123.3234 | 1-May-03 | ST_MAN | 7900 | – | 100 | 50 |
| 123 | Shanta | Vollman | SVOLLMAN | 650.123.4234 | 10-Oct-05 | ST_MAN | 6500 | – | 100 | 50 |
| 124 | Kevin | Mourgos | KMOURGOS | 650.123.5234 | 16-Nov-07 | ST_MAN | 5800 | – | 100 | 50 |
| 125 | Julia | Nayer | JNAYER | 650.124.1214 | 16-Jul-05 | ST_CLERK | 3200 | – | 120 | 50 |
| 126 | Irene | Mikkilineni | IMIKKILI | 650.124.1224 | 28-Sep-06 | ST_CLERK | 2700 | – | 120 | 50 |
| 127 | James | Landry | JLANDRY | 650.124.1334 | 14-Jan-07 | ST_CLERK | 2400 | – | 120 | 50 |
| 128 | Steven | Markle | SMARKLE | 650.124.1434 | 8-Mar-08 | ST_CLERK | 2200 | – | 120 | 50 |
| 129 | Laura | Bissot | LBISSOT | 650.124.5234 | 20-Aug-05 | ST_CLERK | 3300 | – | 121 | 50 |
| 130 | Mozhe | Atkinson | MATKINSO | 650.124.6234 | 30-Oct-05 | ST_CLERK | 2800 | – | 121 | 50 |
| 131 | James | Marlow | JAMRLOW | 650.124.7234 | 16-Feb-05 | ST_CLERK | 2500 | – | 121 | 50 |
| 132 | TJ | Olson | TJOLSON | 650.124.8234 | 10-Apr-07 | ST_CLERK | 2100 | – | 121 | 50 |
| 133 | Jason | Mallin | JMALLIN | 650.127.1934 | 14-Jun-04 | ST_CLERK | 3300 | – | 122 | 50 |
| 134 | Michael | Rogers | MROGERS | 650.127.1834 | 26-Aug-06 | ST_CLERK | 2900 | – | 122 | 50 |
| 135 | Ki | Gee | KGEE | 650.127.1734 | 12-Dec-07 | ST_CLERK | 2400 | – | 122 | 50 |
| 136 | Hazel | Philtanker | HPHILTAN | 650.127.1634 | 6-Feb-08 | ST_CLERK | 2200 | – | 122 | 50 |
| 137 | Renske | Ladwig | RLADWIG | 650.121.1234 | 14-Jul-03 | ST_CLERK | 3600 | – | 123 | 50 |
| 138 | Stephen | Stiles | SSTILES | 650.121.2034 | 26-Oct-05 | ST_CLERK | 3200 | – | 123 | 50 |
| 139 | John | Seo | JSEO | 650.121.2019 | 12-Feb-06 | ST_CLERK | 2700 | – | 123 | 50 |
| 140 | Joshua | Patel | JPATEL | 650.121.1834 | 6-Apr-06 | ST_CLERK | 2500 | – | 123 | 50 |
| 141 | Trenna | Rajs | TRAJS | 650.121.8009 | 17-Oct-03 | ST_CLERK | 3500 | – | 124 | 50 |
| 142 | Curtis | Davies | CDAVIES | 650.121.2994 | 29-Jan-05 | ST_CLERK | 3100 | – | 124 | 50 |
| 143 | Randall | Matos | RMATOS | 650.121.2874 | 15-Mar-06 | ST_CLERK | 2600 | – | 124 | 50 |
| 144 | Peter | Vargas | PVARGAS | 650.121.2004 | 9-Jul-06 | ST_CLERK | 2500 | – | 124 | 50 |
| 145 | John | Russell | JRUSSEL | 011.44.1344.429268 | 1-Oct-04 | SA_MAN | 14000 | 0.4 | 100 | 80 |
| 146 | Karen | Partners | KPARTNER | 011.44.1344.467268 | 5-Jan-05 | SA_MAN | 13500 | 0.3 | 100 | 80 |
| 147 | Alberto | Errazuriz | AERRAZUR | 011.44.1344.429278 | 10-Mar-05 | SA_MAN | 12000 | 0.3 | 100 | 80 |
| 148 | Gerald | Cambrault | GCAMBRAU | 011.44.1344.619268 | 15-Oct-07 | SA_MAN | 11000 | 0.3 | 100 | 80 |
| 149 | Eleni | Zlotkey | EZLOTKEY | 011.44.1344.429018 | 29-Jan-08 | SA_MAN | 10500 | 0.2 | 100 | 80 |
| 150 | Peter | Tucker | PTUCKER | 011.44.1344.129268 | 30-Jan-05 | SA_REP | 10000 | 0.3 | 145 | 80 |
| 151 | David | Bernstein | DBERNSTE | 011.44.1344.345268 | 24-Mar-05 | SA_REP | 9500 | 0.25 | 145 | 80 |
| 152 | Peter | Hall | PHALL | 011.44.1344.478968 | 20-Aug-05 | SA_REP | 9000 | 0.25 | 145 | 80 |
| 153 | Christopher | Olsen | COLSEN | 011.44.1344.498718 | 30-Mar-06 | SA_REP | 8000 | 0.2 | 145 | 80 |
| 154 | Nanette | Cambrault | NCAMBRAU | 011.44.1344.987668 | 9-Dec-06 | SA_REP | 7500 | 0.2 | 145 | 80 |
| 155 | Oliver | Tuvault | OTUVAULT | 011.44.1344.486508 | 23-Nov-07 | SA_REP | 7000 | 0.15 | 145 | 80 |
| 156 | Janette | King | JKING | 011.44.1345.429268 | 30-Jan-04 | SA_REP | 10000 | 0.35 | 146 | 80 |
| 157 | Patrick | Sully | PSULLY | 011.44.1345.929268 | 4-Mar-04 | SA_REP | 9500 | 0.35 | 146 | 80 |
| 158 | Allan | McEwen | AMCEWEN | 011.44.1345.829268 | 1-Aug-04 | SA_REP | 9000 | 0.35 | 146 | 80 |
| 159 | Lindsey | Smith | LSMITH | 011.44.1345.729268 | 10-Mar-05 | SA_REP | 8000 | 0.3 | 146 | 80 |
| 160 | Louise | Doran | LDORAN | 011.44.1345.629268 | 15-Dec-05 | SA_REP | 7500 | 0.3 | 146 | 80 |
| 161 | Sarath | Sewall | SSEWALL | 011.44.1345.529268 | 3-Nov-06 | SA_REP | 7000 | 0.25 | 146 | 80 |
| 162 | Clara | Vishney | CVISHNEY | 011.44.1346.129268 | 11-Nov-05 | SA_REP | 10500 | 0.25 | 147 | 80 |
| 163 | Danielle | Greene | DGREENE | 011.44.1346.229268 | 19-Mar-07 | SA_REP | 9500 | 0.15 | 147 | 80 |
| 164 | Mattea | Marvins | MMARVINS | 011.44.1346.329268 | 24-Jan-08 | SA_REP | 7200 | 0.1 | 147 | 80 |
| 165 | David | Lee | DLEE | 011.44.1346.529268 | 23-Feb-08 | SA_REP | 6800 | 0.1 | 147 | 80 |
| 166 | Sundar | Ande | SANDE | 011.44.1346.629268 | 24-Mar-08 | SA_REP | 6400 | 0.1 | 147 | 80 |
| 167 | Amit | Banda | ABANDA | 011.44.1346.729268 | 21-Apr-08 | SA_REP | 6200 | 0.1 | 147 | 80 |
| 168 | Lisa | Ozer | LOZER | 011.44.1343.929268 | 11-Mar-05 | SA_REP | 11500 | 0.25 | 148 | 80 |
| 169 | Harrison | Bloom | HBLOOM | 011.44.1343.829268 | 23-Mar-06 | SA_REP | 10000 | 0.2 | 148 | 80 |
| 170 | Tayler | Fox | TFOX | 011.44.1343.729268 | 24-Jan-06 | SA_REP | 9600 | 0.2 | 148 | 80 |
| 171 | William | Smith | WSMITH | 011.44.1343.629268 | 23-Feb-07 | SA_REP | 7400 | 0.15 | 148 | 80 |
| 172 | Elizabeth | Bates | EBATES | 011.44.1343.529268 | 24-Mar-07 | SA_REP | 7300 | 0.15 | 148 | 80 |
| 173 | Sundita | Kumar | SKUMAR | 011.44.1343.329268 | 21-Apr-08 | SA_REP | 6100 | 0.1 | 148 | 80 |
| 174 | Ellen | Abel | EABEL | 011.44.1644.429267 | 11-May-04 | SA_REP | 11000 | 0.3 | 149 | 80 |
| 175 | Alyssa | Hutton | AHUTTON | 011.44.1644.429266 | 19-Mar-05 | SA_REP | 8800 | 0.25 | 149 | 80 |
| 176 | Jonathon | Taylor | JTAYLOR | 011.44.1644.429265 | 24-Mar-06 | SA_REP | 8600 | 0.2 | 149 | 80 |
| 177 | Jack | Livingston | JLIVINGS | 011.44.1644.429264 | 23-Apr-06 | SA_REP | 8400 | 0.2 | 149 | 80 |
| 178 | Kimberely | Grant | KGRANT | 011.44.1644.429263 | 24-May-07 | SA_REP | 7000 | 0.15 | 149 | – |
| 179 | Charles | Johnson | CJOHNSON | 011.44.1644.429262 | 4-Jan-08 | SA_REP | 6200 | 0.1 | 149 | 80 |
| 180 | Winston | Taylor | WTAYLOR | 650.507.9876 | 24-Jan-06 | SH_CLERK | 3200 | – | 120 | 50 |
| 181 | Jean | Fleaur | JFLEAUR | 650.507.9877 | 23-Feb-06 | SH_CLERK | 3100 | – | 120 | 50 |
| 182 | Martha | Sullivan | MSULLIVA | 650.507.9878 | 21-Jun-07 | SH_CLERK | 2500 | – | 120 | 50 |
| 183 | Girard | Geoni | GGEONI | 650.507.9879 | 3-Feb-08 | SH_CLERK | 2800 | – | 120 | 50 |
| 184 | Nandita | Sarchand | NSARCHAN | 650.509.1876 | 27-Jan-04 | SH_CLERK | 4200 | – | 121 | 50 |
| 185 | Alexis | Bull | ABULL | 650.509.2876 | 20-Feb-05 | SH_CLERK | 4100 | – | 121 | 50 |
| 186 | Julia | Dellinger | JDELLING | 650.509.3876 | 24-Jun-06 | SH_CLERK | 3400 | – | 121 | 50 |
| 187 | Anthony | Cabrio | ACABRIO | 650.509.4876 | 7-Feb-07 | SH_CLERK | 3000 | – | 121 | 50 |
| 188 | Kelly | Chung | KCHUNG | 650.505.1876 | 14-Jun-05 | SH_CLERK | 3800 | – | 122 | 50 |
| 189 | Jennifer | Dilly | JDILLY | 650.505.2876 | 13-Aug-05 | SH_CLERK | 3600 | – | 122 | 50 |
| 190 | Timothy | Gates | TGATES | 650.505.3876 | 11-Jul-06 | SH_CLERK | 2900 | – | 122 | 50 |
| 191 | Randall | Perkins | RPERKINS | 650.505.4876 | 19-Dec-07 | SH_CLERK | 2500 | – | 122 | 50 |
| 192 | Sarah | Bell | SBELL | 650.501.1876 | 4-Feb-04 | SH_CLERK | 4000 | – | 123 | 50 |
| 193 | Britney | Everett | BEVERETT | 650.501.2876 | 3-Mar-05 | SH_CLERK | 3900 | – | 123 | 50 |
| 194 | Samuel | McCain | SMCCAIN | 650.501.3876 | 1-Jul-06 | SH_CLERK | 3200 | – | 123 | 50 |
| 195 | Vance | Jones | VJONES | 650.501.4876 | 17-Mar-07 | SH_CLERK | 2800 | – | 123 | 50 |
| 196 | Alana | Walsh | AWALSH | 650.507.9811 | 24-Apr-06 | SH_CLERK | 3100 | – | 124 | 50 |
| 197 | Kevin | Feeney | KFEENEY | 650.507.9822 | 23-May-06 | SH_CLERK | 3000 | – | 124 | 50 |
| 198 | Donald | OConnell | DOCONNEL | 650.507.9833 | 21-Jun-07 | SH_CLERK | 2600 | – | 124 | 50 |
| 199 | Douglas | Grant | DGRANT | 650.507.9844 | 13-Jan-08 | SH_CLERK | 2600 | – | 124 | 50 |
| 200 | Jennifer | Whalen | JWHALEN | 515.123.4444 | 17-Sep-03 | AD_ASST | 4400 | – | 101 | 10 |
| 201 | Michael | Hartstein | MHARTSTE | 515.123.5555 | 17-Feb-04 | MK_MAN | 13000 | – | 100 | 20 |
| 202 | Pat | Fay | PFAY | 603.123.6666 | 17-Aug-05 | MK_REP | 6000 | – | 201 | 20 |
| 203 | Susan | Mavris | SMAVRIS | 515.123.7777 | 7-Jun-02 | HR_REP | 6500 | – | 101 | 40 |
| 204 | Hermann | Baer | HBAER | 515.123.8888 | 7-Jun-02 | PR_REP | 10000 | – | 101 | 70 |
| 205 | Shelley | Higgins | SHIGGINS | 515.123.8080 | 7-Jun-02 | AC_MGR | 12008 | – | 101 | 110 |
| 206 | William | Gietz | WGIETZ | 515.123.8181 | 7-Jun-02 | AC_ACCOUNT | 8300 | – | 205 | 110 |
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_id | job_title | min_salary | max_salary |
|---|---|---|---|
| AD_PRES | President | 20080 | 40000 |
| AD_VP | Administration Vice President | 15000 | 30000 |
| AD_ASST | Administration Assistant | 3000 | 6000 |
| FI_MGR | Finance Manager | 8200 | 16000 |
| FI_ACCOUNT | Accountant | 4200 | 9000 |
| AC_MGR | Accounting Manager | 8200 | 16000 |
| AC_ACCOUNT | Public Accountant | 4200 | 9000 |
| SA_MAN | Sales Manager | 10000 | 20080 |
| SA_REP | Sales Representative | 6000 | 12008 |
| PU_MAN | Purchasing Manager | 8000 | 15000 |
| PU_CLERK | Purchasing Clerk | 2500 | 5500 |
| ST_MAN | Stock Manager | 5500 | 8500 |
| ST_CLERK | Stock Clerk | 2008 | 5000 |
| SH_CLERK | Shipping Clerk | 2500 | 5500 |
| IT_PROG | Programmer | 4000 | 10000 |
| MK_MAN | Marketing Manager | 9000 | 15000 |
| MK_REP | Marketing Representative | 4000 | 9000 |
| HR_REP | Human Resources Representative | 4000 | 9000 |
| PR_REP | Public Relations Representative | 4500 | 10500 |
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_id | customer_id | order_total | average_order_total | difference |
|---|---|---|---|---|
| 2440 | 107 | 70576.9 | 38903.3 | 31673.6 |
| 2360 | 107 | 990.4 | 38903.3 | -37912.9 |
| 2419 | 107 | 31574 | 38903.3 | -7329.3 |
| 2442 | 107 | 52471.9 | 38903.3 | 13568.6 |
| 2357 | 108 | 59872.4 | 53349.925 | 6522.475 |
| 2361 | 108 | 120131.3 | 53349.925 | 66781.375 |
| 2420 | 108 | 29750 | 53349.925 | -23599.925 |
| 2443 | 108 | 3646 | 53349.925 | -49703.925 |
| 2399 | 158 | 25270.3 | 25270.3 | 0 |
| 2402 | 161 | 600 | 600 | 0 |
| 2408 | 166 | 309 | 309 | 0 |
| 2358 | 105 | 7826 | 15344.125 | -7518.125 |
| 2356 | 105 | 29473.8 | 15344.125 | 14129.675 |
| 2417 | 105 | 1926.6 | 15344.125 | -13417.525 |
| 2439 | 105 | 22150.1 | 15344.125 | 6805.975 |
| 2394 | 109 | 21863 | 66313.9 | -44450.9 |
| 2362 | 109 | 92829.4 | 66313.9 | 26515.5 |
| 2421 | 109 | 72836 | 66313.9 | 6522.1 |
| 2444 | 109 | 77727.2 | 66313.9 | 11413.3 |
| 2380 | 143 | 27132.6 | 27132.6 | 0 |
| 2400 | 159 | 69286.4 | 69286.4 | 0 |
| 2403 | 162 | 220 | 220 | 0 |
| 2404 | 163 | 510 | 510 | 0 |
| 2410 | 168 | 45175 | 45175 | 0 |
| 2354 | 104 | 46257 | 36651.375 | 9605.625 |
| 2355 | 104 | 94513.5 | 36651.375 | 57862.125 |
| 2416 | 104 | 384 | 36651.375 | -36267.375 |
| 2438 | 104 | 5451 | 36651.375 | -31200.375 |
| 2371 | 118 | 79405.6 | 50495.9 | 28909.7 |
| 2457 | 118 | 21586.2 | 50495.9 | -28909.7 |
| 2455 | 145 | 14087.5 | 14343.58 | -256.08 |
| 2364 | 145 | 9500 | 14343.58 | -4843.58 |
| 2383 | 145 | 36374.7 | 14343.58 | 22031.12 |
| 2423 | 145 | 10367.7 | 14343.58 | -3975.88 |
| 2448 | 145 | 1388 | 14343.58 | -12955.58 |
| 2374 | 121 | 4797 | 4797 | 0 |
| 2377 | 141 | 38017.8 | 38017.8 | 0 |
| 2393 | 155 | 23431.9 | 23431.9 | 0 |
| 2458 | 101 | 78279.6 | 47598.775 | 30680.825 |
| 2447 | 101 | 33893.6 | 47598.775 | -13705.175 |
| 2413 | 101 | 48552 | 47598.775 | 953.225 |
| 2430 | 101 | 29669.9 | 47598.775 | -17928.875 |
| 2454 | 103 | 6653.4 | 5147.85 | 1505.55 |
| 2433 | 103 | 78 | 5147.85 | -5069.85 |
| 2415 | 103 | 310 | 5147.85 | -4837.85 |
| 2437 | 103 | 13550 | 5147.85 | 8402.15 |
| 2436 | 116 | 6394.8 | 8076.75 | -1681.95 |
| 2369 | 116 | 11097.4 | 8076.75 | 3020.65 |
| 2428 | 116 | 14685.8 | 8076.75 | 6609.05 |
| 2453 | 116 | 129 | 8076.75 | -7947.75 |
| 2373 | 120 | 416 | 416 | 0 |
| 2378 | 142 | 25691.3 | 25691.3 | 0 |
| 2389 | 151 | 17620 | 17620 | 0 |
| 2395 | 156 | 68501 | 68501 | 0 |
| 2398 | 157 | 7110.3 | 7110.3 | 0 |
| 2411 | 169 | 15760.5 | 15760.5 | 0 |
| 2379 | 146 | 17848.2 | 17692.52 | 155.68 |
| 2365 | 146 | 27455.3 | 17692.52 | 9762.78 |
| 2384 | 146 | 29249.1 | 17692.52 | 11556.58 |
| 2424 | 146 | 13824 | 17692.52 | -3868.52 |
| 2449 | 146 | 86 | 17692.52 | -17606.52 |
| 2406 | 148 | 2854.2 | 37140.1 | -34285.9 |
| 2367 | 148 | 144054.8 | 37140.1 | 106914.7 |
| 2386 | 148 | 21116.9 | 37140.1 | -16023.2 |
| 2426 | 148 | 7200 | 37140.1 | -29940.1 |
| 2451 | 148 | 10474.6 | 37140.1 | -26665.5 |
| 2434 | 149 | 268651.8 | 80623.94 | 188027.86 |
| 2368 | 149 | 60065 | 80623.94 | -20558.94 |
| 2387 | 149 | 52758.9 | 80623.94 | -27865.04 |
| 2427 | 149 | 9055 | 80623.94 | -71568.94 |
| 2452 | 149 | 12589 | 80623.94 | -68034.94 |
| 2375 | 122 | 103834.4 | 103834.4 | 0 |
| 2390 | 152 | 7616.8 | 7616.8 | 0 |
| 2412 | 170 | 66816 | 66816 | 0 |
| 2435 | 144 | 62303 | 32056.92 | 30246.08 |
| 2363 | 144 | 10082.3 | 32056.92 | -21974.62 |
| 2382 | 144 | 71173 | 32056.92 | 39116.08 |
| 2422 | 144 | 11188.5 | 32056.92 | -20868.42 |
| 2445 | 144 | 5537.8 | 32056.92 | -26519.12 |
| 2372 | 119 | 16447.2 | 16447.2 | 0 |
| 2391 | 153 | 48070.6 | 48070.6 | 0 |
| 2405 | 164 | 1233 | 1233 | 0 |
| 2409 | 167 | 48 | 48 | 0 |
| 2397 | 102 | 42283.2 | 17302.85 | 24980.35 |
| 2432 | 102 | 10523 | 17302.85 | -6779.85 |
| 2414 | 102 | 10794.6 | 17302.85 | -6508.25 |
| 2431 | 102 | 5610.6 | 17302.85 | -11692.25 |
| 2381 | 106 | 23034.6 | 9049.875 | 13984.725 |
| 2359 | 106 | 5543.1 | 9049.875 | -3506.775 |
| 2418 | 106 | 5546.6 | 9049.875 | -3503.275 |
| 2441 | 106 | 2075.2 | 9049.875 | -6974.675 |
| 2396 | 147 | 34930 | 74255.64 | -39325.64 |
| 2366 | 147 | 37319.4 | 74255.64 | -36936.24 |
| 2385 | 147 | 295892 | 74255.64 | 221636.36 |
| 2425 | 147 | 1500.8 | 74255.64 | -72754.84 |
| 2450 | 147 | 1636 | 74255.64 | -72619.64 |
| 2446 | 117 | 103679.3 | 39452.175 | 64227.125 |
| 2370 | 117 | 126 | 39452.175 | -39326.175 |
| 2429 | 117 | 50125 | 39452.175 | 10672.825 |
| 2456 | 117 | 3878.4 | 39452.175 | -35573.775 |
| 2376 | 123 | 11006.2 | 11006.2 | 0 |
| 2388 | 150 | 282694.3 | 282694.3 | 0 |
| 2392 | 154 | 26632 | 26632 | 0 |
| 2401 | 160 | 969.2 | 969.2 | 0 |
| 2407 | 165 | 2519 | 2519 | 0 |
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_id | job_id | salary | average_salary | difference |
|---|---|---|---|---|
| 103 | IT_PROG | 9000 | 5760 | 3240 |
| 168 | SA_REP | 11500 | 8350 | 3150 |
| 174 | SA_REP | 11000 | 8350 | 2650 |
| 162 | SA_REP | 10500 | 8350 | 2150 |
| 145 | SA_MAN | 14000 | 12200 | 1800 |
| 150 | SA_REP | 10000 | 8350 | 1650 |
| 156 | SA_REP | 10000 | 8350 | 1650 |
| 169 | SA_REP | 10000 | 8350 | 1650 |
| 146 | SA_MAN | 13500 | 12200 | 1300 |
| 170 | SA_REP | 9600 | 8350 | 1250 |
| 163 | SA_REP | 9500 | 8350 | 1150 |
| 157 | SA_REP | 9500 | 8350 | 1150 |
| 151 | SA_REP | 9500 | 8350 | 1150 |
| 109 | FI_ACCOUNT | 9000 | 7920 | 1080 |
| 184 | SH_CLERK | 4200 | 3215 | 985 |
| 121 | ST_MAN | 8200 | 7280 | 920 |
| 185 | SH_CLERK | 4100 | 3215 | 885 |
| 137 | ST_CLERK | 3600 | 2785 | 815 |
| 192 | SH_CLERK | 4000 | 3215 | 785 |
| 120 | ST_MAN | 8000 | 7280 | 720 |
| 141 | ST_CLERK | 3500 | 2785 | 715 |
| 193 | SH_CLERK | 3900 | 3215 | 685 |
| 152 | SA_REP | 9000 | 8350 | 650 |
| 158 | SA_REP | 9000 | 8350 | 650 |
| 122 | ST_MAN | 7900 | 7280 | 620 |
| 188 | SH_CLERK | 3800 | 3215 | 585 |
| 129 | ST_CLERK | 3300 | 2785 | 515 |
| 133 | ST_CLERK | 3300 | 2785 | 515 |
| 175 | SA_REP | 8800 | 8350 | 450 |
| 125 | ST_CLERK | 3200 | 2785 | 415 |
| 138 | ST_CLERK | 3200 | 2785 | 415 |
| 189 | SH_CLERK | 3600 | 3215 | 385 |
| 115 | PU_CLERK | 3100 | 2780 | 320 |
| 142 | ST_CLERK | 3100 | 2785 | 315 |
| 110 | FI_ACCOUNT | 8200 | 7920 | 280 |
| 176 | SA_REP | 8600 | 8350 | 250 |
| 104 | IT_PROG | 6000 | 5760 | 240 |
| 186 | SH_CLERK | 3400 | 3215 | 185 |
| 116 | PU_CLERK | 2900 | 2780 | 120 |
| 134 | ST_CLERK | 2900 | 2785 | 115 |
| 177 | SA_REP | 8400 | 8350 | 50 |
| 117 | PU_CLERK | 2800 | 2780 | 20 |
| 130 | ST_CLERK | 2800 | 2785 | 15 |
| 100 | AD_PRES | 24000 | 24000 | 0 |
| 101 | AD_VP | 17000 | 17000 | 0 |
| 102 | AD_VP | 17000 | 17000 | 0 |
| 108 | FI_MGR | 12008 | 12008 | 0 |
| 114 | PU_MAN | 11000 | 11000 | 0 |
| 200 | AD_ASST | 4400 | 4400 | 0 |
| 201 | MK_MAN | 13000 | 13000 | 0 |
| 202 | MK_REP | 6000 | 6000 | 0 |
| 203 | HR_REP | 6500 | 6500 | 0 |
| 204 | PR_REP | 10000 | 10000 | 0 |
| 205 | AC_MGR | 12008 | 12008 | 0 |
| 206 | AC_ACCOUNT | 8300 | 8300 | 0 |
| 180 | SH_CLERK | 3200 | 3215 | -15 |
| 194 | SH_CLERK | 3200 | 3215 | -15 |
| 126 | ST_CLERK | 2700 | 2785 | -85 |
| 139 | ST_CLERK | 2700 | 2785 | -85 |
| 196 | SH_CLERK | 3100 | 3215 | -115 |
| 181 | SH_CLERK | 3100 | 3215 | -115 |
| 112 | FI_ACCOUNT | 7800 | 7920 | -120 |
| 118 | PU_CLERK | 2600 | 2780 | -180 |
| 143 | ST_CLERK | 2600 | 2785 | -185 |
| 147 | SA_MAN | 12000 | 12200 | -200 |
| 187 | SH_CLERK | 3000 | 3215 | -215 |
| 197 | SH_CLERK | 3000 | 3215 | -215 |
| 111 | FI_ACCOUNT | 7700 | 7920 | -220 |
| 119 | PU_CLERK | 2500 | 2780 | -280 |
| 131 | ST_CLERK | 2500 | 2785 | -285 |
| 144 | ST_CLERK | 2500 | 2785 | -285 |
| 140 | ST_CLERK | 2500 | 2785 | -285 |
| 190 | SH_CLERK | 2900 | 3215 | -315 |
| 153 | SA_REP | 8000 | 8350 | -350 |
| 159 | SA_REP | 8000 | 8350 | -350 |
| 127 | ST_CLERK | 2400 | 2785 | -385 |
| 135 | ST_CLERK | 2400 | 2785 | -385 |
| 183 | SH_CLERK | 2800 | 3215 | -415 |
| 195 | SH_CLERK | 2800 | 3215 | -415 |
| 128 | ST_CLERK | 2200 | 2785 | -585 |
| 136 | ST_CLERK | 2200 | 2785 | -585 |
| 198 | SH_CLERK | 2600 | 3215 | -615 |
| 199 | SH_CLERK | 2600 | 3215 | -615 |
| 132 | ST_CLERK | 2100 | 2785 | -685 |
| 182 | SH_CLERK | 2500 | 3215 | -715 |
| 191 | SH_CLERK | 2500 | 3215 | -715 |
| 123 | ST_MAN | 6500 | 7280 | -780 |
| 160 | SA_REP | 7500 | 8350 | -850 |
| 154 | SA_REP | 7500 | 8350 | -850 |
| 171 | SA_REP | 7400 | 8350 | -950 |
| 105 | IT_PROG | 4800 | 5760 | -960 |
| 106 | IT_PROG | 4800 | 5760 | -960 |
| 113 | FI_ACCOUNT | 6900 | 7920 | -1020 |
| 172 | SA_REP | 7300 | 8350 | -1050 |
| 164 | SA_REP | 7200 | 8350 | -1150 |
| 148 | SA_MAN | 11000 | 12200 | -1200 |
| 178 | SA_REP | 7000 | 8350 | -1350 |
| 161 | SA_REP | 7000 | 8350 | -1350 |
| 155 | SA_REP | 7000 | 8350 | -1350 |
| 124 | ST_MAN | 5800 | 7280 | -1480 |
| 165 | SA_REP | 6800 | 8350 | -1550 |
| 107 | IT_PROG | 4200 | 5760 | -1560 |
| 149 | SA_MAN | 10500 | 12200 | -1700 |
| 166 | SA_REP | 6400 | 8350 | -1950 |
| 179 | SA_REP | 6200 | 8350 | -2150 |
| 167 | SA_REP | 6200 | 8350 | -2150 |
| 173 | SA_REP | 6100 | 8350 | -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.