Saturday, November 21, 2015

Best SQL Queries for Practice


Some SQL Simple and Complex Queries, It is best for practice.

1) Display the details of all employees
SQL>Select * from EMP;

2) Display the depart information from department table
SQL>select * from dept;

3) Display the name and job for all the employees
SQL>select ename, job from EMP;

4) Display the name and salary for all the employees
SQL>select ename, sal from EMP;

5) Display the employee no and total salary for all the employees
SQL>select empno, ename, sal, comm, sal+nvl (comm, 0) as”total salary” from
EMP;

6) Display the employee name and annual salary for all employees.
SQL>select ename, 12*(sal+nvl(comm,0)) as “annual Sal” from emp;

7) Display the names of all the employees who are working in depart number 10.
SQL>select emame from emp where deptno=10;

8) Display the names of all the employees who are working as clerks and drawing a salary more than 3000.
SQL>select ename from emp where job=’CLERK’ and sal>3000;

9) Display the employee number and name who are earning comm.
SQL>select empno,ename from emp where comm is not null;

10) Display the employee number and name who do not earn any comm.
SQL>select empno,ename from emp where comm is null;

11) Display the names of employees who are working as clerks,salesman or analyst and drawing a salary more than 3000.
SQL>select ename from emp where job=’CLERK’ OR JOB=’SALESMAN’
OR JOB=’ANALYST’ AND SAL>3000;

12) Display the names of the employees who are working in the company for the past 5 years;
SQL>select ename from emp where to_char(sysdate,’YYYY’)- to_char(hiredate,’YYYY’)>=5;

13) Display the list of employees who have joined the company before
30-JUN-90 or after 31-DEC-90.
SQL> select ename from emp where hiredate < ’30-JUN-1990′ or hiredate >
’31-DEC-90′;
14) Display current Date.
SQL>select sysdate from dual;

15) Display the list of all users in your database (use catalog table).
SQL>select username from all_users;

16) Display the names of all tables from current user;
SQL>select tname from tab;

17) Display the name of the current user.
SQL>show user

18) Display the names of employees working in depart number 10 or 20 or 40 or employees working as CLERKS, SALESMAN or ANALYST.
SQL>select ename from emp where deptno in (10,20,40) or job in(‘CLERKS’,’SALESMAN’,’ANALYST’);

19) Display the names of employees whose name starts with alaphabet S.
SQL>select ename from emp where ename like ‘S%';

20) Display the Employee names for employees whose name ends with alaphabet S.
SQL>select ename from emp where ename like ‘%S';

21) Display the names of employees whose names have second alphabet A in their names.
SQL>select ename from emp where ename like ‘_A%';

22) select the names of the employee whose names is exactly five characters in length.
SQL>select ename from emp where length(ename)=5;

23) Display the names of the employee who are not working as MANAGERS.
SQL>select ename from emp where job not in(‘MANAGER’);

24) Display the names of the employee who are not working as SALESMAN OR CLERK OR ANALYST.
SQL>select ename from emp where job not in (‘SALESMAN’,’CLERK’,’ANALYST’);

25) Display all rows from emp table.The system should wait after every
screen full of informaction.
SQL>set pause on

26) Display the total number of employee working in the company.
SQL>select count(*) from emp;

27) Display the total salary beiging paid to all employees.
SQL>select sum(sal) from emp;

28) Display the maximum salary from emp table.
SQL>select max(sal) from emp;

29) Display the minimum salary from emp table.
SQL>select min(sal) from emp;

30) Display the average salary from emp table.
SQL>select avg(sal) from emp;

31) Display the maximum salary being paid to CLERK.
SQL>select max(sal) from emp where job=’CLERK';

32) Display the maximum salary being paid to depart number 20.
SQL>select max(sal) from emp where deptno=20;

33) Display the minimum salary being paid to any SALESMAN.
SQL>select min(sal) from emp where job=’SALESMAN';

34) Display the average salary drawn by MANAGERS.
SQL>select avg(sal) from emp where job=’MANAGER';

35) Display the total salary drawn by ANALYST working in depart number 40.
SQL>select sum(sal) from emp where job=’ANALYST’ and deptno=40;

36) Display the names of the employee in order of salary i.e the name of the employee earning lowest salary should salary appear first.
SQL>select ename from emp order by sal;

37) Display the names of the employee in descending order of salary.
a) select ename from emp order by sal desc;

38) Display the names of the employee in order of employee name.
SQL> select ename from emp order by ename;

39) Display empno,ename,deptno,sal sort the output first base on name and within name by deptno and with in deptno by sal.
SQL>select empno,ename,deptno,sal from emp order by ename,deptno,sal;

40) Display the name of the employee along with their annual salary (sal*12).The name of the employee earning highest annual salary should apper first.
SQL>select ename,sal*12 from emp order by sal desc;
41) Display name,salary,hra,pf,da,total salary for each employee. The output 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 ename,sal,sal/100*15 as hra,sal/100*5 as pf,sal/100*10 as
da, sal+sal/100*15+sal/100*10-sal/100*5 as total from emp;

42) Display depart numbers and total number of employees working in each department.
SQL>select deptno,count(deptno)from emp group by deptno;

43) Display the various jobs and total number of employees within each job group.
SQL>select job,count(empno)from emp group by job;

