Oracle PL/SQL/Select Query/Order By

Материал из SQL эксперт
Перейти к: навигация, поиск

DESC for descending

   <source lang="sql">
 

SQL> SQL> create table registrations

 2  ( attendee    NUMBER(4)
 3  , course      VARCHAR2(6)
 4  , begindate   DATE
 5  , evaluation  NUMBER(1)
 6  , constraint  R_PK        primary key
 7                            (attendee,course,begindate)
 8  ) ;

Table created. SQL> SQL> insert into registrations values (2,"SQL",date "2009-04-12",4 ); 1 row created. SQL> insert into registrations values (14,"SQL",date "2009-04-12",5 ); 1 row created. SQL> insert into registrations values (6,"SQL",date "2009-04-12",4 ); 1 row created. SQL> insert into registrations values (11,"SQL",date "2009-04-12",2 ); 1 row created. SQL> insert into registrations values (8,"SQL",date "2009-10-04",NULL); 1 row created. SQL> insert into registrations values (9,"SQL",date "2009-10-04",3 ); 1 row created. SQL> insert into registrations values (13,"SQL",date "2009-10-04",4 ); 1 row created. SQL> insert into registrations values (13,"SQL",date "2009-12-13",NULL); 1 row created. SQL> insert into registrations values (6,"SQL",date "2009-12-13",NULL); 1 row created. SQL> insert into registrations values (3,"OAU",date "2009-08-10",4 ); 1 row created. SQL> insert into registrations values (12,"OAU",date "2009-08-10",4 ); 1 row created. SQL> insert into registrations values (13,"OAU",date "2009-08-10",5 ); 1 row created. SQL> SQL> SQL> select evaluation

 2  from   registrations
 3  where  attendee = 8
 4  order  by evaluation DESC;

EVALUATION


SQL> SQL> drop table registrations; Table dropped.


 </source>
   
  


Get order number by using rownum column

   <source lang="sql">
  

SQL> SQL> CREATE TABLE EMP (EMPNO NUMBER(4) NOT NULL,

 2                    ENAME VARCHAR2(10),
 3                    JOB VARCHAR2(9),
 4                    MGR NUMBER(4),
 5                    HIREDATE DATE,
 6                    SAL NUMBER(7, 2),
 7                    COMM NUMBER(7, 2),
 8                    DEPTNO NUMBER(2));

Table created. SQL> SQL> INSERT INTO EMP VALUES (7369, "SMITH", "CLERK", 7902, TO_DATE("17-DEC-1980", "DD-MON-YYYY"), 800, NULL, 20); 1 row created. SQL> INSERT INTO EMP VALUES (7499, "ALLEN", "SALESMAN", 7698, TO_DATE("20-FEB-1981", "DD-MON-YYYY"), 1600, 300, 30); 1 row created. SQL> INSERT INTO EMP VALUES (7521, "WARD", "SALESMAN", 7698, TO_DATE("22-FEB-1981", "DD-MON-YYYY"), 1250, 500, 30); 1 row created. SQL> INSERT INTO EMP VALUES (7566, "JONES", "MANAGER", 7839, TO_DATE("2-APR-1981", "DD-MON-YYYY"), 2975, NULL, 20); 1 row created. SQL> INSERT INTO EMP VALUES (7654, "MARTIN", "SALESMAN", 7698,TO_DATE("28-SEP-1981", "DD-MON-YYYY"), 1250, 1400, 30); 1 row created. SQL> INSERT INTO EMP VALUES (7698, "BLAKE", "MANAGER", 7839,TO_DATE("1-MAY-1981", "DD-MON-YYYY"), 2850, NULL, 30); 1 row created. SQL> INSERT INTO EMP VALUES (7782, "CLARK", "MANAGER", 7839,TO_DATE("9-JUN-1981", "DD-MON-YYYY"), 2450, NULL, 10); 1 row created. SQL> INSERT INTO EMP VALUES (7788, "SCOTT", "ANALYST", 7566,TO_DATE("09-DEC-1982", "DD-MON-YYYY"), 3000, NULL, 20); 1 row created. SQL> INSERT INTO EMP VALUES (7839, "KING", "PRESIDENT", NULL,TO_DATE("17-NOV-1981", "DD-MON-YYYY"), 5000, NULL, 10); 1 row created. SQL> INSERT INTO EMP VALUES (7844, "TURNER", "SALESMAN", 7698,TO_DATE("8-SEP-1981", "DD-MON-YYYY"), 1500, 0, 30); 1 row created. SQL> INSERT INTO EMP VALUES (7876, "ADAMS", "CLERK", 7788,TO_DATE("12-JAN-1983", "DD-MON-YYYY"), 1100, NULL, 20); 1 row created. SQL> INSERT INTO EMP VALUES (7900, "JAMES", "CLERK", 7698,TO_DATE("3-DEC-1981", "DD-MON-YYYY"), 950, NULL, 30); 1 row created. SQL> INSERT INTO EMP VALUES (7902, "FORD", "ANALYST", 7566,TO_DATE("3-DEC-1981", "DD-MON-YYYY"), 3000, NULL, 20); 1 row created. SQL> INSERT INTO EMP VALUES (7934, "MILLER", "CLERK", 7782,TO_DATE("23-JAN-1982", "DD-MON-YYYY"), 1300, NULL, 10); 1 row created. SQL> SQL> SELECT ROWNUM, ename

 2  FROM   emp;
   ROWNUM ENAME

----------
        1 SMITH
        2 ALLEN
        3 WARD
        4 JONES
        5 MARTIN
        6 BLAKE
        7 CLARK
        8 SCOTT
                                                                                                                                     Page           1
   ROWNUM ENAME

----------
        9 KING
       10 TURNER
       11 ADAMS
       12 JAMES
       13 FORD
       14 MILLER


                                                                                                                                     Page           2

14 rows selected. SQL> SQL> SQL> drop table emp; Table dropped.


 </source>
   
  


==order by/td>




   <source lang="sql">
  

SQL> SQL> CREATE TABLE employees

 2  ( employee_id          number(10)      not null,
 3    last_name            varchar2(50)      not null,
 4    email                varchar2(30),
 5    hire_date            date,
 6    job_id               varchar2(30),
 7    department_id        number(10),
 8    salary               number(6),
 9    manager_id           number(6)
10  );

Table created. SQL> SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary,department_id ,manager_id)

 2                values ( 1001, "Lawson", "lawson@g.ru", "01-JAN-2002","MGR", 30000,1 ,1004);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)

 2                values ( 1002, "Wells", "wells@g.ru", "01-JAN-2002", "DBA", 20000,2, 1005 );

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)

 2                 values( 1003, "Bliss", "bliss@g.ru", "01-JAN-2002", "PROG", 24000,3 ,1004);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1004,  "Kyte", "tkyte@a.ru", SYSDATE-3650, "MGR",25000 ,4, 1005);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1005, "Viper", "sdillon@a .ru", SYSDATE, "PROG", 20000, 1, 1006);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id,manager_id)

 2                 values( 1006, "Beck", "clbeck@g.ru", SYSDATE, "PROG", 20000, 2, null);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1007, "Java", "java01@g.ru", SYSDATE, "PROG", 20000, 3, 1006);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1008, "Oracle", "oracle1@g.ru", SYSDATE, "DBA", 20000, 4, 1006);

1 row created. SQL> SQL> select * from employees; EMPLOYEE_ID LAST_NAME EMAIL HIRE_DATE JOB_ID DEPARTMENT_ID SALARY MANAGER_ID


-------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
      1001 Lawson                                             lawson@g.ru                   01-JAN-02 MGR                                        1      30000       1004
      1002 Wells                                              wells@g.ru                    01-JAN-02 DBA                                        2      20000       1005
      1003 Bliss                                              bliss@g.ru                    01-JAN-02 PROG                                       3      24000       1004
      1004 Kyte                                               tkyte@a.ru                    13-JUN-98 MGR                                        4      25000       1005
      1005 Viper                                              sdillon@a .ru                 10-JUN-08 PROG                                       1      20000       1006
      1006 Beck                                               clbeck@g.ru                   10-JUN-08 PROG                                       2      20000
      1007 Java                                               java01@g.ru                   10-JUN-08 PROG                                       3      20000       1006

EMPLOYEE_ID LAST_NAME EMAIL HIRE_DATE JOB_ID DEPARTMENT_ID SALARY MANAGER_ID


-------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
      1008 Oracle                                             oracle1@g.ru                  10-JUN-08 DBA                                        4      20000       1006

8 rows selected. SQL> SQL> SQL> SQL> select last_name, email, hire_date

 2  from employees
 3  order by 3, 2, 1
 4  /

LAST_NAME EMAIL HIRE_DATE


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

Kyte tkyte@a.ru 13-JUN-98 Bliss bliss@g.ru 01-JAN-02 Lawson lawson@g.ru 01-JAN-02 Wells wells@g.ru 01-JAN-02 Beck clbeck@g.ru 10-JUN-08 Java java01@g.ru 10-JUN-08 Viper sdillon@a .ru 10-JUN-08 LAST_NAME EMAIL HIRE_DATE


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

