Dbms print

479 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
479
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Dbms print

  1. 1. SQL> desc employeeName Null? Type----------------------------------------- ------------- ----------------------------EMPNO NOT NULL NUMBER(4)ENAME VARCHAR2(10)JOB NUMBER(3)MGR NUMBER(3)HIREDATE DATESAL NUMBER(8,2)COMM NUMBER(8,2)DEPTNO NOT NULL NUMBER(2)SQL> desc deptName Null? Type----------------------------------------- ------------- ----------------------------DEPTNO NOT NULL NUMBER(2)NAME VARCHAR2(10)LOCATION NOT NULL NUMBER(3)SQL> desc locationName Null? Type----------------------------------------- ------------- ----------------------------CODE NOT NULL NUMBER(3)NAME NOT NULL VARCHAR2(10)SQL> desc jobName Null? Type----------------------------------------- ------------- ----------------------------CODE NOT NULL NUMBER(3)NAME NOT NULL VARCHAR2(10)SQL> select * from dept; DEPTNO AME LOCATION---------- ----------------- ---------- 10 sales 1 20 research 3 30 accounting 4 40 admin 2
  2. 2. SQL> select * from job; CODE NAME---------- ---------- 1 trainee 2 president 3 analyst 4 clerckSQL> select * from location; CODE NAME---------- ---------- 1 chicago 2 kakinada 3 paris 4 hyderabadSQL> SELECT * FROM EMPLOYEE;EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- ---------- ---------- ---------- --------- --------------- --------- --------- ------------ 101 PAVAN 2 01-JAN-10 99000 10 102 KUMAR 3 03-JUN-09 20000 20 103 PRADEEP 3 04-APR-09 40000 30 104 SRINIVAS 3 18-MAR-08 50000 40 105 KEERTHI 4 19-JUN-08 60000 20 106 DENNIS 1 104 20-OCT-10 15000 500 40 107 VISHNU 1 104 19-JUN-09 18000 900 10 108 KARTHIK 1 104 28-FEB-09 12000 150 20 109 KALYANI 1 104 30-APR-10 13000 1000 30 110 ANUSHA 1 103 07-AUG-09 19000 1500 10 111 SIRISHA 1 103 08-SEP-09 13000 1200 20 112 VENKAT 1 103 09-OCT-09 15000 230 30 113 RATNAM 1 104 10-NOV-10 22000 340 4013 rows selected.
  3. 3. Exercise-1  Names of employees with a job of traineeSQL> select ename from employee where job=(select code from job where name=trainee);ENAME----------DENNISVISHNUKARTHIKKALYANIANUSHASIRISHAVENKATRATNAM8 rows selected.  List of distinct department idsSQL> select distinct deptno from employee; DEPTNO---------- 10 20 30 40  Names of employees sorted by their joining dateSQL> select ename from employee order by hiredate;ENAME----------SRINIVASKEERTHIKARTHIKPRADEEPKUMARVISHNUANUSHASIRISHAVENKATPAVANKALYANIDENNISRATNAM13 rows selected.
  4. 4.  Names of employees who is not a sales traineeSQL> select ename from employee where deptno not in(select deptno from dept wherename=sales) and job=(select code from job where name=trainee);ENAME----------DENNISKARTHIKKALYANISIRISHAVENKATRATNAM6 rows selected.  Names of employees who did not get any commissionSQL> select ename from employee where comm is null;ENAME----------PAVANKUMARPRADEEPSRINIVASKEERTHI  Names of employees who do not have a managerSQL> select ename from employee where mgr is null;ENAME----------PAVANKUMARPRADEEPSRINIVASKEERTHI  Names of employees in department id 20SQL> select ename from employee where deptno=20;ENAME----------KUMARKEERTHIKARTHIKSIRISHA
  5. 5.  Employees with a salary of over 20000SQL> select * from employee where sal>20000; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- 101 PAVAN 2 01-JAN-10 99000 10 103 PRADEEP 3 04-APR-09 40000 30 104 SRINIVAS 3 18-MAR-08 50000 40 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- 105 KEERTHI 4 19-JUN-08 60000 20 113 RATNAM 1 104 10-NOV-10 22000 340 40  Employees with a salary of over 40000, but less than 80000SQL> select * from employee where sal between 40000 and 80000; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- 103 PRADEEP 3 04-APR-09 40000 30 104 SRINIVAS 3 18-MAR-08 50000 40 105 KEERTHI 4 19-JUN-08 60000 20  Employees working under either Pradeep or SrinivasSQL> select * from employee where mgr in(select empno from employee whereename=PRADEEP or ename=SRINIVAS); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- 106 DENNIS 1 104 20-OCT-10 15000 50 40 107 VISHNU 1 104 19-JUN-09 18000 900 10 108 KARTHIK 1 104 28-FEB-09 12000 150 20 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- 109 KALYANI 1 104 30-APR-10 13000 1000 30 110 ANUSHA 1 103 07-AUG-09 19000 1500 10 111 SIRISHA 1 103 08-SEP-09 13000 1200 20 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- 112 VENKAT 1 103 09-OCT-09 15000 230 30 113 RATNAM 1 104 10-NOV-10 22000 340 40
  6. 6. 8 rows selected.  Employees who are not working under either Pradeep or SrinivasSQL> select * from employee where mgr not in(select empno from employee whereename=PRADEEP or ename=SRINIVAS) or mgr is null; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- 101 PAVAN 2 01-JAN-10 99000 10 102 KUMAR 3 03-JUN-09 20000 20 103 PRADEEP 3 04-APR-09 40000 30 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- 104 SRINIVAS 3 18-MAR-08 50000 40 105 KEERTHI 4 19-JUN-08 60000 20  Employees working under Pradeep and joined before himSQL> select * from employee where mgr=(select empno from employee whereename=PRADEEP) and hiredate<(select hiredate from employee where ename=PRADEEP);no rows selected  Employees working under Pradeep or Srinivas and joined before themSQL> select * from employee where (mgr=(select empno from employee whereename=PRADEEP)and hiredate<(select hiredate from employee where ename=PRADEEP))or(mgr=(select empno from employee where ename=SRINIVAS)and hiredate<(select hiredatefrom employee where ename=SRINIVAS));no rows selected  Employees with more commission than salarySQL> select * from employee where comm>sal;no rows selected
  7. 7. Exercise-II  Employees in ACCOUNTING departmentSQL> select * from employee where deptno=(select deptno from dept wherename=accounting); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- 103 PRADEEP 3 04-APR-09 40000 30 109 KALYANI 1 104 30-APR-10 13000 1000 30 112 VENKAT 1 103 09-OCT-09 15000 230 30  List of all ANALYSTS in Kakinada areaSQL> select * from employee where job=(select code from job where name=analyst) anddeptno=(select deptno from dept where location=(select code from location wherename=kakinada)); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- 104 SRINIVAS 3 18-MAR-08 50000 40  Employees in SALES departmentSQL> select * from employee where deptno=(select deptno from dept where name=sales); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- 101 PAVAN 2 01-JAN-10 99000 10 107 VISHNU 1 104 19-JUN-09 18000 900 10 110 ANUSHA 1 103 07-AUG-09 19000 1500 10  Find the job function of KeerthiSQL> select name from job where code=(select job from employee where ename=KEERTHI);NAME----------Clerk  All the locations having a SALES departmentSQL> select name from location where code in( select location from dept where name=sales);NAME----------Chicago
  8. 8.  The job ids of all the employees who are managers of other employeesSQL> SELECT JOB FROM EMPLOYEE WHERE EMPNO IN(SELECT MGR FROMEMPLOYEE); JOB---------- 3 3  All the ANALYSTS who are also managersSQL> SELECT * FROM EMPLOYEE WHERE EMPNO IN(SELECT MGR FROMEMPLOYEE) AND JOB=(SELECT CODE FROM JOBWHERE NAME=analyst); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- 103 PRADEEP 3 04-APR-09 40000 30 104 SRINIVAS 3 18-MAR-08 50000 40  All the employees reporting to the PRESIDENTSQL> select * from employee where mgr=(select code from job where name=president);no rows selected  All the ANALYSTS reporting to the PRESIDENTSQL> select * from employee where mgr=(select code from job where name=president) andjob=(select code from job where name=analyst);no rows selected  The job functions of all the employees who are managers of other employeesSQL> select name from job where code in(select job from employee where empno in(select mgrfrom employee));NAME----------analyst  Find departments(names and locations) with some employees earning less than 2000SQL> select * from dept where deptno in(select deptno from employee where sal<2000);no rows selected
  9. 9. Exercise-III • Employees earning more than DENNIS’s manager SQL> select * from employee where sal>(select sal from employee where empno=(select mgr from employee where ename=DENNIS)); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- 101 PAVAN 2 01-JAN-10 99000 10 105 KEERTHI 4 19-JUN-08 60000 20 • Employees working under DENNIS and earning more than DENNIS’s manager SQL> select * from employee where sal>(select sal from employee where empno=(select mgr from employee where ename=DENNIS))and mgr=(select empno from employee where ename=DENNIS);no rows selected • Employees in SALES department with the same job as anyone in the RESEARCH department SQL> select * from employee where job in(select job from employee where deptno=(select deptno from dept where name=sales)) and deptno=(select deptno from dept where name=research); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- 108 KARTHIK 1 104 28-FEB-09 12000 150 20 111 SIRISHA 1 103 08-SEP-09 13000 1200 20 • All employees in CHICAGO area SQL> select * from employee where deptno=(select deptno from dept where location=(select code from location where name=chicago)); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- 101 PAVAN 2 01-JAN-10 99000 10 107 VISHNU 1 104 19-JUN-09 18000 900 10 110 ANUSHA 1 103 07-AUG-09 19000 1500 10
  10. 10. • Find all the employees whose join date is the earliestSQL> select * from employee where hiredate=(select min(hiredate) from employee); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- 104 SRINIVAS 3 18-MAR-08 50000 40Exercise-IV • Find all the employees whose join date is the earliest in their respective departmentsSQL> select * from employee where hiredate in(select min(hiredate) from employee group bydeptno); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- 103 PRADEEP 3 04-APR-09 40000 30 104 SRINIVAS 3 18-MAR-08 50000 40 105 KEERTHI 4 19-JUN-08 60000 20 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO 107 VISHNU 1 104 19-JUN-09 18000 900 10 • Find all the employees whose salary is the maximum in their respective departmentsSQL> select * from employee where sal in(select max(sal) from employee group by deptno); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO 101 PAVAN 2 01-JAN-10 99000 10 103 PRADEEP 3 04-APR-09 40000 30 104 SRINIVAS 3 18-MAR-08 50000 40 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO 105 KEERTHI 4 19-JUN-08 60000 20 • Find all the customers whose sales person is not originally a salesperson Don’t have any salesperson details
  11. 11. Exercise-V• Find all the employees whose join date is the earliestSQL> select * from employee where hiredate=(select min(hiredate) from employee); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- 104 SRINIVAS 3 18-MAR-08 50000 40• Find the minimum salary in the ACCOUNTING departmentSQL> select min(sal) from employee where deptno=(select deptno from dept wherename=accounting); MIN(SAL)---------- 13000• Find the all the employees with the minimum salarySQL> select * from employee where sal=(select min(sal) from employee); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- 108 KARTHIK 1 104 28-FEB-09 12000 150 20• Find the maximum salary of all employeesSQL> select ename,sal+comm from employee;ENAME SAL+COMM---------- ----------PAVAN 99000KUMAR 20000PRADEEP 40000SRINIVAS 50000KEERTHI 60000DENNIS 15500VISHNU 18900KARTHIK 12150KALYANI 14000ANUSHA 20500SIRISHA 14200ENAME SAL+COMM---------- ----------VENKAT 15230RATNAM 22340
  12. 12. 13 rows selected.• Find the maximum salary of all employees from SALES departmentSQL> select ename,sal+comm from employee where deptno=(select deptno from dept wherename=sales);ENAME SAL+COMM---------- ----------PAVAN 99000VISHNU 18900ANUSHA 20500• Find maximum salary of all ANALYSTsSQL> select ename,sal+comm from employee where job=(select code from job wherename=analyst);ENAME SAL+COMM---------- ----------KUMAR 20000PRADEEP 40000SRINIVAS 50000• Find all the ANALYSTs with the maximum salary among ANALYSTsSQL> select * from employee where sal=(select max(sal) from employee where job=(select codefrom job where name=analyst)); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- 104 SRINIVAS 3 18-MAR-08 50000 0 40• Find total commissions paid to all employees togetherSQL> select sum(comm) from employee; SUM(COMM)---------- 5820• Find total salary of all CLERKsSQL> select sum(sal) from employee where job=(select code from job where name=clerck); SUM(SAL)---------- 60000
  13. 13. • Find the number of ANALYSTSSQL> select sum(sal) from employee where job=(select code from job where name=clerck); SUM(SAL)---------- 60000• Find out number of different job_id in the RESEARCH departmentSQL> select count(distinct job) from employee where deptno=(select deptno from dept wherename=research);COUNT(DISTINCT JOB)------------------ 3• Find employees earning more than average salary of all employeesSQL> select * from employee where sal>(select avg(sal) from employee); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- 101 PAVAN 2 01-JAN-10 99000 0 10 103 PRADEEP 3 04-APR-09 40000 0 30 104 SRINIVAS 3 18-MAR-08 50000 0 40 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- 105 KEERTHI 4 19-JUN-08 60000 0 20• Maximum salary of all employeesSQL> select max(sal) from employee; MAX(SAL)---------- 99000• Average salary in each department_idSQL> select avg(sal),deptno from employee group by deptno; AVG(SAL) DEPTNO---------- ----------45333.3333 10 26250 2022666.6667 30 29000 40
  14. 14. • No of employees with each job_idSQL> select count(*),job from employee group by job; COUNT(*) JOB---------- ---------- 8 1 1 2 3 3 1 4• No of employees where job_id is more than 3SQL> select count(*) from employee where job>3; COUNT(*)---------- 1• No of ANALYSTS in each departmentSQL> select count(*) from employee where job=(select code from job where name=analyst)group by deptno; COUNT(*)---------- 1 1 1

×