SQL Certification - SPLessons

SQL Certification Conditional Operators

Home > Lesson > Chapter 11
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

SQL Certification Conditional Operators

Conditional Operators

shape Description

To retrieve data from the table based on some rules, then use conditional operators. Conditional operators returns true or false esteem based on the instance of the variables. The conditional operators in SQL are classified into following types:

Logical AND Operator

shape Description

To specify multiple conditions in the column of SQL table use Logical AND operator. i.e. SQL Logical And operator compares two results and returns true if both of the results are true.

shape Syntax

Select  * from <table_name> where <condition1> and <condition2> and <condition3>;
table_name => Any accurate table. condition => The condition is a logic to get a specific records.

shape Examples

By viewing the below example, the concept of Logical And operator can be easily understood from the table. [sql]sql> select * from employee21; +--------+-------+-------+--------+-----------+ | emp_id | ename | sal | deptno | job | +--------+-------+-------+--------+-----------+ | 1001 | mike | 12000 | 10 | manager | | 1002 | rambo | 13000 | 20 | scalesman | | 1003 | kate | 14000 | 10 | manager | | 1003 | jeo | 14000 | 20 | manager | | 1003 | finn | 14000 | 30 | manager | +--------+-------+-------+--------+-----------+ 5 rows in set (0.00 sec) sql>select * from employee21 where ename='mike'and job='manager'; +--------+-------+-------+--------+---------+ | emp_id | ename | sal | deptno | job | +--------+-------+-------+--------+---------+ | 1001 | mike | 12000 | 10 | manager | +--------+-------+-------+--------+---------+ 1 row in set (0.00 sec)[/sql] The above example tells that, when Logical and operation is performed on a column like ename='mike'and job='manager', then it automatically displays the entire details of that employee like emp_id,salary and deptno.

Logical OR Operator

shape Description

To specify multiple conditions on different columns in SQL table use Logical OR operator. SQL Logical OR operator compares two expressions and returns true if both of the expressions is valid.

shape Syntax

Select * from <table_name> where <condition1> or <condition2> or <condition3>;
table_name => Any accurate table. condition => The condition is a logic to get specific records.

shape Examples

By viewing the below example, the concept of Logical OR operator can be easily understood from the table. [sql]sql> select * from employee21; +--------+-------+-------+--------+-----------+ | emp_id | ename | sal | deptno | job | +--------+-------+-------+--------+-----------+ | 1001 | mike | 12000 | 10 | manager | | 1002 | rambo | 13000 | 20 | scalesman | | 1003 | kate | 14000 | 10 | manager | | 1003 | jeo | 14000 | 20 | manager | | 1003 | finn | 14000 | 30 | manager | +--------+-------+-------+--------+-----------+ 5 rows in set (0.00 sec) sql> select * from employee21 where ename='rambo' or sal>13000; +--------+-------+-------+--------+-----------+ | emp_id | ename | sal | deptno | job | +--------+-------+-------+--------+-----------+ | 1002 | rambo | 13000 | 20 | scalesman | | 1003 | kate | 14000 | 10 | manager | | 1003 | jeo | 14000 | 20 | manager | | 1003 | finn | 14000 | 30 | manager | +--------+-------+-------+--------+-----------+ 4 rows in set (0.00 sec)[/sql] The above example tells that, when Logical OR operation is performed on a column like ename='rambo' and salary>13000 then it automatically displays the entire details of employee rambo and shows the salaries of employees greater than equal to 13000.

IS NOT NULL Operator

shape Description

The IS NOT NULL operator will review whether the esteem IS Not Null or not.

shape Syntax

select * from <table_name> where <column_name> is not null.
table_name =>Any accurate table. column_name =>The condition that one can perform on a column by using IS NOT NULL operator.

shape Examples

By viewing the below example, the concept of IS NOT NULL can be easily understand from the table. [sql]sql> select * from employee22; +--------+--------+----------+-------+------------+ | emp_id | ename | job | sal | comisssion | +--------+--------+----------+-------+------------+ | 1001 | kate | manager | 14000 | 10 | | 1002 | jack | scaleman | 13000 | 20 | | 1003 | maddie | business | 14000 | 30 | | 1004 | madd | business | 14000 | NULL | +--------+--------+----------+-------+------------+ 4 rows in set (0.00 sec) sql> select * from employee22 where comisssion is not null; +--------+--------+----------+-------+------------+ | emp_id | ename | job | sal | comisssion | +--------+--------+----------+-------+------------+ | 1001 | kate | manager | 14000 | 10 | | 1002 | jack | scaleman | 13000 | 20 | | 1003 | maddie | business | 14000 | 30 | +--------+--------+----------+-------+------------+ 3 rows in set (0.00 sec) [/sql] The above example tells that, when IS NOT NULL operation is performed on a column name commission then it automatically remove the null values from column and gives the output.