Oracle oracle1@g.ru 10-JUN-08 8 rows selected.

SQL> SQL> select * from employees; EMPLOYEE_ID LAST_NAME EMAIL HIRE_DATE JOB_ID DEPARTMENT_ID SALARY MANAGER_ID


-------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
      1001 Lawson                                             lawson@g.ru                   01-JAN-02 MGR                                        1      30000       1004
      1002 Wells                                              wells@g.ru                    01-JAN-02 DBA                                        2      20000       1005
      1003 Bliss                                              bliss@g.ru                    01-JAN-02 PROG                                       3      24000       1004
      1004 Kyte                                               tkyte@a.ru                    13-JUN-98 MGR                                        4      25000       1005
      1005 Viper                                              sdillon@a .ru                 10-JUN-08 PROG                                       1      20000       1006
      1006 Beck                                               clbeck@g.ru                   10-JUN-08 PROG                                       2      20000
      1007 Java                                               java01@g.ru                   10-JUN-08 PROG                                       3      20000       1006

EMPLOYEE_ID LAST_NAME EMAIL HIRE_DATE JOB_ID DEPARTMENT_ID SALARY MANAGER_ID


-------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
      1008 Oracle                                             oracle1@g.ru                  10-JUN-08 DBA                                        4      20000       1006

8 rows selected. SQL> SQL> SQL> drop table employees; Table dropped. SQL>


 </source>
   
  


ORDER BY clause with more than one column:by the first column, and within that column, orders by the second

   <source lang="sql">
 

SQL> SQL> SQL> -- create demo table SQL> create table Employee(

 2    ID                 VARCHAR2(4 BYTE)         NOT NULL,
 3    First_Name         VARCHAR2(10 BYTE),
 4    Last_Name          VARCHAR2(10 BYTE),
 5    Start_Date         DATE,
 6    End_Date           DATE,
 7    Salary             Number(8,2),
 8    City               VARCHAR2(10 BYTE),
 9    Description        VARCHAR2(15 BYTE)
10  )
11  /

