Oracle
Oracle
create table emp (empno number(4) not null, ename varchar2(10), job varchar2(9), mgr number(4), hiredate date, sal
number(7,2), comm number(7,2), deptno number(2));
alter table emp add constraint fk_emp_01 foreign key(deptno) references dept;
insert into emp values (7369, ‘smith’, ‘clerk’, 7902, to_date(’17-dec-1980′, ‘dd-mon-yyyy’), 800, null, 20);
insert into emp values (7499, ‘allen’, ‘salesman’, 7698, to_date(’20-feb-1981′, ‘dd-mon-yyyy’), 1600, 300, 30);
insert into emp values (7521, ‘ward’, ‘salesman’, 7698, to_date(’22-feb-1981′, ‘dd-mon-yyyy’), 1250, 500, 30);
insert into emp values (7566, ‘jones’, ‘manager’, 7839, to_date(‘2-apr-1981’, ‘dd-mon-yyyy’), 2975, null, 20);
insert into emp values (7654, ‘martin’, ‘salesman’, 7698,to_date(’28-sep-1981′, ‘dd-mon-yyyy’), 1250, 1400, 30);
insert into emp values (7698, ‘blake’, ‘manager’, 7839,to_date(‘1-may-1981’, ‘dd-mon-yyyy’), 2850, null, 30);
insert into emp values (7782, ‘clark’, ‘manager’, 7839,to_date(‘9-jun-1981’, ‘dd-mon-yyyy’), 2450, null, 10);
insert into emp values (7788, ‘scott’, ‘analyst’, 7566,to_date(’09-dec-1982′, ‘dd-mon-yyyy’), 3000, null, 20);
insert into emp values (7839, ‘king’, ‘president’, null,to_date(’17-nov-1981′, ‘dd-mon-yyyy’), 5000, null, 10);
insert into emp values (7844, ‘turner’, ‘salesman’, 7698,to_date(‘8-sep-1981’, ‘dd-mon-yyyy’), 1500, 0, 30);
insert into emp values (7876, ‘adams’, ‘clerk’, 7788,to_date(’12-jan-1983′, ‘dd-mon-yyyy’), 1100, null, 20);
insert into emp values (7900, ‘james’, ‘clerk’, 7698,to_date(‘3-dec-1981’, ‘dd-mon-yyyy’), 950, null, 30);
insert into emp values (7902, ‘ford’, ‘analyst’, 7566,to_date(‘3-dec-1981’, ‘dd-mon-yyyy’), 3000, null, 20);
insert into emp values (7934, ‘miller’, ‘clerk’, 7782,to_date(’23-jan-1982′, ‘dd-mon-yyyy’), 1300, null, 10);
create table salgrade( grade number(2), losal number, hisal number);
I will post the rest of the queries across the next few days. The difficulty level will slowly increase as we move down the list
. I prefer using aliases as I find them to be easy to edit when required [ if we will have to use the same query in future and
modify it for joins etc]. There might be quite a few ways to attain a particular task. I just listed the solutions that I reached
at. If anyone finds a better way to do it, then feel free to point it out.
7. Display the names of all employees who are working in department number 10
SQL> select e.ename from emp e where e.deptno=10;
8. Display the names of all employees working as clerks and drawing a salary more than 3000
SQL> select e.ename from emp e where e.job=’CLERK’ and e.sal>3000;
9. Display employee number and names for employees who earn commission
SQL> select e.empno, e.ename from emp e where e.comm is not null;
10. Display names of employees who do not earn any commission
SQL> select e.empno, e.ename from emp e where e.comm is null or e.comm=0;
11. Display the names of employees who are working as clerk , salesman or analyst and drawing a salary more than 3000
SQL> select e.ename from emp e where (e.job=’CLERK’ or e.job=’ANALYST’ or e.job=’SALESMAN’) and e.sal>3000;
or
SQL> select e.ename from emp e where e.job in (‘CLERK’ ,’ANALYST’,’SALESMAN’) and e.sal>3000;
//Prefer the second one as it is easier to insert new values if required
12. Display the names of employees who are working in the company for the past 5 years
SQL> select ename from emp e where sysdate – e.hiredate>5*365;
13. Display the list of employees who have joined the company before 30 th june 90 or after 31 st dec 90
SQL> select * from emp e where e.hiredate between ’30-jun-1990′ and ’31-dec-1990′;
15. Display the list of users in your database (using log table)
SQL> select * from all_users;
or
SQL> select * from dba_users;
16. Display the names of all tables from the current user
SQL> select * from tab;
18. Display the names of employees working in department number 10 or 20 or 40 or em employees working as clerks ,
salesman or analyst
SQL> select e.ename from emp e where e.job in (‘CLERK’ ,’ANALYST’,’SALESMAN’) or e.deptno in (10,20,40);
19. Display the names of employees whose name starts with alphabet S
SQL> select e.ename from emp e where e.ename like ‘S%’ or e.ename like ‘s%’;
20. Display employee name from employees whose name ends with alphabet S
SQL> select e.ename from emp e where e.ename like ‘%S’ or e.ename like ‘%s’;
21. Display the names of employees whose names have second alphabet A in their names
REPORT THIS AD
SQL> select e.ename from emp e where e.ename like ‘_a%’ or e.ename like ‘_A%’;
22.Display the names of employees whose name is exactly five characters in length
SQL> select e.ename from emp e where length(e.ename)=5;
25.Display all rows from emp table. The system should wait after every screen full of information
SQL> set pause on;
pause press return;
select * from emp;
//The first statement is going to turn the pause feature on. Once a page size is reached in sql*plus [ this can be run in
sql*plus or command line alone I think], it will pause and wait for user input.
//The next statement is going to print a message saying “press return” whenever it is paused to continue
//The emp records are printed.
36.Display the names of employees in order of salary i.e. the name of the employee earning highest should appear first
SQL>select e.ename from emp e order by e.sal desc;
37.Display the names of employees in ascending order of salary lowest salary shoud appear first
SQL>select e.ename from emp e order by e.sal;
40) Display the name of employees along with their annual salary(sal*12). the name of the employee earning highest
annual salary should appear first?
SQL>select e.ename,e.sal*12 as annualsal from emp e order by annualsal desc;
41) Display name,salary,Hra,pf,da,TotalSalary for each employee. The out put should be in the order of total salary ,hra
15% of salary ,DA 10% of salary .pf 5% salary Total Salary will be (salary+hra+da)-pf?
SQL> select e.ename,e.sal, 0.15*sal as hra,0.10*sal as da,0.05*sal as pf,(e.sal + 0.15*sal + 0.10*sal – 0.05*sal) as totalsal
from emp e;
42) Display Department numbers and total number of employees working in each Department?
SQL>select e.deptno, count(e.empno) from emp e group by e.deptno;
43) Display the various jobs and total number of employees working in each job group?
SQL>select e.job, count(e.empno) from emp e group by e.job;
47)Display each job along with min of salary being paid in each job group?
SQL>select e.job, min(e.sal) from emp e group by e.job;
48) Display the department Number with more than three employees in each department?
SQL>select e.deptno from emp e group by e.deptno having count(empno) >3;
49) Display various jobs along with total salary for each of the job where total salary is greater than 40000?
SQL>select e.job, sum(e.sal) as totalsal from emp e group by e.job having sum(e.sal)>4000;
50) Display the various jobs along with total number of employees in each job.The output should contain only those jobs
with more than three employees?
SQL> select e.job, count(empno) as employees from emp e group by e.job having count(empno) >3;
51) Display the name of employees who earn Highest Salary?
SQL>SELECT E.ENAME FROM EMP E WHERE E.SAL=(SELECT MAX(E.SAL) FROM EMP E);
52) Display the employee Number and name for employee working as clerk and earning highest salary among the clerks?
SQL> SELECT E.EMPNO,E.ENAME FROM EMP E WHERE E.JOB=’CLERK’ AND E.SAL=(SELECT MAX(E.SAL) FROM EMP E WHERE
E.JOB=’CLERK’)
53) Display the names of salesman who earns a salary more than the Highest Salary of the clerk?
SQL>SELECT E.ENAME FROM EMP E WHERE E.JOB=’SALESMAN’ AND E.SAL>(SELECT MAX(E.SAL) FROM EMP E WHERE
E.JOB=’CLERK’)
54) Display the names of clerks who earn a salary more than the lowest Salary of any salesman?
SQL>SELECT E.ENAME FROM EMP E WHERE E.JOB=’CLERK’ AND E.SAL>(SELECT MIN(E.SAL) FROM EMP E WHERE
E.JOB=’SALESMAN’)
55) Display the names of employees who earn a salary more than that of jones or greater than that of scott?
SQL> SELECT E.ENAME FROM EMP E WHERE E.SAL>(SELECT E.SAL FROM EMP E WHERE E.ENAME=’JONES’) AND
E.SAL>(SELECT E.SAL FROM EMP E WHERE E.ENAME=’SCOTT’)
OR
SELECT E.ENAME FROM EMP E WHERE E.SAL>(SELECT MAX(E.SAL) FROM EMP E WHERE E.ENAME IN (‘JONES’,’SCOTT’));
//This is done here as the salary needs to be greater than both Scott & Jones. If it is greater than max, then it is obviously
greater individually. If the condition is changed like less than one and greater than the other etc, then individual queries
need to be written
56) Display the names of employees who earn Highest salary in their respective departments?
SQL>SELECT E.ENAME, E.DEPTNO FROM EMP E WHERE SAL IN (SELECT MAX(SAL) FROM EMP E GROUP BY DEPTNO)
57) Display the names of employees who earn Highest salaries in their respective job Groups?
SQL>SELECT E.ENAME, E.JOB FROM EMP E WHERE SAL IN (SELECT MAX(SAL) FROM EMP E GROUP BY JOB)
60) Display the job groups having Total Salary greater than the maximum salary for Managers?
SQL> SELECT E.JOB FROM EMP E GROUP BY E.JOB HAVING SUM(E.SAL)>(SELECT MAX(E.SAL) FROM EMP E WHERE
E.JOB=’MANAGER’);
61) Display the names of employees from department number 10 with salary greater than that of ANY employee working
in other departments?
SQL>SELECT E.ENAME FROM EMP E WHERE E.DEPTNO=10 AND E.SAL > ANY (SELECT E2.SAL FROM EMP E2 WHERE
E2.DEPTNO!=E.DEPTNO);
62) Display the names of employees from department number 10 with salary greater than of ALL employee working in
other departments ?
SQL> SELECT E.ENAME FROM EMP E WHERE E.DEPTNO=10 AND E.SAL > ALL (SELECT E2.SAL FROM EMP E2 WHERE
E2.DEPTNO!=E.DEPTNO);
69) Use appropriate function and extract 3 characters starting from 2 characters from the following string ‘Oracle’ i.e., the
output should be ac?
SQL> SELECT SUBSTR(‘ORACLE’,3,2) FROM DUAL;
70) Find the first occurrence of character ‘a’ from the following string Computer Maintenance Corporation?
SQL> SELECT INSTR(‘Computer Maintenance Corporation’,’a’,1,1) FROM DUAL;
71) Replace every occurrence of alphabet A with B in the string Aliens (Use Translate function)?
SQL> SELECT REPLACE(‘Aliens’,’A’,’B’) FROM DUAL;
OR
SELECT TRANSLATE(‘Aliens’, ‘A’, ‘B’) FROM DUAL;
72) Display the information from the employee table wherever job Manager is found it should be displayed as Boss?
SQL> SELECT E.EMPNO, E.ENAME, REPLACE (JOB,’MANAGER’,’BOSS’) AS JOB FROM EMP E
73) Display empno, ename, deptno from emp table. Instead of display department numbers display the related
department name (Use decode function)?
SQL> SELECT E.EMPNO,E.ENAME, ,D.DNAME AS DEPTNO FROM EMP E, DEPT D WHERE E.DEPTNO = D.DEPTNO;
OR
SELECT E.EMPNO,E.ENAME,DECODE(E.DEPTNO,10,’ACCOUNTING’,20,’RESEARCH’,30,’SALES’,40,’OPERATIONS’) FROM EMP
E;
78)Display all the records of emp in the following format “Scott has joined the company on 13th August ninteen ninety?”
SQL> select ename||’ has joined the company on ‘||to_char(hiredate,’day ddth month year’) as doj from emp;
81) Display the date three months before the Current date?
SQL> SELECT ADD_MONTHS(SYSDATE,-3) FROM DUAL
82) Display the common jobs from department number 10 and 20?
SQL> SELECT JOB FROM EMP WHERE DEPTNO=10 AND JOB IN (SELECT JOB FROM EMP WHERE DEPTNO=20);
OR
SELECT JOB FROM EMP WHERE DEPTNO=10 INTERSECT SELECT JOB FROM EMP WHERE DEPTNO=20;
83) Display the jobs found in department 10 and 20. Eliminate duplicate jobs [Eliminate the jobs that are common in
10&20]?
SQL> (SELECT JOB FROM EMP WHERE DEPTNO=10 UNION SELECT JOB FROM EMP WHERE DEPTNO=20) MINUS (SELECT JOB
FROM EMP WHERE DEPTNO=10 INTERSECT SELECT JOB FROM EMP WHERE DEPTNO=20);
// This is similar to the sets concept (A-B)U(B-A) OR (AUB)-(A∩B). Suggest a better way to do this as this is not at all
suitable as the deptnos increase. If we add another deptno say 30, then it will be one big mess of a query.
//We can do these effectively by using PL/SQL by storing the repetitive parts as sub queries. Then it will be similar to an
expression.
85) Display the details of those employees who do not have any person working under him?
SQL> SELECT * FROM EMP WHERE EMPNO NOT IN (SELECT MGR FROM EMP WHERE MGR IS NOT NULL);
86) Display the details of those employees who are in sales department and grade is 3?
SQL> SELECT E.* FROM EMP E WHERE E.SAL BETWEEN (SELECT LOSAL FROM SALGRADE WHERE GRADE =3) AND (SELECT
HISAL FROM SALGRADE WHERE GRADE =3) AND DEPTNO = (SELECT DEPTNO FROM DEPT WHERE DNAME=’SALES’);
OR
SELECT E.* FROM EMP E, SALGRADE S, DEPT D WHERE S.GRADE=3 AND E.SAL>S.LOSAL AND E.SAL<S.HISAL AND
E.DEPTNO=D.DEPTNO AND D.DNAME=’SALES’;
// The first query is more cost effective.
88) Display those employees whose name contains not less than 4 characters?
SQL> SELECT * FROM EMP E WHERE LENGTH(E.ENAME)>4;
89) Display those departments whose name starts with “S” while location name ends with “K”?
SQL> SELECT * FROM DEPT D WHERE D.DNAME LIKE ‘S%’ AND D.LOC LIKE ‘%O’;
91) Display those employees whose salary is more than 3000 after giving 20% increment?
SQL> SELECT * FROM EMP WHERE SAL*120/100 > 3000;
94) Display employee name, dept name, salary and commission for those with sal in between 2000 to 5000 while location
is Chicago?
SQL> SELECT E.ENAME, D.DNAME, E.SAL, E.COMM FROM EMP E, DEPT D WHERE E.DEPTNO=D.DEPTNO AND
D.LOC=’CHICAGO’ AND E.SAL BETWEEN 2000 AND 5000;
95) Display those employees whose salary is greater than his manager’s salary?
SQL> SELECT E.* FROM EMP E, EMP E1 WHERE E.MGR = E1.EMPNO AND E.SAL>=E1.SAL;
OR
SELECT * FROM EMP E WHERE E.SAL >= (SELECT E1.SAL FROM EMP E1 WHERE E1.EMPNO=E.MGR);
// Both produce the same results but the second query will be more cost effective as the filter will be executed first thus
reducing no. of records that need to be processed.
96) Display those employees who are working in the same dept where his manager is work?
SQL> SELECT E.* FROM EMP E, EMP E1 WHERE E.MGR = E1.EMPNO AND E.DEPTNO=E1.DEPTNO;
OR
SELECT * FROM EMP E WHERE E.DEPTNO = (SELECT E1.DEPTNO FROM EMP E1 WHERE E1.EMPNO=E.MGR);
//Both produce the same results but the second query will be more cost effective.
97) Display those employees who are not working under any Manager?
SQL> SELECT DISTINCT(E.EMPNO), E.ENAME,E.JOB FROM EMP E,EMP E1 WHERE E.MGR IS NULL OR (E1.EMPNO=E.MGR AND
E1.JOB!=’MANAGER’);
//This ensures that those without a manager, those working under someone other than a manager are displayed.
SQL>SELECT * FROM EMP WHERE MGR IS NULL;
//This only checks if an employee doesn’t report to anyone i.e. has no mgr entry.
98) Display the grade and employees name for the deptno 10 or 30 but grade is not 4 while joined the company before
31-DEC-82?
SQL> SELECT S.GRADE, E.ENAME FROM EMP E, SALGRADE S WHERE E.DEPTNO IN(’10’,’30’) AND E.SAL >= S.LOSAL AND
E.SAL<=S.HISAL MINUS (SELECT S.GRADE, E.ENAME FROM EMP E, SALGRADE S WHERE E.DEPTNO IN(’10’,’30’) AND E.SAL
>= S.LOSAL AND E.SAL<=S.HISAL AND E.HIREDATE<’31-DEC-82′ AND S.GRADE=4);
//This question is ambiguous, to me at least. If the grade!=4 and date<’31-DEC-82’ are separate conditions, then the
below query will do.
SQL>SELECT EMPNO,ENAME,SAL,DEPTNO,HIREDATE,GRADE FROM EMP E,SALGRADE S WHERE E.SAL>=S.LOSAL AND
E.SAL<=S.HISAL AND DEPTNO IN(10,30) AND GRADE<>4 AND HIREDATE<’31-DEC-1982′;
//But this eliminates all the people who joined after that date. Thus if the restriction was ‘if the joining date is before that,
then the grade should not be 4’, then the first query is apt as it eliminates those people alone.
99) Update the salary of each employee by 10% increment who are not eligible for commission?
SQL> UPDATE EMP2 SET SAL=(SAL+0.10*SAL) WHERE COMM IS NULL OR COMM=0;
//I created a copy of emp for this query so as to not affect the original table. We can also ensure that the original table
isn’t affected by using rollback if one hasn’t committed after executing this query on original emp.
100) Delete those employees who joined the company before 31-Dec-82 while their department Location is New York or
Chicago?
SQL> DELETE FROM EMP2 E WHERE E.HIREDATE < TO_DATE(’31-DEC-82′) AND E.DEPTNO IN (SELECT D.DEPTNO FROM
DEPT2 D WHERE D.LOC IN (‘CHICAGO’,’NEW YORK’));
101) Display employee name, job, deptname, loc for all who are working as manager?
SQL> SELECT E.ENAME, E.JOB, D.DNAME, D.LOC FROM EMP E, DEPT D WHERE D.DEPTNO=E.DEPTNO AND E.EMPNO IN
(SELECT MGR FROM EMP WHERE MGR IS NOT NULL);
OR
SELECT E.ENAME, E.JOB, D.DNAME, D.LOC FROM EMP E, DEPT D WHERE D.DEPTNO=E.DEPTNO AND E.JOB=’MANAGER’;
//When we consider the name alone. The first one is based on the mgr column.
102) Display those employees whose manager name is jones and also display their manager name?
SQL> SELECT E.EMPNO, E.ENAME, M.ENAME AS MANAGER FROM EMP E, EMP M WHERE E.MGR=M.EMPNO AND
M.ENAME=’JONES’;
103)Display name and salary of employees whose salary is equal to hisal of the grade of Ford?
SQL> SELECT E1.ENAME FROM EMP E1 WHERE E1.SAL = (SELECT S.HISAL FROM SALGRADE S, EMP E WHERE
E.ENAME=’FORD’ AND E.SAL>=S.LOSAL AND E.SAL<=S.HISAL)
104)Display employee name, job, dept name, his manager name and his grade. Display department wise?
SQL> SELECT E.ENAME,E.JOB,D.DNAME,E1.ENAME AS “MGR NAME”,S.GRADE FROM EMP E,EMP E1,SALGRADE S, DEPT D
WHERE E.DEPTNO=D.DEPTNO AND E.MGR=E1.EMPNO AND E.SAL BETWEEN S.LOSAL AND S.HISAL ORDER BY DNAME;
105) List out all the employee names, job, salary, grade and dept name for everyone in a company except ‘CLERK’ . Sort on
salary display the highest salary?
SQL> SELECT E.ENAME, E.JOB, D.DNAME , S.GRADE FROM EMP E, SALGRADE S, DEPT D WHERE E.JOB!=’CLERK’ AND
E.DEPTNO=D.DEPTNO AND E.SAL BETWEEN S.LOSAL AND S.HISAL ORDER BY E.SAL DESC;
106) Display employee name, job and his manager .Display also employees who are without managers?
SQL> SELECT E.ENAME, E.JOB, E1.ENAME AS “MGR NAME” FROM EMP E, EMP E1 WHERE E.MGR=E1.EMPNO UNION SELECT
E.ENAME,E.JOB,’NO Manager’ AS “MGR NAME” FROM EMP E WHERE E.MGR is null;
OR
SELECT E.ENAME, E.JOB, NVL2(E.MGR,E1.ENAME,’NO MANAGER’) AS “MGR NAME” FROM EMP E1,EMP E WHERE
E1.EMPNO(+)=E.MGR;
108) Display the names of those employees who are getting the highest salary?
SQL> SELECT E.ENAME, E.SAL FROM EMP E WHERE (N-1) = (SELECT COUNT(DISTINCT(E2.SAL)) FROM EMP E2 WHERE E2.SAL
> E.SAL);
OR
SELECT ENAME, SAL FROM EMP WHERE SAL=(SELECT MAX(SAL) FROM EMP);
// The first case can be used to find the nth highest salary. If you want the first highest salary, replace N with 1 and so on
109) Display those employees whose salary is equal to average of maximum and minimum salaries?
SQL> SELECT E.ENAME,E.SAL FROM EMP E WHERE SAL=(SELECT (MAX(E1.SAL) + MIN(E1.SAL))/2 FROM EMP E1);
111) Display dname where atleast three are working and display only deptname?
SQL> SELECT D.DNAME FROM DEPT D WHERE D.DEPTNO IN (SELECT E.DEPTNO FROM EMP E GROUP BY E.DEPTNO HAVING
COUNT(E.DEPTNO)>3);
OR
SELECT D.DNAME FROM EMP E, DEPT D WHERE E.DEPTNO=D.DEPTNO GROUP BY D.DNAME HAVING COUNT(E.DEPTNO)>3;
//The first query is cost effective. When column values depending on one table alone are the required output [Here dname
is from dept alone], it is always preferable to eliminate the no. of joins made. Thus use co-related sub queries instead of
hash join which is done in the second query
112) Display names of those managers whose salary is more than average salary of Company?
SQL> SELECT E.ENAME,E.SAL FROM EMP E WHERE JOB=’MANAGER’ AND E.SAL>=(SELECT AVG(E.SAL) FROM EMP E);
OR
SELECT E.ENAME, E.SAL FROM EMP E WHERE E.EMPNO IN(SELECT E.MGR FROM EMP E) AND E.SAL > (SELECT AVG(E.SAL)
FROM EMP E);
113) Display those managers name whose salary is more than average salary of his employees?
SQL> SELECT E.ENAME FROM EMP E WHERE E.SAL >(SELECT AVG(E1.SAL) FROM EMP E1 WHERE E1.MGR=E.EMPNO);
OR
SELECT DISTINCT EMP.ENAME FROM EMP,EMP E WHERE E.SAL <(SELECT AVG(EMP.SAL) FROM EMP
WHERE EMP.EMPNO=E.MGR GROUP BY EMP.ENAME) AND EMP.EMPNO=E.MGR;
OR
SELECT M.ENAME FROM EMP M WHERE M.JOB=’MANAGER’ AND M.SAL>=(SELECT AVG(SAL) FROM EMP E WHERE E.MGR =
M.EMPNO);
//The first query is the most cost effective followed by the third but it only considers those whose job is managers in the
third case.
114) Display employee name, sal, comm and netpay for those employees whose netpay is greater than or equal to any
other employee salary of the company?
SQL> SELECT ENAME,SAL,NET_SAL FROM (SELECT ENAME,SAL,SAL+NVL(COMM,0) AS NET_SAL FROM EMP) WHERE
NET_SAL>=SAL;
OR
SELECT ENAME, SAL, SAL+NVL(COMM,0) AS NET_SAL FROM EMP WHERE SAL+NVL(COMM,0) >= ANY(SELECT SAL FROM
EMP);
//The first one will be more cost effective
115) Display those employees whose salary is less than his manager but more than salary of other managers?
SQL> select * from emp e where e.sal <( select e1.sal from emp e1 where e.mgr=e1.empno) and e.sal > any (select
e1.sal from emp e1 where e1.empno in( select e.mgr from emp e));
// I am considering the “mgr” column as the basis for deciding managers rather than the job ‘MANAGER’
116) Display all employees names with total sal of company with each employee name?
SQL> SELECT E1.ENAME,(SELECT SUM(SAL) FROM EMP) AS TOTAL_SAL_CMP FROM EMP E1;
118) Find out the number of employees whose salary is greater than their managers salary?
SQL> SELECT COUNT(*) FROM EMP E,EMP E1 WHERE E.MGR = E1.EMPNO AND E.SAL>E1.SAL;
119) Display the manager who are not working under president but they are working under any other manager?
SQL> SELECT E.EMPNO, E.ENAME, M.ENAME AS MANAGER FROM EMP E, EMP M WHERE E.MGR=M.EMPNO AND
M.ENAME!=(SELECT ENAME FROM EMP WHERE JOB=’PRESIDENT’);
121) Delete those records from emp table whose deptno not available in dept table?
SQL> DELETE FROM EMP1 E WHERE E.DEPTNO NOT IN (SELECT D.DEPTNO FROM DEPT D);
122) Display those enames whose salary is out of grade available in salgrade table?
SQL> SELECT * FROM EMP E WHERE E.SAL<(SELECT MIN(LOSAL) FROM SALGRADE) OR E.SAL>(SELECT MAX(HISAL) FROM
SALGRADE);
//This query is based on our analysis that for a salary to be out of grade, either the salary should be lesser than the
minimum or greater the maximum values in losal & hisal respectively.
//What’s the flaw in this.
Say the salgrade table is as follows
1 1000 2000
2 3000 4000
Now say a emp has a salary of 2500, the above query will result that a valid grade exists.
Thus it is wrong.
(SELECT * FROM EMP) MINUS (SELECT E.* FROM EMP E, SALGRADE S WHERE E.SAL BETWEEN S.LOSAL AND S.HISAL);
// This ensures results but won’t be suitable for great no. of data items.
SELECT ENAME FROM EMP,SALGRADE S,SALGRADE S1 WHERE EMP.SAL > S.HISAL AND EMP.SAL<S1.LOSALD S1.GRADE =
S.GRADE+1;
//This is slightly better than the above one as each row in salgrade will be compared .
//If one reaches at a better way to do this, then kindly point that out.
123) Display employee name, sal, comm and whose netpay is greater than any other employee in the company?
SQL> SELECT E.ENAME,E.SAL,E.COMM FROM EMP E WHERE (E.SAL+NVL(E.COMM,0))>ANY(SELECT SAL+NVL(COMM,0) FROM
EMP);
// We are assuming here that the netpay should be better than any one employee atleast
SELECT E.ENAME,E.SAL,E.COMM FROM EMP E WHERE (E.SAL+NVL(E.COMM,0))>= ALL(SELECT SAL+NVL(COMM,0) FROM
EMP);
//Here the netpay is greater than all employees as the question can be deciphered this way too
124) Display name of those employees who are going to retire by 31-Dec-99 if maximum job period is 30 years?
SQL> SELECT * FROM EMP WHERE ADD_MONTHS(HIREDATE,12*30) <= ’31-DEC-99′;
OR
SELECT * FROM EMP WHERE MONTHS_BETWEEN(’31-DEC-1999′,HIREDATE)>=(30*12);
127) Display those employees who joined in the company in the month of Dec?
SQL> SELECT * FROM EMP E WHERE TO_CHAR(HIREDATE,’MM’)=12;
OR
SELECT * FROM EMP WHERE EXTRACT(MONTH FROM HIREDATE) = 12;
130) Display those employees whose salary is less than (first 2 characters from hiredate combined with last 2 characters of
sal)?
SQL> SELECT * FROM EMP WHERE SAL < (substr(hiredate,1,2)||substr(sal,-2,2));
131) Display those employees whose 10% of salary is equal to the year joining?
SQL> SELECT * FROM EMP E WHERE (0.10*SAL)=TO_CHAR(HIREDATE,’YYYY’);
OR
SELECT * FROM EMP E WHERE (E.SAL*0.10) = EXTRACT(YEAR FROM E.HIREDATE);
134) Display those employees who joined the company before 15th of the month?
SQL>SELECT * FROM EMP WHERE TO_CHAR(HIREDATE,’DD’)<15;
OR
SELECT * FROM EMP WHERE EXTRACT(DAY FROM HIREDATE)=15 AND EXTRACT(DAY FROM HIREDATE)<=20;
135) Display those employees who has joined between 15th 20th of the month?
SQL> SELECT * FROM EMP WHERE TO_CHAR(HIREDATE,’DD’) BETWEEN 15 AND 20 ;
OR
SELECT * FROM EMP WHERE EXTRACT(DAY FROM HIREDATE)>=15 AND EXTRACT(DAY FROM HIREDATE)<=20;
136) Delete those records where no of employees in particular department is less than 3?
SQL>DELETE FROM EMP1 WHERE DEPTNO IN (SELECT DEPTNO FROM EMP GROUP BY DEPTNO HAVING COUNT(DEPTNO)<3);
137) Delete those employee who joined the company 10 years back from today?
SQL>DELETE FROM EMP1 WHERE HIREDATE = ADD_MONTHS(SYSDATE,-12*10);
OR
DELETE FROM EMP1 WHERE MONTHS_BETWEEN(SYSDATE,HIREDATE)<(12*10);
140) Display the dept name the number of characters of which is equal to no of employee in any other department?
SQL> SELECT DISTINCT(D.DNAME) FROM DEPT D,EMP E WHERE LENGTH(D.DNAME) IN (SELECT COUNT(E.EMPNO) FROM
EMP E GROUP BY E.DEPTNO) AND E.DEPTNO!=D.DEPTNO;
OR
SELECT * FROM DEPT WHERE LENGTH(DNAME) IN (SELECT COUNT(DEPTNO) FROM EMP GROUP BY DEPTNO);
//The first one ensures that same dept isn’t checked. The second one checks for the length match alone.
141) Display the name of the dept of those employees who joined the company on the same date?
SQL> SELECT DNAME FROM DEPT WHERE DEPTNO IN (SELECT E.DEPTNO FROM EMP E, EMP E1 WHERE E.EMPNO!=E1.EMPNO
AND E.HIREDATE=E1.HIREDATE AND E.DEPTNO = E1.DEPTNO );
//This ensures that we are comparing those who are from the same dept.
OR
SELECT DNAME FROM DEPT WHERE DEPTNO IN (SELECT E.DEPTNO FROM EMP E, EMP E1 WHERE E.EMPNO!=E1.EMPNO AND
E.HIREDATE=E1.HIREDATE);
//This just compares the hiredates and returns the dept names of the employees [But the employees with the same date
might be from different depts..]
142) Display those employees whose grade is equal to any number of sal but not equal to first number of sal?
SQL> SELECT E.ENAME, E.SAL , S.GRADE FROM EMP E, SALGRADE S WHERE E.SAL BETWEEN S.LOSAL AND S.HISAL AND
SUBSTR(E.SAL,1,1)!=GRADE AND INSTR(E.SAL,S.GRADE,1,1)!=0;
144) Display the name of employees who joined the company on the same date?
SQL> SELECT E.ENAME FROM EMP E,EMP E1 WHERE E.EMPNO!=E1.EMPNO AND E.HIREDATE=E1.HIREDATE;
OR
SELECT E.ENAME FROM EMP E WHERE HIREDATE IN(SELECT HIREDATE FROM EMP WHERE EMPNO<>E.EMPNO);
145) Display the manager who is having maximum number of employees working under him?
SQL>SELECT E1.ENAME FROM (SELECT MGR,COUNT(MGR) FROM EMP GROUP BY MGR ORDER BY
COUNT(MGR) DESC) X,EMP E1 WHERE X.MGR=E1.EMPNO AND ROWNUM = 1;
OR
SELECT MGR FROM EMP GROUP BY MGR HAVING COUNT(*)=(SELECT MAX(COUNT(MGR)) FROM EMP GROUP BY MGR);
146) List out the employee name and salary increased by 15% and express as whole number of Dollars?
SQL> SELECT ENAME,’$’||ROUND(1.15*SAL) AS “$_SAL” FROM EMP;
147) Produce the output of the emp table “EMPLOYEE_AND_JOB” for ename and job ?
SQL> SELECT ENAME||’_AND_’||JOB AS “EMPLOYEE_JOB” FROM EMP;
149) Print list of employees displaying ‘Just salary’ if sal more than 1500, if exactly 1500 display ‘on target’, if less than
1500 display below 1500?
SQL> SELECT ENAME, (CASE WHEN SAL>1500 THEN ‘BELOW_TARGET’ WHEN SAL=1500 THEN ‘ON_TARGET’ WHEN
SAL<1500 THEN ‘LESS THAN TARGET’ END ) AS SALCOND FROM EMP
150) Given a string of the format ‘nn/nn’ . Verify that the first and last 2 characters are numbers .And that the middle
character is ‘/’ Print the expressions ‘Yes’ IF valid ‘NO’ of not valid . Use the following values to test your
solution’12/54′,01/1a,’99/98′?
SQL> select (CASE WHEN
DECODE(TRANSLATE(SUBSTR(’11/11′,0,TRUNC(LENGTH(’11/11′)/2)),’#1234567980.’,’.’),NULL,’YES’,’NO’) = ‘YES’ and
DECODE(TRANSLATE(substr(’11/11′,round(length(’11/11′)/2)+1,TRUNC(LENGTH(’11/11′)/2)),’#1234567980.’,’.’),NULL,’YE
S’,’NO’) = ‘YES’ AND substr(’11/11′,round(length(’11/11′)/2),1)=’/’ THEN ‘YES’ ELSE ‘NO’ END) from dual;
// There are few other ways to do this like pull a substring and check between 00 & 99 to verify etc. The above one on the
other hand will work for any entry following that format for eg ‘123/123’ , ‘125/1ab’ etc