You are on page 1of 14

SQL QUERIES 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 totalsalary for all the employees


SQL>select empno,ename,sal,comm, sal+nvl(comm,0) as"total emp

salary" from

6) Display the employee name and annual salary for all employees.

SQL>select ename, 1 *(sal+nvl(comm,0)) as "ann!al Sal" from emp

7) Display the names of all the employees who are working in depart number 10.
SQL>select emame from emp "#ere 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 "#ere job$%&L'()% and sal>*000;

9) Display the employee number and name

who are earning comm. SQL>select empno,ename from emp "#ere comm +s not n!ll;

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 "#ere job$%&L'()% ,( -,.$%S/L'S0/1% ,( -,.$%/1/L2S3% /14 S/L>*000;

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')> !; 13) Display the list of employees who ha e joined the company before
30!"#$!%0 or after 31!D&'!%0. SQL>select ename from emp "#ere #+redate 5 6*07-8171990: or #+redate > "#$-%&'-()*;

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 ta+; 17) Display the name of the current user. SQL>show user 18) Display the names of employees working in depart number 10 or *0 or +0 or
employees working as ',&-./,/0,&/10$ or 0$0,2/3.

SQL>select ename from emp where deptno in($),,),-)) or .o+ in("'L&/0S*,*S1L&S213*,*131LYS4*); 19) Display the names of employees whose name starts with alaphabet /. SQL>select ename from emp where ename li5e "S6*; 20) Display the &mployee names for employees whose name ends with alaphabet
/.

SQL>select ename from emp where ename li5e "6S*; 21) Display the names of employees whose names ha e second alphabet 0 in
their names.

SQL>select ename from emp where ename li5e "_16*; 22) select the names of the employee whose names is e4actly fi e characters
in length.

SQL>select ename from emp where len7th(ename) !; 23) Display the names of the employee who are not working as 10$05&-/. SQL>select ename from emp where .o+ not in("21318&/* ); 24) Display the names of the employee who are not working as /0,&/10$ 6',&-. 6- 0$0,2/3.

SQL>select ename from emp where .o+ not in("S1L&S213*,*'L&/0*,*131LYS4*); 25) Display all rows from emp table.3he system should wait after e ery
screen full of informaction.

SQL>set pause on

26) Display the total number of employee working in the company. SQL>select count(9) from emp; 27) Display the total salary beiging paid to all employees. SQL>select sum(sal) from emp; 28) Display the ma4imum salary from emp table. SQL>select ma:(sal) from emp; 29) Display the minimum salary from emp table. SQL>select min(sal) from emp; 30) Display the a erage salary from emp table. SQL>select a;7(sal) from emp; 31) Display the ma4imum salary being paid to ',&-.. SQL>select ma:(sal) from emp where .o+ *'L&/0*;