44) Display the depart numbers and total salary for each department.
SQL>select deptno,sum(sal) from emp group by deptno;

45) Display the depart numbers and max salary for each department.
SQL>select deptno,max(sal) from emp group by deptno;

46) Display the various jobs and total salary for each job
SQL>select job,sum(sal) from emp group by job;

47) Display the various jobs and total salary for each job
SQL>select job,min(sal) from emp group by job;

48) Display the depart numbers with more than three employees in each dept.
SQL>select deptno,count(deptno) from emp group by deptno having count(*)>3;

49) Display the various jobs along with total salary for each of the jobs
where total salary is greater than 40000.
SQL>select job,sum(sal) from emp group by job having sum(sal)>40000;

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 job,count(empno) from emp group by job having count(job)>3

51) Display the name of the employee who earns highest salary.
SQL>select ename from emp where sal= (select max(sal) from emp);

52) Display the employee number and name for employee working as clerk and earning highest salary among clerks.
SQL>select empno,ename from emp  where job=’CLERK’ and sal=(select max(sal) from emp where job=’CLERK’);
53) Display the names of salesman who earns a salary more than the highest salary of any clerk.
SQL>select ename,sal from emp where job=’SALESMAN’ and sal>(select max(sal) from emp where job=’CLERK’);

54) Display the names of clerks who earn a salary more than the lowest salary of any salesman.
SQL>select ename from emp where job=’CLERK’ and sal>(select min(sal) from emp where job=’SALESMAN’);
Display the names of employees who earn a salary more than that of Jones or that of salary greater than that of scott.
SQL>select ename,sal from emp where sal>(select sal from emp where ename=’JONES’)and sal> (select sal from emp where ename=’SCOTT’);

55) Display the names of the employees who earn highest salary in their respective departments.
SQL>select ename,sal,deptno from emp where sal in(select max(sal) from emp group by deptno);

56) Display the names of the employees who earn highest salaries in their respective job groups.
SQL>select ename,sal,job from emp where sal in(select max(sal) from emp group by job);

57) Display the employee names who are working in accounting department.
SQL>select ename from emp where deptno=(select deptno from dept where dname=’ACCOUNTING’)

58) Display the employee names who are working in Chicago.
SQL>select ename from emp where deptno=(select deptno from dept where LOC=’CHICAGO’)

59) Display the Job groups having total salary greater than the maximum salary for managers.
SQL>SELECT JOB,SUM(SAL) FROM EMP GROUP BY JOB HAVING SUM(SAL)>(SELECT MAX(SAL) FROM EMP WHERE JOB=’MANAGER’);

60) Display the names of employees from department number 10 with salary greater than that of any employee working in other department.
SQL>select ename from emp where deptno=10 and sal>any(select sal from emp where deptno not in 10).

61) Display the names of the employees from department number 10 with salary greater than that of all employee working in other departments.
SQL>select ename from emp where deptno=10 and sal>all(select sal from emp where deptno not in 10).
62) Display the names of the employees in Uppercase.
SQL>select upper(ename)from emp

63) Display the names of the employees in Lower case.
SQL>select lower(ename)from emp

64) Display the names of the employees in Proper case.
SQL>select initcap(ename)from emp;

65) Display the length of Your name using appropriate function.
SQL>select length(‘name’) from dual

66) Display the length of all the employee names.
SQL>select length(ename) from emp;

67) select name of the employee concatenate with employee number.
SQL>select ename||empno from emp;

68) User 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

69) Find the First occurance of character ‘a’ from the following string i.e ‘Computer Maintenance Corporation’.
SQL>SELECT INSTR(‘Computer Maintenance Corporation’,’a’,1) FROM DUAL

70) Replace every occurance of alphabhet A with B in the string Allens(use translate function)
SQL>select translate(‘Allens’,’A’,’B’) from dual

71) Display the informaction from emp table.Where job manager is found it should be displayed as boos(Use replace function).
SQL>select replace(JOB,’MANAGER’,’BOSS’) FROM EMP;

72) Display empno,ename,deptno from emp table.Instead of display department numbers display the related department name(Use decode function).
SQL>select empno, ename, decode (deptno, 10,’ACCOUNTING’, 20,’RESEARCH’, 30,’SALES’, 40,’OPRATIONS’) from emp;

73) Display your age in days.
SQL>select to_date(sysdate)-to_date(’10-sep-77′)from dual

74) Display your age in months.
SQL>select months_between(sysdate,’10-sep-77′) from dual

75) Display the current date as 15th August Friday Nineteen Ninety Seven.
SQL>select to_char(sysdate,’ddth Month day year’) from dual

76) Display the following output for each row from emp table.scott has joined the company on Wednesday 13th August nineteen ninety.
SQL>select ENAME||’ HAS JOINED THE COMPANY ON ‘||to_char(HIREDATE,’day ddth Month year’) from EMP;

77) Find the date for nearest saturday after current date.
SQL>SELECT NEXT_DAY(SYSDATE,’SATURDAY’)FROM DUAL;

78) Display current time.
SQL>select to_char(sysdate,’hh:MM:ss’) from dual.

79) Display the date three months Before the current date.
SQL>select add_months(sysdate,3) from dual;

80) 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);

