Getting Started
How can I access the schemas, tables, and data used in the practice problems? Use livesql.oracle.com. All schemas and tables utilized in the current practice problem sets are readily available without the headache of software installation. Simply create an account and let the SQL mastering begin! For a subset of the problems, a script is required to be executed to create and subsequently populate the table referenced within the problem. Simply copy and paste the corresponding script provided in the Tables Requiring Script Execution section of the SQL PRACTICE PROBLEMS page.
Schema and Table Information
Information on the schemas, corresponding tables, and data used in the example problems can be found within the SQL PRACTICE PROBLEMS page. I recommend opening another browser window with this page pulled up for quick reference.
SELECT AND FROM CLAUSES
Problem 1
Retrieve all employee’s first names from the employees table.
hr.employees
| first_name |
|---|
| Ellen |
| Sundar |
| Mozhe |
| David |
| Hermann |
| Shelli |
| Amit |
| Elizabeth |
| Sarah |
| David |
| Laura |
| Harrison |
| Alexis |
| Anthony |
| Gerald |
| Nanette |
| John |
| Kelly |
| Karen |
| Curtis |
| Lex |
| Julia |
| Jennifer |
| Louise |
| Bruce |
| Alberto |
| Britney |
| Daniel |
| Pat |
| Kevin |
| Jean |
| Tayler |
| Adam |
| Timothy |
| Ki |
| Girard |
| William |
| Douglas |
| Kimberely |
| Nancy |
| Danielle |
| Peter |
| Michael |
| Shelley |
| Guy |
| Alexander |
| Alyssa |
| Charles |
| Vance |
| Payam |
| Alexander |
| Janette |
| Steven |
| Neena |
| Sundita |
| Renske |
| James |
| David |
| Jack |
| Diana |
| Jason |
| Steven |
| James |
| Mattea |
| Randall |
| Susan |
| Samuel |
| Allan |
| Irene |
| Kevin |
| Julia |
| Donald |
| Christopher |
| TJ |
| Lisa |
| Karen |
| Valli |
| Joshua |
| Randall |
| Hazel |
| Luis |
| Trenna |
| Den |
| Michael |
| John |
| Nandita |
| Ismael |
| John |
| Sarath |
| Lindsey |
| William |
| Stephen |
| Martha |
| Patrick |
| Jonathon |
| Winston |
| Sigal |
| Peter |
| Oliver |
| Jose Manuel |
| Peter |
| Clara |
| Shanta |
| Alana |
| Matthew |
| Jennifer |
| Eleni |
SELECT
hr.employees.first_name
FROM
hr.employees;
Problem 2
Retrieve the employee ID, hire date, and the job ID for each employee.
hr.employees
| employee_id | hire_date | job_id |
|---|---|---|
| 100 | 17-Jun-03 | AD_PRES |
| 101 | 21-Sep-05 | AD_VP |
| 102 | 13-Jan-01 | AD_VP |
| 103 | 3-Jan-06 | IT_PROG |
| 104 | 21-May-07 | IT_PROG |
| 105 | 25-Jun-05 | IT_PROG |
| 106 | 5-Feb-06 | IT_PROG |
| 107 | 7-Feb-07 | IT_PROG |
| 108 | 17-Aug-02 | FI_MGR |
| 109 | 16-Aug-02 | FI_ACCOUNT |
| 110 | 28-Sep-05 | FI_ACCOUNT |
| 111 | 30-Sep-05 | FI_ACCOUNT |
| 112 | 7-Mar-06 | FI_ACCOUNT |
| 113 | 7-Dec-07 | FI_ACCOUNT |
| 114 | 7-Dec-02 | PU_MAN |
| 115 | 18-May-03 | PU_CLERK |
| 116 | 24-Dec-05 | PU_CLERK |
| 117 | 24-Jul-05 | PU_CLERK |
| 118 | 15-Nov-06 | PU_CLERK |
| 119 | 10-Aug-07 | PU_CLERK |
| 120 | 18-Jul-04 | ST_MAN |
| 121 | 10-Apr-05 | ST_MAN |
| 122 | 1-May-03 | ST_MAN |
| 123 | 10-Oct-05 | ST_MAN |
| 124 | 16-Nov-07 | ST_MAN |
| 125 | 16-Jul-05 | ST_CLERK |
| 126 | 28-Sep-06 | ST_CLERK |
| 127 | 14-Jan-07 | ST_CLERK |
| 128 | 8-Mar-08 | ST_CLERK |
| 129 | 20-Aug-05 | ST_CLERK |
| 130 | 30-Oct-05 | ST_CLERK |
| 131 | 16-Feb-05 | ST_CLERK |
| 132 | 10-Apr-07 | ST_CLERK |
| 133 | 14-Jun-04 | ST_CLERK |
| 134 | 26-Aug-06 | ST_CLERK |
| 135 | 12-Dec-07 | ST_CLERK |
| 136 | 6-Feb-08 | ST_CLERK |
| 137 | 14-Jul-03 | ST_CLERK |
| 138 | 26-Oct-05 | ST_CLERK |
| 139 | 12-Feb-06 | ST_CLERK |
| 140 | 6-Apr-06 | ST_CLERK |
| 141 | 17-Oct-03 | ST_CLERK |
| 142 | 29-Jan-05 | ST_CLERK |
| 143 | 15-Mar-06 | ST_CLERK |
| 144 | 9-Jul-06 | ST_CLERK |
| 145 | 1-Oct-04 | SA_MAN |
| 146 | 5-Jan-05 | SA_MAN |
| 147 | 10-Mar-05 | SA_MAN |
| 148 | 15-Oct-07 | SA_MAN |
| 149 | 29-Jan-08 | SA_MAN |
| 150 | 30-Jan-05 | SA_REP |
| 151 | 24-Mar-05 | SA_REP |
| 152 | 20-Aug-05 | SA_REP |
| 153 | 30-Mar-06 | SA_REP |
| 154 | 9-Dec-06 | SA_REP |
| 155 | 23-Nov-07 | SA_REP |
| 156 | 30-Jan-04 | SA_REP |
| 157 | 4-Mar-04 | SA_REP |
| 158 | 1-Aug-04 | SA_REP |
| 159 | 10-Mar-05 | SA_REP |
| 160 | 15-Dec-05 | SA_REP |
| 161 | 3-Nov-06 | SA_REP |
| 162 | 11-Nov-05 | SA_REP |
| 163 | 19-Mar-07 | SA_REP |
| 164 | 24-Jan-08 | SA_REP |
| 165 | 23-Feb-08 | SA_REP |
| 166 | 24-Mar-08 | SA_REP |
| 167 | 21-Apr-08 | SA_REP |
| 168 | 11-Mar-05 | SA_REP |
| 169 | 23-Mar-06 | SA_REP |
| 170 | 24-Jan-06 | SA_REP |
| 171 | 23-Feb-07 | SA_REP |
| 172 | 24-Mar-07 | SA_REP |
| 173 | 21-Apr-08 | SA_REP |
| 174 | 11-May-04 | SA_REP |
| 175 | 19-Mar-05 | SA_REP |
| 176 | 24-Mar-06 | SA_REP |
| 177 | 23-Apr-06 | SA_REP |
| 178 | 24-May-07 | SA_REP |
| 179 | 4-Jan-08 | SA_REP |
| 180 | 24-Jan-06 | SH_CLERK |
| 181 | 23-Feb-06 | SH_CLERK |
| 182 | 21-Jun-07 | SH_CLERK |
| 183 | 3-Feb-08 | SH_CLERK |
| 184 | 27-Jan-04 | SH_CLERK |
| 185 | 20-Feb-05 | SH_CLERK |
| 186 | 24-Jun-06 | SH_CLERK |
| 187 | 7-Feb-07 | SH_CLERK |
| 188 | 14-Jun-05 | SH_CLERK |
| 189 | 13-Aug-05 | SH_CLERK |
| 190 | 11-Jul-06 | SH_CLERK |
| 191 | 19-Dec-07 | SH_CLERK |
| 192 | 4-Feb-04 | SH_CLERK |
| 193 | 3-Mar-05 | SH_CLERK |
| 194 | 1-Jul-06 | SH_CLERK |
| 195 | 17-Mar-07 | SH_CLERK |
| 196 | 24-Apr-06 | SH_CLERK |
| 197 | 23-May-06 | SH_CLERK |
| 198 | 21-Jun-07 | SH_CLERK |
| 199 | 13-Jan-08 | SH_CLERK |
| 200 | 17-Sep-03 | AD_ASST |
| 201 | 17-Feb-04 | MK_MAN |
| 202 | 17-Aug-05 | MK_REP |
| 203 | 7-Jun-02 | HR_REP |
| 204 | 7-Jun-02 | PR_REP |
| 205 | 7-Jun-02 | AC_MGR |
| 206 | 7-Jun-02 | AC_ACCOUNT |
SELECT
hr.employees.employee_id,
hr.employees.hire_date,
hr.employees.job_id
FROM
hr.employees;
Problem 3
Retrieve the department ID, department name, manager ID, and location ID for each department.
hr.departments
| department_id | department_name | manager_id | location_id |
|---|---|---|---|
| 10 | Administration | 200 | 1700 |
| 20 | Marketing | 201 | 1800 |
| 30 | Purchasing | 114 | 1700 |
| 40 | Human Resources | 203 | 2400 |
| 50 | Shipping | 121 | 1500 |
| 60 | IT | 103 | 1400 |
| 70 | Public Relations | 204 | 2700 |
| 80 | Sales | 145 | 2500 |
| 90 | Executive | 100 | 1700 |
| 100 | Finance | 108 | 1700 |
| 110 | Accounting | 205 | 1700 |
| 120 | Treasury | – | 1700 |
| 130 | Corporate Tax | – | 1700 |
| 140 | Control And Credit | – | 1700 |
| 150 | Shareholder Services | – | 1700 |
| 160 | Benefits | – | 1700 |
| 170 | Manufacturing | – | 1700 |
| 180 | Construction | – | 1700 |
| 190 | Contracting | – | 1700 |
| 200 | Operations | – | 1700 |
| 210 | IT Support | – | 1700 |
| 220 | NOC | – | 1700 |
| 230 | IT Helpdesk | – | 1700 |
| 240 | Government Sales | – | 1700 |
| 250 | Retail Sales | – | 1700 |
| 260 | Recruiting | – | 1700 |
| 270 | Payroll | – | 1700 |
SELECT
hr.departments.department_id,
hr.departments.department_name,
hr.departments.manager_id,
hr.departments.location_id
FROM
hr.departments;
Problem 4
Retrieve all employee IDs. Rename this column to read as “worker_id.”
hr.employees
| worker_id |
|---|
| 100 |
| 101 |
| 102 |
| 103 |
| 104 |
| 105 |
| 106 |
| 107 |
| 108 |
| 109 |
| 110 |
| 111 |
| 112 |
| 113 |
| 114 |
| 115 |
| 116 |
| 117 |
| 118 |
| 119 |
| 120 |
| 121 |
| 122 |
| 123 |
| 124 |
| 125 |
| 126 |
| 127 |
| 128 |
| 129 |
| 130 |
| 131 |
| 132 |
| 133 |
| 134 |
| 135 |
| 136 |
| 137 |
| 138 |
| 139 |
| 140 |
| 141 |
| 142 |
| 143 |
| 144 |
| 145 |
| 146 |
| 147 |
| 148 |
| 149 |
| 150 |
| 151 |
| 152 |
| 153 |
| 154 |
| 155 |
| 156 |
| 157 |
| 158 |
| 159 |
| 160 |
| 161 |
| 162 |
| 163 |
| 164 |
| 165 |
| 166 |
| 167 |
| 168 |
| 169 |
| 170 |
| 171 |
| 172 |
| 173 |
| 174 |
| 175 |
| 176 |
| 177 |
| 178 |
| 179 |
| 180 |
| 181 |
| 182 |
| 183 |
| 184 |
| 185 |
| 186 |
| 187 |
| 188 |
| 189 |
| 190 |
| 191 |
| 192 |
| 193 |
| 194 |
| 195 |
| 196 |
| 197 |
| 198 |
| 199 |
| 200 |
| 201 |
| 202 |
| 203 |
| 204 |
| 205 |
| 206 |
SELECT
hr.employees.employee_id AS worker_id
FROM
hr.employees;
Problem 5
For each job, retrieve the job code, job title, minimum salary, and maximum salary. Rename these columns to read as job_code, title, minimum_salary, and maximum_salary, respectively.
hr.jobs
| job_code | title | minimum_salary | maximum_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 |
SELECT
hr.jobs.job_id AS job_code,
hr.jobs.job_title AS title,
hr.jobs.min_salary AS minimum_salary,
hr.jobs.max_salary AS maximum_salary
FROM
hr.jobs;
Problem 6
Retrieve the job ID, job title, maximum salary, minimum salary, and salary range for each job. Salary range is defined as the difference between the maximum salary and minimum salary. Alias the difference as “salary_range.”
hr.jobs
| job_id | job_title | max_salary | min_salary | salary_range |
|---|---|---|---|---|
| AD_PRES | President | 40000 | 20080 | 19920 |
| AD_VP | Administration Vice President | 30000 | 15000 | 15000 |
| AD_ASST | Administration Assistant | 6000 | 3000 | 3000 |
| FI_MGR | Finance Manager | 16000 | 8200 | 7800 |
| FI_ACCOUNT | Accountant | 9000 | 4200 | 4800 |
| AC_MGR | Accounting Manager | 16000 | 8200 | 7800 |
| AC_ACCOUNT | Public Accountant | 9000 | 4200 | 4800 |
| SA_MAN | Sales Manager | 20080 | 10000 | 10080 |
| SA_REP | Sales Representative | 12008 | 6000 | 6008 |
| PU_MAN | Purchasing Manager | 15000 | 8000 | 7000 |
| PU_CLERK | Purchasing Clerk | 5500 | 2500 | 3000 |
| ST_MAN | Stock Manager | 8500 | 5500 | 3000 |
| ST_CLERK | Stock Clerk | 5000 | 2008 | 2992 |
| SH_CLERK | Shipping Clerk | 5500 | 2500 | 3000 |
| IT_PROG | Programmer | 10000 | 4000 | 6000 |
| MK_MAN | Marketing Manager | 15000 | 9000 | 6000 |
| MK_REP | Marketing Representative | 9000 | 4000 | 5000 |
| HR_REP | Human Resources Representative | 9000 | 4000 | 5000 |
| PR_REP | Public Relations Representative | 10500 | 4500 | 6000 |
SELECT
hr.jobs.job_id,
hr.jobs.job_title,
hr.jobs.max_salary,
hr.jobs.min_salary,
hr.jobs.max_salary - hr.jobs.min_salary AS salary_range
FROM
hr.jobs;
Problem 7
Retrieve the country id and country name for each country.
hr.countries
| country_id | country_name |
|---|---|
| AR | Argentina |
| AU | Australia |
| BE | Belgium |
| BR | Brazil |
| CA | Canada |
| CH | Switzerland |
| CN | China |
| DE | Germany |
| DK | Denmark |
| EG | Egypt |
| FR | France |
| IL | Israel |
| IN | India |
| IT | Italy |
| JP | Japan |
| KW | Kuwait |
| ML | Malaysia |
| MX | Mexico |
| NG | Nigeria |
| NL | Netherlands |
| SG | Singapore |
| UK | United Kingdom |
| US | United States of America |
| ZM | Zambia |
| ZW | Zimbabwe |
SELECT
hr.countries.country_id,
hr.countries.country_name
FROM
hr.countries;
Problem 8
Retrieve the employee ID, email, and phone number for each employee.
hr.employees
| employee_id | phone_number | |
|---|---|---|
| 100 | SKING | 515.123.4567 |
| 101 | NKOCHHAR | 515.123.4568 |
| 102 | LDEHAAN | 515.123.4569 |
| 103 | AHUNOLD | 590.423.4567 |
| 104 | BERNST | 590.423.4568 |
| 105 | DAUSTIN | 590.423.4569 |
| 106 | VPATABAL | 590.423.4560 |
| 107 | DLORENTZ | 590.423.5567 |
| 108 | NGREENBE | 515.124.4569 |
| 109 | DFAVIET | 515.124.4169 |
| 110 | JCHEN | 515.124.4269 |
| 111 | ISCIARRA | 515.124.4369 |
| 112 | JMURMAN | 515.124.4469 |
| 113 | LPOPP | 515.124.4567 |
| 114 | DRAPHEAL | 515.127.4561 |
| 115 | AKHOO | 515.127.4562 |
| 116 | SBAIDA | 515.127.4563 |
| 117 | STOBIAS | 515.127.4564 |
| 118 | GHIMURO | 515.127.4565 |
| 119 | KCOLMENA | 515.127.4566 |
| 120 | MWEISS | 650.123.1234 |
| 121 | AFRIPP | 650.123.2234 |
| 122 | PKAUFLIN | 650.123.3234 |
| 123 | SVOLLMAN | 650.123.4234 |
| 124 | KMOURGOS | 650.123.5234 |
| 125 | JNAYER | 650.124.1214 |
| 126 | IMIKKILI | 650.124.1224 |
| 127 | JLANDRY | 650.124.1334 |
| 128 | SMARKLE | 650.124.1434 |
| 129 | LBISSOT | 650.124.5234 |
| 130 | MATKINSO | 650.124.6234 |
| 131 | JAMRLOW | 650.124.7234 |
| 132 | TJOLSON | 650.124.8234 |
| 133 | JMALLIN | 650.127.1934 |
| 134 | MROGERS | 650.127.1834 |
| 135 | KGEE | 650.127.1734 |
| 136 | HPHILTAN | 650.127.1634 |
| 137 | RLADWIG | 650.121.1234 |
| 138 | SSTILES | 650.121.2034 |
| 139 | JSEO | 650.121.2019 |
| 140 | JPATEL | 650.121.1834 |
| 141 | TRAJS | 650.121.8009 |
| 142 | CDAVIES | 650.121.2994 |
| 143 | RMATOS | 650.121.2874 |
| 144 | PVARGAS | 650.121.2004 |
| 145 | JRUSSEL | 011.44.1344.429268 |
| 146 | KPARTNER | 011.44.1344.467268 |
| 147 | AERRAZUR | 011.44.1344.429278 |
| 148 | GCAMBRAU | 011.44.1344.619268 |
| 149 | EZLOTKEY | 011.44.1344.429018 |
| 150 | PTUCKER | 011.44.1344.129268 |
| 151 | DBERNSTE | 011.44.1344.345268 |
| 152 | PHALL | 011.44.1344.478968 |
| 153 | COLSEN | 011.44.1344.498718 |
| 154 | NCAMBRAU | 011.44.1344.987668 |
| 155 | OTUVAULT | 011.44.1344.486508 |
| 156 | JKING | 011.44.1345.429268 |
| 157 | PSULLY | 011.44.1345.929268 |
| 158 | AMCEWEN | 011.44.1345.829268 |
| 159 | LSMITH | 011.44.1345.729268 |
| 160 | LDORAN | 011.44.1345.629268 |
| 161 | SSEWALL | 011.44.1345.529268 |
| 162 | CVISHNEY | 011.44.1346.129268 |
| 163 | DGREENE | 011.44.1346.229268 |
| 164 | MMARVINS | 011.44.1346.329268 |
| 165 | DLEE | 011.44.1346.529268 |
| 166 | SANDE | 011.44.1346.629268 |
| 167 | ABANDA | 011.44.1346.729268 |
| 168 | LOZER | 011.44.1343.929268 |
| 169 | HBLOOM | 011.44.1343.829268 |
| 170 | TFOX | 011.44.1343.729268 |
| 171 | WSMITH | 011.44.1343.629268 |
| 172 | EBATES | 011.44.1343.529268 |
| 173 | SKUMAR | 011.44.1343.329268 |
| 174 | EABEL | 011.44.1644.429267 |
| 175 | AHUTTON | 011.44.1644.429266 |
| 176 | JTAYLOR | 011.44.1644.429265 |
| 177 | JLIVINGS | 011.44.1644.429264 |
| 178 | KGRANT | 011.44.1644.429263 |
| 179 | CJOHNSON | 011.44.1644.429262 |
| 180 | WTAYLOR | 650.507.9876 |
| 181 | JFLEAUR | 650.507.9877 |
| 182 | MSULLIVA | 650.507.9878 |
| 183 | GGEONI | 650.507.9879 |
| 184 | NSARCHAN | 650.509.1876 |
| 185 | ABULL | 650.509.2876 |
| 186 | JDELLING | 650.509.3876 |
| 187 | ACABRIO | 650.509.4876 |
| 188 | KCHUNG | 650.505.1876 |
| 189 | JDILLY | 650.505.2876 |
| 190 | TGATES | 650.505.3876 |
| 191 | RPERKINS | 650.505.4876 |
| 192 | SBELL | 650.501.1876 |
| 193 | BEVERETT | 650.501.2876 |
| 194 | SMCCAIN | 650.501.3876 |
| 195 | VJONES | 650.501.4876 |
| 196 | AWALSH | 650.507.9811 |
| 197 | KFEENEY | 650.507.9822 |
| 198 | DOCONNEL | 650.507.9833 |
| 199 | DGRANT | 650.507.9844 |
| 200 | JWHALEN | 515.123.4444 |
| 201 | MHARTSTE | 515.123.5555 |
| 202 | PFAY | 603.123.6666 |
| 203 | SMAVRIS | 515.123.7777 |
| 204 | HBAER | 515.123.8888 |
| 205 | SHIGGINS | 515.123.8080 |
| 206 | WGIETZ | 515.123.8181 |
SELECT
hr.employees.employee_id,
hr.employees.email,
hr.employees.phone_number
FROM
hr.employees;
Problem 9
Retrieve the job ID, job title, and minimum salary for each job at the company.
hr.jobs
| job_id | job_title | min_salary |
|---|---|---|
| AD_PRES | President | 20080 |
| AD_VP | Administration Vice President | 15000 |
| AD_ASST | Administration Assistant | 3000 |
| FI_MGR | Finance Manager | 8200 |
| FI_ACCOUNT | Accountant | 4200 |
| AC_MGR | Accounting Manager | 8200 |
| AC_ACCOUNT | Public Accountant | 4200 |
| SA_MAN | Sales Manager | 10000 |
| SA_REP | Sales Representative | 6000 |
| PU_MAN | Purchasing Manager | 8000 |
| PU_CLERK | Purchasing Clerk | 2500 |
| ST_MAN | Stock Manager | 5500 |
| ST_CLERK | Stock Clerk | 2008 |
| SH_CLERK | Shipping Clerk | 2500 |
| IT_PROG | Programmer | 4000 |
| MK_MAN | Marketing Manager | 9000 |
| MK_REP | Marketing Representative | 4000 |
| HR_REP | Human Resources Representative | 4000 |
| PR_REP | Public Relations Representative | 4500 |
SELECT
hr.jobs.job_id,
hr.jobs.job_title,
hr.jobs.min_salary
FROM
hr.jobs;
Problem 10
Retrieve the location ID, city, and state/province for all warehouse/department locations.
hr.locations
| location_id | city | state_province |
|---|---|---|
| 1000 | Roma | – |
| 1100 | Venice | – |
| 1200 | Tokyo | Tokyo Prefecture |
| 1300 | Hiroshima | – |
| 1400 | Southlake | Texas |
| 1500 | South San Francisco | California |
| 1600 | South Brunswick | New Jersey |
| 1700 | Seattle | Washington |
| 1800 | Toronto | Ontario |
| 1900 | Whitehorse | Yukon |
| 2000 | Beijing | – |
| 2100 | Bombay | Maharashtra |
| 2200 | Sydney | New South Wales |
| 2300 | Singapore | – |
| 2400 | London | – |
| 2500 | Oxford | Oxford |
| 2600 | Stretford | Manchester |
| 2700 | Munich | Bavaria |
| 2800 | Sao Paulo | Sao Paulo |
| 2900 | Geneva | Geneve |
| 3000 | Bern | BE |
| 3100 | Utrecht | Utrecht |
| 3200 | Mexico City | Distrito Federal, |
SELECT
hr.locations.location_id,
hr.locations.city,
hr.locations.state_province
FROM
hr.locations;
WHERE CLAUSE WITH COMPARISON OPERATORS
Problem 11
Retrieve the location ID, street address, city, and state/province for departments or warehouses located in Texas.
Solution
SELECT
hr.locations.location_id,
hr.locations.street_address,
hr.locations.city,
hr.locations.state_province
FROM
hr.locations
WHERE
hr.locations.state_province = 'Texas';
Problem 12
Retrieve the employee ID, job ID, and hire date for employees hired in year 2007 or later.
Solution
SELECT
hr.employees.employee_id,
hr.employees.job_id,
hr.employees.hire_date
FROM
hr.employees
WHERE
hr.employees.hire_date >= '01-JAN-07';
Problem 13
Retrieve the department ID, department name, and location ID for departments/warehouses with location IDs other than 1700.
Solution
SELECT
hr.departments.department_id,
hr.departments.department_name,
hr.departments.location_id
FROM
hr.departments
WHERE
hr.departments.location_id != 1700;
Problem 14
Retrieve the employee ID, first name, last name, and job ID for each programmer (i.e., those employees with a job ID of “IT_PROG.”
Solution
SELECT
hr.employees.employee_id,
hr.employees.first_name,
hr.employees.last_name,
hr.employees.job_id
FROM
hr.employees
WHERE
hr.employees.job_id = 'IT_PROG';
Problem 15
Retrieve the location ID, city, and state/province, and country ID for department/warehouse locations outside of the United States.
Solution
SELECT
hr.locations.location_id,
hr.locations.city,
hr.locations.state_province,
hr.locations.country_id
FROM
hr.locations
WHERE
hr.locations.country_id != 'US';
Problem 16
Retrieve the employee ID, job ID, and salary for employees with a salary of greater than or equal to $10,000.
Solution
SELECT
hr.employees.employee_id,
hr.employees.job_id,
hr.employees.salary
FROM
hr.employees
WHERE
hr.employees.salary >= 10000;
WHERE CLAUSE WITH IN, NOT IN, AND BETWEEN LOGICAL OPERATORS
Problem 17
Retrieve the location ID, city, state/province, and country ID for department/warehouse locations in the United States or Canada.
Solution
SELECT
hr.locations.location_id,
hr.locations.city,
hr.locations.state_province,
hr.locations.country_id
FROM
hr.locations
WHERE
hr.locations.country_id IN ('US', 'CA');
Problem 18
Retrieve the location ID, city, state/province, and country ID for department/warehouse locations outside the United States and Canada.
Solution
SELECT
hr.locations.location_id,
hr.locations.city,
hr.locations.state_province,
hr.locations.country_id
FROM
hr.locations
WHERE
hr.locations.country_id NOT IN ('US', 'CA');
Problem 19
Retrieve the employee ID and salary for all associates with a salary between $6,000 and $10,000, inclusive.
Solution
SELECT
hr.employees.employee_id,
hr.employees.salary
FROM
hr.employees
WHERE
hr.employees.salary BETWEEN 6000 AND 10000;
Problem 20
Retrieve the employee ID, first name, last name, job ID, hire date, and salary for all employees hired between 2004 and 2006, inclusive.
Solution
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 BETWEEN '01-JAN-04' AND '31-DEC-06';
Problem 21
Retrieve the employee ID, job ID, and salary for employees with job IDs other than IT_PROG, SA_MAN, and PU_CLERK.
Solution
SELECT
hr.employees.employee_id,
hr.employees.job_id,
hr.employees.salary
FROM
hr.employees
WHERE
hr.employees.job_id NOT IN ('IT_PROG', 'SA_MAN', 'PU_CLERK');
Problem 22
Retrieve the employee ID, job ID, hire date, and salary for employees with salaries between $15,000 and $30,000, inclusive.
Solution
SELECT
hr.employees.employee_id,
hr.employees.job_id,
hr.employees.hire_date,
hr.employees.salary
FROM
hr.employees
WHERE
hr.employees.salary BETWEEN 15000 AND 30000;
WHERE CLAUSE WITH LIKE AND NOT LIKE LOGICAL OPERATORS
Problem 23
Retrieve the employee ID and first names of employees with a first name starting with the letter ‘A.’
Solution
SELECT
hr.employees.employee_id,
hr.employees.first_name
FROM
hr.employees
WHERE
hr.employees.first_name LIKE 'A%';
Problem 24
Retrieve the employee ID and first names of employees with a first name starting with the letter ‘C’ followed by any sequence of characters and ending with the letter ‘s.’
Solution
SELECT
hr.employees.employee_id,
hr.employees.first_name
FROM
hr.employees
WHERE
hr.employees.first_name LIKE 'C%s';
Problem 25
Retrieve employee phone numbers where the first digit is a 5, 3rd to last digit is a 1, second to last digit is any number, and the final digit is a 9.
Solution
SELECT
hr.employees.phone_number
FROM
hr.employees
WHERE
hr.employees.phone_number LIKE '5%1_9';
Problem 26
Retrieve the employee ID and last name of employees with a last name not starting with the letter ‘B.’
Solution
SELECT
hr.employees.employee_id,
hr.employees.last_name
FROM
hr.employees
WHERE
hr.employees.last_name NOT LIKE 'B%';
Problem 27
Retrieve all employee IDs for employees with a phone number containing the sequence of digits, 423.
Solution
SELECT
hr.employees.employee_id,
hr.employees.phone_number
FROM
hr.employees
WHERE
hr.employees.phone_number LIKE '%423%';
Problem 28
Retrieve the employee ID and job ID for those employees with job IDs starting with the letters ‘AD.’
Solution
SELECT
hr.employees.employee_id,
hr.employees.job_id
FROM
hr.employees
WHERE
hr.employees.job_id LIKE 'AD%';
Problem 29
Retrieve the employee ID and salary for employees with a salary starting with the 1 digit followed by a single digit of any value and ending with 3 zeros (e.g., 14000).
Solution
SELECT
hr.employees.employee_id,
hr.employees.salary
FROM
hr.employees
WHERE
hr.employees.salary LIKE '1_000';
Problem 30
Retrieve the country ID and country name for country names starting with ‘A’ and ending in ‘a.’
Solution
SELECT
hr.countries.country_id,
hr.countries.country_name
FROM
hr.countries
WHERE
hr.countries.country_name LIKE 'A%a';
Problem 31
Retrieve the employee ID and phone number of employees where the phone number doesn’t begin with ‘650.’
Solution
SELECT
hr.employees.employee_id,
hr.employees.phone_number
FROM
hr.employees
WHERE
hr.employees.phone_number NOT LIKE '650%';
WHERE CLAUSE WITH IS NULL AND IS NOT NULL
Problem 32
Retrieve the department ID, department name, and manager ID for departments without a manager ID assigned.
Solution
SELECT
hr.departments.department_id,
hr.departments.department_name,
hr.departments.manager_id
FROM
hr.departments
WHERE
hr.departments.manager_id IS NULL;
Problem 33
Retrieve the department ID, department name, and manager ID for departments with a manager ID assigned.
Solution
SELECT
hr.departments.department_id,
hr.departments.department_name,
hr.departments.manager_id
FROM
hr.departments
WHERE
hr.departments.manager_id IS NOT NULL;
Problem 34
Retrieve the employee ID and commission percentage for those employees with existing commission percentages
Solution
SELECT
hr.employees.employee_id,
hr.employees.commission_pct
FROM
hr.employees
WHERE
hr.employees.commission_pct IS NOT NULL;
Problem 35
Retrieve the employee ID and commission percentage for those employees with non-existing commission percentages.
Solution
SELECT
hr.employees.employee_id,
hr.employees.commission_pct
FROM
hr.employees
WHERE
hr.employees.commission_pct IS NULL;
WHERE CLAUSE WITH MULTIPLE AND AND/OR LOGICAL OPERATORS
Problem 36
Retrieve the employee ID, job ID, salary, and manager ID for all employees with a manager ID of 100 or 108 and a salary greater than $12,000.
Solution
SELECT
hr.employees.employee_id,
hr.employees.job_id,
hr.employees.salary,
hr.employees.manager_id
FROM
hr.employees
WHERE
hr.employees.manager_id IN (100, 108)
AND hr.employees.salary > 12000;
Problem 37
Retrieve the job ID, job title, minimum salary, and maximum salary for management jobs or those jobs with a minimum salary greater than 20,000 and a maximum salary greater than 25,000.
Solution
SELECT
hr.jobs.job_id,
hr.jobs.job_title,
hr.jobs.min_salary,
hr.jobs.max_salary
FROM
hr.jobs
WHERE
hr.jobs.job_title LIKE '%Manager'
OR (hr.jobs.min_salary > 20000
AND hr.jobs.max_salary > 25000);
Problem 38
Retrieve the employee ID, job ID, and department ID employees for employees that meet one or more of the following conditions:
Department ID is equal to 90 and the job ID is equal to ‘AD_PRES’
Department ID is equal to 100 and the job ID is equal to ‘FI_ACCOUNT’
Employee ID is equal to 115
Solution
SELECT
hr.employees.employee_id,
hr.employees.job_id,
hr.employees.department_id
FROM
hr.employees
WHERE
(hr.employees.department_id = 90
AND hr.employees.job_id = 'AD_PRES')
OR (hr.employees.department_id = 100
AND hr.employees.job_id = 'FI_ACCOUNT')
OR hr.employees.employee_id = '115';
Problem 39
Retrieve the employee ID, hire date, job ID, and salary for employees hired on as programmers in year 2007.
Solution
SELECT
hr.employees.employee_id,
hr.employees.hire_date,
hr.employees.job_id,
hr.employees.salary
FROM
hr.employees
WHERE
hr.employees.hire_date >= '01-JAN-07'
AND hr.employees.hire_date < '01-JAN-08'
AND hr.employees.job_id = 'IT_PROG';
Problem 40
Retrieve the employee ID, hire date, job ID, and salary for employees that were hired on as programmers or in the year 2007.
Solution
SELECT
hr.employees.employee_id,
hr.employees.hire_date,
hr.employees.job_id,
hr.employees.salary
FROM
hr.employees
WHERE
(hr.employees.hire_date >= '01-JAN-07'
AND hr.employees.hire_date < '01-JAN-08')
OR hr.employees.job_id = 'IT_PROG';
Problem 41
Retrieve the employee ID, hire date, job ID, and salary for employees that were hired in year 2007 or were hired on as programmers with a salary greater than 5000.
Solution
SELECT
hr.employees.employee_id,
hr.employees.hire_date,
hr.employees.job_id,
hr.employees.salary
FROM
hr.employees
WHERE
(hr.employees.hire_date >= '01-JAN-07'
AND hr.employees.hire_date < '01-JAN-08')
OR (hr.employees.job_id = 'IT_PROG'
AND hr.employees.salary > 5000);
Problem 42
Retrieve the employee ID, hire date, job ID, manager ID, and salary for employees that were hired in year 2007 or were hired on as programmers with a salary greater than 5000 or employees without a manager ID assigned and with a salary greater than 5000.
Solution
SELECT
hr.employees.employee_id,
hr.employees.hire_date,
hr.employees.job_id,
hr.employees.manager_id,
hr.employees.salary
FROM
hr.employees
WHERE
(hr.employees.hire_date >= '01-JAN-07'
AND hr.employees.hire_date < '01-JAN-08')
OR ((hr.employees.job_id = 'IT_PROG'
OR hr.employees.manager_id IS NULL)
AND hr.employees.salary > 5000);
GROUP BY CLAUSE AND COMPUTING AGGREGATES
Problem 43
Retrieve the minimum salary, maximum salary, average salary, and total sum of salaries. Provide appropriate aliases for the aggregations.
Solution
SELECT
MIN(hr.employees.salary) AS minimum_salary,
MAX(hr.employees.salary) AS maximum_salary,
AVG(hr.employees.salary) AS average_salary,
SUM(hr.employees.salary) AS salary_summation
FROM
hr.employees;
Problem 44
For each job ID, compute the average employee salary with respect to the job ID. Provide an appropriate alias for the aggregation.
Solution
SELECT
hr.employees.job_id,
AVG(hr.employees.salary) AS average_salary
FROM
hr.employees
GROUP BY
hr.employees.job_id;
Problem 45
For each job ID, compute the minimum and maximum salary with respect to the job ID. Provide appropriate aliases for the aggregations.
Solution
SELECT
hr.employees.job_id,
MIN(hr.employees.salary) AS minimum_salary,
MAX(hr.employees.salary) AS maximum_salary
FROM
hr.employees
GROUP BY
hr.employees.job_id;
Problem 46
For each existing job ID and manager ID combination, compute the number of employees and the average salary for those employees. Provide appropriate aliases for the aggregations.
Solution
SELECT
hr.employees.job_id,
hr.employees.manager_id,
COUNT(hr.employees.salary) AS employee_count,
AVG(hr.employees.salary) AS average_salary
FROM
hr.employees
GROUP BY
hr.employees.job_id,
hr.employees.manager_id;
Problem 47
For each region ID, compute the number of countries assigned the region ID. Provide an appropriate alias for the aggregation.
Solution
SELECT
hr.countries.region_id,
COUNT(hr.countries.region_id) AS country_count
FROM
hr.countries
GROUP BY
hr.countries.region_id;
Problem 48
For each manager ID, compute the number of employees assigned the manager ID. Provide an appropriate alias for the aggregation.
Solution
SELECT
hr.employees.manager_id,
COUNT(hr.employees.employee_id) AS employee_count
FROM
hr.employees
GROUP BY
hr.employees.manager_id;
Problem 49
For each manager ID, compute the maximum salary for employees assigned the manager ID. Provide an appropriate alias for the aggregation.
Solution
SELECT
hr.employees.manager_id,
MAX(hr.employees.salary) AS maximum_salary
FROM
hr.employees
GROUP BY
hr.employees.manager_id;
Problem 50
For each country ID and state/province combination, compute the number of department/warehouse locations. Provide an appropriate alias for the aggregation.
Solution
SELECT
hr.locations.country_id,
hr.locations.state_province,
COUNT(hr.locations.state_province) AS warehouse_count
FROM
hr.locations
GROUP BY
hr.locations.country_id,
hr.locations.state_province;
HAVING CLAUSE
Problem 51
For each job ID, compute the minimum and maximum salary with respect to the job ID. Return only job IDs with an range (MAX Salary – MIN Salary for each job ID) of salaries greater than or equal to $3,000. Provide appropriate aliases for the aggregations.
Solution
SELECT
hr.employees.job_id,
MIN(hr.employees.salary) AS minimum_salary,
MAX(hr.employees.salary) AS maximum_salary
FROM
hr.employees
GROUP BY
hr.employees.job_id
HAVING
MAX(hr.employees.salary) - MIN(hr.employees.salary) >= 3000;
Problem 52
For each existing job ID and manager ID combination, compute the number of employees and the average salary for those employees. Return only those job ID and manager ID combinations for which at least two employees are employed under the respective job ID and manager ID combination. Provide appropriate aliases for the aggregations.
Solution
SELECT
hr.employees.job_id,
hr.employees.manager_id,
COUNT(hr.employees.salary) AS employee_count,
AVG(hr.employees.salary) AS average_salary
FROM
hr.employees
GROUP BY
hr.employees.job_id,
hr.employees.manager_id
HAVING
COUNT(hr.employees.salary) > 1;
Problem 53
Retrieve the department ID(s) and the sum of the employees’ salaries for each department ID for departments with a total salary of greater than $100,000. Provide an appropriate alias for the aggregation.
Solution
SELECT
hr.employees.department_id,
SUM(hr.employees.salary) AS total_salary
FROM
hr.employees
GROUP BY
hr.employees.department_id
HAVING
SUM(hr.employees.salary) > 100000;
Problem 54
Retrieve the department IDs and the number of employees where more than 5 employees are assigned/work in. Provide an appropriate alias for the aggregation.
Solution
SELECT
hr.employees.department_id,
COUNT(hr.employees.employee_id) AS employee_count
FROM
hr.employees
GROUP BY
hr.employees.department_id
HAVING
COUNT(hr.employees.employee_id) > 5;
Problem 55
Retrieve the employee ID(s) and number of jobs each employee has held for employees that have held more than 2 jobs. Provide an appropriate alias for the aggregation.
Solution
SELECT
hr.job_history.employee_id,
COUNT(*) AS job_count
FROM
hr.job_history
GROUP BY
hr.job_history.employee_id
HAVING
COUNT(*) > 1;
ORDER BY CLAUSE
Problem 56
Retrieve employee IDs, job IDs, hire dates, and salaries for all employees. Sort/order the result set by hire date in ascending order and by salary in descending order.
Solution
SELECT
hr.employees.employee_id,
hr.employees.job_id,
hr.employees.hire_date,
hr.employees.salary
FROM
hr.employees
ORDER BY
hr.employees.hire_date,
hr.employees.salary DESC
Problem 57
Retrieve country IDs, country names, and region IDs for all countries. Sort/order the result set by region ID in ascending order and by country name in ascending order.
Solution
SELECT
hr.countries.country_id,
hr.countries.country_name,
hr.countries.region_id
FROM
hr.countries
ORDER BY
hr.countries.region_id,
hr.countries.country_name;
Problem 58
Retrieve the employee ID and hire date for each employee. Sort/order the result set by hire date in ascending order (i.e., oldest to newest).
Solution
SELECT
hr.employees.employee_id,
hr.employees.job_id,
hr.employees.hire_date
FROM
hr.employees
ORDER BY
hr.employees.hire_date;
Problem 59
Retrieve the employee ID, job ID, and salary for each employee. Sort/order the result set by salary in descending order (i.e., largest salary to smallest salary).
Solution
SELECT
hr.employees.employee_id,
hr.employees.job_id,
hr.employees.salary
FROM
hr.employees
ORDER BY
hr.employees.salary DESC;
Problem 60
Retrieve the manager ID, employee ID, and salary for each employee. Sort/order the result set by manager ID (ascending) and within each manager, sort employee salaries in descending order.
Solution
SELECT
hr.employees.manager_id,
hr.employees.employee_id,
hr.employees.salary
FROM
hr.employees
ORDER BY
hr.employees.manager_id,
hr.employees.salary DESC;
Problem 61
For each country ID, return the number of departments/warehouses. Sort/order the result set by the number of departments/warehouses located in each country. Provide an appropriate alias for the aggregation.
Solution
SELECT
hr.locations.country_id,
COUNT(hr.locations.country_id) AS warehouse_count
FROM
hr.locations
GROUP BY
hr.locations.country_id
ORDER BY
warehouse_count DESC;
ASSORTED PROBLEMS
Problem 62
Write a query that returns a result set containing, for each product, the product ID, product name, product description, list price, and minimum price. In addition, include a derived column containing the difference between the listing price and the minimum price. Alias this column as “amount_above_min.”
Solution
SELECT
oe.product_information.product_id,
oe.product_information.product_name,
oe.product_information.product_description,
oe.product_information.list_price,
oe.product_information.min_price,
oe.product_information.list_price - oe.product_information.min_price AS amount_above_min
FROM
oe.product_information;
Problem 63
Write a query that returns a result set containing, for each customer, the customer ID, customer first name, customer last name, and credit limit. Only include customers with a last name starting with ‘A’ that also have a credit limit less than or equal to 1200. Sort the result set by credit limit, ascending.
Solution
SELECT
oe.customers.customer_id,
oe.customers.cust_first_name,
oe.customers.cust_last_name,
oe.customers.credit_limit
FROM
oe.customers
WHERE
oe.customers.cust_last_name LIKE 'A%'
AND oe.customers.credit_limit <= 1200
ORDER BY
oe.customers.credit_limit;
Problem 64
Write a query that returns a result set containing, for each order, the order ID, order date, and order total. Only include orders placed in year 2008 and later with an order total greater than $50,000 or for those orders placed in year 2007 with an order total greater than $25,000. Sort the result set by order date (ascending) and by order total (descending).
Solution
SELECT
oe.orders.order_id,
oe.orders.order_date,
oe.orders.order_total
FROM
oe.orders
WHERE
(oe.orders.order_date >= '01-JAN-08'
AND oe.orders.order_total > 50000)
OR (oe.orders.order_date BETWEEN '01-JAN-07' AND '31-DEC-07'
AND oe.orders.order_total > 25000)
ORDER BY
oe.orders.order_date,
oe.orders.order_total DESC;
Problem 65
Write a query that returns a result set containing the number of customers for each gender and credit limit combination. Gender and credit limit data should be included in the result set. Sort the result set by credit limit (ascending) and gender (ascending).
Solution
SELECT
oe.customers.gender,
oe.customers.credit_limit,
COUNT(oe.customers.gender)
FROM
oe.customers
GROUP BY
oe.customers.gender,
oe.customers.credit_limit
ORDER BY
oe.customers.credit_limit,
oe.customers.gender;
Problem 66
Write a query that returns a result set containing the number of customers for each natural language support territory (nls_territory), gender, and credit limit combination. NLS territory, gender, and credit limit data should be included in the result set. Only include those combinations for which a single customer is aligned/belongs. Sort the result set by NLS territory (ascending), credit limit (ascending), and gender (ascending).
Solution
SELECT
oe.customers.nls_territory,
oe.customers.gender,
oe.customers.credit_limit,
COUNT(*)
FROM
oe.customers
GROUP BY
oe.customers.nls_territory,
oe.customers.gender,
oe.customers.credit_limit
HAVING
COUNT(*) = 1
ORDER BY
oe.customers.nls_territory,
oe.customers.credit_limit,
oe.customers.gender;
Problem 67
For each order, retrieve the order ID, order date, customer ID, order total, and sales rep ID. Sort the result set by customer ID (ascending) and order date (ascending).
Solution
SELECT
oe.orders.order_id,
oe.orders.order_date,
oe.orders.customer_id,
oe.orders.order_total,
oe.orders.sales_rep_id
FROM
oe.orders
ORDER BY
oe.orders.customer_id,
oe.orders.order_date;
Problem 68
Retrieve only those orders with an order total greater than $100,000. Include the order ID, order date, customer ID, order total, and sales rep ID for each order. Sort the result set by customer ID (ascending) and order date (ascending).
Solution
SELECT
oe.orders.order_id,
oe.orders.order_date,
oe.orders.customer_id,
oe.orders.order_total,
oe.orders.sales_rep_id
FROM
oe.orders
WHERE
oe.orders.order_total > 100000
ORDER BY
oe.orders.customer_id,
oe.orders.order_date;
Problem 69
For each order, retrieve the order ID, line-item ID, product ID, unit price, and quantity of the first line item.
Solution
SELECT
oe.order_items.order_id,
oe.order_items.line_item_id,
oe.order_items.product_id,
oe.order_items.unit_price,
oe.order_items.quantity
FROM
oe.order_items
WHERE
oe.order_items.line_item_id = 1;
Problem 70
For each order, retrieve the order ID, order date, and order total. Only include those orders with an order total between $75,000 and $100,000, inclusive. Sort the result set by order total, descending.
Solution
SELECT
oe.orders.order_id,
oe.orders.order_date,
oe.orders.order_total
FROM
oe.orders
WHERE
oe.orders.order_total BETWEEN 75000 AND 100000
ORDER BY
oe.orders.order_total DESC;
Problem 71
Retrieve all customers with a national language support territory (nls_territory) of America, India, or China. Include the customer ID, customer first name, customer last name, and NLS territory for each customer. Sort the result set by NLS territory (ascending) and customer ID (ascending).
Solution
SELECT
oe.customers.customer_id,
oe.customers.cust_first_name,
oe.customers.cust_last_name,
oe.customers.nls_territory
FROM
oe.customers
WHERE
oe.customers.nls_territory IN ('AMERICA', 'INDIA', 'CHINA')
ORDER BY
oe.customers.nls_territory,
oe.customers.customer_id;
Problem 72
Retrieve product information for all products with product names starting with ‘Inkjet.’ Include the product ID, product name, and product description for each product.
Solution
SELECT
oe.product_information.product_id,
oe.product_information.product_name,
oe.product_information.product_description
FROM
oe.product_information
WHERE
oe.product_information.product_name LIKE 'Inkjet%';
Problem 73
Retrieve the product ID, supplier ID, and catalog URL for products with a catalog URL containing the string, 102094.
Solution
SELECT
oe.product_information.product_id,
oe.product_information.supplier_id,
oe.product_information.catalog_url
FROM
oe.product_information
WHERE
oe.product_information.catalog_url LIKE '%102094%';
Problem 74
Retrieve the order ID, order date, order total, customer ID, and sales rep ID. Only include online orders in your result set. Assume all orders without a sales rep ID are online orders.
Solution
SELECT
oe.orders.order_id,
oe.orders.order_date,
oe.orders.order_total,
oe.orders.customer_id,
oe.orders.sales_rep_id
FROM
oe.orders
WHERE
oe.orders.sales_rep_id IS NULL;
Problem 75
Retrieve online orders. Only include online orders placed in year 2007 or online orders with an order total greater than or equal to $100,000. Include the order ID, order date, order total, and sales rep ID for each online order. Assume all orders without a sales rep ID are online orders. Sort the result set by order date, ascending.
Solution
SELECT
oe.orders.order_id,
oe.orders.order_date,
oe.orders.order_total,
oe.orders.sales_rep_id
FROM
oe.orders
WHERE
oe.orders.sales_rep_id IS NULL
AND (oe.orders.order_date BETWEEN '01-JAN-07' AND '31-DEC-07'
OR oe.orders.order_total >= 100000)
ORDER BY
oe.orders.order_date;
Problem 76
Retrieve product information for products that have a minimum price greater than or equal to $1,000 or less than or equal to $10. In addition, only include products with a category ID of 19. Include the product ID, product name, category ID, and minimum price for each product. Sort the result set by minimum price, descending.
Solution
SELECT
oe.product_information.product_id,
oe.product_information.product_name,
oe.product_information.category_id,
oe.product_information.min_price
FROM
oe.product_information
WHERE
(oe.product_information.min_price >= 1000
OR oe.product_information.min_price <= 10)
AND oe.product_information.category_id = 19
ORDER BY
oe.product_information.min_price DESC;
Problem 77
Compute/retrieve the minimum order total, maximum order total, sum of all order totals, the number of orders placed, and the average of all order totals. Provide meaningful aliases/names to the derived columns.
Solution
SELECT
MIN(oe.orders.order_total) AS min_order_total,
MAX(oe.orders.order_total) AS max_order_total,
SUM(oe.orders.order_total) AS order_total,
COUNT(oe.orders.order_total) AS order_count,
AVG(oe.orders.order_total) AS average_order_total
FROM
oe.orders;
Problem 78
For each customer, compute/retrieve the minimum order total, maximum order total, sum of all order totals, the number of orders placed, and the average of all order totals. Provide meaningful aliases/names to the derived columns. The customer ID should be the first column in your result set. Sort the result set by the sum of order totals, descending.
Solution
SELECT
oe.orders.customer_id,
MIN(oe.orders.order_total) AS min_order_total,
MAX(oe.orders.order_total) AS max_order_total,
SUM(oe.orders.order_total) AS order_total,
COUNT(oe.orders.order_total) AS order_count,
AVG(oe.orders.order_total) AS average_order_total
FROM
oe.orders
GROUP BY
oe.orders.customer_id
ORDER BY
SUM(oe.orders.order_total) DESC;
Problem 79
For each product ID, compute/retrieve the number orders placed for that item. The product ID should be the first column in your result set. Provide a meaningful alias/name to the derived column. Sort the result set by the number of times a product was ordered, descending.
Solution
SELECT
oe.order_items.product_id,
COUNT(oe.order_items.product_id) AS order_count
FROM
oe.order_items
GROUP BY
oe.order_items.product_id
ORDER BY
order_count DESC;
Problem 80
For each existing account manager ID and credit limit combination, compute/retrieve the number of customers aligned to the manager ID and credit limit combination. Account manager ID and credit limit should be included in your result set. Provide a meaningful alias/name to the derived column. Sort the result set by account manager ID (ascending) and by credit limit (descending).
Solution
SELECT
oe.customers.account_mgr_id,
oe.customers.credit_limit,
COUNT(oe.customers.credit_limit) AS customer_count
FROM
oe.customers
GROUP BY
oe.customers.account_mgr_id,
oe.customers.credit_limit
ORDER BY
oe.customers.account_mgr_id,
oe.customers.credit_limit DESC;
Problem 81
Retrieve the number of products containing product information for each product category ID. Only include those product category IDs with more than 25 products aligned to the product category ID. The product category ID should be the first column in your result set. Provide a meaningful alias/name to the derived column. Sort the result set by the number of products in each product category ID, descending.
Solution
SELECT
oe.product_information.category_id,
COUNT(oe.product_information.category_id) AS product_count
FROM
oe.product_information
GROUP BY
oe.product_information.category_id
HAVING
COUNT(oe.product_information.category_id) > 25
ORDER BY
product_count DESC;
Problem 82
For each customer that has placed an order, compute/retrieve the date the customer made their first order and the sum of all order totals. Only include customers that made their first order before year 2007 and have a sum of all order totals greater than $150,000. The customer ID should be the first column in your result set. Provide meaningful aliases/names to the derived columns. Sort the result set by the first order’s order date.
Solution
SELECT
oe.orders.customer_id,
MIN(oe.orders.order_date) AS first_order_date,
SUM(oe.orders.order_total) AS order_total
FROM
oe.orders
GROUP BY
oe.orders.customer_id
HAVING
MIN(oe.orders.order_date) <= '31-DEC-06'
AND SUM(oe.orders.order_total) > 150000
ORDER BY
MIN(oe.orders.order_date);
Problem 83
Retrieve the daily product category sales for April 2023. In the output, include the report date, product category, and total sales. Sort the result set in ascending order by report date and product category.
north_america_sale
| report_date | product_category | total_sale |
|---|---|---|
| 01-APR-23 | Hardware | 390594 |
| 01-APR-23 | Office Supplies | 320527 |
| 01-APR-23 | Software | 141595 |
| 02-APR-23 | Hardware | 464361 |
| 02-APR-23 | Office Supplies | 487635 |
| 02-APR-23 | Software | 183160 |
| 03-APR-23 | Hardware | 487048 |
| 03-APR-23 | Office Supplies | 109011 |
| 03-APR-23 | Software | 179908 |
| 04-APR-23 | Hardware | 437387 |
| 04-APR-23 | Office Supplies | 171126 |
| 04-APR-23 | Software | 156679 |
| 05-APR-23 | Hardware | 163396 |
| 05-APR-23 | Office Supplies | 299903 |
| 05-APR-23 | Software | 125223 |
| 06-APR-23 | Hardware | 241845 |
| 06-APR-23 | Office Supplies | 442197 |
| 06-APR-23 | Software | 121008 |
| 07-APR-23 | Hardware | 454711 |
| 07-APR-23 | Office Supplies | 405102 |
| 07-APR-23 | Software | 221383 |
| 08-APR-23 | Hardware | 251905 |
| 08-APR-23 | Office Supplies | 220887 |
| 08-APR-23 | Software | 107507 |
| 09-APR-23 | Hardware | 227411 |
| 09-APR-23 | Office Supplies | 265518 |
| 09-APR-23 | Software | 310617 |
| 10-APR-23 | Hardware | 310046 |
| 10-APR-23 | Office Supplies | 150205 |
| 10-APR-23 | Software | 422232 |
| 11-APR-23 | Hardware | 320621 |
| 11-APR-23 | Office Supplies | 411607 |
| 11-APR-23 | Software | 332198 |
| 12-APR-23 | Hardware | 274390 |
| 12-APR-23 | Office Supplies | 366354 |
| 12-APR-23 | Software | 384063 |
| 13-APR-23 | Hardware | 382678 |
| 13-APR-23 | Office Supplies | 169193 |
| 13-APR-23 | Software | 280966 |
| 14-APR-23 | Hardware | 158199 |
| 14-APR-23 | Office Supplies | 405273 |
| 14-APR-23 | Software | 288234 |
| 15-APR-23 | Hardware | 375867 |
| 15-APR-23 | Office Supplies | 242540 |
| 15-APR-23 | Software | 312305 |
| 16-APR-23 | Hardware | 306784 |
| 16-APR-23 | Office Supplies | 410744 |
| 16-APR-23 | Software | 457762 |
| 17-APR-23 | Hardware | 128819 |
| 17-APR-23 | Office Supplies | 393735 |
| 17-APR-23 | Software | 134756 |
| 18-APR-23 | Hardware | 277928 |
| 18-APR-23 | Office Supplies | 185693 |
| 18-APR-23 | Software | 156509 |
| 19-APR-23 | Hardware | 352213 |
| 19-APR-23 | Office Supplies | 118006 |
| 19-APR-23 | Software | 475899 |
| 20-APR-23 | Hardware | 105392 |
| 20-APR-23 | Office Supplies | 274407 |
| 20-APR-23 | Software | 486045 |
| 21-APR-23 | Hardware | 412107 |
| 21-APR-23 | Office Supplies | 307669 |
| 21-APR-23 | Software | 137841 |
| 22-APR-23 | Hardware | 440743 |
| 22-APR-23 | Office Supplies | 211778 |
| 22-APR-23 | Software | 420618 |
| 23-APR-23 | Hardware | 306189 |
| 23-APR-23 | Office Supplies | 446075 |
| 23-APR-23 | Software | 437481 |
| 24-APR-23 | Hardware | 309242 |
| 24-APR-23 | Office Supplies | 300044 |
| 24-APR-23 | Software | 369096 |
| 25-APR-23 | Hardware | 433689 |
| 25-APR-23 | Office Supplies | 330579 |
| 25-APR-23 | Software | 347470 |
| 26-APR-23 | Hardware | 465270 |
| 26-APR-23 | Office Supplies | 458689 |
| 26-APR-23 | Software | 147642 |
| 27-APR-23 | Hardware | 345373 |
| 27-APR-23 | Office Supplies | 256230 |
| 27-APR-23 | Software | 131632 |
| 28-APR-23 | Hardware | 478742 |
| 28-APR-23 | Office Supplies | 487975 |
| 28-APR-23 | Software | 159253 |
| 29-APR-23 | Hardware | 308098 |
| 29-APR-23 | Office Supplies | 172834 |
| 29-APR-23 | Software | 235898 |
| 30-APR-23 | Hardware | 370109 |
| 30-APR-23 | Office Supplies | 279783 |
| 30-APR-23 | Software | 429613 |
SELECT
north_america_sale.report_date,
north_america_sale.product_category,
north_america_sale.total_sale
FROM
north_america_sale
WHERE
north_america_sale.report_date >= '01-APR-23'
AND north_america_sale.report_date < '01-MAY-23'
ORDER BY
north_america_sale.report_date,
north_america_sale.product_category;
Problem 84
Building onto your solution from problem 83, retrieve only those daily sales for the Software product category. Remove the sorting based on product category.
north_america_sale
| report_date | product_category | total_sale |
|---|---|---|
| 01-APR-23 | Software | 141595 |
| 02-APR-23 | Software | 183160 |
| 03-APR-23 | Software | 179908 |
| 04-APR-23 | Software | 156679 |
| 05-APR-23 | Software | 125223 |
| 06-APR-23 | Software | 121008 |
| 07-APR-23 | Software | 221383 |
| 08-APR-23 | Software | 107507 |
| 09-APR-23 | Software | 310617 |
| 10-APR-23 | Software | 422232 |
| 11-APR-23 | Software | 332198 |
| 12-APR-23 | Software | 384063 |
| 13-APR-23 | Software | 280966 |
| 14-APR-23 | Software | 288234 |
| 15-APR-23 | Software | 312305 |
| 16-APR-23 | Software | 457762 |
| 17-APR-23 | Software | 134756 |
| 18-APR-23 | Software | 156509 |
| 19-APR-23 | Software | 475899 |
| 20-APR-23 | Software | 486045 |
| 21-APR-23 | Software | 137841 |
| 22-APR-23 | Software | 420618 |
| 23-APR-23 | Software | 437481 |
| 24-APR-23 | Software | 369096 |
| 25-APR-23 | Software | 347470 |
| 26-APR-23 | Software | 147642 |
| 27-APR-23 | Software | 131632 |
| 28-APR-23 | Software | 159253 |
| 29-APR-23 | Software | 235898 |
| 30-APR-23 | Software | 429613 |
SELECT
north_america_sale.report_date,
north_america_sale.product_category,
north_america_sale.total_sale
FROM
north_america_sale
WHERE
north_america_sale.report_date >= '01-APR-23'
AND north_america_sale.report_date < '01-MAY-23'
AND north_america_sale.product_category = 'Software'
ORDER BY
north_america_sale.report_date;
Problem 85
Compute the total sales across all product categories for each day occurring in the months of July, August, and September of 2023. Alias the computed sale amount column as ‘daily_total_sale’. In the output, include the report date and daily total sales. Sort the result set in ascending order by report date.
north_america_sale
| report_date | daily_total_sale |
|---|---|
| 01-JUL-23 | 814774 |
| 02-JUL-23 | 826253 |
| 03-JUL-23 | 776726 |
| 04-JUL-23 | 511317 |
| 05-JUL-23 | 806657 |
| 06-JUL-23 | 779799 |
| 07-JUL-23 | 1147623 |
| 08-JUL-23 | 604928 |
| 09-JUL-23 | 907319 |
| 10-JUL-23 | 565389 |
| 11-JUL-23 | 1124202 |
| 12-JUL-23 | 943577 |
| 13-JUL-23 | 1205384 |
| 14-JUL-23 | 1187425 |
| 15-JUL-23 | 1051398 |
| 16-JUL-23 | 1057940 |
| 17-JUL-23 | 415800 |
| 18-JUL-23 | 721071 |
| 19-JUL-23 | 647166 |
| 20-JUL-23 | 530326 |
| 21-JUL-23 | 697593 |
| 22-JUL-23 | 580278 |
| 23-JUL-23 | 651665 |
| 24-JUL-23 | 988073 |
| 25-JUL-23 | 992175 |
| 26-JUL-23 | 1010617 |
| 27-JUL-23 | 1109915 |
| 28-JUL-23 | 995753 |
| 29-JUL-23 | 1010636 |
| 30-JUL-23 | 996739 |
| 31-JUL-23 | 890989 |
| 01-AUG-23 | 695166 |
| 02-AUG-23 | 371637 |
| 03-AUG-23 | 869690 |
| 04-AUG-23 | 1034226 |
| 05-AUG-23 | 1028273 |
| 06-AUG-23 | 1195365 |
| 07-AUG-23 | 965429 |
| 08-AUG-23 | 723448 |
| 09-AUG-23 | 923629 |
| 10-AUG-23 | 1260461 |
| 11-AUG-23 | 830229 |
| 12-AUG-23 | 800827 |
| 13-AUG-23 | 1108060 |
| 14-AUG-23 | 783417 |
| 15-AUG-23 | 1078274 |
| 16-AUG-23 | 944669 |
| 17-AUG-23 | 972297 |
| 18-AUG-23 | 765486 |
| 19-AUG-23 | 858946 |
| 20-AUG-23 | 1420514 |
| 21-AUG-23 | 1060476 |
| 22-AUG-23 | 863389 |
| 23-AUG-23 | 947669 |
| 24-AUG-23 | 576202 |
| 25-AUG-23 | 876318 |
| 26-AUG-23 | 796264 |
| 27-AUG-23 | 1100756 |
| 28-AUG-23 | 628789 |
| 29-AUG-23 | 1026234 |
| 30-AUG-23 | 979660 |
| 31-AUG-23 | 647812 |
SELECT
north_america_sale.report_date,
SUM(north_america_sale.total_sale) AS daily_total_sale
FROM
north_america_sale
WHERE
north_america_sale.report_date >= '01-JUL-23'
AND north_america_sale.report_date < '01-SEP-23'
GROUP BY
north_america_sale.report_date
ORDER BY
north_america_sale.report_date;
Problem 86
Building onto your previous solution from problem 85, only include those days where the daily total sales amount is greater than 1,000,000. Sort the result set in ascending order by report date.
north_america_sale
| report_date | daily_total_sale |
|---|---|
| 07-JUL-23 | 1147623 |
| 11-JUL-23 | 1124202 |
| 13-JUL-23 | 1205384 |
| 14-JUL-23 | 1187425 |
| 15-JUL-23 | 1051398 |
| 16-JUL-23 | 1057940 |
| 26-JUL-23 | 1010617 |
| 27-JUL-23 | 1109915 |
| 29-JUL-23 | 1010636 |
| 04-AUG-23 | 1034226 |
| 05-AUG-23 | 1028273 |
| 06-AUG-23 | 1195365 |
| 10-AUG-23 | 1260461 |
| 13-AUG-23 | 1108060 |
| 15-AUG-23 | 1078274 |
| 20-AUG-23 | 1420514 |
| 21-AUG-23 | 1060476 |
| 27-AUG-23 | 1100756 |
| 29-AUG-23 | 1026234 |
SELECT
north_america_sale.report_date,
SUM(north_america_sale.total_sale) AS daily_total_sale
FROM
north_america_sale
WHERE
north_america_sale.report_date >= '01-JUL-23'
AND north_america_sale.report_date < '01-SEP-23'
GROUP BY
north_america_sale.report_date
HAVING
SUM(north_america_sale.total_sale) > 1000000
ORDER BY
north_america_sale.report_date;
Problem 87
For each member type, compute the total number of members. Alias the total number of members of each member type as “member_count.” In the output, include the member type ID and member count. Sort the result set in descending order by member_count.
member
| member_type_id | member_count |
|---|---|
| 5 | 25 |
| 2 | 22 |
| 3 | 19 |
| 1 | 19 |
| 4 | 15 |
SELECT
member.member_type_id,
COUNT(member.member_type_id) AS member_count
FROM
member
GROUP BY
member.member_type_id
ORDER BY
COUNT(member.member_type_id) DESC;
Problem 88
For each employee, compute the total number of members enrolled. Alias the total number of members of each member type as “enrollment_count.” In the output, include the employee ID and enrollment count. Sort the result set in descending order by enrollment_count.
member
| employee_id | enrollment_count |
|---|---|
| 5 | 21 |
| 6 | 15 |
| 2 | 15 |
| 3 | 14 |
| 4 | 12 |
| 1 | 10 |
| 7 | 9 |
| 8 | 3 |
| 10 | 1 |
SELECT
member.employee_id,
COUNT(member.member_id) AS enrollment_count
FROM
member
GROUP BY
member.employee_id
ORDER BY
COUNT(member.member_id) DESC;
Problem 89
Compute the average, minimum, maximum, and range (i.e., maximum – minimum) of using all the prices of all products. Provide meaningful aliases/names to the derived columns.
product
| average_price | maximum_price | minimum_price | price_range |
|---|---|---|---|
| 2.8125 | 3.5 | 2 | 1.5 |
SELECT
AVG(product.price) AS average_price,
MAX(product.price) AS maximum_price,
MIN(product.price) AS minimum_price,
MAX(product.price) - MIN(product.price) AS price_range
FROM
product;
Problem 90
Write a query that returns a result set containing the product ID, description, and price for those products with a description containing the sequence of characters, “Chocolate.”
product
| product_id | description | price |
|---|---|---|
| 2 | Chocolate | 2 |
| 4 | Mint Chocolate Chip | 3 |
| 5 | Chocolate Chip Cookie Dough | 3.5 |
SELECT
product.product_id,
product.description,
product.price
FROM
product
WHERE
product.description LIKE '%Chocolate%';
Problem 91
Compute the number of members enrolled each day based on the member enrollment date values. Alias the number of members enrolled as “enrollment_count.” In the output, include the enrollment date and enrollment count. Sort the result set in ascending order by enrollment_date.
member
| enrollment_date | enrollment_count |
|---|---|
| 01-JAN-24 | 1 |
| 02-JAN-24 | 2 |
| 03-JAN-24 | 1 |
| 05-JAN-24 | 1 |
| 10-JAN-24 | 2 |
| 11-JAN-24 | 2 |
| 12-JAN-24 | 1 |
| 13-JAN-24 | 1 |
| 14-JAN-24 | 1 |
| 15-JAN-24 | 1 |
| 16-JAN-24 | 2 |
| 17-JAN-24 | 2 |
| 19-JAN-24 | 3 |
| 20-JAN-24 | 2 |
| 22-JAN-24 | 1 |
| 26-JAN-24 | 5 |
| 27-JAN-24 | 2 |
| 30-JAN-24 | 1 |
| 31-JAN-24 | 2 |
| 02-FEB-24 | 1 |
| 04-FEB-24 | 1 |
| 05-FEB-24 | 2 |
| 08-FEB-24 | 1 |
| 09-FEB-24 | 2 |
| 10-FEB-24 | 2 |
| 11-FEB-24 | 1 |
| 13-FEB-24 | 3 |
| 15-FEB-24 | 2 |
| 16-FEB-24 | 2 |
| 17-FEB-24 | 1 |
| 18-FEB-24 | 2 |
| 19-FEB-24 | 1 |
| 20-FEB-24 | 2 |
| 21-FEB-24 | 2 |
| 22-FEB-24 | 3 |
| 23-FEB-24 | 1 |
| 25-FEB-24 | 2 |
| 26-FEB-24 | 3 |
| 27-FEB-24 | 2 |
| 28-FEB-24 | 1 |
| 01-MAR-24 | 2 |
| 02-MAR-24 | 2 |
| 03-MAR-24 | 2 |
| 04-MAR-24 | 1 |
| 05-MAR-24 | 1 |
| 06-MAR-24 | 2 |
| 11-MAR-24 | 2 |
| 13-MAR-24 | 1 |
| 14-MAR-24 | 1 |
| 15-MAR-24 | 1 |
| 18-MAR-24 | 1 |
| 21-MAR-24 | 3 |
| 22-MAR-24 | 3 |
| 24-MAR-24 | 1 |
| 25-MAR-24 | 2 |
| 26-MAR-24 | 1 |
| 27-MAR-24 | 1 |
| 29-MAR-24 | 2 |
| 30-MAR-24 | 1 |
SELECT
member.enrollment_date,
COUNT(member.member_id) AS enrollment_count
FROM
member
GROUP BY
member.enrollment_date
ORDER BY
member.enrollment_date;
Problem 92
Building onto your previous solution from problem 91, only include those days where the number of members enrolled is at least 3.
member
| enrollment_date | enrollment_count |
|---|---|
| 19-JAN-24 | 3 |
| 26-JAN-24 | 5 |
| 13-FEB-24 | 3 |
| 22-FEB-24 | 3 |
| 26-FEB-24 | 3 |
| 21-MAR-24 | 3 |
| 22-MAR-24 | 3 |
SELECT
member.enrollment_date,
COUNT(member.member_id) AS enrollment_count
FROM
member
GROUP BY
member.enrollment_date
HAVING
COUNT(member.member_id) >= 3
ORDER BY
member.enrollment_date;
Problem 93
Retrieve member details for those members enrolled by the employee with an ID value of 1 or with an enrollment date occurring within the month of March 2024. In the output, include the member ID, member type ID, enrollment date, email address, and employee ID.
member
| member_id | member_type_id | enrollment_date | email_address | employee_id |
|---|---|---|---|---|
| 10 | 1 | 12-JAN-24 | NM2HWVNQ1G@example.com | 1 |
| 11 | 2 | 13-JAN-24 | G15RO0FJ1W@example.com | 1 |
| 13 | 5 | 15-JAN-24 | SXIK09W9ET@example.com | 1 |
| 18 | 3 | 19-JAN-24 | G0XCM7EGF8@example.com | 1 |
| 19 | 3 | 19-JAN-24 | M7Y3YFLF9E@example.com | 1 |
| 20 | 2 | 19-JAN-24 | 0R350BCG4A@example.com | 1 |
| 47 | 2 | 15-FEB-24 | SU1NNV198S@example.com | 1 |
| 48 | 1 | 15-FEB-24 | 86TFB7ILY4@example.com | 1 |
| 57 | 1 | 21-FEB-24 | NMCHWP0Z1U@example.com | 1 |
| 58 | 3 | 21-FEB-24 | F3AXG158RH@example.com | 1 |
| 71 | 3 | 01-MAR-24 | 7SCHXCBAG7@example.com | 3 |
| 72 | 4 | 01-MAR-24 | IMG7HY0N5Y@example.com | 3 |
| 73 | 5 | 02-MAR-24 | G2REPPIBPK@example.com | 3 |
| 74 | 5 | 02-MAR-24 | AR7Q3N45OR@example.com | 3 |
| 75 | 3 | 03-MAR-24 | CWIR32Q5G7@example.com | 6 |
| 76 | 2 | 03-MAR-24 | TV29AL4ZC8@example.com | 6 |
| 77 | 1 | 04-MAR-24 | WT2V2YQYGM@example.com | 2 |
| 78 | 2 | 05-MAR-24 | WB9C308F4Y@example.com | 7 |
| 79 | 4 | 06-MAR-24 | AP7VVCJBMJ@example.com | 2 |
| 80 | 2 | 06-MAR-24 | 98BZXTH795@example.com | 2 |
| 81 | 3 | 11-MAR-24 | B9KXURD43L@example.com | 8 |
| 82 | 3 | 11-MAR-24 | 4OEJT5HWZZ@example.com | 8 |
| 83 | 5 | 13-MAR-24 | 1ZDVN3XAHA@example.com | 10 |
| 84 | 1 | 14-MAR-24 | 6HREL4BM07@example.com | 5 |
| 85 | 5 | 15-MAR-24 | G5L5J47BIX@example.com | 4 |
| 86 | 4 | 18-MAR-24 | DFUM3QJ7WY@example.com | 2 |
| 87 | 5 | 21-MAR-24 | IWPCD8NOXV@example.com | 6 |
| 88 | 1 | 21-MAR-24 | SD7QUPAIF6@example.com | 6 |
| 89 | 2 | 21-MAR-24 | R0DAIBETI4@example.com | 6 |
| 90 | 1 | 22-MAR-24 | NVRQU7A8U6@example.com | 6 |
| 91 | 4 | 22-MAR-24 | 5QH0YE66H0@example.com | 6 |
| 92 | 3 | 22-MAR-24 | QH4ZIISQUT@example.com | 6 |
| 93 | 4 | 24-MAR-24 | 14JH7VV2ZP@example.com | 8 |
| 94 | 5 | 25-MAR-24 | PCRPK8EZXO@example.com | 2 |
| 95 | 3 | 25-MAR-24 | YDAEZK2Q0J@example.com | 2 |
| 96 | 3 | 26-MAR-24 | 3EHNYXQGPE@example.com | 4 |
| 97 | 1 | 27-MAR-24 | HS6PYYP8IS@example.com | 3 |
| 98 | 1 | 29-MAR-24 | 05U625H1CU@example.com | 6 |
| 99 | 1 | 29-MAR-24 | WDR4VOPPWR@example.com | 6 |
| 100 | 1 | 30-MAR-24 | STO9IYZMFK@example.com | 6 |
SELECT
member.member_id,
member.member_type_id,
member.enrollment_date,
member.email_address,
member.employee_id
FROM
member
WHERE
member.employee_id = 1
OR (
enrollment_date >= '01-MAR-24'
AND enrollment_date < '01-APR-24'
);
Problem 94
Compute the number of members associated with each member type. Alias the number of members as “member_count.” In the output, include the member type ID and member count. Sort the result set in ascending order by member type ID.
member
| member_type_id | member_count |
|---|---|
| 1 | 19 |
| 2 | 22 |
| 3 | 19 |
| 4 | 15 |
| 5 | 25 |
SELECT
member.member_type_id,
COUNT(member.member_id) AS member_count
FROM
member
GROUP BY
member.member_type_id
ORDER BY
member_type_id;
Problem 95
Write a query that returns a result set containing the employee ID, last name, first name, hire date, job ID, and salary for those employees hired prior to the ice cream shop’s opening date (i.e., January 1, 2024).
employee
| employee_id | last_name | first_name | hire_date | job_id | salary |
|---|---|---|---|---|---|
| 1 | Cooley | Sonia | 01-DEC-23 | 1001 | 60000 |
| 2 | Edwards | Jason | 15-DEC-23 | 1002 | 45000 |
| 3 | Williams | Kelly | 15-DEC-23 | 1002 | 45000 |
| 4 | Powers | Darren | 15-DEC-23 | 1003 | 34290 |
| 5 | Brockman | Tim | 15-DEC-23 | 1003 | 35172 |
SELECT
employee.employee_id,
employee.last_name,
employee.first_name,
employee.hire_date,
employee.job_id,
employee.salary
FROM
employee
WHERE
employee.hire_date < '01-JAN-24';
Problem 96
Adjust your solution to problem 96 to compute the number of employees hired prior to the ice cream shop’s opening date (i.e., January 1, 2024). Alias the number of employees hired as “employee_count.”
employee
| employee_count |
|---|
| 5 |
SELECT
COUNT(employee.employee_id) AS employee_count
FROM
employee
WHERE
employee.hire_date < '01-JAN-24';
Problem 97
Building onto your solution to the previous problem, break out the total number of employees hired by job ID value. There should be one row per job ID value and the corresponding employee count.
employee
| job_id | employee_count |
|---|---|
| 1001 | 1 |
| 1002 | 2 |
| 1003 | 2 |
SELECT
employee.job_id,
COUNT(employee.employee_id) AS employee_count
FROM
employee
WHERE
employee.hire_date < '01-JAN-24'
GROUP BY
employee.job_id;
Problem 98
Compute the average salary for each job. Only include those job ID values where more than one employee holds the corresponding job. Alias the average salary column as “average_salary.” In the output, include the job ID corresponding average salary.
employee
| job_id | average_salary |
|---|---|
| 1002 | 45000 |
| 1003 | 33865 |
SELECT
employee.job_id,
AVG(employee.salary) AS average_salary
FROM
employee
GROUP BY
employee.job_id
HAVING
COUNT(employee.employee_id) > 1;
Problem 99
Compute the ice cream shop’s daily sales. Alias the daily sales column as “daily_sales.” In the output, include the purchase date and daily sales. Sort the result set in ascending order by purchase date.
purchase
| purchase_date | daily_sales |
|---|---|
| 01-JAN-24 | 15 |
| 02-JAN-24 | 62.81 |
| 03-JAN-24 | 25.2 |
| 05-JAN-24 | 1.8 |
| 10-JAN-24 | 24.71 |
| 11-JAN-24 | 13.83 |
| 12-JAN-24 | 12 |
| 13-JAN-24 | 5.82 |
| 14-JAN-24 | 8.55 |
| 15-JAN-24 | 12.6 |
| 16-JAN-24 | 35.55 |
| 17-JAN-24 | 32.49 |
| 19-JAN-24 | 27.37 |
| 20-JAN-24 | 27.65 |
| 22-JAN-24 | 3.26 |
| 26-JAN-24 | 50.4 |
| 27-JAN-24 | 13.71 |
| 30-JAN-24 | 23.25 |
| 31-JAN-24 | 36 |
| 02-FEB-24 | 14.3 |
| 04-FEB-24 | 11.5 |
| 05-FEB-24 | 27.6 |
| 07-FEB-24 | 25.65 |
| 08-FEB-24 | 21.6 |
| 09-FEB-24 | 28.28 |
| 10-FEB-24 | 11.02 |
| 11-FEB-24 | 15.93 |
| 13-FEB-24 | 34.79 |
| 15-FEB-24 | 44.43 |
| 16-FEB-24 | 32.85 |
| 17-FEB-24 | 24.5 |
| 18-FEB-24 | 21.39 |
| 19-FEB-24 | 14 |
| 20-FEB-24 | 45.29 |
| 21-FEB-24 | 33.95 |
| 22-FEB-24 | 64.54 |
| 23-FEB-24 | 49.95 |
| 25-FEB-24 | 48.7 |
| 26-FEB-24 | 36.3 |
| 27-FEB-24 | 58.88 |
| 28-FEB-24 | 10.88 |
| 29-FEB-24 | 20.12 |
| 01-MAR-24 | 51.12 |
| 02-MAR-24 | 27.42 |
| 03-MAR-24 | 53.67 |
| 04-MAR-24 | 41.35 |
| 05-MAR-24 | 64.66 |
| 06-MAR-24 | 31.85 |
| 07-MAR-24 | 58.62 |
| 08-MAR-24 | 18 |
| 10-MAR-24 | 14.07 |
| 11-MAR-24 | 66.35 |
| 12-MAR-24 | 3.26 |
| 13-MAR-24 | 37.91 |
| 14-MAR-24 | 19.5 |
| 15-MAR-24 | 38.25 |
| 16-MAR-24 | 15.52 |
| 17-MAR-24 | 35.05 |
| 18-MAR-24 | 49.14 |
| 19-MAR-24 | 27.31 |
| 20-MAR-24 | 6.3 |
| 21-MAR-24 | 72.97 |
| 22-MAR-24 | 75.09 |
| 23-MAR-24 | 22.47 |
| 24-MAR-24 | 75.8 |
| 25-MAR-24 | 56.95 |
| 26-MAR-24 | 124.67 |
| 27-MAR-24 | 150.23 |
| 28-MAR-24 | 39.53 |
| 29-MAR-24 | 74.54 |
| 30-MAR-24 | 58.17 |
| 31-MAR-24 | 137.75 |
SELECT
purchase_date,
SUM(purchase_total) AS daily_sales
FROM
purchase
GROUP BY
purchase_date
ORDER BY
purchase_date;
Problem 100
Retrieve the purchase details for purchases where the total purchase value exceeds $10, and the purchase date is in January 2024. In the output, include the purchase ID, member ID, purchase date, employee ID, and purchase total. Sort the result set in ascending order by purchase date.
purchase
| purchase_id | member_id | purchase_date | employee_id | purchase_total |
|---|---|---|---|---|
| 1 | 1 | 01-JAN-24 | 2 | 15 |
| 2 | 2 | 02-JAN-24 | 4 | 28.8 |
| 4 | 3 | 02-JAN-24 | 4 | 32.01 |
| 5 | 4 | 03-JAN-24 | 4 | 25.2 |
| 8 | 6 | 10-JAN-24 | 5 | 22.81 |
| 10 | 9 | 11-JAN-24 | 5 | 10.23 |
| 11 | 10 | 12-JAN-24 | 5 | 12 |
| 14 | 13 | 15-JAN-24 | 5 | 12.6 |
| 15 | 14 | 16-JAN-24 | 1 | 18.9 |
| 16 | 15 | 16-JAN-24 | 1 | 16.65 |
| 17 | 16 | 17-JAN-24 | 3 | 17.46 |
| 19 | 7 | 17-JAN-24 | 3 | 11.88 |
| 22 | 20 | 19-JAN-24 | 2 | 13.59 |
| 23 | 21 | 20-JAN-24 | 4 | 10.19 |
| 24 | 22 | 20-JAN-24 | 4 | 17.46 |
| 26 | 24 | 26-JAN-24 | 4 | 23.28 |
| 27 | 25 | 26-JAN-24 | 4 | 11.5 |
| 33 | 31 | 30-JAN-24 | 5 | 23.25 |
| 35 | 32 | 31-JAN-24 | 1 | 28.8 |
SELECT
purchase.purchase_id,
purchase.member_id,
purchase.purchase_date,
purchase.employee_id,
purchase.purchase_total
FROM
purchase
WHERE
purchase.purchase_total >= 10
AND purchase.purchase_date >= '01-JAN-24'
AND purchase.purchase_date < '01-FEB-24'
ORDER BY
purchase_date;
Problem 101
For each member, compute the total amount spent. Alias this computed amount as “total_sales.” In the output, include the member ID and total sales. Sort the result set in descending order by total sales.
purchase
| member_id | total_sales |
|---|---|
| 84 | 70 |
| 31 | 66.5 |
| 98 | 65 |
| 59 | 60.15 |
| 96 | 58.44 |
| 60 | 58.05 |
| 71 | 57.96 |
| 39 | 56.75 |
| 97 | 53.5 |
| 5 | 50.85 |
| 6 | 50.84 |
| 68 | 47.25 |
| 95 | 47.03 |
| 70 | 46.56 |
| 3 | 45.6 |
| 14 | 44.55 |
| 47 | 44.14 |
| 82 | 43.7 |
| 78 | 42.69 |
| 53 | 41.85 |
| 92 | 41.81 |
| 27 | 39.9 |
| 61 | 38.5 |
| 69 | 36.86 |
| 79 | 36.28 |
| 33 | 35.55 |
| 48 | 35 |
| 46 | 33.48 |
| 45 | 33.3 |
| 50 | 33.02 |
| 88 | 33 |
| 100 | 32 |
| 94 | 31.95 |
| 81 | 31.35 |
| 75 | 30.88 |
| 72 | 30.69 |
| 13 | 30.6 |
| 56 | 30.56 |
| 90 | 30 |
| 63 | 30 |
| 74 | 29.7 |
| 44 | 29.59 |
| 2 | 28.8 |
| 32 | 28.8 |
| 49 | 28.35 |
| 15 | 27 |
| 57 | 27 |
| 25 | 25.5 |
| 16 | 25.22 |
| 4 | 25.2 |
| 87 | 25.2 |
| 30 | 24.75 |
| 40 | 24.7 |
| 22 | 24.25 |
| 19 | 23.75 |
| 24 | 23.28 |
| 67 | 22.5 |
| 42 | 22.33 |
| 52 | 22.32 |
| 38 | 21.6 |
| 77 | 21 |
| 86 | 19.53 |
| 10 | 19 |
| 91 | 18.6 |
| 12 | 18.05 |
| 51 | 17.5 |
| 1 | 17 |
| 55 | 16.63 |
| 99 | 16.5 |
| 37 | 16.2 |
| 11 | 14.55 |
| 54 | 14 |
| 7 | 13.78 |
| 20 | 13.59 |
| 62 | 13.58 |
| 73 | 12.6 |
| 64 | 11.4 |
| 36 | 11.4 |
| 17 | 11.25 |
| 9 | 10.23 |
| 21 | 10.19 |
| 83 | 9.45 |
| 29 | 9.3 |
| 85 | 9 |
| 35 | 9 |
| 43 | 8.73 |
| 41 | 7.76 |
| 76 | 7.76 |
| 58 | 7.13 |
| 23 | 6.52 |
| 93 | 6.51 |
| 28 | 6.51 |
| 89 | 5.82 |
| 66 | 5.4 |
| 26 | 5.23 |
| 18 | 5.23 |
| 34 | 4.85 |
| 8 | 3.6 |
| 80 | 2.91 |
| 65 | 2.7 |
SELECT
purchase.member_id,
SUM(purchase.purchase_total) AS total_sales
FROM
purchase
GROUP BY
purchase.member_id
ORDER BY
SUM(purchase.purchase_total) DESC;
Problem 102
Building onto your solution to the previous problem, only include those members with a total sales value greater than or equal to $50 and more than three purchases.
purchase
| member_id | total_sales |
|---|---|
| 84 | 70 |
| 71 | 57.96 |
| 39 | 56.75 |
| 5 | 50.85 |
SELECT
purchase.member_id,
SUM(purchase.purchase_total) AS total_sales
FROM
purchase
GROUP BY
purchase.member_id
HAVING
SUM(purchase.purchase_total) >= 50
AND COUNT(purchase.purchase_id) > 3
ORDER BY
SUM(purchase.purchase_total) DESC;
Problem 103
Compute the distinct number of times each product has been purchased. Alias this computed value as “purchase_count.” In the output, include the product ID and purchase count. Sort the result set in descending order by purchase count.
purchase_item
| product_id | purchase_count |
|---|---|
| 3 | 57 |
| 7 | 52 |
| 4 | 52 |
| 6 | 44 |
| 5 | 44 |
| 8 | 42 |
| 1 | 40 |
| 2 | 37 |
SELECT
purchase_item.product_id,
COUNT(purchase_item.purchase_id) AS purchase_count
FROM
purchase_item
GROUP BY
purchase_item.product_id
ORDER BY
COUNT(purchase_item.purchase_id) DESC;
Problem 104
For each purchase, compute the total number of items purchased based on quantity. Alias this computed value as “item_count.” In the output, include the purchase ID and item count. Sort the result set in descending order by item count.
purchase_item
| purchase_id | total_quantity |
|---|---|
| 192 | 14 |
| 197 | 14 |
| 100 | 13 |
| 94 | 12 |
| 159 | 12 |
| 2 | 11 |
| 54 | 11 |
| 89 | 11 |
| 199 | 11 |
| 4 | 11 |
| 15 | 10 |
| 118 | 10 |
| 130 | 10 |
| 35 | 10 |
| 157 | 10 |
| 115 | 10 |
| 64 | 9 |
| 113 | 9 |
| 42 | 9 |
| 106 | 9 |
| 33 | 9 |
| 24 | 9 |
| 5 | 9 |
| 173 | 8 |
| 85 | 8 |
| 162 | 8 |
| 184 | 8 |
| 185 | 8 |
| 120 | 8 |
| 181 | 8 |
| 149 | 8 |
| 152 | 8 |
| 95 | 8 |
| 119 | 8 |
| 133 | 8 |
| 171 | 8 |
| 26 | 8 |
| 107 | 7 |
| 138 | 7 |
| 161 | 7 |
| 43 | 7 |
| 78 | 7 |
| 168 | 7 |
| 183 | 7 |
| 200 | 7 |
| 63 | 7 |
| 196 | 7 |
| 8 | 7 |
| 56 | 7 |
| 70 | 7 |
| 66 | 7 |
| 103 | 7 |
| 169 | 7 |
| 112 | 7 |
| 146 | 7 |
| 108 | 7 |
| 1 | 6 |
| 16 | 6 |
| 88 | 6 |
| 127 | 6 |
| 131 | 6 |
| 163 | 6 |
| 172 | 6 |
| 41 | 6 |
| 179 | 6 |
| 17 | 6 |
| 76 | 6 |
| 174 | 6 |
| 137 | 6 |
| 186 | 6 |
| 53 | 6 |
| 97 | 6 |
| 144 | 6 |
| 68 | 6 |
| 27 | 5 |
| 175 | 5 |
| 58 | 5 |
| 105 | 5 |
| 110 | 5 |
| 198 | 5 |
| 44 | 5 |
| 140 | 5 |
| 145 | 5 |
| 155 | 5 |
| 177 | 5 |
| 195 | 5 |
| 81 | 5 |
| 60 | 5 |
| 167 | 5 |
| 117 | 5 |
| 51 | 5 |
| 14 | 4 |
| 50 | 4 |
| 57 | 4 |
| 125 | 4 |
| 158 | 4 |
| 40 | 4 |
| 71 | 4 |
| 99 | 4 |
| 143 | 4 |
| 180 | 4 |
| 189 | 4 |
| 34 | 4 |
| 45 | 4 |
| 62 | 4 |
| 11 | 4 |
| 77 | 4 |
| 187 | 4 |
| 10 | 4 |
| 48 | 4 |
| 101 | 4 |
| 151 | 4 |
| 178 | 4 |
| 22 | 4 |
| 93 | 4 |
| 96 | 4 |
| 19 | 4 |
| 29 | 4 |
| 69 | 4 |
| 111 | 4 |
| 114 | 4 |
| 139 | 4 |
| 61 | 4 |
| 123 | 4 |
| 150 | 4 |
| 160 | 4 |
| 165 | 4 |
| 23 | 3 |
| 154 | 3 |
| 124 | 3 |
| 31 | 3 |
| 79 | 3 |
| 82 | 3 |
| 129 | 3 |
| 12 | 3 |
| 37 | 3 |
| 84 | 3 |
| 121 | 3 |
| 141 | 3 |
| 49 | 3 |
| 65 | 3 |
| 20 | 3 |
| 46 | 3 |
| 74 | 3 |
| 87 | 3 |
| 126 | 3 |
| 148 | 3 |
| 170 | 3 |
| 13 | 3 |
| 132 | 3 |
| 193 | 3 |
| 32 | 3 |
| 39 | 3 |
| 80 | 3 |
| 92 | 3 |
| 102 | 3 |
| 52 | 3 |
| 72 | 2 |
| 176 | 2 |
| 36 | 2 |
| 59 | 2 |
| 134 | 2 |
| 136 | 2 |
| 190 | 2 |
| 21 | 2 |
| 73 | 2 |
| 75 | 2 |
| 86 | 2 |
| 90 | 2 |
| 191 | 2 |
| 116 | 2 |
| 142 | 2 |
| 156 | 2 |
| 9 | 2 |
| 28 | 2 |
| 67 | 2 |
| 153 | 2 |
| 164 | 2 |
| 91 | 2 |
| 147 | 2 |
| 135 | 2 |
| 6 | 1 |
| 182 | 1 |
| 7 | 1 |
| 98 | 1 |
| 109 | 1 |
| 104 | 1 |
| 30 | 1 |
| 188 | 1 |
| 194 | 1 |
| 18 | 1 |
| 3 | 1 |
| 122 | 1 |
| 128 | 1 |
| 38 | 1 |
| 55 | 1 |
| 83 | 1 |
| 25 | 1 |
| 47 | 1 |
| 166 | 1 |
SELECT
purchase_item.purchase_id,
SUM(purchase_item.quantity) AS total_quantity
FROM
purchase_item
GROUP BY
purchase_item.purchase_id
ORDER BY
SUM(purchase_item.quantity) DESC;
Problem 105
Retrieve the purchase ID values for those purchases containing product ID values 1, 4, or 8. In the output, include the purchase ID.
purchase_item
| purchase_id |
|---|
| 6 |
| 27 |
| 50 |
| 51 |
| 57 |
| 107 |
| 113 |
| 124 |
| 125 |
| 161 |
| 166 |
| 7 |
| 15 |
| 31 |
| 36 |
| 40 |
| 42 |
| 43 |
| 68 |
| 98 |
| 99 |
| 109 |
| 110 |
| 131 |
| 136 |
| 159 |
| 180 |
| 183 |
| 189 |
| 34 |
| 41 |
| 44 |
| 45 |
| 63 |
| 82 |
| 104 |
| 118 |
| 129 |
| 184 |
| 185 |
| 196 |
| 2 |
| 11 |
| 17 |
| 21 |
| 30 |
| 54 |
| 56 |
| 70 |
| 73 |
| 84 |
| 86 |
| 90 |
| 115 |
| 121 |
| 140 |
| 145 |
| 155 |
| 177 |
| 191 |
| 194 |
| 4 |
| 5 |
| 10 |
| 35 |
| 48 |
| 49 |
| 65 |
| 66 |
| 81 |
| 101 |
| 103 |
| 116 |
| 120 |
| 137 |
| 151 |
| 157 |
| 169 |
| 197 |
| 199 |
| 3 |
| 9 |
| 33 |
| 74 |
| 87 |
| 93 |
| 112 |
| 128 |
| 149 |
| 19 |
| 29 |
| 38 |
| 55 |
| 83 |
| 95 |
| 133 |
| 144 |
| 164 |
| 193 |
| 16 |
| 24 |
| 32 |
| 39 |
| 61 |
| 80 |
| 89 |
| 100 |
| 102 |
| 117 |
| 138 |
| 147 |
| 160 |
| 165 |
| 192 |
SELECT
DISTINCT purchase_item.purchase_id
FROM
purchase_item
WHERE
purchase_item.product_id IN (1, 4, 8);