Table created. SQL> SQL> -- prepare data SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2               values ("01","Jason",    "Martin",  to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto",  "Programmer")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("02","Alison",   "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 2334.78, "Vancouver","Tester")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("03","James",    "Smith",   to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 2334.78, "Vancouver","Tester")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("04","Celia",    "Rice",    to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2334.78, "Vancouver","Manager")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("05","Robert",   "Black",   to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("06","Linda",    "Green",   to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 2334.78,"New York",  "Tester")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("07","David",    "Larry",   to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 2334.78,"New York",  "Manager")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("08","James",    "Cat",     to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 2334.78,"Vancouver", "Tester")
 3  /

1 row created. SQL> SQL> SQL> SQL> -- display data in the table SQL> select * from Employee

 2  /

ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION


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

01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer 02 Alison Mathews 21-MAR-76 21-FEB-86 2334.78 Vancouver Tester 03 James Smith 12-DEC-78 15-MAR-90 2334.78 Vancouver Tester 04 Celia Rice 24-OCT-82 21-APR-99 2334.78 Vancouver Manager 05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester 06 Linda Green 30-JUL-87 04-JAN-96 2334.78 New York Tester 07 David Larry 31-DEC-90 12-FEB-98 2334.78 New York Manager 08 James Cat 17-SEP-96 15-APR-02 2334.78 Vancouver Tester 8 rows selected. SQL> SQL> SQL> SQL> SQL> SQL> SQL> --ORDER BY clause with more than one column:by the first column, and within that column, orders by the second SQL> SQL> SELECT ID FROM Employee

 2  ORDER BY Salary, City;

ID


01 07 06 05 08 03 02 04 8 rows selected. SQL> SQL> SQL> SQL> SQL> SQL> SQL> -- clean the table SQL> drop table Employee

 2  /

Table dropped. SQL> SQL> SQL> SQL>


 </source>
   
  


Order by columns from different tables

   <source lang="sql">
 

SQL> SQL> create table emp

 2  ( empno      NUMBER(4)    constraint E_PK primary key
 3  , ename      VARCHAR2(8)
 4  , init       VARCHAR2(5)
 5  , job        VARCHAR2(8)
 6  , mgr        NUMBER(4)
 7  , bdate      DATE
 8  , sal        NUMBER(6,2)
 9  , comm       NUMBER(6,2)
10  , deptno     NUMBER(2)    default 10
11  ) ;

Table created. SQL> insert into emp values(1,"Tom","N", "Coder", 13,date "1965-12-17", 800 , NULL, 20); 1 row created. SQL> insert into emp values(2,"Jack","JAM", "Tester",6,date "1961-02-20", 1600, 300, 30); 1 row created. SQL> insert into emp values(3,"Wil","TF" , "Tester",6,date "1962-02-22", 1250, 500, 30); 1 row created. SQL> insert into emp values(4,"Jane","JM", "Designer", 9,date "1967-04-02", 2975, NULL, 20); 1 row created. SQL> insert into emp values(5,"Mary","P", "Tester",6,date "1956-09-28", 1250, 1400, 30); 1 row created. SQL> insert into emp values(6,"Black","R", "Designer", 9,date "1963-11-01", 2850, NULL, 30); 1 row created. SQL> insert into emp values(7,"Chris","AB", "Designer", 9,date "1965-06-09", 2450, NULL, 10); 1 row created. SQL> insert into emp values(8,"Smart","SCJ", "Coder", 4,date "1959-11-26", 3000, NULL, 20); 1 row created. SQL> insert into emp values(9,"Peter","CC", "Designer",NULL,date "1952-11-17", 5000, NULL, 10); 1 row created. SQL> insert into emp values(10,"Take","JJ", "Tester",6,date "1968-09-28", 1500, 0, 30); 1 row created. SQL> insert into emp values(11,"Ana","AA", "Coder", 8,date "1966-12-30", 1100, NULL, 20); 1 row created. SQL> insert into emp values(12,"Jane","R", "Manager", 6,date "1969-12-03", 800 , NULL, 30); 1 row created. SQL> insert into emp values(13,"Fake","MG", "Coder", 4,date "1959-02-13", 3000, NULL, 20); 1 row created. SQL> insert into emp values(14,"Mike","TJA","Manager", 7,date "1962-01-23", 1300, NULL, 10); 1 row created. SQL> SQL> create table departments

 2  ( deptno NUMBER(2)     constraint D_PK
 3                         primary key
 4  , dname  VARCHAR2(10)
 5  , location VARCHAR2(8)
 6  , mgr    NUMBER(4)
 7  ) ;

Table created. SQL> SQL> insert into departments values (10,"ACCOUNTING","NEW YORK",7); 1 row created. SQL> insert into departments values (20,"TRAINING", "DALLAS", 4); 1 row created. SQL> insert into departments values (30,"SALES", "CHICAGO", 6); 1 row created. SQL> insert into departments values (40,"HR", "BOSTON", 9); 1 row created. SQL> SQL> select d.deptno, d.location

 2  ,      e.ename, e.init
 3  from   emp e, departments d
 4  where  e.deptno = d.deptno
 5  order  by d.deptno, e.ename;
   DEPTNO LOCATION ENAME    INIT

-------- -------- -----
       10 NEW YORK Chris    AB
       10 NEW YORK Mike     TJA
       10 NEW YORK Peter    CC
       20 DALLAS   Ana      AA
       20 DALLAS   Fake     MG
       20 DALLAS   Jane     JM
       20 DALLAS   Smart    SCJ
       20 DALLAS   Tom      N
       30 CHICAGO  Black    R
       30 CHICAGO  Jack     JAM
       30 CHICAGO  Jane     R
       30 CHICAGO  Mary     P
       30 CHICAGO  Take     JJ
       30 CHICAGO  Wil      TF

14 rows selected. SQL> SQL> SQL> drop table emp; Table dropped. SQL> drop table departments; Table dropped.


 </source>
   
  


Order by date value then by number value

   <source lang="sql">
  

SQL> SQL> SQL> CREATE TABLE product (

 2       product_name     VARCHAR2(25),
 3       product_price    NUMBER(4,2),
 4       quantity_on_hand NUMBER(5,0),
 5       last_stock_date  DATE);

Table created. SQL> SQL> INSERT INTO product VALUES ("Small Widget", 99, 1, "15-JAN-03"); 1 row created. SQL> INSERT INTO product VALUES ("Wodget", 75, 1000, "15-JAN-02"); 1 row created. SQL> INSERT INTO product VALUES ("Product 1", 50, 100, "15-JAN-03"); 1 row created. SQL> INSERT INTO product VALUES ("Product 2", 25, 10000, null); 1 row created. SQL> SQL> SELECT * FROM product ORDER BY last_stock_date, product_name; PRODUCT_NAME PRODUCT_PRICE QUANTITY_ON_HAND LAST_STOC


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

Wodget 75 1000 15-JAN-02 Product 1 50 100 15-JAN-03 Small Widget 99 1 15-JAN-03 Product 2 25 10000 4 rows selected. SQL> SQL> SQL> DROP TABLE product; Table dropped. SQL> SQL>


 </source>
   
  


Order by index

   <source lang="sql">
 

SQL> SQL> SQL> SQL> create table t (

 2   item number,
 3   bin number,
 4   primary key (bin , item)
 5  );

Table created. SQL> SQL> insert into t values(2, 34353); 1 row created. SQL> insert into t values(45,34353); 1 row created. SQL> insert into t values(76,34353); 1 row created. SQL> insert into t values(76,35667); 1 row created. SQL> insert into t values(89,35667); 1 row created. SQL> SQL> SQL> select * from t order by 1,2;

     ITEM        BIN

----------
        2      34353
       45      34353
       76      34353
       76      35667
       89      35667

5 rows selected. SQL> SQL> select bin, count(*), count(*)/cnt

 2  from ( select bin, count(distinct item) over () AS cnt
 3         from t
 4         where item in (2,45,76,89) )
 5  group by bin, cnt
 6  /
      BIN   COUNT(*) COUNT(*)/CNT

---------- ------------
    34353          3          .75
    35667          2           .5

2 rows selected. SQL> SQL> SQL> drop table t; Table dropped. SQL>


 </source>
   
  


Order by job title ascending but birthday descending

   <source lang="sql">
 

SQL> SQL> SQL> create table emp

 2  ( empno      NUMBER(4)    constraint E_PK primary key
 3  , ename      VARCHAR2(8)
 4  , init       VARCHAR2(5)
 5  , job        VARCHAR2(8)
 6  , mgr        NUMBER(4)
 7  , bdate      DATE
 8  , sal        NUMBER(6,2)
 9  , comm       NUMBER(6,2)
10  , deptno     NUMBER(2)    default 10
11  ) ;

Table created. SQL> insert into emp values(1,"Tom","N", "TRAINER", 13,date "1965-12-17", 800 , NULL, 20); 1 row created. SQL> insert into emp values(2,"Jack","JAM", "Tester",6,date "1961-02-20", 1600, 300, 30); 1 row created. SQL> insert into emp values(3,"Wil","TF" , "Tester",6,date "1962-02-22", 1250, 500, 30); 1 row created. SQL> insert into emp values(4,"Jane","JM", "Designer", 9,date "1967-04-02", 2975, NULL, 20); 1 row created. SQL> insert into emp values(5,"Mary","P", "Tester",6,date "1956-09-28", 1250, 1400, 30); 1 row created. SQL> insert into emp values(6,"Black","R", "Designer", 9,date "1963-11-01", 2850, NULL, 30); 1 row created. SQL> insert into emp values(7,"Chris","AB", "Designer", 9,date "1965-06-09", 2450, NULL, 10); 1 row created. SQL> insert into emp values(8,"Smart","SCJ", "TRAINER", 4,date "1959-11-26", 3000, NULL, 20); 1 row created. SQL> insert into emp values(9,"Peter","CC", "Designer",NULL,date "1952-11-17", 5000, NULL, 10); 1 row created. SQL> insert into emp values(10,"Take","JJ", "Tester",6,date "1968-09-28", 1500, 0, 30); 1 row created. SQL> insert into emp values(11,"Ana","AA", "TRAINER", 8,date "1966-12-30", 1100, NULL, 20); 1 row created. SQL> insert into emp values(12,"Jane","R", "Manager", 6,date "1969-12-03", 800 , NULL, 30); 1 row created. SQL> insert into emp values(13,"Fake","MG", "TRAINER", 4,date "1959-02-13", 3000, NULL, 20); 1 row created. SQL> insert into emp values(14,"Mike","TJA","Manager", 7,date "1962-01-23", 1300, NULL, 10); 1 row created. SQL> SQL> select *

 2  from   emp
 3  order  by job, bdate desc;

Enter...

    4 Jane     JM    Designer      9 02-04-1967   2975  [N/A]     20
    7 Chris    AB    Designer      9 09-06-1965   2450  [N/A]     10
    6 Black    R     Designer      9 01-11-1963   2850  [N/A]     30
    9 Peter    CC    Designer  [N/A] 17-11-1952   5000  [N/A]     10
   12 Jane     R     Manager       6 03-12-1969    800  [N/A]     30
   14 Mike     TJA   Manager       7 23-01-1962   1300  [N/A]     10
   10 Take     JJ    Tester        6 28-09-1968   1500      0     30
    3 Wil      TF    Tester        6 22-02-1962   1250    500     30
    2 Jack     JAM   Tester        6 20-02-1961   1600    300     30
    5 Mary     P     Tester        6 28-09-1956   1250   1400     30
   11 Ana      AA    TRAINER       8 30-12-1966   1100  [N/A]     20
    1 Tom      N     TRAINER      13 17-12-1965    800  [N/A]     20
    8 Smart    SCJ   TRAINER       4 26-11-1959   3000  [N/A]     20
   13 Fake     MG    TRAINER       4 13-02-1959   3000  [N/A]     20

14 rows selected. SQL> SQL> drop table emp; Table dropped.


 </source>
   
  


Order by name and department number for salary higher than 1500

   <source lang="sql">
 

SQL> SQL> create table emp

 2  ( empno      NUMBER(4)    constraint E_PK primary key
 3  , ename      VARCHAR2(8)
 4  , init       VARCHAR2(5)
 5  , job        VARCHAR2(8)
 6  , mgr        NUMBER(4)
 7  , bdate      DATE
 8  , sal       NUMBER(6,2)
 9  , comm       NUMBER(6,2)
10  , deptno     NUMBER(2)    default 10
11  ) ;

Table created. SQL> insert into emp values(1,"Tom","N", "Coder", 13,date "1965-12-17", 800 , NULL, 20); 1 row created. SQL> insert into emp values(2,"Jack","JAM", "Tester",6,date "1961-02-20", 1600, 300, 30); 1 row created. SQL> insert into emp values(3,"Wil","TF" , "Tester",6,date "1962-02-22", 1250, 500, 30); 1 row created. SQL> insert into emp values(4,"Jane","JM", "Designer", 9,date "1967-04-02", 2975, NULL, 20); 1 row created. SQL> insert into emp values(5,"Mary","P", "Tester",6,date "1956-09-28", 1250, 1400, 30); 1 row created. SQL> insert into emp values(6,"Black","R", "Designer", 9,date "1963-11-01", 2850, NULL, 30); 1 row created. SQL> insert into emp values(7,"Chris","AB", "Designer", 9,date "1965-06-09", 2450, NULL, 10); 1 row created. SQL> insert into emp values(8,"Smart","SCJ", "Coder", 4,date "1959-11-26", 3000, NULL, 20); 1 row created. SQL> insert into emp values(9,"Peter","CC", "Designer",NULL,date "1952-11-17", 5000, NULL, 10); 1 row created. SQL> insert into emp values(10,"Take","JJ", "Tester",6,date "1968-09-28", 1500, 0, 30); 1 row created. SQL> insert into emp values(11,"Ana","AA", "Coder", 8,date "1966-12-30", 1100, NULL, 20); 1 row created. SQL> insert into emp values(12,"Jane","R", "Manager", 6,date "1969-12-03", 800 , NULL, 30); 1 row created. SQL> insert into emp values(13,"Fake","MG", "Coder", 4,date "1959-02-13", 3000, NULL, 20); 1 row created. SQL> insert into emp values(14,"Mike","TJA","Manager", 7,date "1962-01-23", 1300, NULL, 10); 1 row created. SQL> SQL> select deptno, ename, init, sal

 2  from   emp
 3  where  sal < 1500
 4  order  by deptno, ename;
   DEPTNO ENAME    INIT         SAL

-------- ----- ----------
       10 Mike     TJA         1300
       20 Ana      AA          1100
       20 Tom      N            800
       30 Jane     R            800
       30 Mary     P           1250
       30 Wil      TF          1250

6 rows selected. SQL> SQL> SQL> drop table emp; Table dropped.


 </source>
   
  


Order by price which is more than 50

   <source lang="sql">
 

SQL> SQL> create table gift(

 2           gift_id                integer         primary key
 3          ,emp_id                integer
 4          ,register_date              date not null
 5          ,total_price        number(7,2)
 6          ,deliver_date           date
 7          ,deliver_time           varchar2(7)
 8          ,payment        varchar2(2)
 9          ,emp_no                 number(3,0)
10          ,deliver_name           varchar2(35)
11          ,message        varchar2(100)
12  );

Table created. SQL> SQL> insert into gift(gift_id,emp_id,register_date,total_price,deliver_date,deliver_time,payment,emp_no,deliver_name,message)values

 2                 (1,1,"14-Feb-1999", 123.12, "14-Feb-1999", "12 noon", "CA",1, null, "Happy Birthday to you");

1 row created. SQL> insert into gift(gift_id ,emp_id ,register_date ,total_price ,deliver_date ,deliver_time ,payment ,emp_no,deliver_name ,message)values

 2                 (2,1,"14-Feb-1999", 50.98, "14-feb-1999", "1 pm", "CA",7, "name1", "Happy Birthday");

1 row created. SQL> insert into gift(gift_id ,emp_id ,register_date ,total_price ,deliver_date ,deliver_time,payment ,emp_no,deliver_name ,message )values

 2                 (3, 2,"14-Feb-1999", 35.99, "14-feb-1999", "1 pm", "VS",2, "Tom", "Happy Birthday");

1 row created. SQL> insert into gift(gift_id ,emp_id ,register_date ,total_price ,deliver_date ,deliver_time,payment ,emp_no,deliver_name ,message )values

 2                 (4, 2,"14-Feb-1999", 19.95, "14-feb-1999", "5 pm", "CA",2, "Mary", "Happy Birthday");

1 row created. SQL> insert into gift(gift_id ,emp_id ,register_date ,total_price ,deliver_date ,deliver_time,payment ,emp_no,deliver_name ,message)values

 2                 (5, 6,"4-mar-1999", 10.95, "5-mar-1999", "4:30 pm", "VS", 2, "Jack", "Happy Birthday");

1 row created. SQL> SQL> SQL> select gift_id, total_price

 2    from gift
 3   where total_price > 50
 4   order by total_price desc;
  GIFT_ID TOTAL_PRICE

-----------
        1      123.12
        2       50.98

2 rows selected. SQL> SQL> drop table gift; Table dropped.


 </source>
   
  


Order by, range unbounded preceding

   <source lang="sql">
 

SQL> SQL> create table emp

 2  ( empno      NUMBER(4)    constraint E_PK primary key
 3  , ename      VARCHAR2(8)
 4  , init       VARCHAR2(5)
 5  , job        VARCHAR2(8)
 6  , mgr        NUMBER(4)
 7  , bdate      DATE
 8  , sal       NUMBER(6,2)
 9  , comm       NUMBER(6,2)
10  , deptno     NUMBER(2)    default 10
11  ) ;

Table created. SQL> insert into emp values(1,"Tom","N", "TRAINER", 13,date "1965-12-17", 800 , NULL, 20); 1 row created. SQL> insert into emp values(2,"Jack","JAM", "Tester",6,date "1961-02-20", 1600, 300, 30); 1 row created. SQL> insert into emp values(3,"Wil","TF" , "Tester",6,date "1962-02-22", 1250, 500, 30); 1 row created. SQL> insert into emp values(4,"Jane","JM", "Designer", 9,date "1967-04-02", 2975, NULL, 20); 1 row created. SQL> insert into emp values(5,"Mary","P", "Tester",6,date "1956-09-28", 1250, 1400, 30); 1 row created. SQL> insert into emp values(6,"Black","R", "Designer", 9,date "1963-11-01", 2850, NULL, 30); 1 row created. SQL> insert into emp values(7,"Chris","AB", "Designer", 9,date "1965-06-09", 2450, NULL, 10); 1 row created. SQL> insert into emp values(8,"Smart","SCJ", "TRAINER", 4,date "1959-11-26", 3000, NULL, 20); 1 row created. SQL> insert into emp values(9,"Peter","CC", "Designer",NULL,date "1952-11-17", 5000, NULL, 10); 1 row created. SQL> insert into emp values(10,"Take","JJ", "Tester",6,date "1968-09-28", 1500, 0, 30); 1 row created. SQL> insert into emp values(11,"Ana","AA", "TRAINER", 8,date "1966-12-30", 1100, NULL, 20); 1 row created. SQL> insert into emp values(12,"Jane","R", "Manager", 6,date "1969-12-03", 800 , NULL, 30); 1 row created. SQL> insert into emp values(13,"Fake","MG", "TRAINER", 4,date "1959-02-13", 3000, NULL, 20); 1 row created. SQL> insert into emp values(14,"Mike","TJA","Manager", 7,date "1962-01-23", 1300, NULL, 10); 1 row created. SQL> SQL> SQL> select mgr, ename, sal

 2  ,      sum(sal) over
 3         ( order by mgr, sal
 4           range unbounded preceding
 5         ) as cumulative
 6  from   emp
 7  order  by mgr, sal;
      MGR ENAME           SAL CUMULATIVE

-------- ---------- ----------
        4 Smart          3000       6000
        4 Fake           3000       6000
        6 Jane            800       6800
        6 Wil            1250       9300
        6 Mary           1250       9300
        6 Take           1500      10800
        6 Jack           1600      12400
        7 Mike           1300      13700
        8 Ana            1100      14800
        9 Chris          2450      17250
        9 Black          2850      20100
        9 Jane           2975      23075
       13 Tom             800      23875
          Peter          5000      28875

14 rows selected. SQL> SQL> SQL> drop table emp; Table dropped.


 </source>
   
  


Order by renamed column

   <source lang="sql">
 

SQL> SQL> SQL> SQL> create table gift(

 2           gift_id                integer         primary key
 3          ,emp_id                integer
 4          ,register_date              date not null
 5          ,total_price        number(7,2)
 6          ,deliver_date           date
 7          ,deliver_time           varchar2(7)
 8          ,payment        varchar2(2)
 9          ,emp_no                 number(3,0)
10          ,deliver_name           varchar2(35)
11          ,message        varchar2(100)
12  );

Table created. SQL> SQL> insert into gift(gift_id,emp_id,register_date,total_price,deliver_date,deliver_time,payment,emp_no,deliver_name,message)values

 2                 (1,1,"14-Feb-1999", 123.12, "14-Feb-1999", "12 noon", "CA",1, null, "Happy Birthday to you");

1 row created. SQL> insert into gift(gift_id ,emp_id ,register_date ,total_price ,deliver_date ,deliver_time ,payment ,emp_no,deliver_name ,message)values

 2                 (2,1,"14-Feb-1999", 50.98, "14-feb-1999", "1 pm", "CA",7, "name1", "Happy Birthday");

1 row created. SQL> insert into gift(gift_id ,emp_id ,register_date ,total_price ,deliver_date ,deliver_time,payment ,emp_no,deliver_name ,message )values

 2                 (3, 2,"14-Feb-1999", 35.99, "14-feb-1999", "1 pm", "VS",2, "Tom", "Happy Birthday");

1 row created. SQL> insert into gift(gift_id ,emp_id ,register_date ,total_price ,deliver_date ,deliver_time,payment ,emp_no,deliver_name ,message )values

 2                 (4, 2,"14-Feb-1999", 19.95, "14-feb-1999", "5 pm", "CA",2, "Mary", "Happy Birthday");

1 row created. SQL> insert into gift(gift_id ,emp_id ,register_date ,total_price ,deliver_date ,deliver_time,payment ,emp_no,deliver_name ,message)values

 2                 (5, 6,"4-mar-1999", 10.95, "5-mar-1999", "4:30 pm", "VS", 2, "Jack", "Happy Birthday");

1 row created. SQL> SQL> select gift_id, total_price, total_price * 1.1 "Increased Price" from gift

 2  order by "Increased Price";
  GIFT_ID TOTAL_PRICE Increased Price

----------- ---------------
        5       10.95          12.045
        4       19.95          21.945
        3       35.99          39.589
        2       50.98          56.078
        1      123.12         135.432

5 rows selected. SQL> SQL> drop table gift; Table dropped.


 </source>
   
  


Order by renamed column descendingly

   <source lang="sql">
 

SQL> SQL> create table emp(

 2           emp_no                 integer         primary key,
 3           lastname               varchar2(20)    not null,
 4           firstname              varchar2(15)    not null,
 5           midinit                varchar2(1),
 6           street                 varchar2(30),
 7           city                   varchar2(20),
 8           state                  varchar2(2),
 9           zip                    varchar2(5),
10           shortZipCode                   varchar2(4),
11           area_code              varchar2(3),
12           phone                  varchar2(8),
13           salary                 number(5,2),
14           birthdate              date,
15           startDate              date,
16           title                  varchar2(20),
17           dept_no                integer         ,
18           mgr                    integer,
19           region                 number,
20           division               number,
21           total_sales            number
22  );

Table created. SQL> -- emp Table Inserts: SQL> insert into emp(emp_no, lastname, firstname, midinit, street, city, state, zip,shortZipCode, area_code, phone, birthdate, title)values

 2                      (1,"Z","Joy","R","1 Ave","New York","NY","12122","2333","212","200-1111","12-nov-1976","President");

1 row created. SQL> insert into emp(emp_no, lastname, firstname, midinit, street, city, state, zip,shortZipCode, area_code, phone, salary, birthdate, startDate,title, dept_no, mgr, region, division, total_sales)valu es

 2                      (2,"X","Lucy","J","1 Street","New York","NY","43552","6633","212","234-4444",7.75,"21-mar-1976","1-feb-1994","Sales Manager",2,1,100,10,40000);

1 row created. SQL> insert into emp(emp_no, lastname, firstname, midinit, street, city, state, zip,shortZipCode, area_code, phone, salary, birthdate, startDate,title, dept_no, mgr, region, division, total_sales)valu es

 2                      (3,"Y","Jordan","E","1 Drive","New York","NY","76822","8763","212","222-2222",7.75,"14-feb-1963","15-mar-1995","Sales Clerk",2,2,100,10,10000);

1 row created. SQL> insert into emp(emp_no, lastname, firstname, midinit, street, city, state, zip, shortZipCode, area_code, phone, salary, birthdate, startDate, title, dept_no, mgr, region, division, total_sales)va lues

 2                      (4,"Washington","Georgia","J","1 13th Street Apt 14","New York","NY","43122","4333","212","340-4365",11.50,"2-jul-1963","21-apr-1994","Designer",1,1,100,10,40000);

1 row created. SQL> insert into emp(emp_no, lastname, firstname, midinit, street, city, state, zip, shortZipCode, area_code, phone, salary, birthdate, startDate, title, dept_no, mgr, region, division, total_sales)va lues

 2                      (5,"Jason","Jordan","J","56 Langer Street","Staten Island","NY","23332","4983","718","777-4365",21.65,"15-may-1958","2-aug-1994","Designer",1,1,100,10,40000);

1 row created. SQL> SQL> SQL> select emp_no,

 2   lastname || ", " || firstname as "Name",
 3   salary * 40 as "Weekly Salary"
 4    from emp
 5   order by "Weekly Salary" desc;
   EMP_NO Name                                  Weekly Salary

------------------------------------- -------------
        1 Z, Joy
        5 Jason, Jordan                                   866
        4 Washington, Georgia                             460
        2 X, Lucy                                         310
        3 Y, Jordan                                       310

5 rows selected. SQL> SQL> SQL> drop table emp; Table dropped.


 </source>
   
  


Order by three columns

   <source lang="sql">
 

SQL> SQL> CREATE TABLE products(

 2    product_id              NUMBER(6),
 3    name            VARCHAR2(50),
 4    subcategory        VARCHAR2(50),
 5    category           VARCHAR2(50),
 6    pack_size          VARCHAR2(30),
 7    price         NUMBER(8,2),
 8    min_price          NUMBER(8,2)
 9  );

Table created. SQL> SQL> SQL> select category, subcategory, name, price

 2  from   products
 3  where  price > 100
 4  order by category, subcategory, name;

no rows selected SQL> drop table products; Table dropped.


 </source>
   
  


ORDER BY with all logic operators

   <source lang="sql">
  

SQL> SQL> CREATE TABLE product (

 2       product_name     VARCHAR2(25),
 3       product_price    NUMBER(4,2),
 4       quantity_on_hand NUMBER(5,0),
 5       last_stock_date  DATE);

Table created. SQL> SQL> INSERT INTO product VALUES ("Small Widget", 99, 1, "15-JAN-03"); 1 row created. SQL> INSERT INTO product VALUES ("Wodget", 75, 1000, "15-JAN-02"); 1 row created. SQL> INSERT INTO product VALUES ("Product 1", 50, 100, "15-JAN-03"); 1 row created. SQL> INSERT INTO product VALUES ("Product 2", 25, 10000, null); 1 row created. SQL> SQL> SQL> SQL> SELECT * FROM product WHERE quantity_on_hand = 1; PRODUCT_NAME PRODUCT_PRICE QUANTITY_ON_HAND LAST_STOC


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

Small Widget 99 1 15-JAN-03 1 row selected. SQL> SQL> SELECT * FROM product WHERE quantity_on_hand < 500; PRODUCT_NAME PRODUCT_PRICE QUANTITY_ON_HAND LAST_STOC


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

Small Widget 99 1 15-JAN-03 Product 1 50 100 15-JAN-03 2 rows selected. SQL> SQL> SELECT * FROM product WHERE quantity_on_hand < 1000; PRODUCT_NAME PRODUCT_PRICE QUANTITY_ON_HAND LAST_STOC


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

Small Widget 99 1 15-JAN-03 Product 1 50 100 15-JAN-03 2 rows selected. SQL> SQL> SELECT * FROM product WHERE quantity_on_hand <= 1000; PRODUCT_NAME PRODUCT_PRICE QUANTITY_ON_HAND LAST_STOC


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

Small Widget 99 1 15-JAN-03 Wodget 75 1000 15-JAN-02 Product 1 50 100 15-JAN-03 3 rows selected. SQL> SQL> SELECT * FROM product WHERE quantity_on_hand > 1000; PRODUCT_NAME PRODUCT_PRICE QUANTITY_ON_HAND LAST_STOC


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

Product 2 25 10000 1 row selected. SQL> SQL> SELECT * FROM product WHERE quantity_on_hand >= 1000; PRODUCT_NAME PRODUCT_PRICE QUANTITY_ON_HAND LAST_STOC


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

Wodget 75 1000 15-JAN-02 Product 2 25 10000 2 rows selected. SQL> SQL> SELECT * FROM product WHERE quantity_on_hand > 1000; PRODUCT_NAME PRODUCT_PRICE QUANTITY_ON_HAND LAST_STOC


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

Product 2 25 10000 1 row selected. SQL> SQL> SELECT * FROM product WHERE quantity_on_hand >= 1000; PRODUCT_NAME PRODUCT_PRICE QUANTITY_ON_HAND LAST_STOC


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

Wodget 75 1000 15-JAN-02 Product 2 25 10000 2 rows selected. SQL> SQL> SELECT * FROM product WHERE product_price IN (50, 99); PRODUCT_NAME PRODUCT_PRICE QUANTITY_ON_HAND LAST_STOC


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

Small Widget 99 1 15-JAN-03 Product 1 50 100 15-JAN-03 2 rows selected. SQL> SQL> SQL> SELECT * FROM product ORDER BY product_price; PRODUCT_NAME PRODUCT_PRICE QUANTITY_ON_HAND LAST_STOC


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

Product 2 25 10000 Product 1 50 100 15-JAN-03 Wodget 75 1000 15-JAN-02 Small Widget 99 1 15-JAN-03 4 rows selected. SQL> SQL> SQL> DROP TABLE product; Table dropped. SQL> SQL> SQL>


 </source>
   
  


Order date value by only year field with extract() function

   <source lang="sql">
 

SQL> SQL> SQL> CREATE TABLE emp (

 2    emp_id               NUMBER,
 3    ename             VARCHAR2(40),
 4    hire_date        DATE DEFAULT sysdate,
 5    end_date DATE,
 6    rate     NUMBER(5,2),
 7    CONSTRAINT emp_pk
 8      PRIMARY KEY (emp_id)
 9  );

Table created. SQL> INSERT INTO emp(emp_id, ename, hire_date,end_date, rate)VALUES (101, "Mary", to_date("15-Nov-1961","dd-mon-yyyy"),null,169); 1 row created. SQL> INSERT INTO emp(emp_id, ename, hire_date,end_date, rate)VALUES (102, "Tom", to_date("16-Sep-1964","dd-mon-yyyy"),to_date("5-May-2004","dd-mon-yyyy"),135); 1 row created. SQL> INSERT INTO emp(emp_id, ename, hire_date,end_date, rate)VALUES (104, "Peter", to_date("29-Dec-1987","dd-mon-yyyy"),to_date("1-Apr-2004","dd-mon-yyyy"),99); 1 row created. SQL> INSERT INTO emp(emp_id, ename, hire_date,end_date, rate)VALUES (105, "Mike", to_date("15-Jun-2004","dd-mon-yyyy"),null,121); 1 row created. SQL> INSERT INTO emp(emp_id, ename, hire_date,end_date, rate)VALUES (107, "Less", to_date("2-Jan-2004","dd-mon-yyyy"),null,45); 1 row created. SQL> INSERT INTO emp(emp_id, ename, hire_date,end_date, rate)VALUES (108, "Park", to_date("1-Mar-1994","dd-mon-yyyy"),to_date("15-Nov-2004","dd-mon-yyyy"),220); 1 row created. SQL> INSERT INTO emp(emp_id, ename, hire_date,end_date, rate)VALUES (110, "Ink", to_date("4-Apr-2004","dd-mon-yyyy"),to_date("30-Sep-2004","dd-mon-yyyy"),84); 1 row created. SQL> INSERT INTO emp(emp_id, ename, hire_date,end_date, rate)VALUES (111, "Tike", to_date("23-Aug-1976","dd-mon-yyyy"),null,100); 1 row created. SQL> INSERT INTO emp(emp_id, ename, hire_date,end_date, rate)VALUES (112, "Inn", to_date("15-Nov-1961","dd-mon-yyyy"),to_date("4-Apr-2004","dd-mon-yyyy"),70); 1 row created. SQL> INSERT INTO emp(emp_id, ename, hire_date,end_date, rate)VALUES (113, "Kate", to_date("3-Mar-2004","dd-mon-yyyy"),to_date("31-Oct-2004","dd-mon-yyyy"),300); 1 row created. SQL> SQL> SET ECHO ON SQL> SELECT e.emp_id "ID", e.ename "Name",

 2         e.hire_date "Hire Date"
 3  FROM emp e
 4  ORDER BY EXTRACT(YEAR FROM hire_date) DESC, ename ASC;
      110

Ink 04-APR-04

      113

Kate 03-MAR-04

      107

Less 02-JAN-04

      105

Mike 15-JUN-04

      108

Park 01-MAR-94

      104

Peter 29-DEC-87

      111

Tike 23-AUG-76

      102

Tom 16-SEP-64

      112

Inn 15-NOV-61

      101

Mary 15-NOV-61

10 rows selected. SQL> SQL> drop table emp; Table dropped.


 </source>
   
  


Order for one column descendingly

   <source lang="sql">
 

SQL> SQL> SQL> create table emp(

 2           emp_id                integer         primary key
 3          ,lastname               varchar2(20)    not null
 4          ,firstname              varchar2(15)    not null
 5          ,midinit                varchar2(1)
 6          ,street                 varchar2(30)
 7          ,city                   varchar2(20)
 8          ,state                  varchar2(2)
 9          ,zip                    varchar2(5)
10          ,shortZipCode                   varchar2(4)
11          ,area_code              varchar2(3)
12          ,phone                  varchar2(8)
13          ,company_name           varchar2(50));

Table created. SQL> SQL> SQL> insert into emp(emp_id,lastname,firstname,midinit,street,city,state,zip,shortZipCode,area_code,phone,company_name)values

 2                      (1,"Jones","Joe","J","1 Ave","New York","NY","11202","1111","212", "221-4333","Big Company");

1 row created. SQL> insert into emp(emp_id,lastname,firstname,midinit,street,city,state,zip,shortZipCode,area_code,phone,company_name)values

 2                      (2,"Smith","Sue","J","1 Street","New York","NY","11444","1111","212", "436-6773","Little Company");

1 row created. SQL> insert into emp(emp_id,lastname,firstname,midinit,street,city,state,zip,shortZipCode,area_code,phone,company_name)values

 2                      (3,"X","Peggy","J","1 Drive","New York","NY","45502","2222","212", "234-4444","Medium Company");

1 row created. SQL> insert into emp(emp_id,lastname,firstname,midinit,street,city,state,zip,shortZipCode,area_code,phone,company_name)values

 2                      (4,"Murdy","Jill", null,"930 Eady St","New York","NY","45452","6458","212", "634-7733","Wilton Company");

1 row created. SQL> insert into emp(emp_id,lastname,firstname,midinit,street,city,state,zip,shortZipCode,area_code,phone,company_name)values

 2                      (5,"Oper","Carl","L","19 Studio Drive","New York","NY","67672","3234","212", "243-4243","Wesson and Smith Company");

1 row created. SQL> SQL> select lastname, area_code, phone

 2    from emp
 3   order by area_code desc, lastname;

LASTNAME ARE PHONE


--- --------

Jones 212 221-4333 Murdy 212 634-7733 Oper 212 243-4243 Smith 212 436-6773 X 212 234-4444 5 rows selected. SQL> SQL> drop table emp; Table dropped. SQL>


 </source>
   
  


Ordering data in the SELECT statement ascending

   <source lang="sql">
 

SQL> SQL> SQL> SQL> -- create demo table SQL> create table Employee(

 2    ID                 VARCHAR2(4 BYTE)         NOT NULL,
 3    First_Name         VARCHAR2(10 BYTE),
 4    Last_Name          VARCHAR2(10 BYTE),
 5    Start_Date         DATE,
 6    End_Date           DATE,
 7    Salary             Number(8,2),
 8    City               VARCHAR2(10 BYTE),
 9    Description        VARCHAR2(15 BYTE)
10  )
11  /

Table created. SQL> SQL> -- prepare data SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2               values ("01","Jason",    "Martin",  to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto",  "Programmer")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("02","Alison",   "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 2334.78, "Vancouver","Tester")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("03","James",    "Smith",   to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 2334.78, "Vancouver","Tester")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("04","Celia",    "Rice",    to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2334.78, "Vancouver","Manager")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("05","Robert",   "Black",   to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("06","Linda",    "Green",   to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 2334.78,"New York",  "Tester")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("07","David",    "Larry",   to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 2334.78,"New York",  "Manager")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("08","James",    "Cat",     to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 2334.78,"Vancouver", "Tester")
 3  /

1 row created. SQL> SQL> SQL> SQL> -- display data in the table SQL> select * from Employee

 2  /

ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION


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

01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer 02 Alison Mathews 21-MAR-76 21-FEB-86 2334.78 Vancouver Tester 03 James Smith 12-DEC-78 15-MAR-90 2334.78 Vancouver Tester 04 Celia Rice 24-OCT-82 21-APR-99 2334.78 Vancouver Manager 05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester 06 Linda Green 30-JUL-87 04-JAN-96 2334.78 New York Tester 07 David Larry 31-DEC-90 12-FEB-98 2334.78 New York Manager 08 James Cat 17-SEP-96 15-APR-02 2334.78 Vancouver Tester 8 rows selected. SQL> SQL> SQL> SQL> SQL> SQL> SQL> -- Ordering data in the SELECT statement ascending SQL> SQL> SELECT ID FROM Employee ORDER BY Last_Name ASC; ID


05 08 06 07 01 02 04 03 8 rows selected. SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> -- clean the table SQL> drop table Employee

 2  /

Table dropped. SQL> SQL>


 </source>
   
  


Ordering data in the SELECT statement descending

   <source lang="sql">
 

SQL> SQL> SQL> -- create demo table SQL> create table Employee(

 2    ID                 VARCHAR2(4 BYTE)         NOT NULL,
 3    First_Name         VARCHAR2(10 BYTE),
 4    Last_Name          VARCHAR2(10 BYTE),
 5    Start_Date         DATE,
 6    End_Date           DATE,
 7    Salary             Number(8,2),
 8    City               VARCHAR2(10 BYTE),
 9    Description        VARCHAR2(15 BYTE)
10  )
11  /

Table created. SQL> SQL> -- prepare data SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2               values ("01","Jason",    "Martin",  to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto",  "Programmer")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("02","Alison",   "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 2334.78, "Vancouver","Tester")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("03","James",    "Smith",   to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 2334.78, "Vancouver","Tester")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("04","Celia",    "Rice",    to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2334.78, "Vancouver","Manager")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("05","Robert",   "Black",   to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("06","Linda",    "Green",   to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 2334.78,"New York",  "Tester")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("07","David",    "Larry",   to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 2334.78,"New York",  "Manager")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("08","James",    "Cat",     to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 2334.78,"Vancouver", "Tester")
 3  /

1 row created. SQL> SQL> SQL> SQL> -- display data in the table SQL> select * from Employee

 2  /

ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION


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

01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer 02 Alison Mathews 21-MAR-76 21-FEB-86 2334.78 Vancouver Tester 03 James Smith 12-DEC-78 15-MAR-90 2334.78 Vancouver Tester 04 Celia Rice 24-OCT-82 21-APR-99 2334.78 Vancouver Manager 05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester 06 Linda Green 30-JUL-87 04-JAN-96 2334.78 New York Tester 07 David Larry 31-DEC-90 12-FEB-98 2334.78 New York Manager 08 James Cat 17-SEP-96 15-APR-02 2334.78 Vancouver Tester 8 rows selected. SQL> SQL> SQL> SQL> SQL> SQL> SQL> -- Ordering data in the SELECT statement descending SQL> SQL> SELECT ID FROM Employee ORDER BY Last_Name DESC; ID


03 04 02 01 07 06 08 05 8 rows selected. SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> -- clean the table SQL> drop table Employee

 2  /

Table dropped. SQL> SQL> SQL>


 </source>
   
  


Order the results by the average salary

   <source lang="sql">
 

SQL> -- create demo table SQL> create table Employee(

 2    ID                 VARCHAR2(4 BYTE)         NOT NULL,
 3    First_Name         VARCHAR2(10 BYTE),
 4    Last_Name          VARCHAR2(10 BYTE),
 5    Start_Date         DATE,
 6    End_Date           DATE,
 7    Salary             Number(8,2),
 8    City               VARCHAR2(10 BYTE),
 9    Description        VARCHAR2(15 BYTE)
10  )
11  /

Table created. SQL> SQL> -- prepare data SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2               values ("01","Jason",    "Martin",  to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto",  "Programmer")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("02","Alison",   "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 6661.78, "Vancouver","Tester")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("03","James",    "Smith",   to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 6544.78, "Vancouver","Tester")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("04","Celia",    "Rice",    to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2344.78, "Vancouver","Manager")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("05","Robert",   "Black",   to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("06","Linda",    "Green",   to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 4322.78,"New York",  "Tester")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("07","David",    "Larry",   to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 7897.78,"New York",  "Manager")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("08","James",    "Cat",     to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 1232.78,"Vancouver", "Tester")
 3  /

1 row created. SQL> SQL> SQL> SQL> -- display data in the table SQL> select * from Employee

 2  /

ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION


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

01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer 02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester 03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester 04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager 05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester 06 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester 07 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager 08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester 8 rows selected. SQL> SQL> SQL> SQL> SQL> -- Order the results by the average salary SQL> SQL> SELECT city, AVG(salary)

 2  FROM employee
 3  WHERE salary < 5000
 4  GROUP BY city
 5  HAVING AVG(salary) > 1300
 6  ORDER BY AVG(salary);

CITY AVG(SALARY)


-----------

Vancouver 1970.78 New York 4322.78 SQL> SQL> SQL> -- clean the table SQL> drop table Employee

 2  /

Table dropped. SQL> SQL>


 </source>
   
  


Order two columns

   <source lang="sql">
  

SQL> SQL> CREATE TABLE employees

 2  ( employee_id          number(10)      not null,
 3    last_name            varchar2(50)      not null,
 4    email                varchar2(30),
 5    hire_date            date,
 6    job_id               varchar2(30),
 7    department_id        number(10),
 8    salary               number(6),
 9    manager_id           number(6)
10  );

Table created. SQL> SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary,department_id ,manager_id)

 2                values ( 1001, "Lawson", "lawson@g.ru", "01-JAN-2002","MGR", 30000,1 ,1004);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)

 2                values ( 1002, "Wells", "wells@g.ru", "01-JAN-2002", "DBA", 20000,2, 1005 );

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)

 2                 values( 1003, "Bliss", "bliss@g.ru", "01-JAN-2002", "PROG", 24000,3 ,1004);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1004,  "Kyte", "tkyte@a.ru", SYSDATE-3650, "MGR",25000 ,4, 1005);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1005, "Viper", "sdillon@a .ru", SYSDATE, "PROG", 20000, 1, 1006);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id,manager_id)

 2                 values( 1006, "Beck", "clbeck@g.ru", SYSDATE, "PROG", 20000, 2, null);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1007, "Java", "java01@g.ru", SYSDATE, "PROG", 20000, 3, 1006);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1008, "Oracle", "oracle1@g.ru", SYSDATE, "DBA", 20000, 4, 1006);