81) Display the jobs found in department 10 and 20 Eliminate duplicate jobs.
SQL>select distinct(job) from emp where deptno=10 or deptno=20
(or)
SQL>select distinct(job) from emp where deptno in(10,20);

82) Display the jobs which are unique to department 10.
SQL>select distinct(job) from emp where deptno=10

83) Display the details of those who do not have any person working under them.
SQL>select e.ename from emp,emp e where emp.mgr=e.empno group by e.ename having count(*)=1;

84) Display the details of those employees who are in sales department and grade is 3.
SQL>select * from emp where deptno=(select deptno from dept where dname=’SALES’)and sal between(select losal from salgrade where grade=3)and (select hisal from salgrade where grade=3);

85) Display those who are not managers and who are managers any one.
i)display the managers names
SQL>select distinct(m.ename) from emp e,emp m where m.empno=e.mgr;
ii)display the who are not managers
SQL>select ename from emp where ename not in(select distinct(m.ename) from emp e,emp m where m.empno=e.mgr);
86) Display those employee whose name contains not less than 4 characters.
SQL>select ename from emp where length(ename)>4;

87) Display those department whose name start with “S” while the location name ends with “K”.
SQL>select dname from dept where dname like ‘S%’ and loc like ‘%K';

88) Display those employees whose manager name is JONES.
SQL>select p.ename from emp e,emp p where e.empno=p.mgr and e.ename=’JONES';

89) Display those employees whose salary is more than 3000 after giving 20% increment.
SQL>select ename,sal from emp where (sal+sal*.2)>3000;

90) Display all employees while their dept names;
SQL>select ename,dname from emp,dept where emp.deptno=dept.deptno;

91) Display ename who are working in sales dept.
SQL>select ename from emp where deptno=(select deptno from dept where dname=’SALES’);

92) Display employee name,deptname,salary and comm for those sal in between 2000 to 5000 while location is chicago.
SQL>select ename,dname,sal,comm from emp,dept where sal between 2000 and 5000 and loc=’CHICAGO’ and emp.deptno=dept.deptno;

93)Display those employees whose salary greter than his manager salary.
SQL>select p.ename from emp e,emp p where e.empno=p.mgr and p.sal>e.sal;

94) Display those employees who are working in the same dept where his manager is work.
SQL>select p.ename from emp e,emp p where e.empno=p.mgr and p.deptno=e.deptno;

95) Display those employees who are not working under any manager.
SQL>select ename from emp where mgr is null

96) Display grade and employees name for the dept no 10 or 30 but grade is not 4 while joined the company before 31-dec-82.
SQL>select ename,grade from emp,salgrade where sal between losal and hisal and deptno in(10,30) and grade<>4 and hiredate<’31-DEC-82′;

97) Update the salary of each employee by 10% increment who are not eligible for commission.
SQL>update emp set sal=sal+sal*10/100 where comm is null;

98) SELECT those employee who joined the company before 31-dec-82 while their dept location is newyork or Chicago.
SQL>SELECT EMPNO,ENAME,HIREDATE,DNAME,LOC FROM EMP,DEPTWHERE (EMP.DEPTNO=DEPT.DEPTNO)AND HIREDATE <’31-DEC-82′ AND DEPT.LOCIN(‘CHICAGO’,’NEW YORK’);

99) DISPLAY EMPLOYEE NAME,JOB,DEPARTMENT,LOCATION FOR ALL WHO ARE WORKING AS MANAGER?
SQL>select ename,JOB,DNAME,LOCATION from emp,DEPT where mgr is not null;

100) DISPLAY THOSE EMPLOYEES WHOSE MANAGER NAME IS JONES? –[AND ALSO DISPLAY THEIR MANAGER NAME]?
SQL> SELECT P.ENAME FROM EMP E, EMP P WHERE E.EMPNO=P.MGR AND E.ENAME=’JONES';

101) Display name and salary of ford if his salary is equal to hisal of his grade.
SQL> select ename,sal,grade from emp,salgrade where sal between losal and hisal and ename =’FORD’ AND HISAL=SAL;

102) Display employee name,job,depart name ,manager name,his grade and make out an under department wise?
SQL>SELECT E.ENAME, E.JOB, DNAME, EMP.ENAME, GRADE FROM EMP,EMP E,SALGRADE, DEPT WHERE EMP.SAL BETWEEN LOSAL AND HISAL ANDEMP.EMPNO=E.MGR AND EMP.DEPTNO=DEPT.DEPTNO ORDER BY DNAME;

103) List out all employees name,job,salary,grade and depart name for everyone in the company except ‘CLERK’.Sort on salary display the highest salary?
SQL>SELECT ENAME, JOB, DNAME, SAL, GRADE FROM EMP, SALGRADE, DEPT WHERE SAL BETWEEN LOSAL AND HISAL AND EMP.DEPTNO=DEPT.DEPTNO AND JOB NOT IN (‘CLERK’) ORDER BY SAL ASC;

104) Display the employee name, job and his manager. Display also employee who are without manager?
SQL>select e.ename,e.job,eMP.ename AS Manager from emp, emp e where emp. empno(+)=e.mgr

105) Find out the top 5 earners of company?
SQL>SELECT DISTINCT SAL FROM EMP E WHERE 5>=(SELECT COUNT(DISTINCT SAL) FROM EMP A WHERE A.SAL>=E.SAL)ORDER BY SAL DESC;

