ORACLE QUERIES
1) Display empno and total sal fro each employee
Select empno, sal+comm from emp;
2) Display ename and annual sal for all employees
Select ename,12*sal+nvl(comm.,0) annualsal from emp;
3) Display the names of all employees working as clerks and drawing a salary more than 3000
Select ename from emp where job=’CLERK’ and sal>3000;
4) Display empno, names for employees who earn commission
Select empno,ename from emp where comm is not null and comm>=0;
5) Display the names of employees who are working as clerk, salesman or analyst and drawing a salary more than 3000
select ename from emp where job in (‘CLERK’,’SALESMAN’,ANALYST’) and sal>3000
6) Display the names of employees who are working in the company for past 5 years
select ename from emp where sysdate – hiredate > 5*365;
7) Display the list of employees who joined the company before 30th June 90 or after 31st Dec 90
select * from emp where hiredate < ’30-jun-1990’ and hiredate>’31-dec-90’;
8) Display current date
select sysdate from dual;
9) Display the list of users in your database ( using log table)
select * from dba_users;
10) Display the names of all tables from the current user
select * from tab;
11) Display the name of the current user
show user;
12) Display the names of employees working in department number 10 or 20 or 40 or employees working as clerks, salesman or analyst
select ename from emp where deptno in( 10,20,40) or job in ( ‘CLERK’,SALESMAN’,ANALYST’)
13) Display the names of employees whose name starts with alphabet S or ends with alphabet S or whose names have second alphabet S in their names
select ename from emp where ename like ‘S%’ or ename like ‘%S’ or ename like ‘_S%’;
14) Display the name of employees whose name is exactly five characters length.
Select ename from emp where length(ename)=5;
15) Display the names of employees who are not working as manager
select ename from emp where empno not in(select mgr from emp));
16) Display the employees who are not working as salesman or clerk
select * from emp where job not in (‘SALESMAN’,’CLERK’);
17) Display all rows from emp table. The system should wait after every screen full of information
set pause on;
18) Display the total number of employees working in the company
select count(*) from emp;
19) Display the total salary being paid to all employees
select sum(sal) + sum( nvl(comm, o)) from emp;
20) Display the max,min,avg salary from emp table
select max(sal), min(sal), avg(sal) from emp;
21) Display the max,min,avg salary being paid to department number 20 or job is manager
select max(sal),min(sal),avg(sal) from emp where deptno=20 or job=’MANAGER’;
22) Display the names of employess in order of salary (lowest to highest)
select enames from emp order by sal;
22) Display the names of employees in descending order of salary (highest to lowest)
select enames from emp order by sal desc;
23) Display the names of the employees along with their annual salary (sal*12). The name of the employees earning highest annual salary should appear first.
Select ename,12*(sal+nvl(comm,0)) annualsal from emp order by 12*(sal+nvl(comm,0)) desc;
24) Display deptno and total number of employees working with each group;
select deptno, count(empno ) from emp group by deptno;
25) Display job and total number of employees working with each group;
select job, count(empno ) from emp group by job;
26) Display deptno and total salary for each group
select deptno, sum(sal) from emp group by deptno;
27) Display deptno, and max,min,avg sal for each deptno
select deptno, max(sal), min(sal), avg(sal) from emp group by deptno;
28) Display the department numbers with more than three employees in each dept.
select deptno, count(*) from emp group by deptno having count(*)>3;
29) Display the name of employees who earns the highest salary
select name, sal from emp where sal=(select max(sal) from emp);
30) Display the name of salesman who earns a salary more than the highest salary of any clerk
select name,sal from emp where job=’SALESMAN’ and sal>(select max(sal) from emp where job=’CLERK’);
31) Display the names of clerks who earn salary more than that of james of that of sal lesser than that of scott
select names,sal from emp where job=’CLERKS’ and sal>(select sal from emp where ename=’JAMES’) and sal<(select sal from emp where ename=’SCOTT’);
32) Display the employees who earn highest salary in their respective departments.
Select * from emp e where sal=(select max(sal) from emp where deptno=e.deptno);
33) Display the employees who earn highest salary in their respective job groups
select * from emp e where sal in(select max(sal) from emp group by job having e.job=job);
34) Display the names who are working in accounting department.
Select ename from emp where deptno=(select deptno from dept where dname=’ACCOUNTING’;
35) Display the employee names who are working in Chicago
select ename from emp where deptno=(select deptno from dept where loc=’CHICAGO’);
36) Display the job grops having total salary greater than the maximum salary for managers
select job, sum(sal) from emp group by job having sum(sal)> (select max(sal) from emp where job=’MANAGER’);
37) Display the names of employees from department number 10 with salary greater than that of any employee working in other departments.
Select ename,sal,deptno from emp e where deptno=10 and sal> any(select sal from emp where e.deptno!=deptno);
38) Display the names inn upper case, lower case, proper case, length of name
select upper(ename),lower(ename),initcap(ename), length(ename) from emp;
39) Display the length of all the employee names
select sum(length(ename)) from emp;
40) Display the name of employee concatenated with empno
select ename || empno from emp;
41) use appropriate function and extract 3 characters starting from 2 char from the following string “ORACLE”
select substr(‘ORACLE’,2,3) from dual; è rac
42) Find the first occurance of character ‘a’ from the following string ‘computer maintenance corporation’
select instr(‘computer maintenance corporation’,’a’,1,1) from dual;
43) Replace every occurance of alphabet A with B in the string ALLEN’s
select replace(‘ALLENS’, ‘A’,’B’) from dual;
44) Display the information from emp table wherever job ‘manager’ is found it should be displayed as BOSS
select empno,ename, replace(job,’MANAGER’,BOSS’) job from emp;
45) Display your age in days
select round(sysdate – to_date(’15-aug—1947’)) from dual;
46) Display your age in months
select floor( months_between(sysdate,’15-aug-1947’)) from dual;
47) Display the current date as 15th august Friday nineteen forty seven
select to_char(sysdate, ‘ddth month day year ‘) from dual;
48) Display the following output for each row from emp table as ‘scott has joined the company on Wednesday 13 the august nineteen ninety’
select ename || ‘has joined the company on ‘ || to_char(hiredate, ‘day ddth month year’) from dual;
49) Find the date of nearest Saturday after current day
select next_day(sysdate, ‘Saturday’) from dual;
50) Display the current time
select to_char(sysdate,’hh:mm:ss’) from dual;
51) Display the date three months before the current date
select add_months(sysdate,-3) from dual;
52) Display the common jobs from department number 10 and 20
select job from emp where deptno=20 intersect select job from emp where deptno=10
53) Display the job which are unique to deptno 10
select job from emp where deptno=10 minus select job from emp where deptno!=10
54) Display the details of those who do not have any persons working under them
select empno from emp where empno not in (select mgr from where mgr is not null)
55) Display the employees whose manager names in JONES
select * from emp where mgr=(select empno from emp where ename=’JONES’);
56) Display those employees whose salary is more than 3000 after giving 20% increment
select * from emp where sal+sal*20/100> 3000;
57) Display the all the employee name with their department names
select ename,dname from emp e, dept d where e.deptno=d.deptno;
58) Display employee name, deptno,sal and comm. For those sal in between 2000 and 5000 while location is Chicago
select ename,deptno ,sal,comm. From emp deptno=(select deptno from dept where loc=’CHICAGO’) and sal between 2000 and 5000;
59) Display those employees whose salary greater than his manager salary.
Select * from emp e where sal>(select sal from emp where empno=e.mgr);
60) Display those employees who are not working under any manager
select * from emp where mgr is null or empno=mgr;
61) Update the salary of each employee by 10% increments that are not tligible for commission
update emp set sal=(sal+10/100) where comm. Is null
62) Delete those employees who joined the company before 31-dec-82 while there dept location is NEWYORK or CHICAGO
delete from emp where hiredate<’31-dec-1982’ and deptno in (select deptno from dept where loc in (‘NEW YORK’,’CHICAGO’));
63) Display those employees whose manager names is Jones, and also display there manager name
select e.empno,e.ename,m.ename MANAGER from emp e ,emp m where e.mgr=m.empno and e.ename=’JONES’;
64) Find out top 5 earners of company
select * from emp e where 5>(select count(*) from emp where sal>e.sal) order by sal desc;
65) Display those employees whose salary is equal to average of maximum and minimum
select * from emp where sal=(select (max(sal)+min(sal))/2 from emp);
66) Display dname where at least 3 are working and display only dname
select dname from dept where deptno in (select deptno from emp group by deptno having count(*)>3);
67) Delete those dept where no employees working
delete from emp where deptno not in(select deptno from dept);
68) Display those employees whose salary is ODD value
select * from emp where mod(sal,2)=1;
69) Display those employees who joined in the company in the month of DEC?
select * from emp where upper(to_char(hiredate,’mon’))=’DEC’;
70) Display those employees whose name contains ‘A’
select * from emp where instr(ename,’A’,1,1)>0;
71) Find out the employees who earned the highest sal in each job typed sort in descending sal order
select * from emp e where sal=(select max(sal) from emp where job=e.job)
72) Display the dept where there are no employees
select * from dept where deptno not in (select distinct(deptno) from emp);
73)Delete the rows of employees who are working in the company for more than 2 years
delete from emp where floor(sysdate-hiredate)>2*365;
74) 3rd maximum salary of employee
select distinct sal from emp a where 3=(select count(distinct sal) from emp b where a.sal<b.sal);
75) Deleting duplicate rows from a table
delete emp where rowid not in (select max(rowid) from emp group by empno);
76) display first 5 max salaries
select sal from (select abs(sal) sal from emp group by sal) where rownum<6;