DATABASE & SQL/PLSQL

Oacle interview Questions And Answer
Previous Home Next
To see current user name ?
       SQL> show user;
      

Change SQL prompt name ?

SQL> set sqlprompt “R4R > “

Switch to DOS prompt ?

      SQL> host;
      

How do I eliminate the duplicate rows ?

SQL> delete from table_name where rowid not in (select max(rowid) 
from table group by duplicate_values_field_name); 

or

SQL> delete duplicate_values_field_name dv from 
table_name ta where rowid 
<(select min(rowid) from table_name tb where ta.dv=tb.dv); 

Example.

  • Table Emp
  • Empno Ename
  • 101 Scott
  • 102 Jiyo
  • 103 Millor
  • 104 Jiyo
  • 105 Smith
delete ename from emp a where rowid < ( select min(rowid) 
from emp b where a.ename = b.ename); 
  • The output like,
  • Empno Ename
  • 101 Scott
  • 102 Millor
  • 103 Jiyo
  • 104 Smith

How do I display row number with records?

To achive this use rownum pseudocolumn with query, like

SQL> select rownum, ename from emp; 
output
  1. Scott
  2. Millor
  3. Jiyo
  4. Smith
Display the records between two range

select rownum, empno, ename from emp where rowid in
(select rowid from emp where rownum <=&upto minus
select rowid from emp where rownum<&Start); 
  • Enter value for upto: 10
  • Enter value for Start: 7
ROWNUMEMPNOENAME
1 7782 CLARK
2 7788 SCOTT
3 7839 KING
4 7844 TURNER

I know the nvl function only allows the same data type(ie. number or char or date Nvl(comm, 0)), if commission is null then the text “Not Applicable” want to display, instead of blank space. How do I write the query?

SQL> select nvl(to_char(comm.),'NA') from emp;
output:

NVL(TO_CHAR(COMM),'NA')

    NA 300 500 NA 1400 NA

Oracle cursor ?

    Implicit & Explicit cursors

    Oracle uses work areas called private SQL areas to create SQL statements.

    PL/SQL construct to identify each and every work are used, is called as Cursor.

    For SQL queries returning a single row, PL/SQL declares all implicit cursors.

    For queries that returning more than one row, the cursor needs to be explicitly declared.

Implicit Cursor attributes ?

Same as explicit cursor but prefixed by the word SQL SQL%Found, SQL%NOTFOUND, SQL%ROWCOUNT, SQL%ISOPEN

Tips:
  1. Here SQL%ISOPEN is false, because oracle automatically closed the implicit cursor after executing SQL statements.
  2. All are Boolean attributes.

Find out nth highest salary from emp table

SELECT DISTINCT (a.sal) FROM EMP A WHERE &N = (SELECT COUNT (DISTINCT (b.sal))
 FROM EMP B WHERE a.sal<=b.sal); 

Enter value for n: 2

SAL

3700

To view installed Oracle version information

 SQL> select banner from v$version;
Previous Home Next