DBMS (Main)
DBMS (Main)
Table rakjay
Eno 7369 7499 7521 7566 7654 7698 7782 7782 7902 Ename Sunita Ashok Rohit Jyoti Martin Binod Chetan Sudhir Sumit vats Job Clerk Salesman Salesman Manager salesman Manager Manager Analyst Analyst Mgr 7902 7698 7698 7839 7698 7839 7839 7566 7566 Hierdate 17-dec-80 20-feb-81 22-feb-81 02-apr-81 28-sep-81 01-may-81 09-jun-81 19-apr-87 03-dec-81 Sal 2800 3600 5250 4975 6250 5850 2450 5000 3500 Comm. 300 500 1400 Dept 20 30 30 20 30 30 10 20 20
3600
Enter value for mgr: 7698 Enter value for hierdate: 20-feb-81 Enter value for sal: 3600 Enter value for comm: 300 Enter value for dept: 30 old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept') new 1: insert into rakjay values('ashok','salesman','7499','7698','20-feb-81','3600','300','30') 1 row created. SQL> / Enter value for ename: rohit Enter value for job: salesman Enter value for eno: 7521 Enter value for mgr: 7698 Enter value for hierdate: 22-feb-81 Enter value for sal: 5250 Enter value for comm: 500 Enter value for dept: 30 old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept') new 1: insert into rakjay values('rohit','salesman','7521','7698','22-feb-81','5250','500','30') 1 row created. SQL> / Enter value for ename: jyoti Enter value for job: manager Enter value for eno: 7566 Enter value for mgr: 7839 Enter value for hierdate: 02-apr-81 Enter value for sal: 4975 Enter value for comm: Enter value for dept: 20 old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept') new 1: insert into rakjay values('jyoti','manager','7566','7839','02-apr-81','4975','','20') 1 row created. SQL> / Enter value for ename: martin Enter value for job: salesman Enter value for eno: 7654 Enter value for mgr: 7698 Enter value for hierdate: 28-sep-81 Enter value for sal: 6250 Enter value for comm: 1400 Enter value for dept: 30 old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept') new 1: insert into rakjay values('martin','salesman','7654','7698','28-sep-81','6250','1400','30') 1 row created. SQL> / Enter value for ename: binod
3
Enter value for job: manager Enter value for eno: 7698 Enter value for mgr: 7839 Enter value for hierdate: 01-may-81 Enter value for sal: 5850 Enter value for comm: Enter value for dept: 30 old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept') new 1: insert into rakjay values('binod','manager','7698','7839','01-may-81','5850','','30') 1 row created. SQL> / Enter value for ename: chetan Enter value for job: manager Enter value for eno: 7782 Enter value for mgr: 7839 Enter value for hierdate: 09-jun-81 Enter value for sal: 2450 Enter value for comm: Enter value for dept: 10 old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept') new 1: insert into rakjay values('chetan','manager','7782','7839','09-jun-81','2450','','10') 1 row created. SQL> / Enter value for ename: sudhir Enter value for job: analyst Enter value for eno: 7781 Enter value for mgr: 7566 Enter value for hierdate: 19-apr-87 Enter value for sal: 5000 Enter value for comm: Enter value for dept: 20 old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept') new 1: insert into rakjay values('sudhir','analyst','7782','7566','19-apr-87','5000','','20') 1 row created. SQL> / Enter value for ename: sumit vats Enter value for job: analyst Enter value for eno: 7902 Enter value for mgr: 7566 Enter value for hierdate: 03-dec-81 Enter value for sal: 3500 Enter value for comm: 3600 Enter value for dept: 20 old 1: insert into rakjay values('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept') new 1: insert into rakjay values('sumit vats','analyst','7902','7566','03-dec-81','3500','3600','2 1 row created.
4
7781 7902
--------------- ---------sunita ashok rohit jyoti martin binod chetan sudhir sumit vats 3100 3900 5550 5275 6550 6150 2750 5300 3800
9 rows selected
--------------- ---------sunita jyoti binod chetan sudhir 2800 4975 5850 2450 5000
---------10 20 30
10.
E_NAME --------------sunita sudhir sumit vats
---------- ---------7521 rohit 500 30 manager 7839 02-APR-81 4975 salesman 7698 22-FEB-81 5250
salesman
7698 28-SEP-81
6250
E_NO E_NAME
JOB
MGR HIREDATE
SAL
---------- ---------7698 binod 30 7781 hir analyst 7566 19-APR-87 5000 manager 7839 01-MAY-81 5850
---------- ---------7369 sunita 20 7499 ashok 300 30 salesman 7698 22-FEB-81 5250 salesman 7698 20-FEB-81 3600 clerk 7902 17-DEC-80 2800
E_NO E_NAME
JOB
MGR HIREDATE
SAL
---------- ---------7566 jyoti 20 7698 binod 30 7782 chetan 10 E_NO E_NAME JOB MGR HIREDATE SAL manager 7839 09-JUN-81 2450 manager 7839 01-MAY-81 5850 manager 7839 02-APR-81 4975
---------- ---------7654 martin 1400 30 analyst 7566 03-DEC-81 3500 9 salesman 7698 28-SEP-81 6250
3600
20
7781 sudhir 20 9
analyst
7566 19-APR-87
5000
rows selected.
13. List all the details in the ascending order of deptno & descending order of salary.
SQL> select * from rakjay order by deptno,sal desc; E_NO E_NAME JOB MGR HIREDATE
---------- ---------- ---------- ---7782 chetan 2450 7781 sudhir 5000 7566 jyoti 4975 E_NO E_NAME 20 JOB MGR HIREDATE 20 manager 7839 02-APR-81 10 analyst 7566 19-APR-87 manager 7839 09-JUN-81
---------- ---------- ---------- ---7902 sumit vats 3500 3600 20 clerk 20 salesman 30 JOB MGR HIREDATE 7698 28-SEP-81 7902 17-DEC-80 analyst 7566 03-DEC-81
E_NO E_NAME
SAL
COMM
DEPTNO GRAD
---------- ---------- ---------- ---7698 binod 5850 7521 rohit 5250 500 30 salesman 30 salesman 30 7698 20-FEB-81 7698 22-FEB-81 manager 7839 01-MAY-81
rows selected.
14. Display the ename & deptno of all employees in dept 20 &30 in alphabetical order by name.
SQL> select e_name, deptno from rakjay where deptno=20 or deptno=30 order by e_name; E_NAME DEPTNO
--------------- ---------ashok binod jyoti martin rohit sudhir sumit vats sunita 30 30 20 30 30 20 20 20
15. List the name & salary of all the employees who earn more than 1200 and are in dept 10 or 40.
SQL> select e_name, sal from rakjay where sal>1200 and (deptno=10 or deptno=40); E_NAME SAL
11
16. List the name of all employees where the second letter of their name is an a.
SQL> select e_name from rakjay where e_name like '_a%'; E_NAME --------------Martin
17. Display the difference of highest department having maximum salary >6000
difference ---------2650
&
lowest
salary
of
each
18. Display the name of employees whose name contains Mas first alphabet ,L as third alphabet.
SQL> select e_name from rakjay where e_name like 'm_l%'; no rows selected
19. Display ename, sal of those employees who do not have their salary in the range of 3000 to 6000.
SQL> select e_name, sal from rakjay where sal not between 3000 and 6000; E_NAME SAL
12
22. List the maximum salary of employee SQL> select max(sal) from rakjay group by deptno; MAX(SAL) ---------2450 5000 6250
23. Display the sum, average, highest and lowest salary of the employees grouped by department number.
SQL> select deptno, sum(sal), max(sal), min(sal), avg(sal) from rakjay group by deptno; DEPTNO SUM(SAL) MAX(SAL) MIN(SAL) AVG(SAL) ---------- ---------- ---------- ---------- ---------10 20 30 2450 16275 20950 2450 5000 6250 2450 2800 3600 2450 4068.75 5237.5
25. Calcualte the average gross & total gross for employees belonging to E4 grade.
SQL> Select avg(sal), sum(sal) from rakjay where grade= E4;
26. To display the jobs where the number of employees is less than 3.
SQL> select job from rakjay group by job having count(job)<3 ; JOB -------------------analyst clerk
27. Create table rakjay with the following structure Name of column Type ID First_Name Last_name User_ID Salary NUMBER(4) Varchar2(30) Varchar2(30) Varchar2(10) Number(9,2)
Ensure the following specification in created table. ID should be declared as primary Key User_Id should be unique Salary must be greater than 5000 First_Name & last_Name must not remain blank.
SQL> create table rakjay(ID number(4) primary key, First_name varchar2(30) not null, last_name varchar2(30) not null, user_id varchar2(10) unique, salary number(9,2), check (salary>5000)); Table created. 14
----------------------------------------- -------- ---------------------------ID FIRST_NAME LAST_NAME USER_ID SALARY NUMBER(4) NOT NULL VARCHAR2(30) NOT NULL VARCHAR2(30) VARCHAR2(10) NUMBER(9,2)
----------------------------------------- -------- ---------------------ENAME JOB ENO MGR HIREDATE SAL COMM DEPT VARCHAR2(10) VARCHAR2(10) NUMBER(5) NUMBER(5) DATE NUMBER(8) NUMBER(5) NUMBER(5) 15
GRADE
CHAR(4)
31. Change the data type of column ename to char(20) in the Table employee.
SQL> alter table rakjay modify(ename varchar(20)); Table altered. SQL> desc rakjay; Name Null? Type
----------------------------------------- -------- ---------------------ENAME JOB ENO MGR HIREDATE SAL COMM DEPT GRADE VARCHAR2(20) VARCHAR2(10) NUMBER(5) NUMBER(5) DATE NUMBER(8) NUMBER(5) NUMBER(5) CHAR(4)
16
Q1. List the name and salary of the employees whose salary is more than 1000.
SQL> select ename, sal from emp where sal>1000; ENAME SAL
---------- ---------ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS FORD ENAME 1600 1250 2975 1250 2850 2450 5500 5000 1500 1100 5500 SAL
12 Rows selected.
7782 RK
Q4. List the employee name and salary, whose salary is between 1000 and 2000.
SQL> select ename, sal from emp where sal between 1000 and 2000; ENAME SAL
---------- ---------ALLEN WARD MARTIN TURNER ADAMS MILLER 6 1600 1250 1250 1500 1100 1300
rows selected.
---------- --------ALLEN WARD MARTIN SCOTT TURNER FORD 6 SALESMAN SALESMAN SALESMAN ANALYST SALESMAN ANALYST rows selected.
18
Q6. List the details of Employees who have joined before end of September 81.
SQL> select ename from emp where hiredate<'30-sep-81' ENAME ---------SMITH ALLEN WARD JONES MARTIN BLAKE CLARK TURNER
rows selected.
---------- --------SMITH ALLEN WARD MARTIN SCOTT KING TURNER ADAMS JAMES FORD MILLER CLERK SALESMAN SALESMAN SALESMAN ANALYST PRESIDENT SALESMAN CLERK CLERK ANALYST CLERK
11 rows selected
Q8. List the employees who are not eligible for commission.
SQL> select ename from emp where comm is NULL; ENAME ---------19
SMITH JONES BLAKE CLARK SCOTT KING ADAMS JAMES FORD MILLER
Q11. List the details of employees, whose salary is more than 2,500/- and commission is null.
SQL> select * from emp where sal>2500 and comm is NULL; EMPNO ENAME JOB MGR HIREDATE SAL ---------- ---------- --------- ---------- --------- ---------- ---------DEPTNO ---------7566 JONES 20 7698 BLAKE 30 20 MANAGER 7839 01-MAY-81 2850 MANAGER 7839 02-APR-81 2975 COMM
7788 SCOTT 20
ANALYST
7566 19-APR-87
5500
EMPNO ENAME JOB MGR HIREDATE SAL ---------- ---------- --------- ---------- --------- ---------- ---------DEPTNO ---------7839 KING 10 7902 FORD 20 ANALYST 7566 03-DEC-81 5500 PRESIDENT 17-NOV-81 5000
COMM
5500
800 2430.35714
34025
21
Q16. List the average salary of number of employees working in department 20.
SQL> select avg(sal), count(ename) from emp where deptno=20; AVG(SAL) COUNT(ENAME) ---------- ------------
3175
Q17. List the department number and number of employees in each department.
SQL> select deptno, count(ename) from emp group by deptno; DEPTNO COUNT(ENAME) ---------- -----------10 20 3 5
30
Q18. List the department number and the total salary payable in each department.
SQL> select deptno, sum(sal) from emp group by deptno; DEPTNO SUM(SAL) ---------- ---------10 20 8750 15875
30
9400
Q20. List the employee names whose salary is greater than the lowest salary of an employee belonging to department number 20.
SQL> select ename from emp where sal>(select min(sal) from emp where deptno=20); ENAME ---------ALLEN WARD JONES MARTIN BLAKE 22
13 rows selected.
Q21. List the employee names whose salary is greater than the highest salary of an employee belonging to department number 20.
SQL> select ename from emp where sal>(select max(sal) from emp where deptno=20); ENAME ---------KING
Q23. Add number of month to a given date and returns a date after those many months.
SQL> select hiredate, ADD_months(hiredate, 6)"probation" from emp where ename='SMITH'; HIREDATE probation --------- --------23
17-DEC-80 17-JUN-81
Q27. Query to pad the left side of the string with the given set of characters.
SQL> select LPAD('AIMT',15,'*') "LPAD EXAMPLE" from DUAL;
OUTPUT:
24
Q28. Query to remove all specified characters from the left side of the string.
SQL> SELECT LTRIM('ARMYINSTITUTE','ARMY') "LTRIM EXAMPLE" FROM DUAL;
Q29. Query to remove all specified characters from the right side of the string.
SQL> SELECT RTRIM('ARMYINSTITUTE', 'INSTITUTE') "RTRIM EXAMPLE" FROM DUAL;
Q30. Query to write a string and grab a specified string from the given set of characters.
SQL> SELECT SUBSTR('ARMYINSTITUTE',6,4)"SUBSTR" FROM DUAL; OUTPUT:
25
SUBS ---NSTI
Q32. Query to find out the starting location of the pattern of the string.
SQL> SELECT JOB,INSTR(JOB,'LE')FROM EMP;
--------- --------------CLERK SALESMAN SALESMAN MANAGER SALESMAN MANAGER MANAGER ANALYST PRESIDENT SALESMAN CLERK 2
26
2 3 3 0 3 0 0 0 0 3
JOB
INSTR(JOB,'LE')
14 rows selected.
PART II PL SQL
27
Q1. Write a PL/SQL program to read two numbers and display their sum.
Ans. declare num1 number(2); num2 number(2); num3 number(4); begin num1:=&number1; num2:=&number2; num3:=num1+num2; dbms_output.put_line('the sum is:'||num3); end; / Enter value for number1: 3 old 6: num1:=&number1; new 6: num1:=3; Enter value for number2: 4 old 7: num2:=&number2; new 7: num2:=4; the sum is:7 PL/SQL procedure successfully completed.
28
Q2. Write a program to read two numbers and display their square.
Ans. declare num1 number(2); sq number(6); begin num1:=&number1; sq:=num1*num1; dbms_output.put_line('the square is:'||sq); end; / Enter value for number1: 3 old 5: num1:=&number1; new 5: num1:=3; the square is:9 PL/SQL procedure successfully completed.
new 6: num1:=4; Enter value for number2: 6 old 7: num2:=&number2; new 7: num2:=6; the area of triangle is:12 PL/SQL procedure successfully completed.
B1 number(4); C1 number(4); D1 varchar(4); begin A1:=&number1; B1:=&number2; D1:=&varchar1; if D1:='+' then C1:=A1+B1; dbms_output.put_line('The sum is:'||C1); else if D1:='-' then C1:=A1-B1; dbms_output.put_line('The subtraction is:'||C1); else if D1:='*' then C1:=A1*B1; dbms_output.put_line('The multiplication is:'||C1); else D1:='/' C1:=A1/B1; dbms_output.put_line('The division is:'||C1); end if; end;
Q6. Program to display that if the number is greater than the other it swaps the two number otherwise it doubles them.
Ans. Declare X number(2); Y number(2); Z number(2); S number(2); D number(2); D1 number(4); Begin
31
X:=& number1; Y:=& number2; If X>Y Then Z:=X; X:=Y; Y:=Z; Dbms_output.put_line(X||Y); Else D=2XY; D1=2XY; Dbms_output.put_line(D||D1); End if; End;
Begin N:=&num1; For I IN 1.....10 Loop T=N*I; Dbms_output.put_line(the table is:||T); End;
Q10. Write a PL/SQL code to increase the salary of employee whose salary is 3000.Get the employee number from the user.
Ans. Declare Empno number(4); Salary number(7,2); Begin Empno:=&empno; Select sal into salary from emp Where
33
Empno=eno; If salary>3000 Update emp set sal=sal+.05*sal; Where empno=eno; End if; End;
Q.11. Write a PL/SQL code that traps zero divide exceptions while calculating price to earnings ratio with a company symbol xyz.
Ans. Declare Pre ratio number(3,1); Begin Select price/earnings into price ratio from stock Where symbol=xyz; Exceptions When zero divides then Insert into stocks (symbol, ratio) values(xyz,NULL); Commit; When other Then rollback; End;
Q.12. Write a PL/SQL code to obtain details of employee drawing maximum salary. Add his/her details into a table new sal (empno, ename, salary) after incrementing salary by Rs.700.
Ans. Declare Eno emp.empno%type; Name emp.ename%type;
34
Salary emp.sal%type; Begin Select empno,ename,sal into eno,ename,salary from emp where sal=(select max(sal) from emp); Salary=salary+700; Insert into new sal values (Eno,ename,salary); End;
Begin Select sal into my sql from emp where empno=7788; When sal>mysql IF sql % found Then Dbms_output.put_line(mysql) If sql % row count>0 Then Rec:=sql % row count; Dbms_output.put_line(rec); End if;
End;
36