106) Display name of those employee who are getting the highest salary?
SQL>select ename from emp where sal=(select max(sal) from emp);

107) Display those employee whose salary is equal to average of maximum and minimum?
SQL>select ename from emp where sal=(select max(sal)+min(sal)/2 from emp);

108) Select count of employee in each department where count greater than 3?
SQL>select count (*) from emp group by deptno having count(deptno)>3

109) Display dname where at least 3 are working and display only department name?
SQL>select distinct d.dname from dept d,emp e where d.deptno=e.deptno and 3>any (select count(deptno) from emp group by deptno)

110) Display name of those managers name whose salary is more than average salary of his company?
SQL>SELECT E.ENAME, EMP.ENAME FROM EMP, EMP E WHEREEMP.EMPNO=E.MGR AND E.SAL>(SELECT AVG(SAL) FROM EMP);

111) Display those managers name whose salary is more than average salary of his employee?
SQL>SELECT DISTINCT EMP.ENAME FROM EMP,EMP E WHERE E.SAL <(SELECT AVG(EMP.SAL) FROM EMP WHERE EMP.EMPNO=E.MGR GROUP BYEMP.ENAME) AND EMP.EMPNO=E.MGR;

112) Display employee name,sal,comm and net pay for those employee whose net pay is greter than or equal to any other employee salary of the company?
SQL>select ename,sal,comm,sal+nvl(comm,0) as NetPay from emp where sal+nvl(comm,0) >any (select sal from emp)

113) Display all employees names with total sal of company with each employee name?
SQL>SELECT ENAME,(SELECT SUM(SAL) FROM EMP) FROM EMP;

114) Find out last 5(least)earners of the company.?
SQL>SELECT DISTINCT SAL FROM EMP E WHERE 5>=(SELECT COUNT(DISTINCT SAL) FROM EMP A WHERE A.SAL<=E.SAL) ORDER BY SAL DESC;

115) Find out the number of employees whose salary is greater than their manager salary?
SQL>SELECT E.ENAME FROM EMP , EMP E WHERE EMP.EMPNO=E.MGR ANDEMP.SAL<E.SAL

116) Display those department where no employee working?
SQL>select dname from emp,dept where emp.deptno not in(emp.deptno)

117) Display those employee whose salary is ODD value?
SQL>select * from emp where sal<0;

118) Display those employee whose salary contains at least 3 digits?
SQL>select * from emp where length(sal)>=3;

119) Display those employee who joined in the company in the month of Dec?
SQL>select ename from emp where to_char(hiredate,’MON’)=’DEC';

120) Display those employees whose name contains “A”?
SQL>select ename from emp where instr(ename,’A’)>0;
or
SQL>select ename from emp where ename like(‘%A%’);

121) Display those employee whose deptno is available in salary?
SQL>select emp. ename from emp, emp e where emp.sal=e.deptno;

122) Display those employee whose first 2 characters from hiredate -last 2 characters of salary?
SQL>select ename,SUBSTR(hiredate,1,2)||ENAME||substr(sal,-2,2) from emp;

123) Display those employee whose 10% of salary is equal to the year of joining?
SQL>select ename from emp where to_char(hiredate,’YY’)=sal*0.1;

124) Display those employee who are working in sales or research?
SQL>SELECT ENAME FROM EMP WHERE DEPTNO IN(SELECT DEPTNO FROM DEPT WHERE DNAME IN(‘SALES’,’RESEARCH’));

125) Display the grade of jones?
SQL>SELECT ENAME,GRADE FROM EMP,SALGRADE WHERE SAL BETWEEN LOSAL AND HISAL AND Ename=’JONES';

126) Display those employees who joined the company before 15 of the month?
SQL> select ename from emp where to_char(hiredate,’DD’)<15;

127) Display those employee who has joined before 15th of the month.
SQL> select ename from emp where to_char(hiredate,’DD’)<15;

128) Delete those records where no of employees in a particular department is less than 3.
SQL>delete from emp where deptno=(select deptno from emp group by deptno having count(deptno)<3);

129) Display the name of the department where no employee working.
SQL> SELECT E.ENAME, E.JOB, M.ENAME, M.JOB FROM EMP E, EMP M WHERE E.MGR=M.EMPNO

130) Display those employees who are working as manager.
SQL>SELECT M.ENAME MANAGER FROM EMP M ,EMP E WHERE E.MGR=M.EMPNO GROUP BY M.ENAME;

131) Display those employees whose grade is equal to any number of sal but not equal to first number of sal?
SQL> SELECT ENAME,GRADE FROM EMP,SALGRADE WHERE GRADE NOT IN(SELECT SUBSTR(SAL,0,1)FROM EMP);

132) Print the details of all the employees who are Sub-ordinate to BLAKE?
SQL>select emp.ename from emp, emp e where emp.mgr=e.empno and e.ename=’BLAKE';

133)  Display employee name and his salary whose salary is greater than highest average of department number?
SQL>SELECT SAL FROM EMP WHERE SAL>(SELECT MAX(AVG(SAL)) FROM EMP GROUP BY DEPTNO);

