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.
Problem 1
Associate Turn-over
The human resource department is concerned that recent associate turn-over is largely attributed to dissatisfaction with salary. The first step HR wishes to take is to raise all employee salaries that are less than the midpoint of the salary range corresponding to the employee’s job. They’ve asked you to generate a report that contains only those employees meeting this condition. The necessary context required for the report is the employee ID, employee first name, employee last name, hire date, job title, current salary, minimum salary for the job, maximum salary for the job, and the new salary. The new salary should be the salary required to bring the employee’s salary to the midpoint value for the job. In addition, HR has asked, if possible, include the percent increase based on the old salary and new salary for each employee meeting the conditions of the report. Sort the report by percent increase, descending.
hr.employees
hr.jobs
| EMPLOYEE_ID | FIRST_NAME | LAST_NAME | HIRE_DATE | JOB_TITLE | SALARY | MIN_SALARY | MAX_SALARY | NEW_SALARY | PERCENT_INCREASE |
|---|---|---|---|---|---|---|---|---|---|
| 132 | TJ | Olson | 10-Apr-07 | Stock Clerk | 2100 | 2008 | 5000 | 3504 | 66.85714286 |
| 107 | Diana | Lorentz | 7-Feb-07 | Programmer | 4200 | 4000 | 10000 | 7000 | 66.66666667 |
| 191 | Randall | Perkins | 19-Dec-07 | Shipping Clerk | 2500 | 2500 | 5500 | 4000 | 60 |
| 182 | Martha | Sullivan | 21-Jun-07 | Shipping Clerk | 2500 | 2500 | 5500 | 4000 | 60 |
| 119 | Karen | Colmenares | 10-Aug-07 | Purchasing Clerk | 2500 | 2500 | 5500 | 4000 | 60 |
| 128 | Steven | Markle | 8-Mar-08 | Stock Clerk | 2200 | 2008 | 5000 | 3504 | 59.27272727 |
| 136 | Hazel | Philtanker | 6-Feb-08 | Stock Clerk | 2200 | 2008 | 5000 | 3504 | 59.27272727 |
| 198 | Donald | OConnell | 21-Jun-07 | Shipping Clerk | 2600 | 2500 | 5500 | 4000 | 53.84615385 |
| 199 | Douglas | Grant | 13-Jan-08 | Shipping Clerk | 2600 | 2500 | 5500 | 4000 | 53.84615385 |
| 118 | Guy | Himuro | 15-Nov-06 | Purchasing Clerk | 2600 | 2500 | 5500 | 4000 | 53.84615385 |
| 173 | Sundita | Kumar | 21-Apr-08 | Sales Representative | 6100 | 6000 | 12008 | 9004 | 47.60655738 |
| 135 | Ki | Gee | 12-Dec-07 | Stock Clerk | 2400 | 2008 | 5000 | 3504 | 46 |
| 127 | James | Landry | 14-Jan-07 | Stock Clerk | 2400 | 2008 | 5000 | 3504 | 46 |
| 106 | Valli | Pataballa | 5-Feb-06 | Programmer | 4800 | 4000 | 10000 | 7000 | 45.83333333 |
| 105 | David | Austin | 25-Jun-05 | Programmer | 4800 | 4000 | 10000 | 7000 | 45.83333333 |
| 167 | Amit | Banda | 21-Apr-08 | Sales Representative | 6200 | 6000 | 12008 | 9004 | 45.22580645 |
| 179 | Charles | Johnson | 4-Jan-08 | Sales Representative | 6200 | 6000 | 12008 | 9004 | 45.22580645 |
| 149 | Eleni | Zlotkey | 29-Jan-08 | Sales Manager | 10500 | 10000 | 20080 | 15040 | 43.23809524 |
| 117 | Sigal | Tobias | 24-Jul-05 | Purchasing Clerk | 2800 | 2500 | 5500 | 4000 | 42.85714286 |
| 183 | Girard | Geoni | 3-Feb-08 | Shipping Clerk | 2800 | 2500 | 5500 | 4000 | 42.85714286 |
| 195 | Vance | Jones | 17-Mar-07 | Shipping Clerk | 2800 | 2500 | 5500 | 4000 | 42.85714286 |
| 166 | Sundar | Ande | 24-Mar-08 | Sales Representative | 6400 | 6000 | 12008 | 9004 | 40.6875 |
| 140 | Joshua | Patel | 6-Apr-06 | Stock Clerk | 2500 | 2008 | 5000 | 3504 | 40.16 |
| 144 | Peter | Vargas | 9-Jul-06 | Stock Clerk | 2500 | 2008 | 5000 | 3504 | 40.16 |
| 131 | James | Marlow | 16-Feb-05 | Stock Clerk | 2500 | 2008 | 5000 | 3504 | 40.16 |
| 116 | Shelli | Baida | 24-Dec-05 | Purchasing Clerk | 2900 | 2500 | 5500 | 4000 | 37.93103448 |
| 190 | Timothy | Gates | 11-Jul-06 | Shipping Clerk | 2900 | 2500 | 5500 | 4000 | 37.93103448 |
| 148 | Gerald | Cambrault | 15-Oct-07 | Sales Manager | 11000 | 10000 | 20080 | 15040 | 36.72727273 |
| 143 | Randall | Matos | 15-Mar-06 | Stock Clerk | 2600 | 2008 | 5000 | 3504 | 34.76923077 |
| 197 | Kevin | Feeney | 23-May-06 | Shipping Clerk | 3000 | 2500 | 5500 | 4000 | 33.33333333 |
| 187 | Anthony | Cabrio | 7-Feb-07 | Shipping Clerk | 3000 | 2500 | 5500 | 4000 | 33.33333333 |
| 165 | David | Lee | 23-Feb-08 | Sales Representative | 6800 | 6000 | 12008 | 9004 | 32.41176471 |
| 101 | Neena | Kochhar | 21-Sep-05 | Administration Vice President | 17000 | 15000 | 30000 | 22500 | 32.35294118 |
| 102 | Lex | De Haan | 13-Jan-01 | Administration Vice President | 17000 | 15000 | 30000 | 22500 | 32.35294118 |
| 139 | John | Seo | 12-Feb-06 | Stock Clerk | 2700 | 2008 | 5000 | 3504 | 29.77777778 |
| 126 | Irene | Mikkilineni | 28-Sep-06 | Stock Clerk | 2700 | 2008 | 5000 | 3504 | 29.77777778 |
| 196 | Alana | Walsh | 24-Apr-06 | Shipping Clerk | 3100 | 2500 | 5500 | 4000 | 29.03225806 |
| 181 | Jean | Fleaur | 23-Feb-06 | Shipping Clerk | 3100 | 2500 | 5500 | 4000 | 29.03225806 |
| 115 | Alexander | Khoo | 18-May-03 | Purchasing Clerk | 3100 | 2500 | 5500 | 4000 | 29.03225806 |
| 161 | Sarath | Sewall | 3-Nov-06 | Sales Representative | 7000 | 6000 | 12008 | 9004 | 28.62857143 |
| 178 | Kimberely | Grant | 24-May-07 | Sales Representative | 7000 | 6000 | 12008 | 9004 | 28.62857143 |
| 155 | Oliver | Tuvault | 23-Nov-07 | Sales Representative | 7000 | 6000 | 12008 | 9004 | 28.62857143 |
| 147 | Alberto | Errazuriz | 10-Mar-05 | Sales Manager | 12000 | 10000 | 20080 | 15040 | 25.33333333 |
| 100 | Steven | King | 17-Jun-03 | President | 24000 | 20080 | 40000 | 30040 | 25.16666667 |
| 130 | Mozhe | Atkinson | 30-Oct-05 | Stock Clerk | 2800 | 2008 | 5000 | 3504 | 25.14285714 |
| 164 | Mattea | Marvins | 24-Jan-08 | Sales Representative | 7200 | 6000 | 12008 | 9004 | 25.05555556 |
| 180 | Winston | Taylor | 24-Jan-06 | Shipping Clerk | 3200 | 2500 | 5500 | 4000 | 25 |
| 194 | Samuel | McCain | 1-Jul-06 | Shipping Clerk | 3200 | 2500 | 5500 | 4000 | 25 |
| 172 | Elizabeth | Bates | 24-Mar-07 | Sales Representative | 7300 | 6000 | 12008 | 9004 | 23.34246575 |
| 171 | William | Smith | 23-Feb-07 | Sales Representative | 7400 | 6000 | 12008 | 9004 | 21.67567568 |
| 134 | Michael | Rogers | 26-Aug-06 | Stock Clerk | 2900 | 2008 | 5000 | 3504 | 20.82758621 |
| 124 | Kevin | Mourgos | 16-Nov-07 | Stock Manager | 5800 | 5500 | 8500 | 7000 | 20.68965517 |
| 154 | Nanette | Cambrault | 9-Dec-06 | Sales Representative | 7500 | 6000 | 12008 | 9004 | 20.05333333 |
| 160 | Louise | Doran | 15-Dec-05 | Sales Representative | 7500 | 6000 | 12008 | 9004 | 20.05333333 |
| 186 | Julia | Dellinger | 24-Jun-06 | Shipping Clerk | 3400 | 2500 | 5500 | 4000 | 17.64705882 |
| 104 | Bruce | Ernst | 21-May-07 | Programmer | 6000 | 4000 | 10000 | 7000 | 16.66666667 |
| 142 | Curtis | Davies | 29-Jan-05 | Stock Clerk | 3100 | 2008 | 5000 | 3504 | 13.03225806 |
| 159 | Lindsey | Smith | 10-Mar-05 | Sales Representative | 8000 | 6000 | 12008 | 9004 | 12.55 |
| 153 | Christopher | Olsen | 30-Mar-06 | Sales Representative | 8000 | 6000 | 12008 | 9004 | 12.55 |
| 146 | Karen | Partners | 5-Jan-05 | Sales Manager | 13500 | 10000 | 20080 | 15040 | 11.40740741 |
| 189 | Jennifer | Dilly | 13-Aug-05 | Shipping Clerk | 3600 | 2500 | 5500 | 4000 | 11.11111111 |
| 138 | Stephen | Stiles | 26-Oct-05 | Stock Clerk | 3200 | 2008 | 5000 | 3504 | 9.5 |
| 125 | Julia | Nayer | 16-Jul-05 | Stock Clerk | 3200 | 2008 | 5000 | 3504 | 9.5 |
| 202 | Pat | Fay | 17-Aug-05 | Marketing Representative | 6000 | 4000 | 9000 | 6500 | 8.333333333 |
| 123 | Shanta | Vollman | 10-Oct-05 | Stock Manager | 6500 | 5500 | 8500 | 7000 | 7.692307692 |
| 145 | John | Russell | 1-Oct-04 | Sales Manager | 14000 | 10000 | 20080 | 15040 | 7.428571429 |
| 177 | Jack | Livingston | 23-Apr-06 | Sales Representative | 8400 | 6000 | 12008 | 9004 | 7.19047619 |
| 129 | Laura | Bissot | 20-Aug-05 | Stock Clerk | 3300 | 2008 | 5000 | 3504 | 6.181818182 |
| 133 | Jason | Mallin | 14-Jun-04 | Stock Clerk | 3300 | 2008 | 5000 | 3504 | 6.181818182 |
| 188 | Kelly | Chung | 14-Jun-05 | Shipping Clerk | 3800 | 2500 | 5500 | 4000 | 5.263157895 |
| 176 | Jonathon | Taylor | 24-Mar-06 | Sales Representative | 8600 | 6000 | 12008 | 9004 | 4.697674419 |
| 114 | Den | Raphaely | 7-Dec-02 | Purchasing Manager | 11000 | 8000 | 15000 | 11500 | 4.545454545 |
| 193 | Britney | Everett | 3-Mar-05 | Shipping Clerk | 3900 | 2500 | 5500 | 4000 | 2.564102564 |
| 175 | Alyssa | Hutton | 19-Mar-05 | Sales Representative | 8800 | 6000 | 12008 | 9004 | 2.318181818 |
| 200 | Jennifer | Whalen | 17-Sep-03 | Administration Assistant | 4400 | 3000 | 6000 | 4500 | 2.272727273 |
| 108 | Nancy | Greenberg | 17-Aug-02 | Finance Manager | 12008 | 8200 | 16000 | 12100 | 0.766155896 |
| 205 | Shelley | Higgins | 7-Jun-02 | Accounting Manager | 12008 | 8200 | 16000 | 12100 | 0.766155896 |
| 141 | Trenna | Rajs | 17-Oct-03 | Stock Clerk | 3500 | 2008 | 5000 | 3504 | 0.114285714 |
| 158 | Allan | McEwen | 1-Aug-04 | Sales Representative | 9000 | 6000 | 12008 | 9004 | 0.044444444 |
| 152 | Peter | Hall | 20-Aug-05 | Sales Representative | 9000 | 6000 | 12008 | 9004 | 0.044444444 |
SELECT
hr.employees.employee_id,
hr.employees.first_name,
hr.employees.last_name,
hr.employees.hire_date,
hr.jobs.job_title,
hr.employees.salary,
hr.jobs.min_salary,
hr.jobs.max_salary,
((hr.jobs.min_salary + hr.jobs.max_salary) / 2) AS new_salary,
(((((hr.jobs.min_salary + hr.jobs.max_salary) / 2)
- hr.employees.salary)
/ hr.employees.salary)
* 100
) AS percent_increase
FROM
hr.employees
INNER JOIN
hr.jobs
ON hr.employees.job_id = hr.jobs.job_id
AND hr.employees.salary < ((hr.jobs.min_salary + hr.jobs.max_salary) / 2)
ORDER BY
percent_increase DESC;
Problem 2
Mentor-mentee Assignments
The organization has decided to start a mentorship program next month. This program is spearheaded by John Wayne. In order to kick-off the mentorship program, John needs a list of mentors and mentees. He’s decided that for the first iteration of the mentorship program, mentors will be the most senior employee, by hire date, for each job and mentees will be the most recently hired employee for each job. Mentees will be assigned a mentor with the same job and mentor-mentee assignments will be made only for those jobs with at least two employees. *Only one mentor-mentee assignment per job is necessary for this iteration of the mentorship program. John has requested that you generate a report containing the mentors and mentees based on his criteria. Include the mentor’s employee ID, mentor’s job ID, mentor’s hire date, mentee’s employee ID, mentee’s job ID, and mentee’s hire date in the report for additional context. Ensure the report is sorted by job ID, ascending.
*In the instances of multiple mentors, mentees, or both, having been hired on the same date and for the same job, the solution does not need to produce deterministic results.
hr.employees
| MENTOR_ID | MENTOR_JOB_ID | MENTOR_HIRE_DATE | MENTEE_ID | MENTEE_JOB_ID | MENTEE_HIRE_DATE |
|---|---|---|---|---|---|
| 102 | AD_VP | 13-JAN-01 | 101 | AD_VP | 21-SEP-05 |
| 109 | FI_ACCOUNT | 16-AUG-02 | 113 | FI_ACCOUNT | 07-DEC-07 |
| 105 | IT_PROG | 25-JUN-05 | 104 | IT_PROG | 21-MAY-07 |
| 115 | PU_CLERK | 18-MAY-03 | 119 | PU_CLERK | 10-AUG-07 |
| 145 | SA_MAN | 01-OCT-04 | 149 | SA_MAN | 29-JAN-08 |
| 156 | SA_REP | 30-JAN-04 | 167 | SA_REP | 21-APR-08 |
| 184 | SH_CLERK | 27-JAN-04 | 183 | SH_CLERK | 03-FEB-08 |
| 137 | ST_CLERK | 14-JUL-03 | 128 | ST_CLERK | 08-MAR-08 |
| 122 | ST_MAN | 01-MAY-03 | 124 | ST_MAN | 16-NOV-07 |
WITH mentor AS (
SELECT
hr.employees.employee_id,
hr.employees.job_id,
hr.employees.hire_date,
ROW_NUMBER() OVER (
PARTITION BY hr.employees.job_id
ORDER BY hr.employees.hire_date
) AS hire_date_sequence_asc
FROM
hr.employees
),
mentee AS (
SELECT
hr.employees.employee_id,
hr.employees.job_id,
hr.employees.hire_date,
ROW_NUMBER() OVER (
PARTITION BY hr.employees.job_id
ORDER BY hr.employees.hire_date DESC
) AS hire_date_sequence_desc
FROM
hr.employees
)
SELECT
mentor.employee_id AS mentor_id,
mentor.job_id AS mentor_job_id,
mentor.hire_date AS mentor_hire_date,
mentee.employee_id AS mentee_id,
mentee.job_id AS mentee_job_id,
mentee.hire_date AS mentee_hire_date
FROM
mentor
INNER JOIN
mentee
ON mentor.job_id = mentee.job_id
AND mentor.employee_id != mentee.employee_id
AND mentor.hire_date_sequence_asc = 1
AND mentee.hire_date_sequence_desc = 1
ORDER BY
mentor.job_id;
-- Solution 2
SELECT
mentor.employee_id AS mentor_id,
mentor.job_id AS mentor_job_id,
mentor.hire_date AS mentor_hire_date,
mentee.employee_id AS mentee_id,
mentee.job_id AS mentee_job_id,
mentee.hire_date AS mentee_hire_date
FROM (
SELECT
hr.employees.employee_id,
hr.employees.job_id,
hr.employees.hire_date,
ROW_NUMBER() OVER (
PARTITION BY hr.employees.job_id
ORDER BY hr.employees.hire_date
) AS hire_date_sequence_asc
FROM
hr.employees
) mentor
INNER JOIN (
SELECT
hr.employees.employee_id,
hr.employees.job_id,
hr.employees.hire_date,
ROW_NUMBER() OVER (
PARTITION BY hr.employees.job_id
ORDER BY hr.employees.hire_date DESC
) AS hire_date_sequence_desc
FROM
hr.employees
) mentee
ON mentor.job_id = mentee.job_id
AND mentor.employee_id != mentee.employee_id
AND mentor.hire_date_sequence_asc = 1
AND mentee.hire_date_sequence_desc = 1
ORDER BY
mentor.job_id;
Problem 3
Fact-checking HR
The human resource department has recently made a claim that pay within each job is based strictly on the duration employed. Today, you’ve been tasked with investigating the claim by creating a report with the necessary data to conclude the claim can be supported or disproved. Is this claim substantiated by the employee data? Assume that every employee has been in the same position since being hired. In your report, only include details related to those jobs held by at least two employees. Sort the result set by job ID (ascending) and employment duration (descending). The method for determining whether the claim can be supported is up to you.
hr.employees
hr.jobs
| EMPLOYEE_ID | EMPLOYEE_NAME | JOB_TITLE | HIRE_DATE | EMPLOYMENT_DURATION | SALARY | INCONSISTENCY_FLAG | SALARY_SEQUENCE_DESC |
|---|---|---|---|---|---|---|---|
| 102 | De Haan, Lex | Administration Vice President | 13-JAN-01 | 23.48 | 17000 | 1 | 1 |
| 101 | Kochhar, Neena | Administration Vice President | 21-SEP-05 | 18.79 | 17000 | 1 | 1 |
| 109 | Faviet, Daniel | Accountant | 16-AUG-02 | 21.89 | 9000 | 1 | 1 |
| 110 | Chen, John | Accountant | 28-SEP-05 | 18.77 | 8200 | 1 | 2 |
| 111 | Sciarra, Ismael | Accountant | 30-SEP-05 | 18.77 | 7700 | 0 | 4 |
| 112 | Urman, Jose Manuel | Accountant | 07-MAR-06 | 18.33 | 7800 | 1 | 3 |
| 113 | Popp, Luis | Accountant | 07-DEC-07 | 16.58 | 6900 | 1 | 5 |
| 105 | Austin, David | Programmer | 25-JUN-05 | 19.03 | 4800 | 0 | 3 |
| 103 | Hunold, Alexander | Programmer | 03-JAN-06 | 18.51 | 9000 | 1 | 1 |
| 106 | Pataballa, Valli | Programmer | 05-FEB-06 | 18.42 | 4800 | 1 | 3 |
| 107 | Lorentz, Diana | Programmer | 07-FEB-07 | 17.41 | 4200 | 0 | 4 |
| 104 | Ernst, Bruce | Programmer | 21-MAY-07 | 17.13 | 6000 | 1 | 2 |
| 115 | Khoo, Alexander | Purchasing Clerk | 18-MAY-03 | 21.13 | 3100 | 1 | 1 |
| 117 | Tobias, Sigal | Purchasing Clerk | 24-JUL-05 | 18.95 | 2800 | 0 | 3 |
| 116 | Baida, Shelli | Purchasing Clerk | 24-DEC-05 | 18.53 | 2900 | 1 | 2 |
| 118 | Himuro, Guy | Purchasing Clerk | 15-NOV-06 | 17.64 | 2600 | 1 | 4 |
| 119 | Colmenares, Karen | Purchasing Clerk | 10-AUG-07 | 16.91 | 2500 | 1 | 5 |
| 145 | Russell, John | Sales Manager | 01-OCT-04 | 19.76 | 14000 | 1 | 1 |
| 146 | Partners, Karen | Sales Manager | 05-JAN-05 | 19.5 | 13500 | 1 | 2 |
| 147 | Errazuriz, Alberto | Sales Manager | 10-MAR-05 | 19.32 | 12000 | 1 | 3 |
| 148 | Cambrault, Gerald | Sales Manager | 15-OCT-07 | 16.73 | 11000 | 1 | 4 |
| 149 | Zlotkey, Eleni | Sales Manager | 29-JAN-08 | 16.44 | 10500 | 1 | 5 |
| 156 | King, Janette | Sales Representative | 30-JAN-04 | 20.44 | 10000 | 1 | 4 |
| 157 | Sully, Patrick | Sales Representative | 04-MAR-04 | 20.34 | 9500 | 0 | 6 |
| 174 | Abel, Ellen | Sales Representative | 11-MAY-04 | 20.15 | 11000 | 1 | 2 |
| 158 | McEwen, Allan | Sales Representative | 01-AUG-04 | 19.93 | 9000 | 0 | 7 |
| 150 | Tucker, Peter | Sales Representative | 30-JAN-05 | 19.44 | 10000 | 1 | 4 |
| 159 | Smith, Lindsey | Sales Representative | 10-MAR-05 | 19.32 | 8000 | 0 | 11 |
| 168 | Ozer, Lisa | Sales Representative | 11-MAR-05 | 19.32 | 11500 | 1 | 1 |
| 175 | Hutton, Alyssa | Sales Representative | 19-MAR-05 | 19.3 | 8800 | 0 | 8 |
| 151 | Bernstein, David | Sales Representative | 24-MAR-05 | 19.28 | 9500 | 1 | 6 |
| 152 | Hall, Peter | Sales Representative | 20-AUG-05 | 18.88 | 9000 | 0 | 7 |
| 162 | Vishney, Clara | Sales Representative | 11-NOV-05 | 18.65 | 10500 | 1 | 3 |
| 160 | Doran, Louise | Sales Representative | 15-DEC-05 | 18.56 | 7500 | 0 | 12 |
| 170 | Fox, Tayler | Sales Representative | 24-JAN-06 | 18.45 | 9600 | 0 | 5 |
| 169 | Bloom, Harrison | Sales Representative | 23-MAR-06 | 18.29 | 10000 | 1 | 4 |
| 176 | Taylor, Jonathon | Sales Representative | 24-MAR-06 | 18.28 | 8600 | 1 | 9 |
| 153 | Olsen, Christopher | Sales Representative | 30-MAR-06 | 18.27 | 8000 | 0 | 11 |
| 177 | Livingston, Jack | Sales Representative | 23-APR-06 | 18.2 | 8400 | 1 | 10 |
| 161 | Sewall, Sarath | Sales Representative | 03-NOV-06 | 17.67 | 7000 | 0 | 15 |
| 154 | Cambrault, Nanette | Sales Representative | 09-DEC-06 | 17.57 | 7500 | 0 | 12 |
| 171 | Smith, William | Sales Representative | 23-FEB-07 | 17.37 | 8000 | 0 | 11 |
| 163 | Greene, Danielle | Sales Representative | 19-MAR-07 | 17.3 | 9500 | 1 | 6 |
| 172 | Bates, Elizabeth | Sales Representative | 24-MAR-07 | 17.28 | 7300 | 1 | 13 |
| 178 | Grant, Kimberely | Sales Representative | 24-MAY-07 | 17.12 | 7000 | 1 | 15 |
| 155 | Tuvault, Oliver | Sales Representative | 23-NOV-07 | 16.62 | 7000 | 1 | 15 |
| 179 | Johnson, Charles | Sales Representative | 04-JAN-08 | 16.51 | 6200 | 0 | 18 |
| 164 | Marvins, Mattea | Sales Representative | 24-JAN-08 | 16.45 | 7200 | 1 | 14 |
| 165 | Lee, David | Sales Representative | 23-FEB-08 | 16.37 | 6800 | 1 | 16 |
| 166 | Ande, Sundar | Sales Representative | 24-MAR-08 | 16.28 | 6400 | 1 | 17 |
| 167 | Banda, Amit | Sales Representative | 21-APR-08 | 16.21 | 6200 | 1 | 18 |
| 173 | Kumar, Sundita | Sales Representative | 21-APR-08 | 16.21 | 6100 | 1 | 19 |
| 184 | Sarchand, Nandita | Shipping Clerk | 27-JAN-04 | 20.44 | 4200 | 1 | 1 |
| 192 | Bell, Sarah | Shipping Clerk | 04-FEB-04 | 20.42 | 4000 | 0 | 3 |
| 185 | Bull, Alexis | Shipping Clerk | 20-FEB-05 | 19.38 | 4100 | 1 | 2 |
| 193 | Everett, Britney | Shipping Clerk | 03-MAR-05 | 19.34 | 3900 | 1 | 4 |
| 188 | Chung, Kelly | Shipping Clerk | 14-JUN-05 | 19.06 | 3800 | 1 | 5 |
| 189 | Dilly, Jennifer | Shipping Clerk | 13-AUG-05 | 18.9 | 3600 | 1 | 6 |
| 180 | Taylor, Winston | Shipping Clerk | 24-JAN-06 | 18.45 | 3200 | 1 | 8 |
| 181 | Fleaur, Jean | Shipping Clerk | 23-FEB-06 | 18.37 | 3100 | 1 | 9 |
| 196 | Walsh, Alana | Shipping Clerk | 24-APR-06 | 18.2 | 3100 | 1 | 9 |
| 197 | Feeney, Kevin | Shipping Clerk | 23-MAY-06 | 18.12 | 3000 | 0 | 10 |
| 186 | Dellinger, Julia | Shipping Clerk | 24-JUN-06 | 18.03 | 3400 | 1 | 7 |
| 194 | McCain, Samuel | Shipping Clerk | 01-JUL-06 | 18.01 | 3200 | 1 | 8 |
| 190 | Gates, Timothy | Shipping Clerk | 11-JUL-06 | 17.99 | 2900 | 0 | 11 |
| 187 | Cabrio, Anthony | Shipping Clerk | 07-FEB-07 | 17.41 | 3000 | 1 | 10 |
| 195 | Jones, Vance | Shipping Clerk | 17-MAR-07 | 17.3 | 2800 | 1 | 12 |
| 198 | OConnell, Donald | Shipping Clerk | 21-JUN-07 | 17.04 | 2600 | 1 | 13 |
| 182 | Sullivan, Martha | Shipping Clerk | 21-JUN-07 | 17.04 | 2500 | 1 | 14 |
| 191 | Perkins, Randall | Shipping Clerk | 19-DEC-07 | 16.55 | 2500 | 0 | 14 |
| 199 | Grant, Douglas | Shipping Clerk | 13-JAN-08 | 16.48 | 2600 | 0 | 13 |
| 183 | Geoni, Girard | Shipping Clerk | 03-FEB-08 | 16.42 | 2800 | 1 | 12 |
| 137 | Ladwig, Renske | Stock Clerk | 14-JUL-03 | 20.98 | 3600 | 1 | 1 |
| 141 | Rajs, Trenna | Stock Clerk | 17-OCT-03 | 20.72 | 3500 | 1 | 2 |
| 133 | Mallin, Jason | Stock Clerk | 14-JUN-04 | 20.06 | 3300 | 1 | 3 |
| 142 | Davies, Curtis | Stock Clerk | 29-JAN-05 | 19.44 | 3100 | 1 | 5 |
| 131 | Marlow, James | Stock Clerk | 16-FEB-05 | 19.39 | 2500 | 0 | 10 |
| 125 | Nayer, Julia | Stock Clerk | 16-JUL-05 | 18.97 | 3200 | 0 | 4 |
| 129 | Bissot, Laura | Stock Clerk | 20-AUG-05 | 18.88 | 3300 | 1 | 3 |
| 138 | Stiles, Stephen | Stock Clerk | 26-OCT-05 | 18.7 | 3200 | 1 | 4 |
| 130 | Atkinson, Mozhe | Stock Clerk | 30-OCT-05 | 18.69 | 2800 | 1 | 7 |
| 139 | Seo, John | Stock Clerk | 12-FEB-06 | 18.4 | 2700 | 1 | 8 |
| 143 | Matos, Randall | Stock Clerk | 15-MAR-06 | 18.31 | 2600 | 1 | 9 |
| 140 | Patel, Joshua | Stock Clerk | 06-APR-06 | 18.25 | 2500 | 1 | 10 |
| 144 | Vargas, Peter | Stock Clerk | 09-JUL-06 | 17.99 | 2500 | 0 | 10 |
| 134 | Rogers, Michael | Stock Clerk | 26-AUG-06 | 17.86 | 2900 | 1 | 6 |
| 126 | Mikkilineni, Irene | Stock Clerk | 28-SEP-06 | 17.77 | 2700 | 1 | 8 |
| 127 | Landry, James | Stock Clerk | 14-JAN-07 | 17.48 | 2400 | 1 | 11 |
| 132 | Olson, TJ | Stock Clerk | 10-APR-07 | 17.24 | 2100 | 0 | 13 |
| 135 | Gee, Ki | Stock Clerk | 12-DEC-07 | 16.57 | 2400 | 1 | 11 |
| 136 | Philtanker, Hazel | Stock Clerk | 06-FEB-08 | 16.42 | 2200 | 1 | 12 |
| 128 | Markle, Steven | Stock Clerk | 08-MAR-08 | 16.33 | 2200 | 1 | 12 |
| 122 | Kaufling, Payam | Stock Manager | 01-MAY-03 | 21.18 | 7900 | 0 | 3 |
| 120 | Weiss, Matthew | Stock Manager | 18-JUL-04 | 19.97 | 8000 | 0 | 2 |
| 121 | Fripp, Adam | Stock Manager | 10-APR-05 | 19.24 | 8200 | 1 | 1 |
| 123 | Vollman, Shanta | Stock Manager | 10-OCT-05 | 18.74 | 6500 | 1 | 4 |
| 124 | Mourgos, Kevin | Stock Manager | 16-NOV-07 | 16.64 | 5800 | 1 | 5 |
WITH applicable_job AS (
SELECT
hr.employees.job_id,
hr.jobs.job_title
FROM
hr.employees
INNER JOIN
hr.jobs
ON hr.employees.job_id = hr.jobs.job_id
GROUP BY
hr.employees.job_id,
hr.jobs.job_title
HAVING
COUNT(hr.employees.employee_id) > 1
)
SELECT
hr.employees.employee_id,
hr.employees.last_name || ', ' || hr.employees.first_name AS employee_name,
applicable_job.job_title,
hr.employees.hire_date,
ROUND(MONTHS_BETWEEN(SYSDATE, hr.employees.hire_date) / 12, 2) AS employment_duration,
hr.employees.salary,
CASE
WHEN LEAD(hr.employees.salary, 1, hr.employees.salary) OVER (
PARTITION BY hr.employees.job_id
ORDER BY hr.employees.hire_date, hr.employees.salary DESC
) <= hr.employees.salary
THEN 1
ELSE 0
END AS inconsistency_flag,
DENSE_RANK() OVER (
PARTITION BY hr.employees.job_id
ORDER BY hr.employees.salary DESC
) AS salary_sequence_desc
FROM
hr.employees
INNER JOIN
applicable_job
ON hr.employees.job_id = applicable_job.job_id
ORDER BY
hr.employees.job_id,
employment_duration DESC;
Problem 4
Single-order Customers
The company is attempting to understand customer purchasing behavior. Management has noticed that some customers never place an additional order after the first order. In an attempt to understand why, management wishes to acquire a list of the customers that exhibited this behavior. For each customer in the list, the order data is required. Include customer ID, order ID, order date, order total, line-item ID, unit price, and quantity data for each order. In addition to this data, include a line-item total. Line-item total is defined as the product of unit price and quantity for each line-item in the customer’s order. Alias this column as “line_item_total.” Sort the result set by customer ID (ascending) and line-item ID (ascending) for ease of use.
oe.orders
oe.order_items
| CUSTOMER_ID | ORDER_ID | ORDER_DATE | LINE_ITEM_ID | UNIT_PRICE | QUANTITY | LINE_ITEM_TOTAL | ORDER_TOTAL |
|---|---|---|---|---|---|---|---|
| 119 | 2372 | 27-FEB-07 12.22.33.356789 AM | 1 | 48 | 6 | 288 | 16447.2 |
| 119 | 2372 | 27-FEB-07 12.22.33.356789 AM | 2 | 74 | 2 | 148 | 16447.2 |
| 119 | 2372 | 27-FEB-07 12.22.33.356789 AM | 3 | 42 | 7 | 294 | 16447.2 |
| 119 | 2372 | 27-FEB-07 12.22.33.356789 AM | 4 | 81 | 10 | 810 | 16447.2 |
| 119 | 2372 | 27-FEB-07 12.22.33.356789 AM | 5 | 496 | 13 | 6448 | 16447.2 |
| 119 | 2372 | 27-FEB-07 12.22.33.356789 AM | 6 | 17 | 17 | 289 | 16447.2 |
| 119 | 2372 | 27-FEB-07 12.22.33.356789 AM | 7 | 15 | 21 | 315 | 16447.2 |
| 119 | 2372 | 27-FEB-07 12.22.33.356789 AM | 8 | 30 | 30 | 900 | 16447.2 |
| 119 | 2372 | 27-FEB-07 12.22.33.356789 AM | 9 | 54 | 32 | 1728 | 16447.2 |
| 119 | 2372 | 27-FEB-07 12.22.33.356789 AM | 10 | 145.2 | 36 | 5227.2 | 16447.2 |
| 120 | 2373 | 27-FEB-08 01.34.51.220065 AM | 1 | 49 | 8 | 392 | 416 |
| 120 | 2373 | 27-FEB-08 01.34.51.220065 AM | 2 | 24 | 1 | 24 | 416 |
| 121 | 2374 | 27-FEB-08 02.41.45.109654 AM | 1 | 150 | 10 | 1500 | 4797 |
| 121 | 2374 | 27-FEB-08 02.41.45.109654 AM | 2 | 78 | 6 | 468 | 4797 |
| 121 | 2374 | 27-FEB-08 02.41.45.109654 AM | 3 | 78 | 15 | 1170 | 4797 |
| 121 | 2374 | 27-FEB-08 02.41.45.109654 AM | 4 | 79 | 21 | 1659 | 4797 |
| 122 | 2375 | 26-FEB-07 03.49.50.459233 AM | 1 | 42 | 140 | 5880 | 103834.4 |
| 122 | 2375 | 26-FEB-07 03.49.50.459233 AM | 2 | 71 | 84 | 5964 | 103834.4 |
| 122 | 2375 | 26-FEB-07 03.49.50.459233 AM | 3 | 38 | 85 | 3230 | 103834.4 |
| 122 | 2375 | 26-FEB-07 03.49.50.459233 AM | 4 | 488.4 | 86 | 42002.4 | 103834.4 |
| 122 | 2375 | 26-FEB-07 03.49.50.459233 AM | 5 | 45 | 88 | 3960 | 103834.4 |
| 122 | 2375 | 26-FEB-07 03.49.50.459233 AM | 6 | 17 | 90 | 1530 | 103834.4 |
| 122 | 2375 | 26-FEB-07 03.49.50.459233 AM | 7 | 15 | 93 | 1395 | 103834.4 |
| 122 | 2375 | 26-FEB-07 03.49.50.459233 AM | 8 | 17 | 93 | 1581 | 103834.4 |
| 122 | 2375 | 26-FEB-07 03.49.50.459233 AM | 9 | 45 | 98 | 4410 | 103834.4 |
| 122 | 2375 | 26-FEB-07 03.49.50.459233 AM | 10 | 30 | 99 | 2970 | 103834.4 |
| 122 | 2375 | 26-FEB-07 03.49.50.459233 AM | 11 | 36 | 103 | 3708 | 103834.4 |
| 122 | 2375 | 26-FEB-07 03.49.50.459233 AM | 12 | 132 | 107 | 14124 | 103834.4 |
| 122 | 2375 | 26-FEB-07 03.49.50.459233 AM | 13 | 120 | 109 | 13080 | 103834.4 |
| 123 | 2376 | 07-JUN-07 06.18.08.883310 AM | 1 | 60 | 14 | 840 | 11006.2 |
| 123 | 2376 | 07-JUN-07 06.18.08.883310 AM | 2 | 236.5 | 4 | 946 | 11006.2 |
| 123 | 2376 | 07-JUN-07 06.18.08.883310 AM | 3 | 99 | 13 | 1287 | 11006.2 |
| 123 | 2376 | 07-JUN-07 06.18.08.883310 AM | 4 | 73 | 17 | 1241 | 11006.2 |
| 123 | 2376 | 07-JUN-07 06.18.08.883310 AM | 5 | 133.1 | 21 | 2795.1 | 11006.2 |
| 123 | 2376 | 07-JUN-07 06.18.08.883310 AM | 6 | 95 | 25 | 2375 | 11006.2 |
| 123 | 2376 | 07-JUN-07 06.18.08.883310 AM | 7 | 21 | 27 | 567 | 11006.2 |
| 123 | 2376 | 07-JUN-07 06.18.08.883310 AM | 8 | 25 | 32 | 800 | 11006.2 |
| 123 | 2376 | 07-JUN-07 06.18.08.883310 AM | 9 | 1.1 | 33 | 36.3 | 11006.2 |
| 123 | 2376 | 07-JUN-07 06.18.08.883310 AM | 10 | 3.3 | 36 | 118.8 | 11006.2 |
| 141 | 2377 | 07-JUN-07 07.03.01.001100 AM | 1 | 42 | 130 | 5460 | 38017.8 |
| 141 | 2377 | 07-JUN-07 07.03.01.001100 AM | 2 | 147 | 119 | 17493 | 38017.8 |
| 141 | 2377 | 07-JUN-07 07.03.01.001100 AM | 3 | 95 | 121 | 11495 | 38017.8 |
| 141 | 2377 | 07-JUN-07 07.03.01.001100 AM | 4 | 25 | 131 | 3275 | 38017.8 |
| 141 | 2377 | 07-JUN-07 07.03.01.001100 AM | 5 | 1.1 | 132 | 145.2 | 38017.8 |
| 141 | 2377 | 07-JUN-07 07.03.01.001100 AM | 6 | 1.1 | 136 | 149.6 | 38017.8 |
| 142 | 2378 | 24-MAY-07 08.59.10.010101 AM | 1 | 113.3 | 20 | 2266 | 25691.3 |
| 142 | 2378 | 24-MAY-07 08.59.10.010101 AM | 2 | 95 | 2 | 190 | 25691.3 |
| 142 | 2378 | 24-MAY-07 08.59.10.010101 AM | 3 | 438.9 | 7 | 3072.3 | 25691.3 |
| 142 | 2378 | 24-MAY-07 08.59.10.010101 AM | 4 | 27 | 11 | 297 | 25691.3 |
| 142 | 2378 | 24-MAY-07 08.59.10.010101 AM | 5 | 79 | 11 | 869 | 25691.3 |
| 142 | 2378 | 24-MAY-07 08.59.10.010101 AM | 6 | 217.8 | 15 | 3267 | 25691.3 |
| 142 | 2378 | 24-MAY-07 08.59.10.010101 AM | 7 | 4.4 | 25 | 110 | 25691.3 |
| 142 | 2378 | 24-MAY-07 08.59.10.010101 AM | 8 | 624.8 | 25 | 15620 | 25691.3 |
| 143 | 2380 | 16-MAY-07 09.53.02.909090 AM | 1 | 42 | 26 | 1092 | 27132.6 |
| 143 | 2380 | 16-MAY-07 09.53.02.909090 AM | 2 | 75 | 18 | 1350 | 27132.6 |
| 143 | 2380 | 16-MAY-07 09.53.02.909090 AM | 3 | 38 | 23 | 874 | 27132.6 |
| 143 | 2380 | 16-MAY-07 09.53.02.909090 AM | 4 | 488.4 | 24 | 11721.6 | 27132.6 |
| 143 | 2380 | 16-MAY-07 09.53.02.909090 AM | 5 | 46 | 28 | 1288 | 27132.6 |
| 143 | 2380 | 16-MAY-07 09.53.02.909090 AM | 6 | 20 | 30 | 600 | 27132.6 |
| 143 | 2380 | 16-MAY-07 09.53.02.909090 AM | 7 | 15 | 31 | 465 | 27132.6 |
| 143 | 2380 | 16-MAY-07 09.53.02.909090 AM | 8 | 17 | 33 | 561 | 27132.6 |
| 143 | 2380 | 16-MAY-07 09.53.02.909090 AM | 9 | 45 | 33 | 1485 | 27132.6 |
| 143 | 2380 | 16-MAY-07 09.53.02.909090 AM | 10 | 32 | 36 | 1152 | 27132.6 |
| 143 | 2380 | 16-MAY-07 09.53.02.909090 AM | 11 | 52 | 37 | 1924 | 27132.6 |
| 143 | 2380 | 16-MAY-07 09.53.02.909090 AM | 12 | 113.3 | 40 | 4532 | 27132.6 |
| 143 | 2380 | 16-MAY-07 09.53.02.909090 AM | 13 | 2.2 | 40 | 88 | 27132.6 |
| 150 | 2388 | 04-JUN-07 04.41.12.554435 PM | 1 | 43 | 150 | 6450 | 282694.3 |
| 150 | 2388 | 04-JUN-07 04.41.12.554435 PM | 2 | 94 | 90 | 8460 | 282694.3 |
| 150 | 2388 | 04-JUN-07 04.41.12.554435 PM | 3 | 56 | 96 | 5376 | 282694.3 |
| 150 | 2388 | 04-JUN-07 04.41.12.554435 PM | 4 | 1.1 | 105 | 115.5 | 282694.3 |
| 150 | 2388 | 04-JUN-07 04.41.12.554435 PM | 5 | 2341.9 | 112 | 262292.8 | 282694.3 |
| 151 | 2389 | 04-JUN-08 05.49.43.546954 PM | 1 | 43 | 180 | 7740 | 17620 |
| 151 | 2389 | 04-JUN-08 05.49.43.546954 PM | 2 | 73 | 18 | 1314 | 17620 |
| 151 | 2389 | 04-JUN-08 05.49.43.546954 PM | 3 | 80 | 21 | 1680 | 17620 |
| 151 | 2389 | 04-JUN-08 05.49.43.546954 PM | 4 | 46 | 22 | 1012 | 17620 |
| 151 | 2389 | 04-JUN-08 05.49.43.546954 PM | 5 | 15 | 30 | 450 | 17620 |
| 151 | 2389 | 04-JUN-08 05.49.43.546954 PM | 6 | 46 | 33 | 1518 | 17620 |
| 151 | 2389 | 04-JUN-08 05.49.43.546954 PM | 7 | 34 | 43 | 1462 | 17620 |
| 151 | 2389 | 04-JUN-08 05.49.43.546954 PM | 8 | 52 | 47 | 2444 | 17620 |
| 152 | 2390 | 18-NOV-07 04.18.50.546851 PM | 1 | 14 | 4 | 56 | 7616.8 |
| 152 | 2390 | 18-NOV-07 04.18.50.546851 PM | 2 | 14 | 2 | 28 | 7616.8 |
| 152 | 2390 | 18-NOV-07 04.18.50.546851 PM | 3 | 470.8 | 16 | 7532.8 | 7616.8 |
| 153 | 2391 | 27-FEB-06 05.03.03.828330 PM | 1 | 101 | 5 | 505 | 48070.6 |
| 153 | 2391 | 27-FEB-06 05.03.03.828330 PM | 2 | 262.9 | 3 | 788.7 | 48070.6 |
| 153 | 2391 | 27-FEB-06 05.03.03.828330 PM | 3 | 348 | 7 | 2436 | 48070.6 |
| 153 | 2391 | 27-FEB-06 05.03.03.828330 PM | 4 | 961.4 | 10 | 9614 | 48070.6 |
| 153 | 2391 | 27-FEB-06 05.03.03.828330 PM | 5 | 55 | 15 | 825 | 48070.6 |
| 153 | 2391 | 27-FEB-06 05.03.03.828330 PM | 6 | 52 | 18 | 936 | 48070.6 |
| 153 | 2391 | 27-FEB-06 05.03.03.828330 PM | 7 | 1433.3 | 23 | 32965.9 | 48070.6 |
| 154 | 2392 | 21-JUL-07 08.59.57.571057 PM | 1 | 43 | 63 | 2709 | 26632 |
| 154 | 2392 | 21-JUL-07 08.59.57.571057 PM | 2 | 73 | 57 | 4161 | 26632 |
| 154 | 2392 | 21-JUL-07 08.59.57.571057 PM | 3 | 38 | 58 | 2204 | 26632 |
| 154 | 2392 | 21-JUL-07 08.59.57.571057 PM | 4 | 77 | 63 | 4851 | 26632 |
| 154 | 2392 | 21-JUL-07 08.59.57.571057 PM | 5 | 45 | 66 | 2970 | 26632 |
| 154 | 2392 | 21-JUL-07 08.59.57.571057 PM | 6 | 21 | 68 | 1428 | 26632 |
| 154 | 2392 | 21-JUL-07 08.59.57.571057 PM | 7 | 18 | 72 | 1296 | 26632 |
| 154 | 2392 | 21-JUL-07 08.59.57.571057 PM | 8 | 49 | 77 | 3773 | 26632 |
| 154 | 2392 | 21-JUL-07 08.59.57.571057 PM | 9 | 40 | 81 | 3240 | 26632 |
| 155 | 2393 | 10-FEB-08 07.53.19.528202 PM | 1 | 12 | 10 | 120 | 23431.9 |
| 155 | 2393 | 10-FEB-08 07.53.19.528202 PM | 2 | 295 | 2 | 590 | 23431.9 |
| 155 | 2393 | 10-FEB-08 07.53.19.528202 PM | 3 | 1017 | 5 | 5085 | 23431.9 |
| 155 | 2393 | 10-FEB-08 07.53.19.528202 PM | 4 | 385 | 8 | 3080 | 23431.9 |
| 155 | 2393 | 10-FEB-08 07.53.19.528202 PM | 5 | 260.7 | 8 | 2085.6 | 23431.9 |
| 155 | 2393 | 10-FEB-08 07.53.19.528202 PM | 6 | 78 | 10 | 780 | 23431.9 |
| 155 | 2393 | 10-FEB-08 07.53.19.528202 PM | 7 | 211 | 13 | 2743 | 23431.9 |
| 155 | 2393 | 10-FEB-08 07.53.19.528202 PM | 8 | 108.9 | 14 | 1524.6 | 23431.9 |
| 155 | 2393 | 10-FEB-08 07.53.19.528202 PM | 9 | 278 | 19 | 5282 | 23431.9 |
| 155 | 2393 | 10-FEB-08 07.53.19.528202 PM | 10 | 3.3 | 19 | 62.7 | 23431.9 |
| 155 | 2393 | 10-FEB-08 07.53.19.528202 PM | 11 | 69.3 | 30 | 2079 | 23431.9 |
| 156 | 2395 | 02-FEB-06 08.19.11.227550 PM | 1 | 3.3 | 110 | 363 | 68501 |
| 156 | 2395 | 02-FEB-06 08.19.11.227550 PM | 2 | 332.2 | 27 | 8969.4 | 68501 |
| 156 | 2395 | 02-FEB-06 08.19.11.227550 PM | 3 | 788.7 | 30 | 23661 | 68501 |
| 156 | 2395 | 02-FEB-06 08.19.11.227550 PM | 4 | 690.8 | 34 | 23487.2 | 68501 |
| 156 | 2395 | 02-FEB-06 08.19.11.227550 PM | 5 | 199.1 | 34 | 6769.4 | 68501 |
| 156 | 2395 | 02-FEB-06 08.19.11.227550 PM | 6 | 71 | 37 | 2627 | 68501 |
| 156 | 2395 | 02-FEB-06 08.19.11.227550 PM | 7 | 64 | 41 | 2624 | 68501 |
| 157 | 2398 | 19-NOV-07 09.22.53.224175 PM | 1 | 482.9 | 5 | 2414.5 | 7110.3 |
| 157 | 2398 | 19-NOV-07 09.22.53.224175 PM | 2 | 193.6 | 23 | 4452.8 | 7110.3 |
| 157 | 2398 | 19-NOV-07 09.22.53.224175 PM | 3 | 9 | 27 | 243 | 7110.3 |
| 158 | 2399 | 19-NOV-07 10.22.38.340990 PM | 1 | 44 | 120 | 5280 | 25270.3 |
| 158 | 2399 | 19-NOV-07 10.22.38.340990 PM | 2 | 94 | 12 | 1128 | 25270.3 |
| 158 | 2399 | 19-NOV-07 10.22.38.340990 PM | 3 | 76 | 15 | 1140 | 25270.3 |
| 158 | 2399 | 19-NOV-07 10.22.38.340990 PM | 4 | 149 | 17 | 2533 | 25270.3 |
| 158 | 2399 | 19-NOV-07 10.22.38.340990 PM | 5 | 56 | 17 | 952 | 25270.3 |
| 158 | 2399 | 19-NOV-07 10.22.38.340990 PM | 6 | 86.9 | 20 | 1738 | 25270.3 |
| 158 | 2399 | 19-NOV-07 10.22.38.340990 PM | 7 | 22 | 24 | 528 | 25270.3 |
| 158 | 2399 | 19-NOV-07 10.22.38.340990 PM | 8 | 1.1 | 27 | 29.7 | 25270.3 |
| 158 | 2399 | 19-NOV-07 10.22.38.340990 PM | 9 | 1.1 | 28 | 30.8 | 25270.3 |
| 158 | 2399 | 19-NOV-07 10.22.38.340990 PM | 10 | 100 | 33 | 3300 | 25270.3 |
| 158 | 2399 | 19-NOV-07 10.22.38.340990 PM | 11 | 226.6 | 38 | 8610.8 | 25270.3 |
| 159 | 2400 | 10-JUL-07 01.34.29.559387 AM | 1 | 52 | 4 | 208 | 69286.4 |
| 159 | 2400 | 10-JUL-07 01.34.29.559387 AM | 2 | 41 | 1 | 41 | 69286.4 |
| 159 | 2400 | 10-JUL-07 01.34.29.559387 AM | 3 | 123 | 4 | 492 | 69286.4 |
| 159 | 2400 | 10-JUL-07 01.34.29.559387 AM | 4 | 880 | 16 | 14080 | 69286.4 |
| 159 | 2400 | 10-JUL-07 01.34.29.559387 AM | 5 | 2866.6 | 19 | 54465.4 | 69286.4 |
| 160 | 2401 | 10-JUL-07 02.22.53.554822 AM | 1 | 41 | 4 | 164 | 969.2 |
| 160 | 2401 | 10-JUL-07 02.22.53.554822 AM | 2 | 268.4 | 3 | 805.2 | 969.2 |
| 161 | 2402 | 02-JUL-07 03.34.44.665170 AM | 1 | 75 | 8 | 600 | 600 |
| 162 | 2403 | 01-JUL-07 04.49.13.615512 PM | 1 | 44 | 5 | 220 | 220 |
| 163 | 2404 | 01-JUL-07 04.49.13.664085 PM | 1 | 85 | 6 | 510 | 510 |
| 163 | 2404 | 01-JUL-07 04.49.13.664085 PM | 2 | 0 | 37 | 0 | 510 |
| 164 | 2405 | 01-JUL-07 04.49.13.678123 PM | 1 | 137 | 9 | 1233 | 1233 |
| 165 | 2407 | 29-JUN-07 07.03.21.526005 AM | 1 | 85 | 5 | 425 | 2519 |
| 165 | 2407 | 29-JUN-07 07.03.21.526005 AM | 2 | 86 | 18 | 1548 | 2519 |
| 165 | 2407 | 29-JUN-07 07.03.21.526005 AM | 3 | 26 | 21 | 546 | 2519 |
| 166 | 2408 | 29-JUN-07 08.59.31.333617 AM | 1 | 61 | 3 | 183 | 309 |
| 166 | 2408 | 29-JUN-07 08.59.31.333617 AM | 2 | 26 | 1 | 26 | 309 |
| 166 | 2408 | 29-JUN-07 08.59.31.333617 AM | 3 | 10 | 10 | 100 | 309 |
| 167 | 2409 | 29-JUN-07 09.53.41.984501 AM | 1 | 6 | 8 | 48 | 48 |
| 168 | 2410 | 24-MAY-08 10.19.51.985501 AM | 1 | 46 | 10 | 460 | 45175 |
| 168 | 2410 | 24-MAY-08 10.19.51.985501 AM | 2 | 40 | 5 | 200 | 45175 |
| 168 | 2410 | 24-MAY-08 10.19.51.985501 AM | 3 | 120 | 6 | 720 | 45175 |
| 168 | 2410 | 24-MAY-08 10.19.51.985501 AM | 4 | 68 | 8 | 544 | 45175 |
| 168 | 2410 | 24-MAY-08 10.19.51.985501 AM | 5 | 2866.6 | 15 | 42999 | 45175 |
| 168 | 2410 | 24-MAY-08 10.19.51.985501 AM | 6 | 12 | 21 | 252 | 45175 |
| 169 | 2411 | 24-MAY-07 11.22.10.548639 AM | 1 | 81 | 2 | 162 | 15760.5 |
| 169 | 2411 | 24-MAY-07 11.22.10.548639 AM | 2 | 208 | 2 | 416 | 15760.5 |
| 169 | 2411 | 24-MAY-07 11.22.10.548639 AM | 3 | 2.2 | 6 | 13.2 | 15760.5 |
| 169 | 2411 | 24-MAY-07 11.22.10.548639 AM | 4 | 3.3 | 7 | 23.1 | 15760.5 |
| 169 | 2411 | 24-MAY-07 11.22.10.548639 AM | 5 | 73 | 8 | 584 | 15760.5 |
| 169 | 2411 | 24-MAY-07 11.22.10.548639 AM | 6 | 45 | 11 | 495 | 15760.5 |
| 169 | 2411 | 24-MAY-07 11.22.10.548639 AM | 7 | 72 | 17 | 1224 | 15760.5 |
| 169 | 2411 | 24-MAY-07 11.22.10.548639 AM | 8 | 75 | 17 | 1275 | 15760.5 |
| 169 | 2411 | 24-MAY-07 11.22.10.548639 AM | 9 | 84 | 17 | 1428 | 15760.5 |
| 169 | 2411 | 24-MAY-07 11.22.10.548639 AM | 10 | 488.4 | 18 | 8791.2 | 15760.5 |
| 169 | 2411 | 24-MAY-07 11.22.10.548639 AM | 11 | 43 | 23 | 989 | 15760.5 |
| 169 | 2411 | 24-MAY-07 11.22.10.548639 AM | 12 | 15 | 24 | 360 | 15760.5 |
| 170 | 2412 | 29-MAR-06 10.22.09.509801 AM | 1 | 46 | 170 | 7820 | 66816 |
| 170 | 2412 | 29-MAR-06 10.22.09.509801 AM | 2 | 98 | 68 | 6664 | 66816 |
| 170 | 2412 | 29-MAR-06 10.22.09.509801 AM | 3 | 71.5 | 68 | 4862 | 66816 |
| 170 | 2412 | 29-MAR-06 10.22.09.509801 AM | 4 | 492 | 72 | 35424 | 66816 |
| 170 | 2412 | 29-MAR-06 10.22.09.509801 AM | 5 | 18 | 75 | 1350 | 66816 |
| 170 | 2412 | 29-MAR-06 10.22.09.509801 AM | 6 | 20 | 79 | 1580 | 66816 |
| 170 | 2412 | 29-MAR-06 10.22.09.509801 AM | 7 | 16 | 80 | 1280 | 66816 |
| 170 | 2412 | 29-MAR-06 10.22.09.509801 AM | 8 | 30 | 92 | 2760 | 66816 |
| 170 | 2412 | 29-MAR-06 10.22.09.509801 AM | 9 | 54 | 94 | 5076 | 66816 |
-- Solution 1
WITH single_order_customer AS (
SELECT
oe.orders.customer_id,
COUNT(oe.orders.order_id) AS order_count
FROM
oe.orders
GROUP BY
oe.orders.customer_id
HAVING
COUNT(oe.orders.order_id) = 1
)
SELECT
oe.orders.customer_id,
oe.orders.order_id,
oe.orders.order_date,
oe.order_items.line_item_id,
oe.order_items.unit_price,
oe.order_items.quantity,
oe.order_items.unit_price * oe.order_items.quantity AS line_item_total,
oe.orders.order_total
FROM
oe.orders
INNER JOIN
single_order_customer
ON oe.orders.customer_id = single_order_customer.customer_id
INNER JOIN
oe.order_items
ON oe.orders.order_id = oe.order_items.order_id
ORDER BY
oe.orders.customer_id,
oe.order_items.line_item_id;
-- Solution 2
SELECT
oe.orders.customer_id,
oe.orders.order_id,
oe.orders.order_date,
oe.order_items.line_item_id,
oe.order_items.unit_price,
oe.order_items.quantity,
oe.order_items.unit_price * oe.order_items.quantity AS line_item_total,
oe.orders.order_total
FROM
oe.orders
INNER JOIN (
SELECT
oe.orders.customer_id,
COUNT(oe.orders.order_id) AS order_count
FROM
oe.orders
GROUP BY
oe.orders.customer_id
HAVING
COUNT(oe.orders.order_id) = 1
) single_order
ON oe.orders.customer_id = single_order.customer_id
INNER JOIN
oe.order_items
ON oe.orders.order_id = oe.order_items.order_id
ORDER BY
oe.orders.customer_id,
oe.order_items.line_item_id;
Problem 5
Product Category Performance
It’s year-end review time at the company and managers will either be praised or reprimanded for their product categories performance in year 2007. Performance is based on the number of times a product category appeared across all customer orders, not quantity sold. It has been asked of you to generate a report containing the top 5 and bottom 5 product categories. Ties in order count should be assigned the same rank (i.e., 50, 49, 48, 48, 47, 46 -> 1, 2, 3, 3, 4, 5). The category ID, category description, and number of times the product category appeared in year 2007 orders are the required data points. Sort the result set by order count, descending.
oe.orders
oe.order_items
oe.product_information
oe.categories_tab
| CATEGORY_ID | CATEGORY_DESCRIPTION | ORDER_COUNT |
|---|---|---|
| 19 | miscellaneous hardware (cables, screws, power supplies …) | 102 |
| 16 | keyboards, mouses, mouse pads | 48 |
| 39 | miscellaneous office supplies | 44 |
| 15 | processors, sound and video cards, network cards, motherboards | 42 |
| 32 | office supplies for daily use (pencils, erasers, staples, …) | 34 |
| 14 | memory components/upgrades | 13 |
| 24 | operating systems | 12 |
| 31 | capitalizable assets (desks, chairs, phones …) | 9 |
| 17 | other peripherals (CD-ROM, DVD, tape cartridge drives, …) | 8 |
| 13 | harddisks | 8 |
| 21 | spreadsheet software | 2 |
| 25 | software development tools (including languages) | 2 |
| 22 | word processing software | 2 |
-- Solution 1
WITH category_popularity AS (
SELECT
oe.product_information.category_id,
oe.categories_tab.category_description,
COUNT(oe.product_information.category_id) AS order_count,
DENSE_RANK() OVER (
ORDER BY COUNT(oe.product_information.category_id)
) AS category_popularity_sequence_asc,
DENSE_RANK() OVER (
ORDER BY COUNT(oe.product_information.category_id) DESC
) AS category_popularity_sequence_desc
FROM
oe.orders
INNER JOIN
oe.order_items
ON oe.orders.order_id = oe.order_items.order_id
INNER JOIN
oe.product_information
ON oe.order_items.product_id = oe.product_information.product_id
INNER JOIN
oe.categories_tab
ON oe.product_information.category_id = oe.categories_tab.category_id
WHERE
oe.orders.order_date >= '01-JAN-07'
AND oe.orders.order_date < '01-JAN-08'
GROUP BY
oe.product_information.category_id,
oe.categories_tab.category_description
)
SELECT
category_popularity.category_id,
category_popularity.category_description,
category_popularity.order_count
FROM
category_popularity
WHERE
category_popularity.category_popularity_sequence_asc <= 5
OR category_popularity.category_popularity_sequence_desc <= 5
ORDER BY
category_popularity.order_count DESC;
-- Solution 2
SELECT
category_popularity.category_id,
category_popularity.category_description,
category_popularity.order_count
FROM (
SELECT
oe.product_information.category_id,
oe.categories_tab.category_description,
COUNT(oe.product_information.category_id) AS order_count,
DENSE_RANK() OVER (
ORDER BY COUNT(oe.product_information.category_id)
) AS category_popularity_sequence_asc,
DENSE_RANK() OVER (
ORDER BY COUNT(oe.product_information.category_id) DESC
) AS category_popularity_sequence_desc
FROM
oe.orders
INNER JOIN
oe.order_items
ON oe.orders.order_id = oe.order_items.order_id
INNER JOIN
oe.product_information
ON oe.order_items.product_id = oe.product_information.product_id
INNER JOIN
oe.categories_tab
ON oe.product_information.category_id = oe.categories_tab.category_id
WHERE
oe.orders.order_date >= '01-JAN-07'
AND oe.orders.order_date < '01-JAN-08'
GROUP BY
oe.product_information.category_id,
oe.categories_tab.category_description
) category_popularity
WHERE
category_popularity.category_popularity_sequence_asc <= 5
OR category_popularity.category_popularity_sequence_desc <= 5
ORDER BY
category_popularity.order_count DESC;
Problem 6
Online Ordering System Adoption
In late 2005, the company heavily invested in developing an online ordering system. The goal was by the end of 2008 for online orders to account for at least 50% of all orders within the year. Management is asking you to generate a report including the number of online orders placed, total number of orders placed across all order modes, and the number of online orders represented as a percentage of all orders for each year. Use the function, ROUND, to round the percentage values to two decimal places. Sort the result set by order year, ascending.
oe.orders
| ORDER_MODE | ORDER_YEAR | MODE_COUNT | ORDER_COUNT | PERCENT_OF_TOTAL |
|---|---|---|---|---|
| online | 2006 | 2 | 16 | 12.5 |
| online | 2007 | 20 | 69 | 28.99 |
| online | 2008 | 10 | 19 | 52.63 |
-- Solution 1
WITH yearly_mode_count AS (
SELECT
oe.orders.order_mode,
EXTRACT(YEAR FROM oe.orders.order_date) AS order_year,
COUNT(oe.orders.order_id) AS order_count
FROM
oe.orders
WHERE
oe.orders.order_mode = 'online'
GROUP BY
oe.orders.order_mode,
EXTRACT(YEAR FROM oe.orders.order_date)
),
yearly_count AS (
SELECT
EXTRACT(YEAR FROM oe.orders.order_date) AS order_year,
COUNT(oe.orders.order_id) AS order_count
FROM
oe.orders
GROUP BY
EXTRACT(YEAR FROM oe.orders.order_date)
)
SELECT
yearly_mode_count.order_mode,
yearly_mode_count.order_year,
yearly_mode_count.order_count AS mode_count,
yearly_count.order_count,
ROUND((yearly_mode_count.order_count / yearly_count.order_count) * 100, 2) AS percent_of_total
FROM
yearly_mode_count
INNER JOIN
yearly_count
ON yearly_mode_count.order_year = yearly_count.order_year
ORDER BY
yearly_mode_count.order_year;
-- Solution 2
SELECT
yearly_mode_count.order_mode,
yearly_mode_count.order_year,
yearly_mode_count.order_count AS mode_count,
yearly_count.order_count,
ROUND((yearly_mode_count.order_count / yearly_count.order_count) * 100, 2) AS percent_of_total
FROM (
SELECT
oe.orders.order_mode,
EXTRACT(YEAR FROM oe.orders.order_date) AS order_year,
COUNT(oe.orders.order_id) AS order_count
FROM
oe.orders
WHERE
oe.orders.order_mode = 'online'
GROUP BY
oe.orders.order_mode,
EXTRACT(YEAR FROM oe.orders.order_date)
) yearly_mode_count
INNER JOIN (
SELECT
EXTRACT(YEAR FROM oe.orders.order_date) AS order_year,
COUNT(oe.orders.order_id) AS order_count
FROM
oe.orders
GROUP BY
EXTRACT(YEAR FROM oe.orders.order_date)
) yearly_count
ON yearly_mode_count.order_year = yearly_count.order_year
ORDER BY
yearly_mode_count.order_year;
Problem 7
Top 5 Customers
The company distributes rewards to customers based on the amount spent each year. Only the top 5 customers are rewarded. Management requires a report containing customer IDs, customer names, order totals across all orders, and customer’s rank for the top 5 customers. Customer rank is based on the sum of all order totals for the customer compared to all other customers’ sums. The customer with the largest amount spent for the year is rank 1, second largest spent for the year is rank 2, and so on. Ties in customer yearly spending will receive the same rank. Use consecutive integers (e.g., 1, 1, 2, 3, 4, 4, 5). Only consider orders placed in year 2008 and format the customer’s name as “last name, first name.” Alias the derived columns with meaningful names. Sort the result set by customer’s sum of order totals, descending.
oe.customers
oe.orders
| CUSTOMER_ID | CUSTOMER_NAME | ORDER_TOTAL | ORDER_TOTAL_SEQUENCE |
|---|---|---|---|
| 148 | Steenburgen, Gustav | 144054.8 | 1 |
| 101 | Welles, Constantin | 82445.6 | 2 |
| 144 | Landis, Sivaji | 71173 | 3 |
| 149 | Rampling, Markus | 60065 | 4 |
| 104 | Sutherland, Harrison | 46257 | 5 |
-- Solution 1
WITH customer_spending AS (
SELECT
oe.customers.customer_id,
oe.customers.cust_last_name || ', ' || oe.customers.cust_first_name AS customer_name,
SUM(oe.orders.order_total) AS order_total,
DENSE_RANK() OVER (ORDER BY SUM(oe.orders.order_total) DESC) AS order_total_sequence
FROM
oe.customers
INNER JOIN
oe.orders
ON oe.customers.customer_id = oe.orders.customer_id
WHERE
oe.orders.order_date >= '01-JAN-08'
AND oe.orders.order_date < '01-JAN-09'
GROUP BY
oe.customers.customer_id,
oe.customers.cust_last_name || ', ' || oe.customers.cust_first_name
)
SELECT
customer_spending.customer_id,
customer_spending.customer_name,
customer_spending.order_total,
customer_spending.order_total_sequence
FROM
customer_spending
WHERE
customer_spending.order_total_sequence <= 5
ORDER BY
customer_spending.order_total DESC;
-- Solution 2
SELECT
customer_spending.customer_id,
customer_spending.customer_name,
customer_spending.order_total,
customer_spending.order_total_sequence
FROM (
SELECT
oe.customers.customer_id,
oe.customers.cust_last_name || ', ' || oe.customers.cust_first_name AS customer_name,
SUM(oe.orders.order_total) AS order_total,
DENSE_RANK() OVER (ORDER BY SUM(oe.orders.order_total) DESC) AS order_total_sequence
FROM
oe.customers
INNER JOIN
oe.orders
ON oe.customers.customer_id = oe.orders.customer_id
WHERE
oe.orders.order_date >= '01-JAN-08'
AND oe.orders.order_date < '01-JAN-09'
GROUP BY
oe.customers.customer_id,
oe.customers.cust_last_name || ', ' || oe.customers.cust_first_name
) customer_spending
WHERE
customer_spending.order_total_sequence <= 5
ORDER BY
customer_spending.order_total DESC;
Problem 8
Supplier Replacement
The company’s only supplier of operating systems is going out of business, leaving the company only a few weeks to find a new supplier before stock levels hit zero. The company wishes to replace the former supplier with a supplier they already do business with for closely related products. Here, closely related is defined as supplying products in any software category. Management wishes to receive a report containing all suppliers currently supplying software products to the company. In addition to the list of suppliers, include the number of software products each supplier currently supplies. Do not include the supplier going out of business, supplier ID 103092. Sort the supplier list by the number of software products supplied, descending. HINT: category names starting with the word “software” satisfy the requirement of the supplier being closely related.
oe.product_information
oe.categories_tab
| SUPPLIER_ID | PRODUCT_COUNT |
|---|---|
| 103089 | 10 |
| 103080 | 7 |
| 103093 | 6 |
| 103088 | 4 |
| 103086 | 3 |
| 103082 | 3 |
| 103094 | 2 |
| 103096 | 1 |
| 103087 | 1 |
| 103083 | 1 |
SELECT
oe.product_information.supplier_id,
COUNT(*) AS product_count
FROM
oe.product_information
INNER JOIN
oe.categories_tab
ON oe.product_information.category_id = oe.categories_tab.category_id
WHERE
oe.product_information.supplier_id != '103092'
AND oe.categories_tab.category_name LIKE 'software%'
GROUP BY
oe.product_information.supplier_id
ORDER BY
product_count DESC;
Problem 9
Sales Representative Performance
All sales representatives for the company report to Mary. She has recently mentioned that she would like to see a report containing the number of orders placed by customers assisted by a sales representative in years 2007 and 2008. This task has been assigned to you. Ensure that the report is sorted by order year (ascending), order count (ascending), and sales representative ID (ascending).
oe.orders
| SALES_REP_ID | ORDER_YEAR | ORDER_COUNT |
|---|---|---|
| 156 | 2007 | 2 |
| 155 | 2007 | 3 |
| 158 | 2007 | 3 |
| 153 | 2007 | 5 |
| 160 | 2007 | 5 |
| 163 | 2007 | 6 |
| 159 | 2007 | 7 |
| 154 | 2007 | 8 |
| 161 | 2007 | 8 |
| 158 | 2008 | 1 |
| 160 | 2008 | 1 |
| 155 | 2008 | 2 |
| 156 | 2008 | 2 |
| 161 | 2008 | 3 |
SELECT
oe.orders.sales_rep_id,
EXTRACT(YEAR FROM oe.orders.order_date) AS order_year,
COUNT(*) AS order_count
FROM
oe.orders
WHERE
oe.orders.sales_rep_id IS NOT NULL
AND oe.orders.order_date >= '01-JAN-07'
AND oe.orders.order_date < '01-JAN-09'
GROUP BY
oe.orders.sales_rep_id,
EXTRACT(YEAR FROM oe.orders.order_date)
ORDER BY
order_year,
order_count,
oe.orders.sales_rep_id;
Problem 10
Consecutive Top Product Category Performances
It is the beginning of year 2024 and management over the North American sales region is developing a new program to reward product category managers. The reward program is designed to compensate a product category manager when their category performs better than all others for consecutive months. Performance is measured by total sales. To better understand how frequently product category managers will be compensated because of this new program, management is asking you to provide a report containing information on the instances where a product category has generated the most sales for consecutive months in the previous year (i.e., 2023). An instance is defined by a product category generating the most sales in a specific month and the following month. The report should contain the month, product category, and total sale value associated with each instance. Additionally, ensure the report is sorted in ascending order by reporting month.
north_america_sale
| REPORTING_MONTH | PRODUCT_CATEGORY | TOTAL_SALE |
|---|---|---|
| 01-MAR-23 | Hardware | 10107952 |
| 01-APR-23 | Hardware | 9981157 |
| 01-AUG-23 | Software | 9848671 |
| 01-SEP-23 | Software | 9532093 |
| 01-NOV-23 | Office Supplies | 9474652 |
| 01-DEC-23 | Office Supplies | 10441283 |
WITH product_category_performance AS (
SELECT
TRUNC(north_america_sale.report_date, 'MONTH') AS reporting_month,
DENSE_RANK() OVER (
PARTITION BY TRUNC(north_america_sale.report_date, 'MONTH')
ORDER BY SUM(north_america_sale.total_sale) DESC
) AS product_category_rank,
north_america_sale.product_category,
SUM(north_america_sale.total_sale) AS total_sale
FROM
north_america_sale
GROUP BY
TRUNC(north_america_sale.report_date, 'MONTH'),
north_america_sale.product_category
),
performance_window AS (
SELECT
product_category_performance.reporting_month,
product_category_performance.product_category_rank,
product_category_performance.product_category,
product_category_performance.total_sale,
LAG(product_category_performance.product_category_rank) OVER (
PARTITION BY product_category_performance.product_category
ORDER BY product_category_performance.reporting_month
) AS previous_month_rank,
LEAD(product_category_performance.product_category_rank) OVER (
PARTITION BY product_category_performance.product_category
ORDER BY product_category_performance.reporting_month
) AS next_month_rank
FROM
product_category_performance
)
SELECT
performance_window.reporting_month,
performance_window.product_category,
performance_window.total_sale
FROM
performance_window
WHERE
performance_window.product_category_rank = 1
AND (
previous_month_rank = 1
OR next_month_rank = 1
)
ORDER BY
performance_window.reporting_month;
Problem 11
Identifying Top Email Domains
Last year, you helped your company launch a website to begin selling their goods and services online. Today you have been asked by your manager, Jane, to develop a means to allow shoppers to create an account and login using their pre-existing email account information. Jane realizes that you cannot solve this problem for every domain used by shoppers, so she would like you to examine the email address data on file supplied by past customers to acquire an idea on which types of domains to focus on based on their popularity among customers. She has requested you to generate a report containing the top two domains based on the number of customers associated with each domain. Ties are to be addressed by assigning the same rank to each domain involved. When a tie occurs, assign the next most popular domain a consecutive rank value (e.g., 1, 1, 2, 2, 3). Sort the report in descending order by the number of email addresses associated with the domains.
oe.customers
| DOMAIN | EMAIL_COUNT |
|---|---|
| DUNLIN.EXAMPLE.COM | 8 |
| ANHINGA.EXAMPLE.COM | 7 |
WITH domain_extraction AS (
SELECT
oe.customers.cust_email,
SUBSTR(oe.customers.cust_email,
INSTR(oe.customers.cust_email,'@', 1, 1) + 1,
LENGTH(oe.customers.cust_email)
- INSTR(oe.customers.cust_email,'@', 1, 1)
) AS domain
FROM
oe.customers
),
domain_popularity AS (
SELECT
domain_extraction.domain,
COUNT(*) AS email_count,
DENSE_RANK() OVER (
ORDER BY COUNT(*) DESC
) AS domain_ranking
FROM
domain_extraction
GROUP BY
domain_extraction.domain
)
SELECT
domain_popularity.domain,
domain_popularity.email_count
FROM
domain_popularity
WHERE
domain_popularity.domain_ranking <= 2
ORDER BY
domain_popularity.email_count DESC;
Problem 12
Member Type Upgrade
Sonia, the ice cream shop’s manager, is hoping to send each non-platinum member a summary of their purchases via email. She needs each summary to contain the member’s ID, member type, annual member cost, member discount, number of purchases made, amount spent across purchases, and amount saved across purchases. In hopes of persuading members to upgrade to the next member type tier, she also wishes to include the member type, annual member cost, member discount, and the amount saved corresponding to the member type tier one level above the member’s current member tier. She doesn’t want members to have to compute the increased benefit in terms of amount saved, so she needs you to derive the raw increase in the amount each member would have saved with the upgraded member type for them. This is simply the amount saved under the upgraded member type less the original savings. Make the column names report friendly through aliases and sort the result set in ascending order by the members’ current member type ID values.
member
member_type
purchase
| Member ID | Member Type ID | Member Type | Annual Member Cost | Member Discount | Purchase Count | Amount Spent | Amount Saved | Next-tier Member Type | Next-tier Annual Member Cost | Next-tier Member Discount | Next-tier Amount Saved | Increased Savings Amount |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 1 | Free | 0 | 0 | 2 | 17 | 0 | Bronze | 15 | .03 | .51 | .51 |
| 10 | 1 | Free | 0 | 0 | 2 | 19 | 0 | Bronze | 15 | .03 | .57 | .57 |
| 25 | 1 | Free | 0 | 0 | 3 | 25.5 | 0 | Bronze | 15 | .03 | .77 | .77 |
| 35 | 1 | Free | 0 | 0 | 2 | 9 | 0 | Bronze | 15 | .03 | .27 | .27 |
| 48 | 1 | Free | 0 | 0 | 2 | 35 | 0 | Bronze | 15 | .03 | 1.05 | 1.05 |
| 51 | 1 | Free | 0 | 0 | 1 | 17.5 | 0 | Bronze | 15 | .03 | .53 | .53 |
| 54 | 1 | Free | 0 | 0 | 1 | 14 | 0 | Bronze | 15 | .03 | .42 | .42 |
| 57 | 1 | Free | 0 | 0 | 2 | 27 | 0 | Bronze | 15 | .03 | .81 | .81 |
| 61 | 1 | Free | 0 | 0 | 3 | 38.5 | 0 | Bronze | 15 | .03 | 1.16 | 1.16 |
| 63 | 1 | Free | 0 | 0 | 3 | 30 | 0 | Bronze | 15 | .03 | .9 | .9 |
| 67 | 1 | Free | 0 | 0 | 1 | 22.5 | 0 | Bronze | 15 | .03 | .68 | .68 |
| 77 | 1 | Free | 0 | 0 | 1 | 21 | 0 | Bronze | 15 | .03 | .63 | .63 |
| 84 | 1 | Free | 0 | 0 | 4 | 70 | 0 | Bronze | 15 | .03 | 2.1 | 2.1 |
| 88 | 1 | Free | 0 | 0 | 2 | 33 | 0 | Bronze | 15 | .03 | .99 | .99 |
| 90 | 1 | Free | 0 | 0 | 2 | 30 | 0 | Bronze | 15 | .03 | .9 | .9 |
| 97 | 1 | Free | 0 | 0 | 3 | 53.5 | 0 | Bronze | 15 | .03 | 1.61 | 1.61 |
| 98 | 1 | Free | 0 | 0 | 3 | 65 | 0 | Bronze | 15 | .03 | 1.95 | 1.95 |
| 99 | 1 | Free | 0 | 0 | 1 | 16.5 | 0 | Bronze | 15 | .03 | .5 | .5 |
| 100 | 1 | Free | 0 | 0 | 2 | 32 | 0 | Bronze | 15 | .03 | .96 | .96 |
| 3 | 2 | Bronze | 15 | .03 | 2 | 45.6 | 1.41 | Silver | 20 | .05 | 2.35 | .94 |
| 11 | 2 | Bronze | 15 | .03 | 2 | 14.55 | .45 | Silver | 20 | .05 | .75 | .3 |
| 16 | 2 | Bronze | 15 | .03 | 2 | 25.22 | .78 | Silver | 20 | .05 | 1.3 | .52 |
| 20 | 2 | Bronze | 15 | .03 | 1 | 13.59 | .42 | Silver | 20 | .05 | .7 | .28 |
| 21 | 2 | Bronze | 15 | .03 | 1 | 10.19 | .32 | Silver | 20 | .05 | .53 | .21 |
| 22 | 2 | Bronze | 15 | .03 | 2 | 24.25 | .75 | Silver | 20 | .05 | 1.25 | .5 |
| 24 | 2 | Bronze | 15 | .03 | 1 | 23.28 | .72 | Silver | 20 | .05 | 1.2 | .48 |
| 34 | 2 | Bronze | 15 | .03 | 1 | 4.85 | .15 | Silver | 20 | .05 | .25 | .1 |
| 39 | 2 | Bronze | 15 | .03 | 5 | 56.75 | 1.76 | Silver | 20 | .05 | 2.93 | 1.17 |
| 41 | 2 | Bronze | 15 | .03 | 1 | 7.76 | .24 | Silver | 20 | .05 | .4 | .16 |
| 43 | 2 | Bronze | 15 | .03 | 1 | 8.73 | .27 | Silver | 20 | .05 | .45 | .18 |
| 44 | 2 | Bronze | 15 | .03 | 2 | 29.59 | .92 | Silver | 20 | .05 | 1.53 | .61 |
| 47 | 2 | Bronze | 15 | .03 | 2 | 44.14 | 1.37 | Silver | 20 | .05 | 2.28 | .91 |
| 56 | 2 | Bronze | 15 | .03 | 1 | 30.56 | .95 | Silver | 20 | .05 | 1.58 | .63 |
| 59 | 2 | Bronze | 15 | .03 | 3 | 60.15 | 1.86 | Silver | 20 | .05 | 3.1 | 1.24 |
| 62 | 2 | Bronze | 15 | .03 | 1 | 13.58 | .42 | Silver | 20 | .05 | .7 | .28 |
| 69 | 2 | Bronze | 15 | .03 | 2 | 36.86 | 1.14 | Silver | 20 | .05 | 1.9 | .76 |
| 70 | 2 | Bronze | 15 | .03 | 3 | 46.56 | 1.44 | Silver | 20 | .05 | 2.4 | .96 |
| 76 | 2 | Bronze | 15 | .03 | 1 | 7.76 | .24 | Silver | 20 | .05 | .4 | .16 |
| 78 | 2 | Bronze | 15 | .03 | 2 | 42.69 | 1.32 | Silver | 20 | .05 | 2.2 | .88 |
| 80 | 2 | Bronze | 15 | .03 | 1 | 2.91 | .09 | Silver | 20 | .05 | .15 | .06 |
| 89 | 2 | Bronze | 15 | .03 | 1 | 5.82 | .18 | Silver | 20 | .05 | .3 | .12 |
| 6 | 3 | Silver | 20 | .05 | 3 | 50.84 | 2.68 | Gold | 25 | .07 | 3.75 | 1.07 |
| 7 | 3 | Silver | 20 | .05 | 2 | 13.78 | .73 | Gold | 25 | .07 | 1.02 | .29 |
| 12 | 3 | Silver | 20 | .05 | 2 | 18.05 | .95 | Gold | 25 | .07 | 1.33 | .38 |
| 18 | 3 | Silver | 20 | .05 | 1 | 5.23 | .28 | Gold | 25 | .07 | .39 | .11 |
| 19 | 3 | Silver | 20 | .05 | 2 | 23.75 | 1.25 | Gold | 25 | .07 | 1.75 | .5 |
| 26 | 3 | Silver | 20 | .05 | 1 | 5.23 | .28 | Gold | 25 | .07 | .39 | .11 |
| 27 | 3 | Silver | 20 | .05 | 4 | 39.9 | 2.1 | Gold | 25 | .07 | 2.94 | .84 |
| 36 | 3 | Silver | 20 | .05 | 1 | 11.4 | .6 | Gold | 25 | .07 | .84 | .24 |
| 40 | 3 | Silver | 20 | .05 | 2 | 24.7 | 1.3 | Gold | 25 | .07 | 1.82 | .52 |
| 55 | 3 | Silver | 20 | .05 | 2 | 16.63 | .88 | Gold | 25 | .07 | 1.23 | .35 |
| 58 | 3 | Silver | 20 | .05 | 1 | 7.13 | .38 | Gold | 25 | .07 | .53 | .15 |
| 64 | 3 | Silver | 20 | .05 | 2 | 11.4 | .6 | Gold | 25 | .07 | .84 | .24 |
| 71 | 3 | Silver | 20 | .05 | 4 | 57.96 | 3.05 | Gold | 25 | .07 | 4.27 | 1.22 |
| 75 | 3 | Silver | 20 | .05 | 1 | 30.88 | 1.63 | Gold | 25 | .07 | 2.28 | .65 |
| 81 | 3 | Silver | 20 | .05 | 2 | 31.35 | 1.65 | Gold | 25 | .07 | 2.31 | .66 |
| 82 | 3 | Silver | 20 | .05 | 3 | 43.7 | 2.3 | Gold | 25 | .07 | 3.22 | .92 |
| 92 | 3 | Silver | 20 | .05 | 3 | 41.81 | 2.2 | Gold | 25 | .07 | 3.08 | .88 |
| 95 | 3 | Silver | 20 | .05 | 3 | 47.03 | 2.48 | Gold | 25 | .07 | 3.47 | .99 |
| 96 | 3 | Silver | 20 | .05 | 3 | 58.44 | 3.08 | Gold | 25 | .07 | 4.31 | 1.23 |
| 9 | 4 | Gold | 25 | .07 | 1 | 10.23 | .77 | Platinum | 30 | .1 | 1.1 | .33 |
| 23 | 4 | Gold | 25 | .07 | 2 | 6.52 | .49 | Platinum | 30 | .1 | .7 | .21 |
| 28 | 4 | Gold | 25 | .07 | 2 | 6.51 | .49 | Platinum | 30 | .1 | .7 | .21 |
| 29 | 4 | Gold | 25 | .07 | 2 | 9.3 | .7 | Platinum | 30 | .1 | 1 | .3 |
| 31 | 4 | Gold | 25 | .07 | 3 | 66.5 | 5.01 | Platinum | 30 | .1 | 7.15 | 2.14 |
| 42 | 4 | Gold | 25 | .07 | 2 | 22.33 | 1.68 | Platinum | 30 | .1 | 2.4 | .72 |
| 46 | 4 | Gold | 25 | .07 | 2 | 33.48 | 2.52 | Platinum | 30 | .1 | 3.6 | 1.08 |
| 50 | 4 | Gold | 25 | .07 | 4 | 33.02 | 2.49 | Platinum | 30 | .1 | 3.55 | 1.06 |
| 52 | 4 | Gold | 25 | .07 | 2 | 22.32 | 1.68 | Platinum | 30 | .1 | 2.4 | .72 |
| 53 | 4 | Gold | 25 | .07 | 3 | 41.85 | 3.15 | Platinum | 30 | .1 | 4.5 | 1.35 |
| 72 | 4 | Gold | 25 | .07 | 1 | 30.69 | 2.31 | Platinum | 30 | .1 | 3.3 | .99 |
| 79 | 4 | Gold | 25 | .07 | 2 | 36.28 | 2.73 | Platinum | 30 | .1 | 3.9 | 1.17 |
| 86 | 4 | Gold | 25 | .07 | 2 | 19.53 | 1.47 | Platinum | 30 | .1 | 2.1 | .63 |
| 91 | 4 | Gold | 25 | .07 | 2 | 18.6 | 1.4 | Platinum | 30 | .1 | 2 | .6 |
| 93 | 4 | Gold | 25 | .07 | 1 | 6.51 | .49 | Platinum | 30 | .1 | .7 | .21 |
SELECT
member.member_id AS "Member ID",
current_member_type.member_type_id AS "Member Type ID",
current_member_type.description AS "Member Type",
current_member_type.annual_cost AS "Annual Member Cost",
current_member_type.discount AS "Member Discount",
COUNT(purchase.purchase_id) AS "Purchase Count",
SUM(purchase.purchase_total) AS "Amount Spent",
ROUND(
(
(SUM(purchase.purchase_total)
/ (1 - current_member_type.discount))
- SUM(purchase.purchase_total)
),
2
) AS "Amount Saved",
upgraded_member_type.description AS "Next-tier Member Type",
upgraded_member_type.annual_cost AS "Next-tier Annual Member Cost",
upgraded_member_type.discount AS "Next-tier Member Discount",
ROUND(
(
(SUM(purchase.purchase_total)
/ (1 - current_member_type.discount))
* upgraded_member_type.discount
),
2
) AS "Next-tier Amount Saved",
(
ROUND(
(
(SUM(purchase.purchase_total)
/ (1 - current_member_type.discount))
* upgraded_member_type.discount
),
2
) - ROUND(
(
(SUM(purchase.purchase_total)
/ (1 - current_member_type.discount))
- SUM(purchase.purchase_total)
),
2
)
) AS "Increased Savings Amount"
FROM
member
INNER JOIN
member_type current_member_type
ON member.member_type_id = current_member_type.member_type_id
INNER JOIN
member_type upgraded_member_type
ON current_member_type.member_type_id = upgraded_member_type.member_type_id - 1
INNER JOIN
purchase
ON member.member_id = purchase.member_id
GROUP BY
member.member_id,
current_member_type.member_type_id,
current_member_type.description,
current_member_type.annual_cost,
current_member_type.discount,
upgraded_member_type.description,
upgraded_member_type.annual_cost,
upgraded_member_type.discount
ORDER BY
current_member_type.member_type_id;