You are on page 1of 4

Section - 1 Lesson - 3

SELECT last_name, hire_date + 60


FROM employees;
SELECT last_name, (SYSDATE
FROM employees;
SELECT employee_id, (end_date
FROM job_history;

hire_date)/7
start_date)/365 AS Tenure in last job

SELECT employee_id, hire_date,


ROUND(MONTHS_BETWEEN(SYSDATE, hire_date)) AS TENURE,
ADD_MONTHS (hire_date, 6) AS REVIEW,
NEXT_DAY(hire_date, 'FRIDAY'),
LAST_DAY(hire_date)
FROM employees
WHERE MONTHS_BETWEEN (SYSDATE, hire_date) > 36;
select hire_date,round(hire_date,'month'),round(hire_date,'year') from employees
;
--------------------------------------------------------------------------------------Section - 2 Lesson - 1
SELECT hire_date,TO_CHAR (hire_date, 'fmMonth dd, YYYY'),TO_CHAR (hire_date, 'Mo
nth dd, YYYY') FROM employees;
SELECT TO_CHAR (hire_date, 'Year DDD D DAY') FROM employees;
SELECT TO_CHAR(salary, '$99,999') COST
FROM employees;
select to_char(hire_date,'dd-mon-yyyy') from employees
where hire_date <to_date('01-jan-90','dd-mon-yy');
Section - 2 Lesson - 2
SELECT salary,commission_pct, nvl(commission_pct,0),NVL2(commission_pct, salary
+ (salary * commission_pct), salary) AS income
from employees
SELECT first_name, LENGTH(first_name) "Length FN",
last_name, LENGTH(last_name) "Length LN",
NULLIF(LENGTH(first_name),
LENGTH(last_name)) AS "Compare Them"
FROM employees;
SELECT last_name, COALESCE(commission_pct, salary, 10) comm
FROM employees
ORDER BY commission_pct;
------------------------------------------------------------------------------------Section - 3 Lesson - 4
select * from (select rownum rn, tableName.* from tableName) where mod(rn,2) = 0
;

SELECT worker.last_name || ' works for ' || manager.last_name FROM employees wor
ker JOIN employees manager ON worker.manager_id = manager.employee_id;
Hierarchy Example:SELECT employee_id, last_name, job_id, manager_id
FROM employees
START WITH employee_id = 100
CONNECT BY PRIOR employee_id = manager_id
SELECT last_name || ' reports to ' || prior last_name as "Walk Top Down"
FROM employees
START WITH last_name = 'King'
CONNECT BY PRIOR employee_id = manager_id
SELECT LEVEL, last_name || ' reports to ' || PRIOR last_name as "Walk Top Down"
FROM employees
START WITH last_name = 'King'
CONNECT BY PRIOR employee_id = manager_id
SELECT LPAD(last_name, LENGTH(last_name)+(LEVEL*2)-2,'_') AS ORG_CHART
FROM employees
START WITH last_name = 'King'
CONNECT BY PRIOR employee_id = manager_id
SELECT last_name
FROM employees
WHERE last_name != 'Higgins
START WITH last_name = Kochhar
CONNECT BY PRIOR employee_id = manager_id
SELECT last_name
FROM employees
START WITH last_name = 'Kochhar'
CONNECT BY PRIOR employee_id = manager_id
AND last_name != 'Higgins'
--------------------------------------------------------------------------------------Section - 2 Lesson - 5
SELECT department_id, job_id, SUM(salary)
FROM employees
WHERE department_id < 50
GROUP BY ROLLUP (department_id, job_id)
Cube Example:SELECT department_id, job_id, SUM(salary)
FROM employees
WHERE department_id < 50
GROUP BY CUBE (department_id, job_id)
Grouping Set Example
SELECT department_id, job_id, manager_id, SUM(salary)
FROM employees
WHERE department_id < 50
GROUP BY GROUPING SETS
((job_id, manager_id), (department_id, job_id), (department_id, manager_id));
SELECT department_id, job_id, SUM(salary),
GROUPING(department_id) Dept_sub_total,
DECODE(GROUPING(department_id),
1,'Dept Aggregate row', department_id) AS DT,

GROUPING(job_id) job_sub_total,
DECODE(GROUPING(job_id),
1,'JobID Aggregate row',job_id) AS JI
FROM employees
WHERE department_id < 50
GROUP BY CUBE (department_id, job_id);
Select count(*) , department_id , Department_name from employees join department
s
using (department_id)
group by department_id, department_name
having count(*) = (Select max(count(*)) from employees group by department_id)
--------------------------------------------------------------------------Section - 6 Lesson - 4
SELECT o.first_name,
o.last_name,
o.salary
FROM employees o
WHERE o.salary >
(SELECT AVG(i.salary)
FROM employees i
WHERE i.department_id =
o.department_id);
WITH dept_costs AS (
SELECT d.department_name, SUM(e.salary) AS dept_total
FROM employees e JOIN departments d
ON e.department_id = d.department_id
GROUP BY d.department_name),
avg_cost AS (
SELECT SUM(dept_total)/COUNT(*) AS dept_avg
FROM dept_costs)
SELECT *
FROM dept_costs
WHERE dept_total >
(SELECT dept_avg
FROM avg_cost)
ORDER BY department_name;

Related Interests