Conditional selection & operation

      CONDITIONAL SELECTIONS AND OPERATORS

 

We have two clauses used in this

Ø  Where

Ø  Order by

 

USING WHERE

 

Syntax:

     select * from <table_name> where <condition>;

     the following are the different types of operators used in where clause.

 

v  Arithmetic operators         

v  Comparison operators

v  Logical operators

 

v  Arithmetic operators          -- highest precedence

+, -, *, /

v  Comparison operators

Ø  =, !=, >, <, >=, <=, <>

Ø  between, not between

Ø  in, not in

Ø  null, not null

Ø  like

v       Logical operators

Ø  And

Ø  Or                                -- lowest precedence

Ø  not

 

a) USING =, >, <, >=, <=, !=, <>

   

     Ex:

        SQL> select * from student where no = 2;

 

 

        NO NAME            MARKS

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

         2   Saketh            200

         2   Naren             400

        

        SQL> select * from student where no < 2;

 

        NO NAME            MARKS

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

         1   Sudha             100

         1   Jagan             300

 

        SQL> select * from student where no > 2;

 

         NO NAME            MARKS

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

         3   Ramesh

         4   Madhu

         5   Visu

         6   Rattu

 

         SQL> select * from student where no <= 2;

 

         NO NAME            MARKS

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

         1   Sudha             100

         2   Saketh            200

         1   Jagan             300

         2   Naren             400

      

         SQL> select * from student where no >= 2;

 

         NO NAME            MARKS

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

         2   Saketh            200

         2   Naren             400

         3   Ramesh

         4   Madhu

         5   Visu

         6   Rattu

 

         SQL> select * from student where no != 2;

 

         NO NAME            MARKS

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

         1   Sudha             100

         1   Jagan             300

         3   Ramesh

         4   Madhu

         5   Visu

         6   Rattu

 

         SQL> select * from student where no <> 2;

 

         NO NAME            MARKS

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

         1   Sudha             100

         1   Jagan             300

         3   Ramesh

         4   Madhu

         5   Visu

         6   Rattu

 

b) USING AND

    

     This will gives the output when all the conditions become true.

    

     Syntax:

          select * from <table_name> where <condition1> and <condition2> and .. <conditionn>;

    

     Ex:

 

         SQL> select * from student where no = 2 and marks >= 200;

 

 

                              NO NAME            MARKS

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

         2   Saketh            200

         2   Naren             400

 

c) USING OR

 

     This will gives the output when either of the conditions become true.

 

     Syntax:

         select * from <table_name> where <condition1> and <condition2> or .. <conditionn>;

 

     Ex:

         SQL> select * from student where no = 2 or marks >= 200;

 

         NO NAME            MARKS

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

         2   Saketh            200

         1   Jagan             300

         2   Naren             400

 

d) USING BETWEEN

 

     This will gives the output based on the column and its lower bound, upperbound.

 

     Syntax:

         select * from <table_name> where <col> between <lower bound> and <upper bound>;

 

     Ex:

         SQL> select * from student where marks between 200 and 400;

 

         NO NAME            MARKS

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

         2   Saketh            200

         1   Jagan              300

         2   Naren              400

 

e) USING NOT BETWEEN

 

     This will gives the output based on the column which values are not in its lower bound,

     upperbound.

 

     Syntax:

     select * from <table_name> where <col> not between <lower bound> and <upper bound>;

 

     Ex:

         SQL> select * from student where marks not between 200 and 400;

 

         NO NAME            MARKS

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

         1   Sudha             100

 

f) USING IN

 

    This will gives the output based on the column and its list of values specified.

 

    Syntax:

         select * from <table_name> where <col> in ( value1, value2, value3 … valuen);

 

     Ex:

         SQL> select * from student where no in (1, 2, 3);

 

         NO NAME            MARKS

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

         1   Sudha             100

         2   Saketh            200

         1   Jagan             300

         2   Naren             400

         3   Ramesh

 

g) USING NOT IN

 

     This will gives the output  based on the column which values are not in the list of values  

     specified.

 

     Syntax: