You are on page 1of 7

CREATE TABLE TRAINEE(TID NUMBER(3),TNAME VARCHAR2(20),TOPIC VARCHAR2(50),DOT

DATE,LOCATION_ID NUMBER(2));

INSERT INTO TRAINEE VALUES(1,'RICHA','ERP','28-oct-2019',11);


INSERT INTO TRAINEE VALUES(2,'MAHESH','ERP','28-oct-2019',11);
INSERT INTO TRAINEE VALUES(3,'RAHUL','SQL','22-oct-2019',12);
INSERT INTO TRAINEE VALUES(4,'sAMEER','EBS','23-oct-2019',13);
INSERT INTO TRAINEE VALUES(5,'MIRA','DOTNET','23-oct-2019',13);

desc all_tab_columns;

CREATE TABLE TRAINEE


(T_REQ_ID NUMBER(5) PRIMARY KEY,
EMPLOYEE_ID REFERENCES EMPLOYEES(EMPLOYEE_ID),
TOPIC VARCHAR2(30) NOT NULL,
DOT DATE,
LOCATION_ID NUMBER(2),
TRAINING_STATUS CHAR(1)
);

INSERT INTO TRAINEE VALUES(1,102,'SQL',SYSDATE,11,'Y');


INSERT INTO TRAINEE VALUES(2,106,'ERP',SYSDATE+2,11,'N');
INSERT INTO TRAINEE VALUES(3,115,'SQL',SYSDATE-2,13,'Y');
INSERT INTO TRAINEE VALUES(4,139,'PDB',SYSDATE+2,11,'Y');
INSERT INTO TRAINEE VALUES(5,155,'PLSQL',SYSDATE+1,11,'N');
INSERT INTO TRAINEE VALUES(6,119,'ERP',SYSDATE11,10,'Y');

Assignments
_______________

Customer wants the following reports for employees.


Experience report that lists the name, Joining date and total experience of all
employees
Department wise total salary report with department_id and total salary for every
department
Create a job-wise employee count report
Find out the hiredate of the seniormost and junior most employees.
Find out how many reportees are there for every manager. (A person having a
manager_id 100 is said to be 100�s reportee.)

SELECT CONCAT(FIRST_NAME,LAST_NAME)EMPLOYEE_NAME,HIRE_DATE,TO_CHAR(SYSDATE,'YYYY')-
TO_CHAR(HIRE_DATE,'YYYY') TOTAL_EXP FROM EMPLOYEES ORDER BY TOTAL_EXP;

SELECT DEPARTMENT_ID,SUM(SALARY) TOTAL_SALARY FROM EMPLOYEES GROUP BY DEPARTMENT_ID


ORDER BY DEPARTMENT_ID;

SELECT JOB_ID,COUNT(EMPLOYEE_ID) TOTAL_EMPLOYEES FROM EMPLOYEES GROUP BY JOB_ID;

SELECT MAX(HIRE_DATE)JUNIORMOST,MIN(HIRE_DATE) SENIORMOST FROM EMPLOYEES;


SELECT MANAGER_ID,COUNT(EMPLOYEE_ID) REPORTEES FROM EMPLOYEES GROUP BY MANAGER_ID
ORDER BY REPORTEES DESC;

joins
_______

SELECT EMP.DEPARTMENT_ID,DEPT.DEPARTMENT_NAME,SUM(SALARY) TOTAL_SALARY


FROM EMPLOYEES EMP,DEPARTMENTS DEPT
WHERE EMP.DEPARTMENT_ID = DEPT.DEPARTMENT_ID
GROUP BY EMP.DEPARTMENT_ID,DEPT.DEPARTMENT_NAME
ORDER BY EMP.DEPARTMENT_ID;

SELECT EMPLOYEE_ID,FIRST_NAME || ' ' || LAST_NAME


EMPLOYEE_NAME,EMP.JOB_ID,JOB_TITLE
FROM EMPLOYEES EMP,JOBS JOB
WHERE EMP.JOB_ID=JOB.JOB_ID;

SELECT
employee_id,
first_name
|| ' '
|| last_name employee_name,
emp.job_id,
job_title,
dept.Department_id,
department_name
FROM
employees emp,
jobs job,
departments dept
WHERE
emp.job_id = job.job_id
AND emp.department_id=dept.department_id;

SELECT EMP.EMPLOYEE_ID,CONCAT(EMP.FIRST_NAME,EMP.LAST_NAME)
EMPLOYEE_NAME,DEPT.DEPARTMENT_ID,DEPT.DEPARTMENT_NAME
FROM EMPLOYEES EMP LEFT OUTER JOIN DEPARTMENTS DEPT ON
EMP.DEPARTMENT_ID=DEPT.DEPARTMENT_ID;

SELECT DEPT.DEPARTMENT_ID,DEPARTMENT_NAME,EMPLOYEE_ID,FIRST_NAME
FROM EMPLOYEES EMP RIGHT OUTER JOIN DEPARTMENTS DEPT ON
EMP.DEPARTMENT_ID=DEPT.DEPARTMENT_ID;

SELECT EMPLOYEE_ID,FIRST_NAME,DEPARTMENT_NAME,JOB_TITLE
FROM EMPLOYEES EMP LEFT OUTER JOIN DEPARTMENTS DEPT ON
EMP.DEPARTMENT_ID=DEPT.DEPARTMENT_ID LEFT OUTER JOIN JOBS JB ON
EMP.JOB_ID=JB.JOB_ID;

SELECT EMPLOYEE_ID,FIRST_NAME ||' ' || LAST_NAME


EMPLOYEE_NAME,DEPARTMENT_NAME,SALARY
FROM EMPLOYEES EMP LEFT OUTER JOIN DEPARTMENTS DEPT ON
EMP.DEPARTMENT_ID=DEPT.DEPARTMENT_ID
WHERE SALARY>5000;

SELECT DEPT.DEPARTMENT_ID,DEPARTMENT_NAME,COUNT(EMPLOYEE_ID),SUM(EMP.SALARY)
TOTAL_SALARY
FROM EMPLOYEES EMP LEFT OUTER JOIN DEPARTMENTS DEPT
ON DEPT.DEPARTMENT_ID=EMP.DEPARTMENT_ID
WHERE SALARY>5000
GROUP BY DEPT.DEPARTMENT_ID,DEPT.DEPARTMENT_NAME
ORDER BY 1;

SELECT DEPT.DEPARTMENT_ID,DEPARTMENT_NAME,COUNT(EMPLOYEE_ID),SUM(EMP.SALARY)
TOTAL_SALARY
FROM EMPLOYEES EMP LEFT OUTER JOIN DEPARTMENTS DEPT
ON DEPT.DEPARTMENT_ID=EMP.DEPARTMENT_ID
WHERE SALARY>5000
GROUP BY DEPT.DEPARTMENT_ID,DEPT.DEPARTMENT_NAME
HAVING TOTAL_SALARY>10000
ORDER BY 1;

sub-query
_____________

SELECT FIRST_NAME,SALARY FROM EMPLOYEES WHERE SALARY>(SELECT MIN(SALARY) FROM


EMPLOYEES WHERE JOB_ID LIKE '%CLERK%')ORDER BY SALARY;

select first_name from employees where department_id=(select department_id from


departments where location_id =(select location_id from locations where
city='London'));

SELECT
first_name
FROM
employees
WHERE
department_id = (
SELECT
department_id
FROM
departments d,
locations l
WHERE
d.location_id = l.location_id
AND l.city = 'London'
);

multirow subquery(operators IN,ALL,ANY)


___________________________

Q:select all the employees whose location_id=1700?


select first_name from employees where department_id IN (select d.department_id
from departments d,locations l where l.location_id=1700);

select first_name from employees where department_id =ALL(select dept_id from


departments where location_id=1700);
CORELATED SUBQUERY (operator EXISTS returns true , NOT EXISTS returns False)
____________________________________________________________________________

select department_name from departments dept where EXISTS(select department_id from


employees emp where emp.department_id=dept.department_id);

select department_name from departments dept where NOT EXISTS(select department_id


from employees emp where emp.department_id=dept.department_id);

substirng
_______________

select department_name,substr(department_name,1,5) department_code from


departments;

select department_name,(length(substr(department_name,1,5))) department_code