1 row created. SQL> SQL> select * from employees; EMPLOYEE_ID LAST_NAME EMAIL HIRE_DATE JOB_ID DEPARTMENT_ID SALARY MANAGER_ID


-------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
      1001 Lawson                                             lawson@g.ru                   01-JAN-02 MGR                                        1      30000       1004
      1002 Wells                                              wells@g.ru                    01-JAN-02 DBA                                        2      20000       1005
      1003 Bliss                                              bliss@g.ru                    01-JAN-02 PROG                                       3      24000       1004
      1004 Kyte                                               tkyte@a.ru                    13-JUN-98 MGR                                        4      25000       1005
      1005 Viper                                              sdillon@a .ru                 10-JUN-08 PROG                                       1      20000       1006
      1006 Beck                                               clbeck@g.ru                   10-JUN-08 PROG                                       2      20000
      1007 Java                                               java01@g.ru                   10-JUN-08 PROG                                       3      20000       1006

EMPLOYEE_ID LAST_NAME EMAIL HIRE_DATE JOB_ID DEPARTMENT_ID SALARY MANAGER_ID


-------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
      1008 Oracle                                             oracle1@g.ru                  10-JUN-08 DBA                                        4      20000       1006

8 rows selected. SQL> SQL> SQL> select last_name, job_id, salary

 2        from employees
 3       where salary > 12000
 4       order by job_id, salary
 5      /