IS NULL Operator

shape Description

SQL IS NULL operator will review whether a esteem is NULL.

shape Syntax

Select * from <table_name> where <column_name> is null;
table_name =>Any accurate table. column_name =>The condition that one can perform on a column by using IS NULL operator.

shape Examples

By viewing the below example, the concept of IS NULL can be easily understood from the table. [sql]sql> select * from employee22; +--------+--------+----------+-------+------------+ | emp_id | ename | job | sal | comisssion | +--------+--------+----------+-------+------------+ | 1001 | kate | manager | 14000 | 10 | | 1002 | jack | scaleman | 13000 | 20 | | 1003 | maddie | business | 14000 | 30 | | 1004 | madd | business | 14000 | NULL | +--------+--------+----------+-------+------------+ 4 rows in set (0.00 sec) sql> select * from employee22 where comisssion is null; +--------+-------+----------+-------+------------+ | emp_id | ename | job | sal | comisssion | +--------+-------+----------+-------+------------+ | 1004 | madd | business | 14000 | NULL | +--------+-------+----------+-------+------------+ 1 row in set (0.00 sec) [/sql] The above example tells that, when IS NULL operation is performed on a column name commission then it shows the null values column details like emp_id, ename, job and salary.

BETWEEN AND OPERATOR

shape Description

SQL BETWEEN AND operator checks whether a value is present between minimum and maximum range of an expression.

shape Syntax

Select * from <table_name> where <column_name> between minimum and maximum;
table_name =>Any accurate table. column_name =>The condition that one can perform on a column by using Between and operator.

shape Examples

By viewing the below example, the concept of Between And operations can be easily understood from the table. [sql]sql> select * from employee22; +--------+--------+-----------+-------+------------+ | emp_id | ename | job | sal | comisssion | +--------+--------+-----------+-------+------------+ | 1001 | kate | manager | 14000 | 10 | | 1002 | jack | scaleman | 13000 | 20 | | 1003 | maddie | business | 14000 | 30 | | 1004 | madd | business | 14000 | NULL | | 1005 | winni | marketing | 14500 | NULL | +--------+--------+-----------+-------+------------+ 5 rows in set (0.00 sec) sql> select * from employee22 where sal&gt; 13000 and sal&lt;14500; +--------+--------+----------+-------+------------+ | emp_id | ename | job | sal | comisssion | +--------+--------+----------+-------+------------+ | 1001 | kate | manager | 14000 | 10 | | 1003 | maddie | business | 14000 | 30 | | 1004 | madd | business | 14000 | NULL | +--------+--------+----------+-------+------------+ 3 rows in set (0.00 sec) [/sql] The above example tells that, when Between and operation is performed on a column name salary, then it automatically show the salary, according to the condition and  gives the output.

NOT EQUAL Operator

shape Description

SQL NOT EQUAL operator is used to return a set of rows from the table, after making sure that two expressions placed on either sides of the table are NOT NULL.

shape Syntax

Select * from <table_name> where <column_name> is not equal to;
table_name =>Any accurate table. column_name =>The condition that one can perform on columns by using Not Equal operator.

shape Examples

By viewing the below example, the concept of NOT EQUAL can be easily understand from the table. [sql]sql> select * from employee22; +--------+--------+-----------+-------+------------+ | emp_id | ename | job | sal | comisssion | +--------+--------+-----------+-------+------------+ | 1001 | kate | manager | 14000 | 10 | | 1002 | jack | scaleman | 13000 | 20 | | 1003 | maddie | business | 14000 | 30 | | 1004 | madd | business | 14000 | NULL | | 1005 | winni | marketing | 14500 | NULL | +--------+--------+-----------+-------+------------+ 5 rows in set (0.00 sec) sql> select * from employee22 where job&lt;&gt;'business'; +--------+-------+-----------+-------+------------+ | emp_id | ename | job | sal | comisssion | +--------+-------+-----------+-------+------------+ | 1001 | kate | manager | 14000 | 10 | | 1002 | jack | scaleman | 13000 | 20 | | 1005 | winni | marketing | 14500 | NULL | +--------+-------+-----------+-------+------------+ 3 rows in set (0.00 sec)[/sql] The above example tells that, when Not Equal operation is performed on a column name job<>business then it automatically display remaining employee who are working in another sectors.

Summary

shape Key Points

  • Logical AND Operator - Returns true if two conditions are true.
  • Logical OR Operator - Returns true if single condition is valid.
  • IS NOT NULL Operator - Removes null values and IS NULL displays null values.
  • BETWEEN AND Operator - Checks the values between the condition.
  • NOT EQUAL Operator - Show all the details except the given condition.