32) Display the ma4imum salary being paid to depart number *0. SQL>select ma:(sal) from emp where deptno ,); 33) Display the minimum salary being paid to any /0,&/10$. SQL>select min(sal) from emp where .o+ *S1L&S213*; 34) Display the a erage salary drawn by 10$05&-/. SQL>select a;7(sal) from emp where .o+ *21318&/* ; 35) Display the total salary drawn by 0$0,2/3 working in depart number +0. SQL>select sum(sal) from emp where .o+ *131LYS4* and deptno$;0; 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 +y sal; 37) Display the names of the employee in descending order of salary. SQL>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 +y 40) Display the name of the employee along with their annual SQL>select ename,sal9$, from emp order +y sal desc;
salary(sal71*).3he name of the employee earning highest annual salary should apper first.

41) Display name,salary,hra,pf,da,total salary for each employee. 3he


output should be in the order of total salary,hra 158 of salary,da 108 of salary,pf 58 salary,total salary will be(salary9hra9da)!pf.

SQL>select ename,sal,sal<$))9$! as hra,sal<$))9! as pf,sal<$))9$) as da, sal+sal<100*1=+sal<100*107sal<100*= as total from emp; 42) Display depart numbers and total number of employees working in each
department.

SQL>select deptno,count(deptno)from emp 7roup +y deptno; 43) Display the


group. arious jobs and total number of employees within each job

SQL>select .o+,count(.o+)from emp 7roup +y .o+; 44) Display the depart numbers and total salary for each department. SQL>select deptno,sum(sal) from emp 7roup +y deptno; 45) Display the depart numbers and ma4 salary for each department. SQL>select deptno,ma:(sal) from emp 7roup +y deptno; 46) Display the arious jobs and total salary for each job SQL>select .o+,sum(sal) from emp 7roup +y .o+; 47) Display the
arious jobs and total salary for each job

SQL>select .o+,min(sal) from emp 7roup +y .o+; 48) Display the depart numbers with more than three employees in each dept. SQL>select deptno,count(deptno) from emp 7roup +y deptno ha;in7 count(9)>#; 49) Display the
arious jobs along with total salary for each of the jobs where total salary is greater than +0000.

SQL>select .o+,sum(sal) from emp 7roup +y .o+ ha;in7 sum(sal)>-)))); 50) Display the
arious jobs along with total number of employees in each job.3he output should contain only those jobs with more than three employees.

SQL>select .o+,count(empno) from emp 7roup +y .o+ ha;in7 count(.o+)># 51) Display the name of the empployee who earns highest salary. SQL>select ename from emp where sal (select ma:(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 where .o+ ''L&/0' and sal (select ma:(sal) from emp where .o+ ''L&/0'); 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 .o+ 'S1L&S213' and sal>(select ma:(sal) from emp where .o+ ''L&/0'); 54) Display the names of clerks who earn a salary more than the lowest
salary of any salesman.

SQL>select ename from emp where .o+ ''L&/0' and sal>(select min(sal) from emp
"#ere job$%S/L'S0/1%);

Display the names ! empl yees "h ea#n a sala#y m #e than that ! $ nes # that ! sala#y %#ethe# than that of scott. SQL>select ename,sal from emp where sal> (select sal from emp where ename '=>3&S')and sal> (select sal from emp where ename 'S'>44'); 55) Display the names of the employees who earn highest salary in their
respecti e departments.

SQL>select ename,sal,deptno from emp where sal in(select ma:(sal) from emp 7roup +y deptno); 56) Display the names of the employees who earn highest salaries in their
respecti e job groups.

SQL>select ename,sal,.o+ from emp where sal in(select ma:(sal) from emp 7roup +y .o+) 57) Display the employee names who are working in accounting department. SQL>select ename from emp where deptno (select deptno from dept where dname '1''>?34@38') 58) Display the employee names who are working in 'hicago.

SQL>select ename from emp where deptno (select deptno from dept where L>' ''A@'18>') 59) Display the "ob groups ha ing total salary greater than the ma4imum
salary for managers.

SQL>S&L&'4 =>B,S?2(S1L) C/>2 &2D 8/>?D BY =>B A1E@38 S?2(S1L)>(S&L&'4 21F(S1L) C/>2 &2D GA&/& =>B '21318&/'); 60) Display the names of employees from department number 10 with salary
grether than that of any employee working in other department.

SQL>select ename from emp where deptno $) and sal>any(select sal from emp where deptno not in $))H 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 $) and sal>all(select sal from emp where deptno not in $))H 62) Display the names of the employees in #ppercase. SQL>select upper(ename)from emp 63) Display the names of the employees in ,owecase. SQL>select lower(ename)from emp 64) Display the names of the employees in :ropercase. SQL>select initcap(ename)from emp; 65) Display the length of 2our name using appropriate function. SQL>select len7th('name') from dual 66) Display the length of all the employee names. SQL>select len7th(ename) from emp; 67) select name of the employee concatenate with employee number. SQL>select enameIIempno from emp; 68) #ser appropriate function and e4tract 3 characters starting from *
characters from the following ;ac;. string ;6racle;. i.e the out put should be

SQL>select su+str('oracle',#,,) from dual 69) <ind the <irst occurance of character ;a; from the following string i.e
;'omputer 1aintenance 'orporation;.

SQL>S&L&'4 @3S4/(''omputer 2aintenance 'orporation','a',$) C/>2 %?1L 70) -eplace e ery occurance of alphabhet 0 with = in the string 0llens(use
translate function)

SQL>select translate('1llens','1','B') from dual 71) Display the informaction from emp table.>here job manager is found it
should be displayed as boos(#se replace function).

SQL>select replace(=>B,'21318&/','B>SS') C/>2 &2D; 72) Display empno,ename,deptno from emp table.?nstead of display department
numbers display the related department name(#se decode function).

SQL>select

empno,ename,decode(deptno,10,%/&&,813>1?%, 0,%('S'/(&@%,*0,%S/L'S%,;0,%,A(/3> ,1S%) from emp;

73) Display your age in days. SQL>select to_date(sysdate)-to_date('$)-sep-JJ')from dual 74) Display your age in months. SQL>select months_+etween(sysdate,'$)-sep-JJ') from dual 75) Display the current date as 15th 0ugest <riday $ineteen $inety /a en. SQL>select to_char(sysdate,'ddth 2onth day year') from dual 76) Display the following output for each row from emp table. s& tt has ' ine( the & mpany n "e(nes(ay 13th )*%*st ninten nintey+ SQL>select &312&II' A1S =>@3&% 4A& '>2D13Y >3 'IIto_char(A@/&%14&,'day ddth 2onth year') from &2D; 77) <ind the date for nearest saturday after current date. SQL>S&L&'4 3&F4_%1Y(SYS%14&,'S14?/%1Y')C/>2 %?1L; 78) Display current time. SQL>select to_char(sysdate,'hhK22Kss') from dualH 79) Display the date three months =efore the current date. SQL>select add_months(sysdate,#) from dual; 80) Display the common jobs from department number 10 and *0. SQL>select .o+ from emp where deptno $) and .o+ in(select .o+ from emp where deptno ,)); 81) Display the jobs found in department 10 and *0 &liminate duplicate jobs. SQL>select distinct(.o+) from emp where deptno $) or deptno ,) (or) SQL>select distinct(.o+) from emp where deptno in($),,)); 82) Display the jobs which are uni@ue to department 10. SQL>select distinct(.o+) from emp where deptno $) 83) Display the details of those who do not ha e any person working under
them.

SQL>select eHename from emp,emp e where empHm7r eHempno 7roup +y eHename ha;in7 count(9) $; 84) Display the details of those employees who are in sales department and
grade is 3.

SQL>select 9 from emp where deptno (select deptno from dept where dname 'S1L&S')and sal +etween(select losal from sal7rade where 7rade #)and (select hisal from sal7rade where 7rade #); 85) Display those who are not managers and who are managers any one.
i)display the managers names

SQL>select distinct(mHename) from emp e,emp m where mHempno eHm7r;

ii)(isplay the "h a#e n t mana%e#s SQL>select ename from emp where ename not in(select distinct(mHename) from emp e,emp m where mHempno eHm7r); 86) Display those employee whose name contains not less than + characters. SQL>select ename from emp where len7th(ename)>-; 87) Display those department whose name start with A/A while the location
name ends with A.A.

SQL>select dname from dept where dname li5e 'S6' and loc li5e '60'; 88) Display those employees whose manager name is "6$&/. SQL>select pHename from emp e,emp p where eHempno pHm7r and eHename '=>3&S'; 89) Display those employees whose salary is more than 3000 after gi ing *08
increment.

SQL>select ename,sal from emp where (salLsal9H,)>#))); 90) Display all employees while their dept names; SQL>select ename,dname from emp,dept where empHdeptno deptHdeptno 91) Display ename who are working in sales dept. SQL>select ename from emp where deptno (select deptno from dept where dname 'S1L&S');

92) Display employee name,deptname,salary and comm for those sal in between
*000 to 5000 while location is chicago.

SQL>select ename,dname,sal,comm from emp,dept where sal +etween ,))) and !))) and loc ''A@'18>' and empHdeptno deptHdeptno; 93)Display those employees whose salary greter than his manager salary. SQL>select pHename from emp e,emp p where eHempno pHm7r and pHsal>eHsal 94) Display those employees who are working in the same dept where his
manager is work.

SQL>select pHename from emp e,emp p where eHempno pHm7r and pHdeptno eHdeptno; 95) Display those employees who are not working under any manager. SQL>select ename from emp where m7r is null 96) Display grade and employees name for the dept no 10 or 30 but grade is
not + while joined the company before 31!dec!B*.

SQL>select ename,7rade from emp,sal7rade where sal +etween losal and hisal and deptno +n(10,*0) and Brade5>; and #+redate5%*174'&7C %; 97) #pdate the salary of each employee by 108 increment who are not
eligiblw for commission.

SQL>update emp set sal salLsal9$)<$)) where comm is null;

98) /&,&'3 those employee who joined the company before 31!dec!B* while their
dept location is newyork or 'hicago.

SQL>S&L&'4 &2D3>,&312&,A@/&%14&,%312&,L>' C/>2 &2D,%&D4 GA&/& (&2DH%&D43> %&D4H%&D43>)13% A@/&%14& M'#$-%&'-N,' 13% %&D4HL>' @3(''A@'18>','3&G Y>/0'); 99) D?/:,02 &1:,62&& $01&,"6=,D&:0-31&$3,,6'03?6$ <6- 0,, >C6 0-& >6-.?$5 0/
10$05&-D

SQL>select ename,=>B,%312&,L>'14@>3 from emp,%&D4 where m7r is not null; 100) D?/:,02 3C6/& &1:,62&&/ >C6/& 10$05&- $01& ?/ "6$&/D !! SQL> S&L&'4 DH&312& C/>2 &2D &, &2D D GA&/& &H&2D3> DH28/ 13% &H&312& '=>3&S'; 101) Display name and salary of ford if his salary is e@ual to hisal of his
grade E0$D 0,/6 D?/:,02 3C&?- 10$05&- $01&FD

SQL>select ename,sal,Brade from emp,salBrade "#ere sal bet"een losal and


#+sal and ename $%D,(4% /14 @>S/L$S/L;

102) Display employee name,job,depart name ,manager name,his grade and make
out an under department wiseD

SQL>S&L&'4 &H&312&,&H=>B,%312&,&2DH&312&,8/1%& C/>2 &2D,&2D &,S1L8/1%&,%&D4 GA&/& &2DHS1L B&4G&&3 L>S1L 13% A@S1L 13% &2DH&2D3> &H28/ 13% &2DH%&D43> %&D4H%&D43> >/%&/ BY %312& 103) ,ist out all employees name,job,salary,grade and depart name for e ery
one in the company e4cept ;',&-.;./ort on salary display the highest salaryD

SQL>S&L&'4 &312&,=>B,%312&,S1L,8/1%& C/>2 &2D,S1L8/1%&,%&D4 GA&/& S1L B&4G&&3 L>S1L 13% A@S1L 13% &2DH%&D43> %&D4H%&D43> 13% =>B 3>4 @3(''L&/0')>/%&/ BY S1L 1S'; 104) Display the employee name,job and his manager.Display also employee who
are without managerD

SQL>select eHename,eH.o+,e2DHename 1S 2ana7er from emp,emp e where empHempno(L) eHm7r 105) <ind out the top 5 earners of companyD SQL>S&L&'4 %@S4@3'4 S1L C/>2 &2D & GA&/& !> (S&L&'4 '>?34(%@S4@3'4 S1L) C/>2 &2D 1 GA&/& 1HS1L> &HS1L)>/%&/ BY S1L %&S'; 106) Display name of those employee who are getting the highest salaryD SQL>select ename from emp where sal (select ma:(sal) from emp); 107) Display those employee whose salary is e@ual to a erage of ma4imum and
minimumD

SQL>select ename from emp where sal (select ma:(sal)Lmin(sal)<, from emp);

108) /elect count of employee in each department where count greater than 3D SQL>select count(9) from emp 7roup +y deptno ha;in7 count(deptno)># 109) Display dname where at least 3 are working and display only department
nameD

SQL>select distinct dHdname from dept d,emp e where dHdeptno eHdeptno and #>any (select count(deptno) from emp 7roup +y deptno) 110) Display name of those managers name whose salary is more than a erage
salary of his companyD

SQL>S&L&'4 &H&312&,&2DH&312& C/>2 &2D,&2D & GA&/& &2DH&2D3> &H28/ 13% &HS1L>(S&L&'4 1E8(S1L) C/>2 &2D);

111)Display those managers name whose salary is more than a erage salary of
his employeeD

SQL>S&L&'4 %@S4@3'4 &2DH&312& C/>2 &2D,&2D & GA&/& &HS1L M(S&L&'4 1E8(&2DHS1L) C/>2 &2D GA&/& &2DH&2D3> &H28/ 8/>?D BY &2DH&312&) 13% &2DH&2D3> &H28/;

112) Display employee name,sal,comm and net pay for those employee
whose net pay is greter than or e@ual to any other employee salary of the companyD

SQL>select ename,sal,comm,salLn;l(comm,)) as 3etDay from emp where salLn;l(comm,)) >any (select sal from emp) 113) Display all employees names with total sal of company with each
employee nameD

SQL>S&L&'4 &312&,(S&L&'4 S?2(S1L) C/>2 &2D) C/>2 &2D; 114) <ind out last 5(least)earners of the company.D SQL>S&L&'4 %@S4@3'4 S1L C/>2 &2D & GA&/& !> (S&L&'4 '>?34(%@S4@3'4 S1L) C/>2 &2D 1 GA&/& 1HS1LM &HS1L) >/%&/ BY S1L %&S'; 115) <ind out the number of employees whose salary is greater than their
manager salaryD

SQL>S&L&'4 &H&312& C/>2 &2D ,&2D & GA&/& &2DH&2D3> &H28/ 13% &2DHS1LM&HS1L; 116) Display those department where no employee workingD SQL>select dname from emp,dept where empHdeptno not in(empHdeptno) 117) Display those employee whose salary is 6DD SQL>select 9 from emp where salM);
alueD

118) Display those employee whose salary contains alleast 3 digitsD

SQL>select 9 from emp where len7th(sal)> #; 119) Display those employee who joined in the company in the month of DecD SQL>select ename from emp where to_char(hiredate,'2>3') '%&''; 120) Display those employees whose name contains A0AD SQL>select ename from emp where instr(ename,'1')>); or SQL>select ename from emp where ename li5e('616'); 121) Display those employee whose deptno is a ailable in salaryD SQL>select empHename from emp, emp e where empHsal eHdeptno; 122) Display those employee whose first * characters from hiredate !last *
characters of salaryD

SQL>select ename,S?BS4/(hiredate,$,,)II&312&IIsu+str(sal,-,,,) from emp 123) Display those employee whose 108 of salary is e@ual to the year of
joiningD

SQL>select ename from emp where to_char(hiredate,'YY') sal9)H$; 124) Display those employee who are working in sales or researchD SQL>S&L&'4 &312& C/>2 &2D GA&/& %&D43> @3(S&L&'4 %&D43> C/>2 %&D4 GA&/& %312& @3('S1L&S','/&S&1/'A')); 125) Display the grade of jonesD SQL>S&L&'4 &312&,8/1%& C/>2 &2D,S1L8/1%& GA&/& S1L B&4G&&3 L>S1L 13% A@S1L 13% &name '=>3&S'; 126) Display those employees who joined the company before 15 of the monthD SQL>select ename from emp "#ere toEc#ar(#+redate,%44%)51=; 127) Display those employee who has joined before 15th of the month. SQL>select ename from emp "#ere toEc#ar(#+redate,%44%)51=; 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 7roup +y deptno ha;in7 count(deptno)M#); 129) Display the name of the department where no employee working. SQL> S&L&'4 &H&312&,&H=>B,2H&312&,2H=>B C/>2 &2D &,&2D 2 GA&/& &H28/ 2H&2D3> 130) Display those employees who are working as manager. SQL>S&L&'4 2H&312& 21318&/ C/>2 &2D 2 ,&2D & GA&/& &H28/ 2H&2D3> 8/>?D BY 2H&312& 131) Display those employees whose grade is e@ual to any number of sal but
not e@ual to first number of salD

SQL> S&L&'4 &312&,8/1%& C/>2 &2D,S1L8/1%& GA&/& 8/1%& 3>4 @3(S&L&'4 S?BS4/(S1L,),$)C/>2 &2D)

132) :rint the details of all the employees who are /ub!ordinate to =,0.&D SQL>select empHename from emp, emp e where empHm7r eHempno and eHename 'BL10&'; 133) Display employee name and his salary whose salary is greater than
highest a erage of department numberD

SQL>S&L&'4 S1L C/>2 &2D GA&/& S1L>(S&L&'4 21F(1E8(S1L)) C/>2 &2D 8/>?D BY %&D43>); 134) Display the 10th record of emp table(without using rowid) SQL>S&L&'4 9 C/>2 &2D GA&/& />G3?2M$$ 2@3?S S&L&'4 9 C/>2 &2D GA&/& />G3?2M$) 135) Display the half of the ename;s in upper case and remaining lowercaseD SQL>S&L&'4 S?BS4/(L>G&/(&312&),$,#)IIS?BS4/(?DD&/(&312&),#,L&384A(&312&)) C/>2 &2D; 136) Display the 10th record of emp table without using group by and rowidD SQL>S&L&'4 9 C/>2 &2D GA&/& />G3?2M$$ 2@3?S S&L&'4 9 C/>2 &2D GA&/& />G3?2M$) %elete the $)th record of emp ta+leH SQL>%&L&4& C/>2 &2D GA&/& &2D3> (S&L&'4 &2D3> C/>2 &2D GA&/& />G3?2M$$ 2@3?S S&L&'4 &2D3> C/>2 &2D GA&/& />G3?2M$)) 137) 'reate a copy of emp table; SQL>create ta+le new_ta+le as select 9 from emp where $ ,; 138) /elect ename if ename e4ists more than once. SQL>select ename from emp e 7roup +y ename ha;in7 count(9)>$; 139) Display all enames in re erse orderD(/1?3CGC3?1/). SQL>S&L&'4 /&E&/S&(&312&)C/>2 &2D; 140) Display those employee whose joining of month and grade is e@ual. SQL>S&L&'4 &312& C/>2 &2D GA&/& S1L B&4G&&3 (S&L&'4 L>S1L C/>2 S1L8/1%& GA&/& 8/1%& 4>_'A1/(A@/&%14&,'22')) 13% (S&L&'4 A@S1L C/>2 S1L8/1%& GA&/& 8/1%& 4>_'A1/(A@/&%14&,'22')); 141) Display those employee whose joining D03& is a ailable in deptno. SQL>S&L&'4 &312& C/>2 &2D GA&/& 4>_'A1/(A@/&%14&,'%%') %&D43> 142) Display those employees name as follows 1 1LL&3 B BL10&

SQL> S&L&'4 S?BS4/(&312&,$,$),&312& C/>2 &2D; 143) ,ist out the employees ename,sal,:<(*08 6< /0,) from emp; SQL>S&L&'4 &312&,S1L,S1L9H, 1S DC C/>2 &2D; 144) 'reate table emp with only one column empno; SQL>'reate ta+le emp as select empno from emp where $ ,; 145) 0dd this column to emp table ename rachar*(*0). SQL>alter ta+le emp add(ename ;archar,(,))); 146) 6ops ? forgot gi e the primary key constraint. SQL>alter ta+le emp add primary 5ey(empno);
0dd in now.

147) $ow increase the length of ename column to 30 characters. SQL>alter ta+le emp modify(ename ;archar,(#))); 148) 0dd salary column to emp table. SQL>alter ta+le emp add(sal num+er($))); 149) ? want to gi e a
alidation saying that salary cannot be greater 10,000 (note gi e a name to this constraint)

SQL>alter ta+le emp add constraint ch5_))$ chec5(salM $))))) 150) <or the time being ? ha e decided that ? will not impose this
alidation.1y boss has agreed to pay more than 10,000.

SQL>a7ain alter the ta+le or drop constraint with alter table emp drop
constra+nt c#FE001 (or)4+sable t#e constra+nt by !s+nB mod+fy constra+nt c#FE001 d+sable;

alter table emp

151) 1y boss has changed his mind.

$ow he doesn;t want to pay more than 10,000.so re oke that salary constraint.

SQL>alter ta+le emp modify constraint ch5_))$ ena+le; 152) 0dd column called as mgr to your emp table; SQL>alter ta+le emp add(m7r num+er(!)); 153) 6hH 3his column should be related to empno.
constraint. 5i e a command to add this

SQL>1L4&/ 41BL& &2D 1%% '>3S4/1@34 28/_%&D4 C>/&@83 0&Y(28/) /&C&/&3'&S &2D(&2D3>) 154) 0dd deptno column to your emp table; SQL>alter ta+le emp add(deptno num+er(!)); 155) 3his deptno column should be related to deptno column of dept table; SQL>alter ta+le emp add constraint dept_))$ forei7n 5ey(deptno) reference dept(deptno)
Gdeptno s#o!ld be pr+mary FeyH

156) 5i e the command to add the constraint. SQL>alter ta+le Mta+le_name) add constraint Mconstraint_name> Mconstraint type>

157) 'reate table called as newemp.

#sing single command create this table as well as get data into this table(use create table as);

SQL>create ta+le newemp as select 9 from emp; ,#eate ta-le &alle( as ne"emp+ .his ta-le sh *l( & ntain nly empn /ename/(name+ SQL>create ta+le newemp as select empno,ename,dname from emp,dept where $ ,; 158) Delete the rows of employees who are working in the company for more
than * years.

SQL>delete from emp where (sysdate-hiredate)<#O!>,; 159) :ro ide a commission(108 'omm 6f /al) to employees who are not earning
any commission.

SQL>select sal9)H$ from emp where comm is null 160) ?f any employee has commission his commission should be incremented by
108 of his salary.

SQL>update emp set comm sal9H$ where comm is not null; 161) Display employee name and department name for each employee. SQL>select empno,dname from emp,dept where empHdeptno deptHdeptno 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 empHdeptno deptHdeptno; 163) Display ename,dname e en if there are no employees working in a
particular department(use outer join).

SQL>select ename,dname from emp,dept where empHdeptno deptHdeptno(L) 164) Display employee name and his manager name. SQL>select pHename,eHename from emp e,emp p where eHempno pHm7r; 165) Display the department name and total number of employees in each
department.

SQL>select dname,count(ename) from emp,dept where empHdeptno deptHdeptno 7roup +y dname; 166)Display the department name along with total salary in each department. SQL>select dname,sum(sal) from emp,dept where empHdeptno deptHdeptno 7roup +y dname; 167) Display itemname and total sales amount for each item. SQL>select itemname,sum(amount) from item 7roup +y itemname; 168) >rite a Iuery 3o Delete 3he -epeted -ows from emp table; SQL>%elete from emp where rowid not in(select min(rowid)from emp 7roup +y ename)

169) 36 D?/:,02 5 36 J -6>/ <-61 0 30=,& SQL>select ename from emp where rowid in(select rowid from emp where rownumM J minus select rowid from empi where rownumM!) 170) D?/:,02 36: $ -6>/ <-61 30=,&D SQL>S&L&'4 9 C/>2 (S&L&'4 9 C/>2 &2D >/%&/ BY &312& %&S') GA&/& />G3?2 M$); 171) D?/:,02 36: 3 /0,0-?&/ <-61 &1:; SQL>S&L&'4 S1L C/>2 ( S&L&'4 9 C/>2 &2D >/%&/ BY S1L %&S' ) GA&/& />G3?2 M172) D?/:,02 %th <-61 3C& &1: 30=,&D SQL>S&L&'4 &312& C/>2 &2D GA&/& />G@% (S&L&'4 />G@% C/>2 &2D GA&/& />G3?2M $) 2@3?S S&L&'4 />G@% C/>2 &2D GA&/& />G3?2 M$)) select second ma: salary from emp; select ma:(sal) fromemp where salM(select ma:(sal) from emp); ------999999------

You might also like