LAST_NAME JOB_ID SALARY


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

Oracle DBA 20000 Wells DBA 20000 Kyte MGR 25000 Lawson MGR 30000 Viper PROG 20000 Beck PROG 20000 Java PROG 20000 LAST_NAME JOB_ID SALARY


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

Bliss PROG 24000 8 rows selected. SQL> SQL> drop table employees; Table dropped. SQL>


 </source>
   
  


Query a number type column with order by desc

   <source lang="sql">
  

SQL> SQL> CREATE TABLE employees

 2  ( employee_id          number(10)      not null,
 3    last_name            varchar2(50)      not null,
 4    email                varchar2(30),
 5    hire_date            date,
 6    job_id               varchar2(30),
 7    department_id        number(10),
 8    salary               number(6),
 9    manager_id           number(6)
10  );

Table created. SQL> SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary,department_id ,manager_id)

 2                values ( 1001, "Lawson", "lawson@g.ru", "01-JAN-2002","MGR", 30000,1 ,1004);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)

 2                values ( 1002, "Wells", "wells@g.ru", "01-JAN-2002", "DBA", 20000,2, 1005 );

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)

 2                 values( 1003, "Bliss", "bliss@g.ru", "01-JAN-2002", "PROG", 24000,3 ,1004);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1004,  "Kyte", "tkyte@a.ru", SYSDATE-3650, "MGR",25000 ,4, 1005);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1005, "Viper", "sdillon@a .ru", SYSDATE, "PROG", 20000, 1, 1006);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id,manager_id)

 2                 values( 1006, "Beck", "clbeck@g.ru", SYSDATE, "PROG", 20000, 2, null);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1007, "Java", "java01@g.ru", SYSDATE, "PROG", 20000, 3, 1006);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1008, "Oracle", "oracle1@g.ru", SYSDATE, "DBA", 20000, 4, 1006);

