Saturday, October 18, 2014

SQL Important Queries


SQL Important  QUERIES

1)   To find the nth row of a table
SQL> Select *from emp where rowid = (select max(rowid) from emp where rownum <= 4);
Or
   SQL> Select *from emp where rownum <= 4 minus select *from emp where rownum <= 3;

2)   To find duplicate rows
SQL> Select *from emp where rowid in (select max(rowid) from emp group by empno,          
         ename, mgr, job, hiredate, comm, deptno, sal);
Or
 SQL> Select empno,ename,sal,job,hiredate,comm , count(*) from emp group by
         empno,ename,sal,job,hiredate,comm  having count(*) >=1;

3)   To delete duplicate rows
      SQL> Delete emp where rowid in (select max(rowid) from emp group by
              empno,ename,mgr,job,hiredate,sal,comm,deptno having count(*)>1);

4)   To find the count of duplicate rows
      SQL> Select ename, count(*) from emp group by ename having count(*) >= 1;

5)   How to display alternative rows in a table?
          SQL> select *from emp where (rowid,0) in (select rowid,mod(rownum,2) from emp);

6)   Getting employee details of each department who is drawing maximum sal?
       SQL> select *from emp where (deptno,sal) in
               ( select deptno,max(sal)  from emp group by deptno);

7)   How to get number of employees in each department  , in which department is having more than 2500 employees?
       SQL> Select deptno,count(*) from emp group by  deptno having count(*) >2500;

           9) To reset the time to the beginning of the day

                  SQL> Select to_char(trunc(sysdate),’dd-mon-yyyy hh:mi:ss am’) from dual;

10) To find nth maximum sal
  SQL> Select *from emp where sal in (select max(sal) from (select *from emp order by sal)
          where rownum <= 5);

Oracle SQL SET Commands with examples



SET COMMANDS

These commands does not require statement terminator and applicable to the sessions , those will be automatically cleared when session was closed.

LINESIZE

This will be used to set the linesize. Default linesize is 80.

Syntax:
            Set linesize <value>

Ex:
            SQL> set linesize 100

PAGESIZE

This will be used to set the pagesize. Default pagesize is 14.

Syntax:
            Set pagesize <value>

Ex:
            SQL> set pagesize 30

DESCRIBE

This will be used to see the object’s structure.

Syntax:
            Describe or desc <object_name>

Ex:
            SQL> desc dept
           
Name                                                              Null?                    Type
----------------------------------------------------------------- ---------------------
DEPTNO                                                            NOT NULL  NUMBER(2)
DNAME                                                                                 VARCHAR2(14)
LOC                                                                                      VARCHAR2(13)

PAUSE

When the displayed data contains hundreds or thousands of lines, when you select it then it will automatically scrolls and displays the last page data. To prevent this you can use this pause option. By using this it will display the data correspoinding to the pagesize with a break which will continue by hitting the return key. By default this will be off.

Syntax:
            Set pause on | off

Ex:
            SQL> set pause on

FEEDBACK

This will give the information regarding howmany rows you selected the object. By default the feedback message will be displayed, only when the object contains more than 5 rows.

Syntax:
            Set feedback <value>

Ex:
            SQL> set feedback 4
SQL> select * from dept;

 DEPTNO    DNAME         LOC
---------- -------------- -------------
        10   ACCOUNTING   NEW YORK
        20   RESEARCH        DALLAS
        30   SALES               CHICAGO
        40   OPERATIONS    BOSTON

4 rows selected.

HEADING

If you want to display data without headings, then you can achieve with this. By default heading is on.

Syntax:
            Set heading on | off

Ex:
            SQL> set heading off
SQL> select * from dept;

        10   ACCOUNTING   NEW YORK
        20   RESEARCH        DALLAS
        30   SALES               CHICAGO
        40   OPERATIONS    BOSTON

SERVEROUTPUT

This will be used to display the output of the PL/SQL programs. By default this will be off.

Syntax:
            Set serveroutput on | off

Ex:
            SQL> set serveroutput on

TIME

This will be used to display the time. By default this will be off.
Syntax:
            Set time on | off

Ex:
            SQL> set time on
19:56:33 SQL>

TIMING

This will give the time taken to execute the current SQL statement. By default this will be off.

Syntax:
            Set timing on | off

Ex:
            SQL> set timing on
SQL> select * from dept;

 DEPTNO    DNAME         LOC
---------- -------------- -------------
        10   ACCOUNTING   NEW YORK
        20   RESEARCH        DALLAS
        30   SALES               CHICAGO
        40   OPERATIONS    BOSTON

Elapsed: 00:00:00.06

SQLPROMPT

This will be used to change the SQL prompt.

Syntax:
            Set sqlprompt <prompt>


Ex:
SQL> set sqlprompt 'ORACLE>'
ORACLE>

SQLCASE

This will be used to change the case of the SQL statements. By default the case is mixed.

Syntax:
            Set sqlcase upper | mixed | lower

Ex:
SQL> set sqlcase upper

SQLTERMINATOR

This will be used to change the terminator of the SQL statements. By default the terminator is ;.

Syntax:
            Set sqlterminator <termination_character>

Ex:
SQL> set sqlterminator :
SQL> select * from dept:

DEFINE

By default if the & character finds then it will treat as bind variable and ask for the input. Suppose your want to treat it as a normal character while inserting data, then you can prevent this by using the define option. By default this will be on

Syntax:
            Set define on | off

Ex:
            SQL>insert into dept values(50,'R&D','HYD');
Enter value for d:
old   1: insert into dept values(50,'R&D','HYD')
new   1: INSERT INTO DEPT VALUES(50,'R','HYD')

            SQL> set define off
            SQL>insert into dept values(50,'R&D','HYD');               -- here it won’t ask for value

ECHO
VERIFY
NEWPAGE
HEADSEP
PNO