134) Display the 10th record of emp table(without using rowid)
SQL>SELECT * FROM EMP WHERE ROWNUM<11
MINUS
SELECT * FROM EMP WHERE ROWNUM<10

135) Display the half of the ename’s in upper case and remaining lowercase?
SQL>SELECT  SUBSTR(LOWER(ENAME),1,3)||SUBSTR(UPPER(ENAME),3,LENGTH(ENAME)) FROM EMP;

136) Display the 10th record of emp table without using group by and rowid?
SQL>SELECT * FROM EMP WHERE ROWNUM<11
MINUS
SELECT * FROM EMP WHERE ROWNUM<10
Delete the 10th record of emp table.
SQL>DELETE FROM EMP WHERE EMPNO=(SELECT EMPNO FROM EMP WHERE ROWNUM<11 MINUS SELECT EMPNO FROM EMP WHERE ROWNUM<10)

137) Create a copy of emp table;
SQL>create table new_table as select * from emp where 1=2;

138) Select ename if ename exists more than once.
SQL>select ename from emp e group by ename having count(*)>1;

139) Display all enames in reverse order?(SMITH:HTIMS).
SQL>SELECT REVERSE(ENAME)FROM EMP;

140) Display those employee whose joining of month and grade is equal.
SQL>SELECT ENAME FROM EMP WHERE SAL BETWEEN (SELECT LOSAL FROM SALGRADE WHERE GRADE=TO_CHAR(HIREDATE,’MM’)) AND (SELECT HISAL FROM SALGRADE WHERE GRADE=TO_CHAR(HIREDATE,’MM’));

141) Display those employee whose joining DATE is available in deptno.
SQL>SELECT ENAME FROM EMP WHERE TO_CHAR(HIREDATE,’DD’)=DEPTNO;

142) Display those employees name as follows
A ALLEN
B BLAKE
SQL> SELECT SUBSTR (ENAME, 1, 1), ENAME FROM EMP;

143) List out the employees ename,sal,PF(20% OF SAL) from emp;
SQL>SELECT ENAME,SAL,SAL*.2 AS PF FROM EMP;

144) Create table EMP with only one column empno;
SQL>Create table EMP as select empno from emp where 1=2;

145) Add this column to emp table ename varchar2(20).
SQL>alter table emp add(ename varchar2(20));

146) Oops I forgot give the primary key constraint. Add in now.
SQL>alter table emp add primary key(empno);

147) Now increase the length of ename column to 30 characters.
SQL>alter table emp modify (ename varchar2 (30));

148) Add salary column to emp table.
SQL>alter table emp add(sal number(10));

149) I want to give a validation saying that salary cannot be greater 10,000 (note give a name to this constraint)
SQL>alter table emp add constraint chk_001 check(sal<=10000)

150) For the time being I have decided that I will not impose this validation. My boss has agreed to pay more than 10,000.
SQL>again alter the table or drop constraint with alter table emp drop constraint chk_001 (or)Disable the constraint by using alter table emp modify constraint chk_001 disable;

151) My boss has changed his mind. Now he doesn’t want to pay more than 10,000.so revoke that salary constraint.
SQL>alter table emp modify constraint chk_001 enable;

152) Add column called as mgr to your emp table;
SQL>alter table emp add(mgr number(5));
153) Oh! This column should be related to empno. Give a command to add this constraint.
SQL>ALTER TABLE EMP ADD CONSTRAINT MGR_DEPT FOREIGN KEY(MGR) REFERENCES EMP(EMPNO);

154) Add deptno column to your emp table;
SQL>alter table emp add(deptno number(5));

155) This deptno column should be related to deptno column of dept table;
SQL>alter table emp add constraint dept_001 foreign key(deptno)  reference dept(deptno) [deptno should be primary key]

156) Give the command to add the constraint.
SQL>alter table <table_name) add constraint <constraint_name>
<constraint type>

157) Create table called as newemp. Using single command create this table as well as get data into this table(use create table as);
SQL>create table newemp as select * from emp;
SQL>Create table called as newemp. This table should contain only
empno,ename,dname.
SQL>create table newemp as select empno,ename,dname from emp,dept where 1=2;

158) Delete the rows of employees who are working in the company for more than 2 years.
SQL>delete from emp where (sysdate-hiredate)/365>2;

159) Provide a commission (10% Comm Of Sal) to employees who are not earning any commission.
SQL>select sal*0.1 from emp where comm is null;

160) If any employee has commission his commission should be incremented by 10% of his salary.
SQL>update emp set comm=sal*.1 where comm is not null;

161) Display employee name and department name for each employee.
SQL>select empno,dname from emp,dept where emp.deptno=dept.deptno;

162)Display employee number,name and location of the department in which he is working.
SQL>select empno,ename,loc,dname from emp,dept where emp.deptno=dept.deptno;

163) Display ename,dname even if there are no employees working in a particular department(use outer join).
SQL>select ename,dname from emp,dept where emp.deptno=dept.deptno(+)


164) Display employee name and his manager name.
SQL>select p.ename,e.ename from emp e,emp p where e.empno=p.mgr;

165) Display the department name and total number of employees in each department.
SQL>select dname,count(ename) from emp,dept where emp.deptno=dept.deptno group by dname;