1 row created. SQL> SQL> select * from employees; EMPLOYEE_ID LAST_NAME EMAIL HIRE_DATE JOB_ID DEPARTMENT_ID SALARY MANAGER_ID


-------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
      1001 Lawson                                             lawson@g.ru                   01-JAN-02 MGR                                        1      30000       1004
      1002 Wells                                              wells@g.ru                    01-JAN-02 DBA                                        2      20000       1005
      1003 Bliss                                              bliss@g.ru                    01-JAN-02 PROG                                       3      24000       1004
      1004 Kyte                                               tkyte@a.ru                    13-JUN-98 MGR                                        4      25000       1005
      1005 Viper                                              sdillon@a .ru                 10-JUN-08 PROG                                       1      20000       1006
      1006 Beck                                               clbeck@g.ru                   10-JUN-08 PROG                                       2      20000
      1007 Java                                               java01@g.ru                   10-JUN-08 PROG                                       3      20000       1006

EMPLOYEE_ID LAST_NAME EMAIL HIRE_DATE JOB_ID DEPARTMENT_ID SALARY MANAGER_ID


-------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
      1008 Oracle                                             oracle1@g.ru                  10-JUN-08 DBA                                        4      20000       1006

8 rows selected. SQL> SQL> select last_name, salary

 2        from employees
 3       where salary > 12000
 4       order by salary desc
 5      /

