Oracle Database Fundamentals Exercise

1. Select all the information of employees in department 30 
 select * from emp where deptno=30;

2 List the number and name of the employee whose position is (MANAGER)
 
select ename,empno from emp where job='MANAGER'

3 Find out the bonus Employee with higher salary 

select ename from emp where comm>sal;

4 Find the sum of bonus and salary for each employee 

select ename,sal+ nvl(comm,0) total from emp;
select ename,nvl2(comm,sal+comm, sal) total from emp;

5 find out the managers in department 10 (MANAGER) and the common employees in department 20 (CLERK) 

select * from emp where (deptno=10 and job='MANAGER') or (deptno=20 and job ='CLERK');
 
6 Find out the employees in department 10 who are neither managers nor ordinary employees, and whose salary is greater than or equal to 2000

 select ename from emp where (deptno=10 and sal>2000)and job!='MANAGER' and job !='CLERK' ;
 select ename from emp where (deptno=10 and sal>2000)and job not in('MANAGER','CLERK') ;

7 find different jobs of employees with bonuses 

select distinct job from emp where comm>0 and comm is not null;

8 Find employees with no bonus or bonus less than 500 
select ename from emp where comm<500 or comm is null or comm=0;
 
9 Display employee names, and rank oldest employees at the top according to their years of service Select ename
,to_char(hiredate,'yyyy-mm-dd') from emp order by to_char(hiredate,'yyyy-mm-dd');

select ename,hiredate from emp order by hiredate

***************************************************************************************************************

--1 Find the employees hired on the third to last day of each month (eg: 2009-5-29)
 
select ename from emp where hiredate=last_day(hiredate)-2; --2

Find the employees hired 25 years ago 
select ename, to_char(hiredate,'yyyy-mm-dd') from emp hiredate<add_months(sysdate,-25*12);
select ename, to_char(hiredate,'yyyy-mm-dd') from emp where months_between(sysdate ,hiredate)>25*12;
select ename, to_char(hiredate,'yyyy-mm-dd') from emp where (to_char(sysdate,'yyyy')-to_char(hiredate,'yyyy')) >25; -- 3 Add Dear before all employee names, and capitalize the first letter of the name  select concat('Dear',initcap(ename)) from emp;



4 Find the employee whose name is 5 letters 
select ename from emp where length(ename)=5; -- ename like '_____';


5 Find employees without the letter R in their name 
select ename from emp where ename not like '%R%';


6 Display the first word of the names of all employees 
select ename,substr(ename,0,1) from emp;


7 Display all employees in descending order by name, if they are the same, sort by salary in ascending order 
select ename from emp order by ename desc , sal asc;


8 Assuming that a month is 30 days, find the daily salary of all employees, excluding decimals 
select ename,sal,round(sal/30,0) from emp; 


9 Find the employee hired in February 
select ename from emp where to_char(hiredate,'mm')='02';


10 List the number of days the employee joined the company (rounded up) 
select ename,round(sysdate-hiredate) from emp;


11 Use the case and decode functions to list the department where the employee is located,
   deptno=10 display 'department 10', 
   deptno=20 display 'department 20'    
   deptno=30 display 'department 30'    
   deptno=40 display 'department 40'  
  otherwise ' other department' 
select deptno,ename,sal,( case deptno when 10 then 'department 10' when 20 then 'department 20'
when 30 then 'department 30' when 40 then 'department 40' else 'other department' end) dept from emp;

select deptno,ename,sal,decode(deptno,10,'department 10',20,
 'department 20',30,'department 30','other departments') dept from emp;



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325608109&siteId=291194637