166) Display the department name along with total salary in each department.
SQL>select dname,sum(sal) from emp,dept where emp.deptno=dept.deptno group by dname;

167) Display itemname and total sales amount for each item.
SQL>select itemname,sum(amount) from item group by itemname;

168)  Write a Query To Delete The Repeated Rows from emp table;
SQL>Delete from emp where rowid not in(select min(rowid)from emp group by ename);

169) TO DISPLAY 5 TO 7 ROWS FROM A TABLE
SQL>select ename from emp where rowid in(select rowid from emp where rownum<=7 minus select rowid from empi where rownum<5);

170) DISPLAY TOP N ROWS FROM TABLE?
SQL>SELECT * FROM (SELECT * FROM EMP ORDER BY ENAME DESC)WHERE ROWNUM <10;

171) DISPLAY TOP 3 SALARIES FROM EMP;
SQL>SELECT SAL FROM ( SELECT * FROM EMP ORDER BY SAL DESC ) WHERE ROWNUM <4;
 
172)  DISPLAY 9th FROM THE EMP TABLE?
SQL>SELECT ENAME FROM EMPWHERE ROWID=(SELECT ROWID FROM EMP WHERE ROWNUM<=10 MINUS SELECT ROWID FROM EMP WHERE ROWNUM <10);

173) select second max salary from emp;
SQL> select max(sal) fromemp where sal<(select max(sal) from emp);

174) to fetch ALTERNATE records from a table. (EVEN NUMBERED)
SQL> select * from emp where rowid in (select decode(mod(rownum,2),0,rowid, null) from emp);

175) to select ALTERNATE records from a table. (ODD NUMBERED)
SQL> select * from emp where rowid in (select decode(mod(rownum,2),0,null ,rowid) from emp);
176) find the 3rd MAX salary in the emp table.
SQL> select distinct sal from emp e1 where 3 = (select count(distinct sal) from emp e2 where e1.sal <= e2.sal);

177) find the 3rd MIN salary in the emp table.
SQL> select distinct sal from emp e1 where 3 = (select count(distinct sal) from emp e2where e1.sal >= e2.sal);

178) Select FIRST n records from a table.
SQL> select * from emp where rownum <= &n;

179) Select LAST n records from a table
SQL> select * from emp minus select * from emp where rownum <= (select count(*) - &n from emp);
180) List dept no., Dept name for all the departments in which there are no employees in the department.
SQL> select * from dept where deptno not in (select deptno from emp);
alternate solution1:  select * from dept a where not exists (select * from emp b where a.deptno = b.deptno);
altertnate solution2:  select empno,ename,b.deptno,dname from emp a, dept b where a.deptno(+) = b.deptno and empno is null;

181) How to get nth max salaries ?
SQL> select distinct sal from emp a where &n =  (select count(distinct sal) from emp b where a.sal <= b.sal);

182) Select DISTINCT RECORDS from emp table.
SQL> select * from emp a where  rowid = (select max(rowid) from emp b where  a.empno=b.empno);

183) How to delete duplicate rows in a table?
SQL> delete from emp a where rowid != (select max(rowid) from emp b where  a.empno=b.empno);

184)  Count of number of employees in department wise.
SQL> select count(EMPNO), b.deptno, dname from emp a, dept b  where a.deptno(+)=b.deptno  group by b.deptno,dname;

184) suppose there is annual salary information provided by emp table. How to fetch monthly salary of each and every employee?
SQL> select ename,sal/12 as monthlysal from emp;

185) Select all record from emp table where deptno =10 or 40.
SQL> select * from emp where deptno=30 or deptno=10;
186) Select all record from emp table where deptno=30 and sal>1500.
SQL> select * from emp where deptno=30 and sal>1500;

187) Select  all record  from emp where job not in SALESMAN  or CLERK.
SQL> select * from emp where job not in ('SALESMAN','CLERK');

188) Select all record from emp where ename in 'BLAKE' ‘SCOTT’,'KING' and 'FORD'.
SQL> select * from emp where ename in('JONES','BLAKE','SCOTT','KING','FORD');

189) Select all records where ename starts with ‘S’ and its lenth is 6 char.
SQL> select * from emp where ename like 'S____';

190) Select all records where ename may be any no of  character but it should end with ‘R’.
SQL> select * from emp where ename like '%R';

191) Count  MGR and their salary in emp table.
SQL> select count(MGR),count(sal) from emp;

192) In emp table add comm+sal as totalsal .
SQL> select ename,(sal+nvl(comm,0)) as totalsal from emp;

193) Select  any salary <3000 from emp table. 
SQL> select * from emp  where sal> any(select sal from emp where sal<3000);

194) Select  all salary <3000 from emp table. 
SQL> select * from emp  where sal> all(select sal from emp where sal<3000);

195) select all the employee group by deptno and sal in descending order.
SQL> select ename,deptno,sal from emp order by deptno,sal desc;

196) How can I create an empty table emp1 with same structure as emp?
SQL> Create table emp1 as select * from emp where 1=2;

197) How to retrieve record where sal between 1000 to 2000?
SQL> Select * from emp where sal>=1000 And  sal<2000;

198) Select all records where dept no of both emp and dept table matches.
SQL> select * from emp where exists (select * from dept where emp.deptno=dept.deptno);