LAST_NAME SALARY


----------

Lawson 30000 Kyte 25000 Bliss 24000 Oracle 20000 Viper 20000 Beck 20000 Java 20000 LAST_NAME SALARY


----------

Wells 20000 8 rows selected. SQL> drop table employees; Table dropped. SQL> SQL>


 </source>
   
  


Query specific columns with order by clause

   <source lang="sql">
  

SQL> SQL> SQL> CREATE TABLE employees

 2  ( employee_id          number(10)      not null,
 3    last_name            varchar2(50)      not null,
 4    email                varchar2(30),
 5    hire_date            date,
 6    job_id               varchar2(30),
 7    department_id        number(10),
 8    salary               number(6),
 9    manager_id           number(6)
10  );

Table created. SQL> SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary,department_id ,manager_id)

 2                values ( 1001, "Lawson", "lawson@g.ru", "01-JAN-2002","MGR", 30000,1 ,1004);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)

 2                values ( 1002, "Wells", "wells@g.ru", "01-JAN-2002", "DBA", 20000,2, 1005 );

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)

 2                 values( 1003, "Bliss", "bliss@g.ru", "01-JAN-2002", "PROG", 24000,3 ,1004);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1004,  "Kyte", "YourName@a.ru", SYSDATE-3650, "MGR",25000 ,4, 1005);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1005, "Dillon", "sdillon@a .ru", SYSDATE, "PROG", 20000, 1, 1006);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id,manager_id)

 2                 values( 1006, "Beck", "clbeck@g.ru", SYSDATE, "PROG", 20000, 2, null);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1007, "Java", "java01@g.ru", SYSDATE, "PROG", 20000, 3, 1006);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1008, "Oracle", "oracle1@g.ru", SYSDATE, "DBA", 20000, 4, 1006);

1 row created. SQL> SQL> select * from employees; EMPLOYEE_ID LAST_NAME EMAIL HIRE_DATE JOB_ID DEPARTMENT_ID SALARY MANAGER_ID


-------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
      1001 Lawson                                             lawson@g.ru                   01-JAN-02 MGR                                        1      30000       1004
      1002 Wells                                              wells@g.ru                    01-JAN-02 DBA                                        2      20000       1005
      1003 Bliss                                              bliss@g.ru                    01-JAN-02 PROG                                       3      24000       1004
      1004 Kyte                                               YourName@a.ru                 13-JUN-98 MGR                                        4      25000       1005
      1005 Dillon                                             sdillon@a .ru                 10-JUN-08 PROG                                       1      20000       1006
      1006 Beck                                               clbeck@g.ru                   10-JUN-08 PROG                                       2      20000
      1007 Java                                               java01@g.ru                   10-JUN-08 PROG                                       3      20000       1006

EMPLOYEE_ID LAST_NAME EMAIL HIRE_DATE JOB_ID DEPARTMENT_ID SALARY MANAGER_ID


-------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
      1008 Oracle                                             oracle1@g.ru                  10-JUN-08 DBA                                        4      20000       1006

