Oracle 12c Sql Hands-on Assignments Solutions Info

SELECT employee_id, first_name, last_name, salary FROM employees ORDER BY salary DESC FETCH FIRST 5 ROWS WITH TIES; Note: Without WITH TIES , it returns exactly 5 rows. With WITH TIES , it respects the salary rank.

SELECT department_id, last_name, salary, RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rank, DENSE_RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS dense_rank, ROW_NUMBER() OVER (PARTITION BY department_id ORDER BY salary DESC) AS row_num FROM employees WHERE department_id IS NOT NULL ORDER BY department_id, salary DESC; Find the salary difference between each employee and the next highest paid employee in the same department.

Write a query to page through employee data, showing rows 21 to 30 (Offset 20, Fetch 10).

CREATE TABLE emp_analytics AS SELECT department_id, last_name, salary, RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS salary_rank FROM employees; Oracle 12c SQL is robust, but the secret to passing hands-on assignments is understanding the subtle differences in windowing functions and the modern Top-N syntax . Always test your OFFSET/FETCH logic on a small subset first to verify sorting order. oracle 12c sql hands-on assignments solutions

SELECT email, SUBSTR(email, 1, 2) || '****@oracle.com' AS masked_email FROM employees; Problem 9: Rank employees within each department by salary. Show rank, dense rank, and row number.

SELECT first_name, last_name, hire_date, TRUNC(MONTHS_BETWEEN(SYSDATE, hire_date) / 12) AS years, TRUNC(MOD(MONTHS_BETWEEN(SYSDATE, hire_date), 12)) AS months, TRUNC(MONTHS_BETWEEN(SYSDATE, hire_date) / 12) || ' years, ' || TRUNC(MOD(MONTHS_BETWEEN(SYSDATE, hire_date), 12)) || ' months' AS tenure FROM employees; Mask email addresses for a report (Show first 2 letters, then ' ** ', then the domain 'oracle.com').

SELECT e.first_name, e.last_name, e.salary, e.department_id FROM employees e WHERE e.salary > (SELECT AVG(salary) FROM employees e2 WHERE e2.department_id = e.department_id) ORDER BY e.department_id, e.salary DESC; Problem 5: Fetch the top 5 highest paid employees, but show ties (i.e., if the 5th highest salary is shared by 3 people, show all of them). Write a query to page through employee data,

SELECT d.department_name, l.city, COUNT(e.employee_id) AS employee_count FROM departments d LEFT JOIN employees e ON d.department_id = e.department_id LEFT JOIN locations l ON d.location_id = l.location_id GROUP BY d.department_name, l.city ORDER BY employee_count DESC; List employees who earn more than the average salary of their own department.

SELECT employee_id, first_name, last_name, hire_date FROM employees WHERE EXTRACT(YEAR FROM hire_date) = 2012 ORDER BY hire_date ASC; You can also use the TO_CHAR method:

Oracle 12c, SQL, Assignments, PL/SQL, Window Functions SELECT email, SUBSTR(email, 1, 2) || '****@oracle

Oracle 12c SQL: Step-by-Step Solutions to Hands-On Assignments (Employee & Sales Schema)

SELECT employee_id, first_name, last_name, hire_date FROM employees ORDER BY hire_date OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY; Problem 7: Calculate the exact number of months and years each employee has worked as of today's date. Output format: "14 years, 3 months".