199) If there are two tables emp1 and emp2, and both have common record. How can I fetch all the recods but common records only once?
SQL> (Select * from emp) Union (Select * from emp1);

200) How to fetch only common records from two tables emp and emp1?
SQL> (Select * from emp) Intersect (Select * from emp1);

201) How can I retrive all records of emp1 those should not present in emp2?
SQL> (Select * from emp) Minus (Select * from emp1)

202) Count the totalsa  deptno wise where more than 2 employees exist.
SQL> SELECT  deptno, sum(sal) As totalsal FROM emp GROUP BY deptno
HAVING COUNT(empno) > 2;

203) how do I eliminate the duplicate rows?
SQL> delete from table_name where rowid not in (select max(rowid) from table group by duplicate_values_field_name);
Or
SQL> Delete duplicate_values_field_name dv from table_name ta where rowid <(select
min(rowid) from table_name tb where ta.dv=tb.dv);

204) How do I display row number with records?
SQL> Select rownum,emp.* from emp;

205) Display the records between two range?
SQL> select rownum, empno, ename from emp where rowid in (select rowid from emp where rownum<=&upto minus select rowid from emp where rownum<&Start);
Enter value for upto: 10
Enter value for Start: 7
206) I know the nvl function only allows the same data type(ie. number or char or date Nvl(comm, 0)), if commission is null then the text “Not Applicable” want to display,instead of blank space. How do I write the query?
SQL> select nvl(to_char(comm.),'NA') from emp;

207) find out nth highest salary from emp table?
SQL> SELECT DISTINCT (a.sal) FROM EMP A WHERE &N = (SELECT COUNT (DISTINCT (b.sal)) FROM EMP B WHERE a.sal<=b.sal);
or
SQL> SELECT * FROM (SELECT DISTINCT (SAL), DENSE_RANK () OVER (ORDER BY SAL DESC) AS RNK FROM EMP) WHERE RNK=&N
or
SQL> select min(sal) from (select distinct sal from emp order by sal desc) where rownum <=&n;

208) Find out nth highest salary DEPT wise from emp table?
SQL>  SELECT * FROM (SELECT DISTINCT(SAL),DENSE_RANK() OVER (PARTITION BY DEPTNO ORDER BY SAL DESC) AS RNK FROM EMP) WHERE RNK=&N;

209) Display Odd/ Even number of records?
Ans:
 Odd number of records:
SQL> select * from emp where (rowid,1) in (select rowid, mod(rownum,2) from emp);
Even number of records:
SQL> select * from emp where (rowid,0) in (select rowid, mod(rownum,2) from emp);

210) What are the more common pseudo-columns?
Ans: SYSDATE, USER , UID, CURVAL, NEXTVAL, ROWID, ROWNUM

211) How To Display last 5 records in a table?
SQL>  select * from (select rownum r, emp.* from emp) where r between (Select count(*)-5 from emp) and (Select count(*) from emp)

212) How to Display last record in a table?
SQL>  select * from (select rownum r, emp.* from emp) where r in (Select count(*) from emp)

213) How to Display particular nth record in a table?
SQL> select * from (select rownum r, emp.* from emp) where r in (2) or r=2;

214) How to Display even or odd records in a table?
SQL> select * from (select emp.* , rownum r from emp) where mod (r,2)=0;

215) what is the difference between a HAVING CLAUSE and a WHERE CLAUSE?
Ans: Specifies a search condition for a group or an aggregate. HAVING can be used only with the SELECT statement. HAVING is typically used in a GROUP BY clause. When GROUP BY is not used, HAVING behaves like a WHERE clause. Having Clause is basically used only with the GROUP BY function in a query. WHERE Clause is applied to each row before they are part of the GROUP BY function in a query.

216)  what is sub-query? Explain properties of sub-query?
Ans: Sub-queries are often referred to as sub-selects, as they allow a SELECT statement to be executed arbitrarily within the body of another SQL statement. A sub-query is executed by enclosing it in a set of parentheses. Sub-queries are generally used to return a single row as an atomic value, though they may be used to compare values against multiple rows with the IN keyword.
A subquery is a SELECT statement that is nested within another T-SQL statement. A subquery SELECT statement if executed independently of the T-SQL statement, in which it is nested, will return a result set. Meaning a subquery SELECT statement can standalone and is not depended on the statement in which it is nested. A subquery SELECT statement can return any number of values, and can be found in, the column list of a SELECT statement, a FROM, GROUP BY, HAVING, and/or ORDER BY clauses of a T-SQL statement. A Subquery can also be used as a parameter to a function call. Basically a subquery can be used anywhere an expression can be used.

217) Properties of Sub-Query
Ans: A subquery must be enclosed in the parenthesis.
A subquery must be put in the right hand of the comparison operator, and
A subquery cannot contain a ORDER-BY clause.
A query can contain more than one sub-queries.

218)  what are types of sub-queries?
Ans: Single-row sub query, where the sub query returns only one row. Multiple-row sub query, where the sub query returns multiple rows,. And multiple column sub query, where the sub query returns multiple columns.

219) what is the output for query select * from emp where rownum<=3
Ans: it display first 3 Records

220) what is the output for query select * from emp where rownum=1;
Ans: it display first Record in the table

221) what is the output for query select * from emp where rownum=2;
Ans: it will not display any record