,decode((length(substr(department_name,1,5))),'1','bad','2','poor','3','moderate','
4','good','5','very good') rating from departments;

select department_name,(length(substr(department_name,1,5))) department_code from


departments where length(substr(department_name,1,5))!=5;
select department_name,(length(substr(department_name,1,5))) department_code from
departments where length(substr(department_name,1,5))=5;

using trunc for current date comparison


____________________________________________

update trainee set dot=sysdate where trunc(dot)=trunc(to_date('30-OCT-2019'));


update trainee set dot=sysdate+1 where dot>to_date('31-oct-2019');

View
__________

create view employee_details as select emp.first_name


,dept.department_name,jb.job_title,emp.salary from employees emp,departments
dept,jobs jb where emp.department_id=dept.department_id and emp.job_id=jb.job_id;

create view dept_sal as select dept.department_name,min(emp.salary)


minimum_sal,max(emp.salary) maximum_sal,ROUND(avg(emp.salary),2) avg_sal from
employees emp,departments dept,jobs jb where emp.department_id=dept.department_id
and emp.job_id=jb.job_id group by department_name;

joining all tables of HR schema


_______________________________________

select first_name,department_name,job_title,city,country_name,region_name from


employees emp,departments dept,jobs jb,locations loc,countries con,regions reg
where emp.department_id=dept.department_id and emp.job_id=jb.job_id and
dept.location_id=loc.location_id and con.country_id=loc.country_id and
reg.region_id=con.region_id;

select count(t_req_id) from trainee where dot like '%Oct%';


select count(t_req_id) from trainee where substr(to_char(dot,'dd-mon-yy'),4,3)
like'%Oct%';(within quotes data is case sensitive)

select count(t_req_id) from trainee where substr(to_char(dot,'dd-mon-yy'),4,3)

select count(t_req_id) from trainee where upper(substr(to_char(dot,'dd-mon-


yy'),4,3)) like'%Oct%';

select count(t_req_id) from trainee where upper(substr(to_char(dot,'dd-mon-


yy'),4,3)) like upper('%Oct%');

select empno from emp where dept=&deptno;(&-substitutional operator)

select empno from emp where deptno=to_number('&deptno');

select to_date('10-nov-19','dd-mon-yy') from dual;

select to_date('10-nov-19')from dual;(default)

select to_date('10-11-19')from dual; (this will give error saying invalid month)

select to_date('10-11-19','dd-mm-yy') from dual;

select to_date('2019,11,20','yy-mm-dd') from dual;

select to_date('2019,30,11','yy-dd-mm') from dual;

select to_date('2019,30,11','yy/dd/mm') from dual;


_______________________________________________

TRANSLATE

translate(string,from string,to string);

translate('222tech','2ec','3it');--------333tith

REPLACE

select ename,replace(ename,'i','y')from emp;

nullif
_____________
nullif(expr1,expr2). when expr1=expr2 then null else return expr1 end.

select ename,sal,nullif(sal,3000)from emp;

coalesce
_____________
can take 3 or more expressions and returns the first non null expression.

select ename,commission.sal,coalesce(0.5*comm,sal+1000,1000) from emp;

soundex
_______________

select ename from emp where soundex(ename)=soundex('smyth');--------smith


DATE AND TIME
_____________________

select to_char(sysdate,'dd-mm-yyyy hh24:mi:ss') from dual;

select to_char(dot,'dd-mon-yy hh24:mi:ss'),to_char(sysdate,'dd-mon-yy hh24:mi:ss')


from trainee;

when we compare date with sysdate it will not be equal bcoz along with date there
is time also, so we need to use trunc in order to get date portion.

select to_char(trunc(dot),'dd-mon-yy') from trainee;

select to_char(trunc(dot),'dd-mon-yy hh24:mi:ss'),to_char(trunc(sysdate),'dd-mon-yy


hh24:mi:ss') from trainee;

case
__________

select topic,
(case
when dot<sysdate then 'completed'
when dot=trunc(sysdate) then 'in progress'
else 'yet to complete' end) training_Remarks
from trainee;

***with trunc***

select topic,
(case
when trunc(dot)<trunc(sysdate) then 'completed'
when trunc(dot)=trunc(sysdate) then 'in progress'
when trunc(dot)>trunc(sysdate) then 'yet to complete' end) training_Remarks
from trainee;

___________________________________________________________________________________
________
select pers_email,substr(pers_email,1,
(instr(pers_email,'@')))||'*********************' email from staff;

select pers_email,
(case
when substr(pers_email,(instr(pers_email,'@')+1))='kmail.com'
then substr(pers_email,1,(instr(pers_email,'@')))||'hotmail.com'
else substr(pers_email,1,(instr(pers_email,'@')))||'yahoo.com' end)email
from staff;

select pers_email,decode(substr(pers_email,(instr(pers_email,'@')
+1)),'kmail.com',substr(pers_email,1,
(instr(pers_email,'@')))||'hotmail.com','rediffmail.com',substr(pers_email,1,
(instr(pers_email,'@')))||'yahoo.com','yahoo.com',substr(pers_email,1,
(instr(pers_email,'@')))||'yahoo.com','hotmail.com',substr(pers_email,1,
(instr(pers_email,'@')))||'yahoo.com')email from staff;

You might also like