Ram PLSQL Questions

download Ram PLSQL Questions

of 25

Transcript of Ram PLSQL Questions

  • 8/11/2019 Ram PLSQL Questions

    1/25

    Document Modified by Ramesh..

    **** SUBQUERIES & MULTIPLE SUBQUERIES ****ROWNUM

    =>1a. Select FIRST n records from a table

    select * from emp where rownum 1b. Select LAST n records from a table

    select * from emp minus select * from emp

    where rownum 2a. Select * from emp where ROWNUM >= 1; --- what happens=>

    ## returns all rows of table

    =>2b. Select * from emp where ROWNUM > 1; --- what happens=>

    ## gives ?NO ROWS SELECTED?

    =>2c. Select * from emp where ROWNUM > 0; --- what happens=>

    ## returns all rows of table

    =>2d. Select * from emp where ROWNUM = 1; --- what happens=>

    ## Yes, this returns the 1st row

    =>2e. Select * from emp where ROWNUM > 5; --- what happens=>

    ## gives ?NO ROWS SELECTED?

    =>2f. Select * from emp where ROWID is null; --- what happens=>

  • 8/11/2019 Ram PLSQL Questions

    2/25

    ## gives 'NO ROWS SELECTED'

    =>2g. Select * from emp where ROWID is not null; --- what happens=>

    ## Returns all rows from the table.

    Decode()

    =>3. To select ALTERNATE(EVEN NUMBERED) records from a table

    select * from emp where rowid in (select

    decode(mod(rownum,2),0,rowid,

    null) from emp);

    ## here in the DECODE function,

    1st argument (mod(rownum,2)) is expression to check,

    2nd argument (0) is a search value,

    3rd argument (rowid) is to return the even rows if expr returns 0,

    4th argument (null) is to return no rows if expr does not return 0.

    =>3a. To select ALTERNATE(ODD NUMBERED) records from a table

    select * from emp where rowid in (select

    decode(mod(rownum,2),0,null,

    rowid) from emp);

    =>4. If sal >= 2000, increment salary by 1000 else return sal from emp table

    select ename,empno,job,sal,

    decode(mod(sal,2000),sal,sal,

    sal+1000) salinc from emp;

    n AND nth MAXIMUM & MINIMUM SALARIES

    =>5. Find the 3rd MAX salary in the emp table

    select distinct sal from emp a

    where 3 = (select count(distinct sal) from emp b

    where a.sal

  • 8/11/2019 Ram PLSQL Questions

    3/25

    arranged in descending order (since 6. Find the 3rd MIN salary in the emp table

    select distinct sal from emp a

    where 3 = (select count(distinct sal) from emp b

    where a.sal >= b.sal); -- sals in ascending order

    =>7. Find 3 MAX salaries in the emp table

    select distinct sal from emp a

    where 3 >= (select count(distinct sal) from emp b

    where a.sal 8. Find 3 MIN salaries in the emp table

    select distinct sal from emp a

    where 3 >= (select count(distinct sal) from emp b

    where a.sal >= b.sal);

    =>9. Find the nth MAX date from emp table

    select distinct hiredate from emp a

    where &n = (select count(distinct to_char(hiredate,'ddd'))

    from emp b where a.hiredate

  • 8/11/2019 Ram PLSQL Questions

    4/25

    =>10. Delete DUPLICATE records based on deptno

    delete from emp a where rowid !=

    (select max(rowid) from emp b wherea.deptno=b.deptno);

    ## this query will retain all the records which are having unique deptno that are having

    maximum rowid and delete the rest duplicate ones ie., which are having repeated deptnos.

    =>11. Select DISTINCT RECORDS from emp table

    select * from emp a where

    rowid = (select max(rowid) from emp b where

    a.empno=b.empno);

    ## here we can have 'in' also instead of '='

    **** JOINS ****

    OUTER JOIN

    =>1. List employees' names and their managers' names (USING OUTER JOIN)

    select lo.ename "EMP NAMES", hi.ename "MGR NAMES" from

    emp lo, emp hi where lo.mgr = hi.empno(+);

    ## where (we can find the empno against each manager) each lo.mgr is a manager of a

    hi.empno

    or

    select a.empno,a.ename,a.mgr,b.ename from emp a, emp b where

    b.ename = (select b.ename from emp b where a.mgr = b.empno) ;

    or

    select a.empno,a.ename,a.mgr,b.ename from emp a, emp b where

    where a.mgr = b.empno ;

    =>2. List Dept no., Dept name for all the departments in which there are no employees in the

    department (REPEATED QUESTION NO(3) BELOW UNDER ?INTERVIEW QUESTIONS?)

  • 8/11/2019 Ram PLSQL Questions

    5/25

    select empno,ename,b.deptno,dname from emp a, dept b

    where a.deptno(+) = b.deptno and empno is null; -- (1)

    ## (1) gives empno and ename as null also

    or

    select * from dept where deptno not in (select deptno from emp); -- (2)

    or

    select * from dept a where

    not exists (select * from emp b where

    a.deptno = b.deptno); -- (3)

    ## (2) & (3) GIVE THE SAME RESULT as (1)

    =>3. List the count of number of employees in each department (REPEATED QUESTION

    NO(4) BELOW UNDER ?INTERVIEW QUESTIONS?)

    select count(EMPNO), b.deptno, dname from emp a, dept b

    where a.deptno(+)=b.deptno

    group by b.deptno,dname;

    ## NOTE : Here if we give count(*) it counts as 1 for deptno = 40. So we must give

    count(empno) only for our required correct output.

    =>4a. Creating a table 'parts' with some constraints

    create table parts(

    partno number(2),

    partname char(15),

    alt_partno number(2) references parts(partno),

    constraint pky primary key(partno));

    ## 1st record insertion shd be null for alt_partno

    eg. insert into parts values (09,'refil',null);

    =>4b. List all partnames and their alternate partnames

    select a.partname "PARTNAME" ,b.partname "ALT PARTNAME" from

    parts a, parts b where

  • 8/11/2019 Ram PLSQL Questions

    6/25

    b.partno(+) = a.alt_partno

    =>5. Details of employees along with their location (EQUI JOIN)

    select empno,ename,a.deptno,b.dname,b.locfrom emp a, dept b where a.deptno = b.deptno;

    =>6. Details of ALLEN along with his location

    select empno,ename,a.deptno,b.dname,b.loc

    from emp a, dept b where a.deptno=b.deptno

    and ename = 'ALLEN';

    =>7. List down the employees working in CHICAGO

    select empno,ename,loc from

    emp a, dept b where a.deptno=b.deptno and

    loc = 'CHICAGO';

    =>8. List down details of employees getting sal < that of ALLEN

    select b.empno, b.ename,b.deptno,b.sal

    from emp a, emp b where a.ename = 'ALLEN'and b.sal create view empv1 as select * from emp;

    .To create a view and insert - ( To insert a record in a view, the view should consist of the NOT

    NULL (MANDATORY) column of the base table)

  • 8/11/2019 Ram PLSQL Questions

    7/25

    SQL>create view emp_view as select ename,job,sal from

    emp where sal insert into emp_view values('xxx','clerk',1000);

    SQL> insert into emp_view values('xxx','clerk',1000)

    2 *

    3 ERROR at line 1:

    4 ORA-01400: mandatory (NOT NULL) column is missing or NULL during insert

    CREATE VIEWS WITH THE CHECK OPTIONS

    SQL> create or replace view empview as select

    sal,empno,ename from emp where sal>1500;

    ## in the above case no record can be inserted into the table through the view EMPVIEW if

    the value of sal create or replace view empview sal,empno,ename,dname

    as select sal,empno,ename,dname from emp a, dept b where

    a.deptno=b.deptno and sal >1500 with check option;

    ## information on the check option can be obtained from the table called ALL_VIEWS

    To create a view with 'with check option' and updating

    SQL>create view emp_view as select ename,job,sal

    from emp where sal update emp_view set sal=sal + 1000;

    update emp_view set sal=sal + 1000

    *

    ERROR at line 1:

    ORA-01402: view WITH CHECK OPTION where-clause violation

    MULTI TABLE VIEWS

    SQL>create or replace view empview

  • 8/11/2019 Ram PLSQL Questions

    8/25

    as select empno,ename,a.deptno,dname from

    emp a, dept b where a.deptno=b.deptno;

    ## in complex table views no update,insert, etc.(i.e. DML commands) will be applicable

    SQL>create view emp_dept_view as

    select empno,ename,a.deptno,dname from

    emp a,dept b where a.deptno=b.deptno;

    SQL>create view dept_grp_view as

    select deptno,max(sal) "max sal" from

    emp group by deptno;

    CREATE VIEW FROM A NON EXISTING TABLE USING 'FORCE' KEYWORD

    SQL> create FORCE view v1 as select * from aaa;

    Warning: View created with compilation errors.

    CREATING VIEWS WITH COLUMNS RENAMED

    create or replace view empview (emp_number,emp_name,salary,dept_name)

    as select empno,ename,sal,dname from emp a, dept b where

    a.deptno=b.deptno;

    ALTERING VIEWS

    alter view empview compile;

    ## this command is useful if the table was altered using the command "alter table ...." , the

    command now regenerates the view ###

    TO DROP A VIEW

    drop view empview ;

    **** SET OPERATORS ****

    1. MINUS

    SQL> select job from emp where deptno=20 MINUS

    select job from emp where deptno=30;

  • 8/11/2019 Ram PLSQL Questions

    9/25

    or

    SQL> select distinct

    job from emp where deptno=20 and

    job not in (select job from emp where deptno=30);

    ## Here if we don't give distinct in subquery then repeated jobs may be displayed if jobis repeated in the dept=20.

    2. UNION

    SQL> select job from emp where deptno=10 UNION

    select job from emp where deptno=20;

    or

    SQL> select distinct job from emp where deptno in(10,20);

    or

    SQL> select distinct job from emp where deptno=10 or deptno=20;

    3. UNION ALL

    SQL> select job from emp where deptno=10 UNION ALL

    select job from emp where deptno=20;

    or

    SQL> select job from emp where deptno in(10,20);

    4. INTERSECT

    SQL> select job from emp where deptno=20 INTERSECT

    select job from emp where deptno=30;

    or

    SQL> select distinct job from emp where deptno=20

    and job in (select distinct job from emp where deptno=30);

    **** INT QUESTIONS (QUERIES) ****

    =>1. Produce the following report: dept no dept name avg_sal of

    dept

    select b.deptno "dept no",dname "dept name",avg(sal) "avg sal of dept"

    from emp a, dept b where a.deptno = b.deptno

    group by b.deptno, dname;

  • 8/11/2019 Ram PLSQL Questions

    10/25

    =>2. Produce the above report for sal > 2000

    select b.deptno "dept no",dname "dept name",avg(sal) "avg sal of dept"

    from emp a, dept b where a.deptno = b.deptno

    group by b.deptno, dname having avg(sal) > 2000;

    =>3. List dept no., Dept name for all the departments in which there are no employees in the

    department (REPEATED QUESTION NO.(2) AT TOP UNDER ?JOINS?)

    select empno,ename,b.deptno,dname from emp a, dept b

    where a.deptno(+) = b.deptno and empno is null; -- (1)

    ## (1) gives empno and ename as null also

    or

    select * from dept where deptno not in (select deptno from emp); -- (2)

    or

    select * from dept a where

    not exists (select * from emp b where

    a.deptno = b.deptno); -- (3)

    ## (2) & (3) GIVE THE SAME RESULT as (1)

    =>4. List the count of number of employees in each department (REPEATED QUESTION

    NO(3) AT TOP UNDER ?JOINS?)

    select count(EMPNO), b.deptno, dname from emp a, dept b

    where a.deptno(+)=b.deptno

    group by b.deptno,dname;

    ## NOTE : Here if we give count(*) it counts as 1 for deptno = 40. So we must give

    count(empno) only for our required correct output.

    =>5. List ename,sal,new salary with 25% rise in existing sal for those employees who have

    present sal< 2000 and are from departments 10 & 20

    select ename, deptno, sal, sal+(sal*.25) " new sal" from emp

    where sal < 2000 and deptno in (10,20);

    =>6. List empno,ename,sal,manager's name, manager's sal for all employees who earn more

    than their manager

  • 8/11/2019 Ram PLSQL Questions

    11/25

    select a.empno empno, a.ename name, a.sal "emp sal",

    b.ename "mgr name", b.sal "mgr sal" from emp a, emp b

    where a.mgr = b.empno and a.sal > b.sal;

    =>7. List the name and salary of all the employees working in all the departments who earn

    more salary than everybody in deptno=20

    select ename, deptno, sal from emp where

    sal > (select max(sal) from emp where deptno=30);

    =>8. Copy only the structure(not records) from an existing table while creating a table

    create table empp as select * from emp where 1= 2;

    ## Here 1=2 is a false condition hence copies only the structure of emp to empp table(but not

    the records)

    **** SOME QUESTIONS ****

    =>1. Create referential integrity on emp table with respect to deptno on deptno of dept table

    alter table emp add (constraint emp_fk foreign key(deptno)

    references dept(deptno));

    =>2. Create a check to ensure that only ename and job is entered in uppercase

    alter table emp1 add (constraint nm_upp check(ename = upper(ename)),

    constraint job_upp check(job = upper(job)));

    =>3. List all employees who have joined before the 'president'

    select empno, ename, hiredate from emp1 where hiredate < (select hiredate

    from emp1 where job = 'PRESIDENT');

    =>4. List all employees who have joined after 12th march 1982

    select * from emp where hiredate > '12-MAR-82';

  • 8/11/2019 Ram PLSQL Questions

    12/25

    =>5. Assuming hiredate as birthdate of an employee list hiredate and retirement of all

    employees

    select empno,ename,hiredate,to_number(to_char(hiredate,'yy')) + 58 "RETIREMENT DATE" from

    emp; /* not correct */

    =>6. List empno,ename,'Manager' or 'Staff', sal with an employee appearing only once in the

    list. Print 'Manager' for a manager and 'Staff' for non-managers

    select empno,ename,sal, decode(job,'MANAGER','Manager','Staff')

    from emp ;

    =>7. List the empno,ename,sal of the first 5 highest paid employees. (Do not use the rownum or

    rowid functions. Use only std SQL features)

    select max(sal) from emp where sal select * from emp where sal in

  • 8/11/2019 Ram PLSQL Questions

    18/25

    (select sal from emp where deptno = 20);

    ## for multiple rows returned the SQ must be preceded with the clause "IN" or "NOT IN" or

    "ANY" or "ALL"

    ## the "=" clause will yield error

    SQL> select * from emp where exists (select deptno from deptno)

    ## if the SQ following 'exists' returns atleast one row then the main query returns all the rows

    SQL> select dept.deptno,dept.dname from dept where

    dept.deptno = (select emp.deptno,dept.dname from emp,dept

    where emp.deptno!=dept.deptno);

    24. Select with more than one column

    select * from emp where (job,deptno) in

    (select job,deptno from emp where sal>1000);

    25. Select with Multiple sqs

    select * from emp where empno in

    (select empno from emp where deptno =

    (select deptno from dept where dname='SALES'));

    26. To select 1st 'm' rows and last 'n' rows from a table

    select * from emp where rownum => select * from emp where rownum

  • 8/11/2019 Ram PLSQL Questions

    19/25

    ## say rownum 5 to 8

    select * from emp where rownum 2000 order by sal desc, ename asc;

    30. EQUI JOIN conditions i.e. '=' operator

    select empno,ename,dname from emp,dept where emp.deptno=dept.deptno;

    31. OUTER JOIN conditions (REFER JOINS-OUTER JOINS)

    ##to select all departments

    select b.deptno,dname,empno,ename from dept b,emp a where

    b.deptno=a.deptno(+) and b.deptno in (select b.deptno from dept);

  • 8/11/2019 Ram PLSQL Questions

    20/25

    ##to select only departments where no employees are present

    select b.deptno,dname,empno,ename from dept b,emp a where

    b.deptno=a.deptno(+) and b.deptno in (select b.deptno from dept)

    and empno is null;or

    select deptno,dname from dept where deptno not in (select deptno

    from emp);

    or

    select deptno,dname from dept a where not exists

    (select * from emp b where a.deptno = b.deptno)

    32. SELF JOIN conditions applicable only for common tables

    SQL> select a.ename,a.job, b.ename from emp a, emp b where a.empno=b.empno;

    SQL> select a.empno "Emp No",a.ename "Emp Name", a.mgr "Mgr No",

    b.ename "Mgr Name" from emp a, emp b where

    b.ename = (select ename from emp where a.mgr=empno);

    33. NON_EQUI join conditions

    select sal,empno from emp,dept where dept.deptno=emp.deptno

    and sal between 1000 and 3000;

    ****INDEXES****

    ## information on indexes is available in the table called USER_INDEXES

    Creating indexes on tables

    create index empndx on emp (empno asc, sal desc, mgr desc);

    ## Only a maximum of 16 columns can be included in the index column this is same as

    primary key where only 16 columns can be included at a time

    create an UNIQUE INDEXES

  • 8/11/2019 Ram PLSQL Questions

    21/25

    create unique index empuniq on emp (empno,mgr,sal);

    To drop an index

    drop index empuniq;

    How to disable an index

    ## actually no index can be disabled, only an index usage can be avoided through an SQL

    statement

    ## for if there is an index table called empndx on the empno then the following SQL fools the

    index

    select * from emp where empno + 1 - 1 = 7902;

    ## here actually there is no index by name empno+1-1 and hence can be avoided

    ****SEQUENCES ****

    To create a sequence which is also a database object

    ## always the 2 psuedocolumns - nextval & currval are affiliated with sequences

    create sequence empnoincrement by 2

    start with 100

    maxvalue 500

    minvalue 150 - gives error

    cycle

    cache 25; -- here 25 is the ready memory of numbers

    ## START WITH cannot be less than MINVALUE

    To select from sequences

    ## always once the sequence is created to know the current val first use "select

    empno.nextval from dual" and then use the command "select empno.currval/empno.nextval

    from dual"

    SQL> select empno.nextval from dual;

  • 8/11/2019 Ram PLSQL Questions

    22/25

    SQL> select empno.currval from dual;

    To alter the sequence

    alter sequence empno increment by .....

    How to generate a sequence without using the sequences

    select max(empno)+1 from emp;

    If the sequence is for the very first instance then

    select nvl(max(empno),0)+1 from dual

    update emp set empno = nvl(max(empno),0)+1 where

    ## information on the sequences can be obtained from the table called USER_SEQUENCES

    To get the listing of sequences already created

    select * from cat; -- catalogue table

    ## synonym table name for cat is user_catalog;

    **** CREATION OF CLUSTERS ****

    SQL> create cluster clust (deptno number(2));

    SQL> alter cluster clust (deptno number(2));

    SQL> alter table emp1 (empno number(5), ename char(10), deptno number(2))

    cluster clust(deptno);

    SQL> Create table emp2

    (empno number(4) primary key,

    ename varchar2(20) constraint ch1 check(ename = upper(ename)),

    mgr number(4) references emp2(empno),

    comm number(7,2),

    job varchar2(15) constraint ch2 check(job = upper(job)),

    deptno number(2) not null);

  • 8/11/2019 Ram PLSQL Questions

    23/25

    =>=>=> delete from emp where rowid = (select * from emp minus

    (select * from emp where rownum What are the advantages of Oracle =>

    What are potential disadvantages of Oracle =>

    What is SQL =>

    What is PL/SQL =>

    What is Procedural Option =>

    What products are available from Oracle =>

    What Public Domain interfaces are there =>

    What third party interfaces are available =>

    How portable are Oracle applications to other RDBMS =>

    What Query Optimisers are there =>

    Is there an anonymous FTP site for Oracle stuff =>

    What mail lists are there =>

    What bulletin boards are there =>

    What news groups are there =>

    How does Oracle compare to ... =>

    SQL QUESTIONS

    How can I avoid a divide by zero error =>

    Can I update tables from other tables=>

    Can I remove duplicate rows=>

    Can I update using a view =>

    Are views automatically updated when I update base tables =>Should we use complex views that cruel performance =>

    Can I implement tree structured queries =>

    How can I get information on the row based on group information=>

    How can I get a name for a temporary table that will not clash =>

    How can I find out about what tables, columns and indices there are =>

    Is there a formatter for SQL statements =>

    What is the DUAL table =>

    What is the difference between CHAR and VARCHAR =>

    What is ROWNUM good for =>

    How do I get a top ten =>

    How can I control the rollback segment I use =>

    How can I order a union =>

    How can I rename a column =>

    Who are SCOTT/TIGER, SYSTEM and SYS =>

    Who do various access methods compare =>

    What are clusters =>

    How can I update a big table without blowing rollback segments =>

  • 8/11/2019 Ram PLSQL Questions

    24/25

    Why don?t I get records for the date I want =>

    SQL*PLUS QUESTIONS

    How can I control the startup configuration of SQL*Plus =>

    Can I get a column value into a substitution variable =>

    How can I change the (hated default) editor to my favorite =>

    What is the difference between & and && =>What is the difference between ?host? and ?!? =>

    Why can?t I use a table name in a substitution variable =>

    How can I see all of a LONG =>

    How can I force a column to begin on the left of the page =>

    Can I alias SQL commands =>

    Can I escape significant punctuation marks =>

    SQL*FORMS 3 QUESTIONS

    How can I get a list of values from a hard coded list =>

    How can I get find to look at description with list of values =>

    Can I edit SQL*Forms code with my text editor =>

    Can I edit SQL*Forms code by updating the database =>

    Why can?t I see data in a control field =>

    Why is my terminal scrambled in a user exit =>

    What happens to LONGs =>

    What are user-written form level functions =>

    How can I use regular expressions for field validation =>

    What is a user-exit =>

    How can I call a popup window for field validation =>

    SQL*FORMS 4 QUESTIONS

    What new features can be expected in forms 4 generator from CASE =>

    What new features can be expected in forms 4 =>PRO*C QUESTIONS

    Why are my C variables overwritten =>

    Can I use C preprocessor definitions for VARCHAR size =>

    What can I do about ?line too long? errors with version control=>

    Why do my compiles crash or weird things happen =>

    How do I use OPS$login =>

    CASE QUESTIONS

    Can CASE generate forms with owner prepended to table names =>

    Can CASE generate V7 databases =>

    UNIX QUESTIONS

    Can I create a compressed export on the fly without needing to have the space for both the

    export file and the compressed file=>

    How can I prevent trailing spaces in a spooled report =>

    How can I get an environmental variable into SQL*Plus variables=>

    Can I pipe stuff through SQL*Plus =>

    Why do Pro*C compiles or programs crash on my Sun =>

    How can I find a lost Oracle export file =>

  • 8/11/2019 Ram PLSQL Questions

    25/25

    How can I tell make about SQL*Forms =>

    MISC QUESTIONS

    How can I alter table storage parameters from an export file=>

    What makes the best Oracle server for a network =>

    How can I implement version control =>

    What books are available about Oracle =>