222) what is the output for query select * from emp where rownum>1;
Ans: even this also will not display the records. why because when it fetch the first record rownum is 1 so condition fail so it will not get first record when it fetches 2nd record rownum is again 1 because it didn't pick up first record so 2nd time also condition failed.

223) How to display Top N salaries in emp?
SQL>  select * from (select distinct sal from emp order by sal desc) where rownum<=&n;

224) How To display Last Record in emp table?
SQL> Select * from ( select rownum as rn,emp.* from emp) where rn in(select count(*) from emp);

225) How To display First and last Records in emp table?
SQL>  select * from ( select rownum as rn,emp.* from emp) where rn in(1,(select count(*) from emp));

226) How to Diplay 1,5,8 records in emp table?
SQL> select * from ( select rownum as rn,emp.* from emp) where rn in (1,5,8);

227) In Oracle, can we add a Not Null column to a table with data? If "No" then how can we do that?
Ans: No, we cannot add a Not Null column to a table with data. Oracle throws Error ORA-01758.
Eg: alter table EMP add comm2 number not null
Error: ORA-01758: table must be empty to add mandatory (NOT NULL) column.
Workaround:
Provide a Default value to the column being added, along with the NOT NULL constraint. Then the column will get added with the default value for all existing rows.
Eg: alter table EMP add comm2 number not null default 100 -- Comm2 will have 100 for all rows

228) While doing an ascending order sort on a column having NULL values, where does the NULLs show up in the result set? At the beginning or at the end?
Ans: Ascending order sort - NULLs come last because Oracle treats NULLs are the largest possible values Descending order sort - NULLs come first
* How to make NULLs come last in descending order sort?
Add NULLS LAST to the order by desc clause
Eg: select col1 from table1 order by col1 desc NULLS LAST

229) how to set Time of execution of an SQL Statement?
Ans: first run this in sql prompt: set timing on After execution of each query we get the time take for it if you don't want run this: set timing off

230) what is the Datatype of NULL in Oracle?
Ans: Datatype of NULL is "char(0)" and size is '0'

231) Oracle Functions - Replace versus Trim?
SQL> select replace('jose. antony@ yahoo.com',' ', null) as Replace1 from dual;
REPLACE1
--------------------
jose.antony@yahoo.com  --Removes all spaces from in-between
SQL> select trim('jose. antony@ yahoo.com') as Trim1 from dual;
TRIM1
----------------------
jose. antony@ yahoo.com --Removes spaces from both sides only

232) Explain ROWID in Oracle?
Ans: ROWID is a unique hexadecimal value which Oracle inserts to identify each record being inserted. It is used for all Full Table scans.
Structure:
OOOOOOFFFBBBBBBRRR
OOOOOO - First six characters is the Object Number which identities the Data Segment
FFF - Next 3 characters is the Database File number
BBBBBB - Next 6 characters shows the Data Block number
RRR -Next 3 characters identified the Row within the block

233) what is difference between Co-related sub query and nested sub query?
Ans: Correlated subquery runs once for each row selected by the outer query. It contains a reference to a value from the row selected by the outer query.
Nested subquery runs only once for the entire nesting (outer) query. It does not contain any reference to the outer query row.
Correlated Subquery:
SQL> select e1.empname, e1.basicsal, e1.deptno from emp e1 where e1.basicsal = (select max(basicsal) from emp e2 where e2.deptno = e1.deptno)
Nested Subquery:
SQL> select empname, basicsal, deptno from emp where (deptno, basicsal) in (select deptno, max(basicsal) from emp group by deptno)

234) what is the difference between TRUNCATE and DELETE commands?
Ans: Both will result in deleting all the rows in the table .TRUNCATE call cannot be rolled back as it is a DDL command and all memory space for that table is released back to the server. TRUNCATE is much faster. Whereas DELETE call is an DML command and can be rolled back.

235) How to find out the duplicate column
SQL> select column_name, count (*) from table_name having count (*)>1
if the result more than 1 then we can say that this column having duplicate records

236) How to find 2nd max salary from EMP?
SQL> select max (sal) from EMP where sal not in (select max (sal) from EMP)

237) How to find max salary department wise in EMP table?
SQL> select deptno,max(sal) from emp group by deptno;

238) how to find 2nd max salary department wise in EMP table?
SQL> Select deptno,max(sal) from emp where (deptno,sal) not in(select deptno,max(sal) from emp group by deptno) group by deptno;

239) Table1 having 10 records and table2 having 10 records both tables having 5 matching records. Then how many records will display in
1. Equi join
2. Left outer join
3. Right outer join
4. Full outer join
Ans:
1. In equi join matching records will display it means 5records will display
2. In left outer join matching 5 and non-matching 5 records in left table so total 10 will display
3. In right outer join matching 5 and non-matching 5 records in right table so total 10 will display.
4. In full outer join matching 5 and non-matching 5 records in left table and non-matching records in right table so total 15 will display

239) EMP table, for those emp whose Hiredate is same, update their sal by "sal+500" or else for others keep the sal as it is, how to do it by SQL query?

SQL> UPDATE EMP SET sal=sal+500 WHERE hiredate IN (SELECT hiredate FROM employees HAVING COUNT (*)>1 GROUP BY hiredate)