DBMS [Main]

Embed Size (px)

Citation preview

  • 7/29/2019 DBMS [Main]

    1/36

    1

    PART I

    SQL

  • 7/29/2019 DBMS [Main]

    2/36

    Table rakjay

    Eno Ename Job Mgr Hierdate Sal Comm. Dept

    7369 Sunita Clerk 7902 17-dec-80 2800 207499 Ashok Salesman 7698 20-feb-81 3600 300 30

    7521 Rohit Salesman 7698 22-feb-81 5250 500 30

    7566 Jyoti Manager 7839 02-apr-81 4975 20

    7654 Martin salesman 7698 28-sep-81 6250 1400 30

    7698 Binod Manager 7839 01-may-81 5850 30

    7782 Chetan Manager 7839 09-jun-81 2450 10

    7782 Sudhir Analyst 7566 19-apr-87 5000 20

    7902 Sumit vats Analyst 7566 03-dec-81 3500 3600 20

    1. Create the above table and insert the same values.SQL> create table rakjay(ename varchar(10), job varchar(10), eno number(5), mgr number(5), hierdate

    date, sal number(8), comm number(5), dept number(5));

    Table created.

    SQL> desc rakjay;

    Name Null? Type

    ----------------------------------------- -------- ----------------------------

    ENAME VARCHAR2(10)

    JOB VARCHAR2(10)ENO NUMBER(5)

    MGR NUMBER(5)

    HIERDATE DATE

    SAL NUMBER(8)

    COMM NUMBER(5)

    DEPT NUMBER(5)

    SQL>insert into rakjay values ('&ename','&job','&eno','&mgr','&hierdate','&sal','&comm','&dept');

    Enter value for ename: Sunita

    Enter value for job: clerk

    Enter value for eno: 7369Enter value for mgr: 7902

    Enter value for hierdate: 17-dec-80

    Enter value for sal: 2800

    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('Sunita','clerk','7369',' 7902',' 17-dec-80','2800','','20')

    1 row created.

    SQL> /

    Enter value for ename: ashokEnter value for job: salesman

    Enter value for eno: 7499

    2

  • 7/29/2019 DBMS [Main]

    3/36

    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: 30old 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: martinEnter 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

  • 7/29/2019 DBMS [Main]

    4/36

    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: 2450Enter 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

  • 7/29/2019 DBMS [Main]

    5/36

    2. Select all the columns of the above table.

    SQL> select * from rakjay;

    ENAME JOB ENO MGR HIERDATE SAL COMM---------- ---------- ---------- ---------- --------- ---------- ----------

    DEPT

    ----------

    Sunita clerk 7369 7902 17-DEC-80 2800

    20

    ashok salesman 7499 7698 20-FEB-81 3600 300

    30

    rohit salesman 7521 7698 22-FEB-81 5250 500

    30

    ENAME JOB ENO MGR HIERDATE SAL COMM---------- ---------- ---------- ---------- --------- ---------- ----------

    DEPT

    ----------

    jyoti manager 7566 7839 02-APR-81 4975

    20

    martin salesman 7654 7698 28-SEP-81 6250 1400

    30

    binod manager 7698 7839 01-MAY-81 5850

    30

    ENAME JOB ENO MGR HIERDATE SAL COMM

    ---------- ---------- ---------- ---------- --------- ---------- ----------

    DEPT

    ----------

    chetan manager 7782 7839 09-JUN-81 2450

    10

    sudhir analyst 7781 7566 19-APR-87 5000

    20

    sumit vats analyst 7902 7566 03-DEC-81 3500 3600

    20

    9 rows selected.

    3. List the name & eno from the above table.SQL> select ename, eno from rakjay;

    ENAME ENO

    ---------- ----------

    Sunita 7369

    ashok 7499

    rohit 7521

    jyoti 7566martin 7654

    binod 7698

    chetan 7782

    5

  • 7/29/2019 DBMS [Main]

    6/36

    sudhir 7781

    sumit vats 7902

    4. Display the ename and the incremented value of sal as sal+300.

    SQL> Select ename, sal+300 from rakjay;

    E_NAME increment

    --------------- ----------

    sunita 3100

    ashok 3900

    rohit 5550

    jyoti 5275

    martin 6550

    binod 6150

    chetan 2750

    sudhir 5300

    sumit vats 3800

    9 rows selected

    5. Display the ename & sal where comm. Is null.

    SQL> select e_name, sal from rakjay where comm is NULL;

    E_NAME SAL

    --------------- ----------

    sunita 2800

    jyoti 4975

    binod 5850

    chetan 2450

    sudhir 5000

    6. List the distinct deptno from the table.

    SQL> select distinct (deptno) from rakjay;

    DEPTNO

    6

  • 7/29/2019 DBMS [Main]

    7/36

    ----------

    10

    20

    30

    7. List the unique jobs from the table.

    SQL> select distinct (job) from rakjay;

    JOB

    --------------------

    analyst

    clerk

    manager

    salesman

    8. List the sal where sal is less than the commission.

    SQL> select sal from rakjay where sal select e_name, mgr from rakjay where mgr in (7902,7566,7788);

    E_NAME MGR

    --------------- ----------

    sunita 7902

    sudhir 7566

    sumit vats 7566

    7

  • 7/29/2019 DBMS [Main]

    8/36

    10. List the ename starting with sSQL> select e_name from rakjay where e_name like 's%';

    E_NAME

    ---------------

    sunita

    sudhir

    sumit vats

    11.List all the columns where salary is greater than 4100.SQL> select * from rakjay where sal>4100;

    E_NO E_NAME JOB MGR HIREDATE SAL

    ---------- --------------- -------------------- ---------- --------- ----------

    COMM DEPTNO

    ---------- ----------

    7521 rohit salesman 7698 22-FEB-81 5250

    500 30

    7566 jyoti manager 7839 02-APR-81 4975

    20

    7654 martin salesman 7698 28-SEP-81 6250

    1400 30

    E_NO E_NAME JOB MGR HIREDATE SAL

    ---------- --------------- -------------------- ---------- --------- ----------

    COMM DEPTNO

    ---------- ----------

    7698 binod manager 7839 01-MAY-81 5850

    30

    7781 hir analyst 7566 19-APR-87 5000

    8

  • 7/29/2019 DBMS [Main]

    9/36

    12. List all the columns in the ascending order of hiredate.SQL> select * from rakjay order by hiredate;

    E_NO E_NAME JOB MGR HIREDATE SAL

    ---------- --------------- -------------------- ---------- --------- ----------

    COMM DEPTNO

    ---------- ----------

    7369 sunita clerk 7902 17-DEC-80 2800

    20

    7499 ashok salesman 7698 20-FEB-81 3600

    300 30

    7521 rohit salesman 7698 22-FEB-81 5250

    500 30

    E_NO E_NAME JOB MGR HIREDATE SAL

    ---------- --------------- -------------------- ---------- --------- ----------

    COMM DEPTNO

    ---------- ----------

    7566 jyoti manager 7839 02-APR-81 4975

    20

    7698 binod manager 7839 01-MAY-81 5850

    30

    7782 chetan manager 7839 09-JUN-81 2450

    10

    E_NO E_NAME JOB MGR HIREDATE SAL

    ---------- --------------- -------------------- ---------- --------- ----------

    COMM DEPTNO

    ---------- ----------

    7654 martin salesman 7698 28-SEP-81 6250

    1400 30

    7902 sumit vats analyst 7566 03-DEC-81 3500

    9

  • 7/29/2019 DBMS [Main]

    10/36

    3600 20

    7781 sudhir analyst 7566 19-APR-87 5000

    20

    9 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

    ---------- -------------------- -------------------- ---------- ---------

    SAL COMM DEPTNO GRAD

    ---------- ---------- ---------- ----

    7782 chetan manager 7839 09-JUN-81

    2450 10

    7781 sudhir analyst 7566 19-APR-87

    5000 20

    7566 jyoti manager 7839 02-APR-81

    4975 20

    E_NO E_NAME JOB MGR HIREDATE

    ---------- -------------------- -------------------- ---------- ---------

    SAL COMM DEPTNO GRAD

    ---------- ---------- ---------- ----

    7902 sumit vats analyst 7566 03-DEC-81

    3500 3600 20

    7369 sunita clerk 7902 17-DEC-80

    2800 20

    7654 martin salesman 7698 28-SEP-81

    6250 1400 30

    E_NO E_NAME JOB MGR HIREDATE

    ---------- -------------------- -------------------- ---------- ---------

    10

  • 7/29/2019 DBMS [Main]

    11/36

    SAL COMM DEPTNO GRAD

    ---------- ---------- ---------- ----

    7698 binod manager 7839 01-MAY-81

    5850 30

    7521 rohit salesman 7698 22-FEB-81

    5250 500 30

    7499 ashok salesman 7698 20-FEB-81

    3600 300 30

    9 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 30

    binod 30

    jyoti 20

    martin 30

    rohit 30

    sudhir 20

    sumit vats 20

    sunita 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

    --------------- ----------

    chetan 2450

    11

  • 7/29/2019 DBMS [Main]

    12/36

    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 & lowest salary of each

    department having maximum salary >6000SQL> select (max(sal)-min(sal))"difference" from rakjay group by deptno having max(sal)>6000;

    difference

    ----------

    2650

    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

    --------------- ----------

    sunita 2800

    martin 6250

    chetan 2450

    20. Display the name of employee whose name contain M as firstSQL> select e_name from rakjay where e_name like 'm%';

    E_NAME

    ---------------

    Martin

    12

  • 7/29/2019 DBMS [Main]

    13/36

    21. Display total salary of all employees listed in table emp.SQL> select sum(sal)"total salary" from rakjay;

    total salary

    ------------

    39675

    22.List the maximum salary of employee grouped by their department number.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 2450 2450 2450 2450

    20 16275 5000 2800 4068.75

    30 20950 6250 3600 5237.5

    24. Add a field Grade in above tableSQL> alter table rakjay add(grade char(4));

    Table altered.

    SQL> desc rakjay;

    Name Null? Type

    ----------------------------------------- -------- ----------------------

    ENAME VARCHAR2(10)

    JOB VARCHAR2(10)

    ENO NUMBER(5)

    13

  • 7/29/2019 DBMS [Main]

    14/36

    MGR NUMBER(5)

    HIREDATE DATE

    SAL NUMBER(8)

    COMM NUMBER(5)

    DEPT NUMBER(5)

    GRADE CHAR(4)

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

  • 7/29/2019 DBMS [Main]

    15/36

    SQL> desc rakjay;

    Name Null? Type

    ----------------------------------------- -------- ----------------------------

    ID NUMBER(4)

    FIRST_NAME NOT NULL VARCHAR2(30)

    LAST_NAME NOT NULL VARCHAR2(30)

    USER_ID VARCHAR2(10)

    SALARY NUMBER(9,2)

    28. Delete the employee Record having ename as Jyoti.SQL> delete from rakjay where e_name='jyoti';

    1 row deleted.

    29. Rename rakjay to vr.SQL> alter table rakjay rename to vr;

    Table altered.

    SQL> desc rakjay;

    ERROR:

    ORA-04043: object employee does not exist

    SQL> desc vr;

    Name Null? Type

    ----------------------------------------- -------- ----------------------

    ENAME VARCHAR2(10)

    JOB VARCHAR2(10)

    ENO NUMBER(5)

    MGR NUMBER(5)

    HIREDATE DATE

    SAL NUMBER(8)

    COMM NUMBER(5)

    DEPT NUMBER(5)

    15

  • 7/29/2019 DBMS [Main]

    16/36

    GRADE CHAR(4)

    30. Drop table vr.SQL> drop table vr;

    Table dropped.

    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 VARCHAR2(20)

    JOB VARCHAR2(10)

    ENO NUMBER(5)

    MGR NUMBER(5)

    HIREDATE DATE

    SAL NUMBER(8)

    COMM NUMBER(5)

    DEPT NUMBER(5)

    GRADE CHAR(4)

    32. Make the changes permanent.SQL> Commit;

    16

  • 7/29/2019 DBMS [Main]

    17/36

    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 1600

    WARD 1250

    JONES 2975

    MARTIN 1250

    BLAKE 2850

    CLARK 2450

    SCOTT 5500

    KING 5000

    TURNER 1500

    ADAMS 1100

    FORD 5500

    ENAME SAL

    ---------- ----------

    MILLER 1300

    12 Rows selected.

    Q2. List the employee number and name of manager.

    SQL> select empno, ename from emp where job='MANAGER';

    EMPNO ENAME

    ---------- ----------

    7566 JONES

    7698 BLAKE

    17

  • 7/29/2019 DBMS [Main]

    18/36

    7782 RK

    Q3. List the name of Clerks working in Department 20.

    SQL> select ename from emp where job='CLERK' and deptno='20';

    ENAME

    ----------

    SMITH

    ADAMS

    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 1600

    WARD 1250

    MARTIN 1250

    TURNER 1500

    ADAMS 1100

    MILLER 1300

    6 rows selected.

    Q5. List the names of analysts and salesman.

    SQL> select ename, job from emp where job='ANALYST' or job='SALESMAN';

    ENAME JOB

    ---------- ---------

    ALLEN SALESMAN

    WARD SALESMAN

    MARTIN SALESMAN

    SCOTT ANALYST

    TURNER SALESMAN

    FORD ANALYST

    6 rows selected.

    18

  • 7/29/2019 DBMS [Main]

    19/36

    Q6. List the details of Employees who have joined before end of September 81 .

    SQL> select ename from emp where hiredate select ename, job from emp where job !='MANAGER';

    ENAME JOB

    ---------- ---------

    SMITH CLERK

    ALLEN SALESMAN

    WARD SALESMAN

    MARTIN SALESMAN

    SCOTT ANALYST

    KING PRESIDENT

    TURNER SALESMAN

    ADAMS CLERK

    JAMES CLERK

    FORD ANALYST

    MILLER 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

  • 7/29/2019 DBMS [Main]

    20/36

    SMITH

    JONES

    BLAKE

    CLARK

    SCOTT

    KING

    ADAMS

    JAMES

    FORD

    MILLER

    Q9. List the employees not assigned to any department.

    SQL> select ename from emp where deptno is NULL;

    no rows selected

    Q10. List the employees who are eligible for commission.

    SQL> select ename from emp where comm is NOT NULL;

    ENAME

    ----------

    ALLEN

    WARD

    MARTIN

    TURNER

    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 COMM

    ---------- ---------- --------- ---------- --------- ---------- ----------

    DEPTNO

    ----------

    7566 JONES MANAGER 7839 02-APR-81 2975

    20

    7698 BLAKE MANAGER 7839 01-MAY-81 2850

    30

    20

  • 7/29/2019 DBMS [Main]

    21/36

    7788 SCOTT ANALYST 7566 19-APR-87 5500

    20

    EMPNO ENAME JOB MGR HIREDATE SAL COMM

    ---------- ---------- --------- ---------- --------- ---------- ----------

    DEPTNO

    ----------

    7839 KING PRESIDENT 17-NOV-81 5000

    10

    7902 FORD ANALYST 7566 03-DEC-81 5500

    20

    Q12. List the number of employees working with the company.

    SQL> select count(ename) from emp;

    COUNT(ENAME)

    ------------

    14

    Q13. List the number of jobs available in the emp table.

    SQL> select count(job) from emp;

    COUNT(JOB)

    ----------

    14

    Q14. List the maximum, minimum, average and sum of salary.

    SQL> select max(sal), min(sal), avg(sal), sum(sal) from emp;

    MAX(SAL) MIN(SAL) AVG(SAL) SUM(SAL)

    ---------- ---------- ---------- ----------

    5500 800 2430.35714 34025

    Q15. List the maximum salary of the employee working as a Salesman.

    SQL> select max(sal) from emp where job='SALESMAN';

    MAX(SAL)

    ----------

    1600

    21

  • 7/29/2019 DBMS [Main]

    22/36

    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 2

    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 3

    20 5

    30 6

    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 8750

    20 15875

    30 9400

    Q19. Select the jobs that are unique to department 20.

    SQL> select unique(job) from emp where deptno=20;

    JOB

    ---------

    ANALYST

    CLERK

    MANAGER

    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

  • 7/29/2019 DBMS [Main]

    23/36

    CLARK

    SCOTT

    KING

    TURNER

    ADAMS

    JAMES

    ENAME

    ----------

    FORD

    MILLER

    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

    Q22. List the employees who joined in 1981.

    SQL> select ename from emp where hiredate between '31-dec-80' and '01-jan-82';

    ENAME

    ----------

    ALLEN

    WARD

    JONES

    MARTIN

    BLAKE

    CLARK

    KING

    TURNER

    JAMES

    FORD

    10 rows selected.

    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

  • 7/29/2019 DBMS [Main]

    24/36

    17-DEC-80 17-JUN-81

    Q24. Convert a date into equivalent character string.

    SQL> select TO_char(hiredate, 'month,dd,yyyy') from emp where ename='SCOTT';

    TO_CHAR(HIREDATE,

    -----------------april ,19,1987

    Q25. Query to make the first letter capital.

    SQL> select INITCAP('the soap') "capitals" from dual;

    OUTPUT:

    capitals

    --------

    The Soap

    Q26. Query to change the statement from uppercase to lower case.

    SQL> select LOWER ('MR.SCOTT MCMILLAN') "LOWER" from dual;

    OUTPUT:

    LOWER

    -----------------

    mr.scott mcmillan

    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

  • 7/29/2019 DBMS [Main]

    25/36

    LPAD EXAMPLE

    ---------------

    ***********AIMT

    Q28. Query to remove all specified characters from the left side of the string.

    SQL> SELECT LTRIM('ARMYINSTITUTE','ARMY') "LTRIM EXAMPLE" FROM DUAL;

    OUTPUT:

    LTRIM EXA

    ---------

    INSTITUTE

    Q29. Query to remove all specified characters from the right side of the string.

    SQL> SELECT RTRIM('ARMYINSTITUTE', 'INSTITUTE') "RTRIM EXAMPLE" FROM

    DUAL;

    OUTPUT:

    RTRI

    ----

    ARMY

    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

  • 7/29/2019 DBMS [Main]

    26/36

    SUBS

    ----

    NSTI

    Q31. Query to replace characters.

    SQL> SELECT REPLACE('JACK AND JUE','J','BL') "CHANGES" FROM DUAL;

    OUTPUT:

    CHANGES

    --------------

    BLACK AND BLUE

    Q32. Query to find out the starting location of the pattern of the string.

    SQL> SELECT JOB,INSTR(JOB,'LE')FROM EMP;

    OUTPUT:

    JOB INSTR(JOB,'LE')

    --------- ---------------

    CLERK 2

    SALESMAN 3

    SALESMAN 3

    MANAGER 0

    SALESMAN 3

    MANAGER 0

    MANAGER 0

    ANALYST 0

    PRESIDENT 0

    SALESMAN 3

    CLERK 2

    26

  • 7/29/2019 DBMS [Main]

    27/36

    JOB INSTR(JOB,'LE')

    --------- ---------------

    CLERK 2

    ANALYST 0

    CLERK 2

    14 rows selected.

    27

    PART II

    PL SQL

  • 7/29/2019 DBMS [Main]

    28/36

    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

  • 7/29/2019 DBMS [Main]

    29/36

  • 7/29/2019 DBMS [Main]

    30/36

    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.

    Q4. Program to display area of rectangle.

    Ans. declare

    num1 number(2);

    num2 number(2);

    num3 number(4);

    begin

    num1:=&number1;

    num2:=&number2;

    num3:=num1*num2;

    dbms_output.put_line('the area of rectangle is:'||num3);

    end;

    /

    Enter value for number1: 3

    old 6: num1:=&number1;

    new 6: num1:=3;

    Enter value for number2: 8

    old 7: num2:=&number2;

    new 7: num2:=8;

    the area of rectangle is:24

    PL/SQL procedure successfully completed.

    Q5. Program to display sum, difference, multiplication and division.

    Ans. declare

    A1 number(4);

    30

  • 7/29/2019 DBMS [Main]

    31/36

    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

  • 7/29/2019 DBMS [Main]

    32/36

    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;

    Q7. Program to print first N natural number & their sum.

    Ans. Declare

    N number;

    Sum number:=0;

    Begin

    N:=&number;

    For I IN 1.....N

    loop

    SUM=SUM+I;

    Dbms_output.put_line(som of first n natural number||SUM);

    End;

    Q8. Program to print table

    Ans. Declare

    N number(2);

    T number(2);

    32

  • 7/29/2019 DBMS [Main]

    33/36

    Begin

    N:=&num1;

    For I IN 1.....10

    Loop

    T=N*I;

    Dbms_output.put_line(the table is:||T);

    End;

    Q9. Program to print table through while loop.

    Ans. Declare

    I number:=0;

    Begin

    While I

  • 7/29/2019 DBMS [Main]

    34/36

    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

  • 7/29/2019 DBMS [Main]

    35/36

    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;

    Q13. Write a PL/SQL code in reference with Explicit cursor.

    Ans.Declare

    Cursor emp_cur IS

    Select * from emp;

    Emprec emp% rowtype;

    Begin

    If not emp_cur IS open

    Then open emp_cur

    End if

    Fetch emp_cur into emprec

    While emp_cur % found

    Loop

    Dbms_output.put_line(emp_cur % row count);

    End loop;

    Close emp_cur;

    End;

    Q14. Write a PL/SQL code in reference with implicit cursor.

    Ans. Declare

    My sql numer(7,2);

    Rec number;

    35

  • 7/29/2019 DBMS [Main]

    36/36

    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;