8 rows selected. SQL> SQL> SQL> select job_id, salary

 2  from employees
 3  order by job_id;

JOB_ID SALARY


----------

DBA 20000 DBA 20000 MGR 30000 MGR 25000 PROG 20000 PROG 20000 PROG 20000 JOB_ID SALARY


----------

PROG 24000 8 rows selected. SQL> SQL> drop table employees; Table dropped. SQL>


 </source>
   
  


Query the same column in where statement and order by clause

   <source lang="sql">
  

SQL> CREATE TABLE employees

 2  ( employee_id          number(10)      not null,
 3    last_name            varchar2(50)      not null,
 4    email                varchar2(30),
 5    hire_date            date,
 6    job_id               varchar2(30),
 7    department_id        number(10),
 8    salary               number(6),
 9    manager_id           number(6)
10  );

Table created. SQL> SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary,department_id ,manager_id)

 2                values ( 1001, "Lawson", "lawson@g.ru", "01-JAN-2002","MGR", 30000,1 ,1004);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)

 2                values ( 1002, "Wells", "wells@g.ru", "01-JAN-2002", "DBA", 20000,2, 1005 );

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)

 2                 values( 1003, "Bliss", "bliss@g.ru", "01-JAN-2002", "PROG", 24000,3 ,1004);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1004,  "Kyte", "tkyte@a.ru", SYSDATE-3650, "MGR",25000 ,4, 1005);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1005, "Viper", "sdillon@a .ru", SYSDATE, "PROG", 20000, 1, 1006);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id,manager_id)

 2                 values( 1006, "Beck", "clbeck@g.ru", SYSDATE, "PROG", 20000, 2, null);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1007, "Java", "java01@g.ru", SYSDATE, "PROG", 20000, 3, 1006);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1008, "Oracle", "oracle1@g.ru", SYSDATE, "DBA", 20000, 4, 1006);

1 row created. SQL> SQL> select * from employees; EMPLOYEE_ID LAST_NAME EMAIL HIRE_DATE JOB_ID DEPARTMENT_ID SALARY MANAGER_ID


-------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
      1001 Lawson                                             lawson@g.ru                   01-JAN-02 MGR                                        1      30000       1004
      1002 Wells                                              wells@g.ru                    01-JAN-02 DBA                                        2      20000       1005
      1003 Bliss                                              bliss@g.ru                    01-JAN-02 PROG                                       3      24000       1004
      1004 Kyte                                               tkyte@a.ru                    13-JUN-98 MGR                                        4      25000       1005
      1005 Viper                                              sdillon@a .ru                 10-JUN-08 PROG                                       1      20000       1006
      1006 Beck                                               clbeck@g.ru                   10-JUN-08 PROG                                       2      20000
      1007 Java                                               java01@g.ru                   10-JUN-08 PROG                                       3      20000       1006

EMPLOYEE_ID LAST_NAME EMAIL HIRE_DATE JOB_ID DEPARTMENT_ID SALARY MANAGER_ID


-------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
      1008 Oracle                                             oracle1@g.ru                  10-JUN-08 DBA                                        4      20000       1006

8 rows selected. SQL> SQL> SQL> select last_name, salary

 2        from employees
 3       where salary > 12000
 4       order by salary
 5      /

LAST_NAME SALARY


----------

Java 20000 Wells 20000 Oracle 20000 Viper 20000 Beck 20000 Bliss 24000 Kyte 25000 LAST_NAME SALARY


----------

Lawson 30000 8 rows selected. SQL> SQL> drop table employees; Table dropped. SQL> SQL>


 </source>
   
  


Specify ascending or descending for each column

   <source lang="sql">
 

SQL> SQL> SQL> -- create demo table SQL> create table Employee(

 2    ID                 VARCHAR2(4 BYTE)         NOT NULL,
 3    First_Name         VARCHAR2(10 BYTE),
 4    Last_Name          VARCHAR2(10 BYTE),
 5    Start_Date         DATE,
 6    End_Date           DATE,
 7    Salary             Number(8,2),
 8    City               VARCHAR2(10 BYTE),
 9    Description        VARCHAR2(15 BYTE)
10  )
11  /

Table created. SQL> SQL> -- prepare data SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2               values ("01","Jason",    "Martin",  to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto",  "Programmer")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("02","Alison",   "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 2334.78, "Vancouver","Tester")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("03","James",    "Smith",   to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 2334.78, "Vancouver","Tester")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("04","Celia",    "Rice",    to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2334.78, "Vancouver","Manager")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("05","Robert",   "Black",   to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("06","Linda",    "Green",   to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 2334.78,"New York",  "Tester")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("07","David",    "Larry",   to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 2334.78,"New York",  "Manager")
 3  /

1 row created. SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

 2                values("08","James",    "Cat",     to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 2334.78,"Vancouver", "Tester")
 3  /

1 row created. SQL> SQL> SQL> SQL> -- display data in the table SQL> select * from Employee

 2  /

ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION


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

01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer 02 Alison Mathews 21-MAR-76 21-FEB-86 2334.78 Vancouver Tester 03 James Smith 12-DEC-78 15-MAR-90 2334.78 Vancouver Tester 04 Celia Rice 24-OCT-82 21-APR-99 2334.78 Vancouver Manager 05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester 06 Linda Green 30-JUL-87 04-JAN-96 2334.78 New York Tester 07 David Larry 31-DEC-90 12-FEB-98 2334.78 New York Manager 08 James Cat 17-SEP-96 15-APR-02 2334.78 Vancouver Tester 8 rows selected. SQL> SQL> SQL> SQL> SQL> SQL> SQL> -- Specify ascending or descending for each column SQL> SQL> SELECT ID, First_Name, Last_Name FROM Employee

 2  ORDER BY Salary ASC, Description DESC;

ID FIRST_NAME LAST_NAME


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

01 Jason Martin 02 Alison Mathews 03 James Smith 08 James Cat 05 Robert Black 06 Linda Green 07 David Larry 04 Celia Rice 8 rows selected. SQL> SQL> -- clean the table SQL> drop table Employee

 2  /

Table dropped. SQL> SQL>


 </source>
   
  


Use aggregate function in order by clause

   <source lang="sql">
  

SQL> SQL> CREATE TABLE employees

 2  ( employee_id          number(10)      not null,
 3    last_name            varchar2(50)      not null,
 4    email                varchar2(30),
 5    hire_date            date,
 6    job_id               varchar2(30),
 7    department_id        number(10),
 8    salary               number(6),
 9    manager_id           number(6)
10  );

Table created. SQL> SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary,department_id ,manager_id)

 2                values ( 1001, "Lawson", "lawson@g.ru", "01-JAN-2002","MGR", 30000,1 ,1004);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)

 2                values ( 1002, "Wells", "wells@g.ru", "01-JAN-2002", "DBA", 20000,2, 1005 );

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)

 2                 values( 1003, "Bliss", "bliss@g.ru", "01-JAN-2002", "PROG", 24000,3 ,1004);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1004,  "Kyte", "YourName@a.ru", SYSDATE-3650, "MGR",25000 ,4, 1005);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1005, "Dillon", "sdillon@a .ru", SYSDATE, "PROG", 20000, 1, 1006);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id,manager_id)

 2                 values( 1006, "Beck", "clbeck@g.ru", SYSDATE, "PROG", 20000, 2, null);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1007, "Java", "java01@g.ru", SYSDATE, "PROG", 20000, 3, 1006);

1 row created. SQL> SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)

 2                 values( 1008, "Oracle", "oracle1@g.ru", SYSDATE, "DBA", 20000, 4, 1006);

1 row created. SQL> SQL> select * from employees; EMPLOYEE_ID LAST_NAME EMAIL HIRE_DATE JOB_ID DEPARTMENT_ID SALARY MANAGER_ID


-------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
      1001 Lawson                                             lawson@g.ru                   01-JAN-02 MGR                                        1      30000       1004
      1002 Wells                                              wells@g.ru                    01-JAN-02 DBA                                        2      20000       1005
      1003 Bliss                                              bliss@g.ru                    01-JAN-02 PROG                                       3      24000       1004
      1004 Kyte                                               YourName@a.ru                 13-JUN-98 MGR                                        4      25000       1005
      1005 Dillon                                             sdillon@a .ru                 10-JUN-08 PROG                                       1      20000       1006
      1006 Beck                                               clbeck@g.ru                   10-JUN-08 PROG                                       2      20000
      1007 Java                                               java01@g.ru                   10-JUN-08 PROG                                       3      20000       1006

EMPLOYEE_ID LAST_NAME EMAIL HIRE_DATE JOB_ID DEPARTMENT_ID SALARY MANAGER_ID


-------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
      1008 Oracle                                             oracle1@g.ru                  10-JUN-08 DBA                                        4      20000       1006

8 rows selected. SQL> SQL> select job_id, avg(salary) avg_salary

 2        from employees
 3       group by job_id
 4       order by avg(salary) desc
 5      /

JOB_ID AVG_SALARY


----------

MGR 27500 PROG 21000 DBA 20000 3 rows selected. SQL> drop table employees; Table dropped. SQL>


 </source>