Вы находитесь на странице: 1из 23

15IT0YA - Database Management

Systems

Sri Vinitha V
Assistant Professor
Department of IT
Alpha Breathing

 Breathe in (deeply)
 Breathe out (slowly)
 Hold the Breath
Micro planning
Discussion, 2 Mind
Map, 3
Formative Summary, 2
Assessment 2, 3 Stimulating
Questions, 8

Alpha Breathing, 2

SO3, 15

Prerequisites, 3

Evocation, 3

SO1 & SO2, 15 GO & SO, 2


Formative
Assessment 1, 2
Objectives
General Objective
 Students will be able to know how to write
queries to satisfy multiple filtering criteria.
Specific Objectives
 How to use CASE statement (T) (U-P)
 Discover how to filter data based on multiple
condition (T) (An-C)
 List out the use of OR and NOT (T) (An-C)
CASE and WHEN Statement
Case and When
It is similar to IF-THEN-ELSE statement
If condition given in WHEN is true, it will execute
THEN part
Otherwise it will return the value in the ELSE
clause
ELSE clause is optional
If the ELSE clause is omitted and no condition is
found to be true, then the CASE statement will
return NULL
Conditions are evaluated in the order listed
Syntax:
Simple CASE expression
Case expression
when value_1 then result_1
when value_2 then result_2

when value_n then result_n
else result
End

(or)
Syntax:
Searched CASE expression
Case
when condition_1 then result_1
when condition_2 then result_2

when condition_n then result_n
else result
end
Example:
Simple CASE expression
Select eno, ename, dept, salary,
Case dept
when ‘ECE’ then 1.10*salary
when ‘EEE’ then 1.15*salary
when ‘EIE’ then 1.20*salary
when ‘ME’ then 1.25*salary
else salary
End “revised_salary”
from employee;
Example:
Searched CASE expression
Select eno, ename, dept, salary,
Case
when salary<5000 then ‘Low’
when salary<10000 then ‘Medium’
when salary<20000 then ‘Good’
else ‘Excellent’
End “revised_salary”
from employee;
AND
 It returns TRUE if both the conditions are true

Syntax:
Select * from table_name where condition_1 and
condition_2;

Example:
Select * from employee where salary > 25000 and
dept=‘civil’;
OR
 It returns TRUE if either of the condition is true
Syntax:
Select * from table_name where condition_1 or
condition_2;

Example:
Select * from employee where salary > 25000 or
dept=‘civil’;
NOT
 It returns TRUE if the condition is FALSE
Syntax:
Select * from table_name where column_name not
in (values1, … , valuesn);
Example:
Select * from employee where dept not in (‘ECE’,
‘EEE’, ‘EIE’);
NOT
 NOT operator can also be used with other SQL
operators, such as BETWEEN, LIKE, and
NULL
 … where dept NOT IN (‘ECE’, ‘EEE’, ‘EIE’)
 … where salary NOT BETWEEN 20000 and
30000
 … where name NOT LIKE ‘%a%’
 … where commission IS NOT NULL
NULL
 A field with a NULL value is a field with no value.
 If a field in a table is optional, it is possible to insert a new
record or update a record without adding a value to this
field. Then, the field will be saved with a NULL value.
 Comparing a column to NULL using the = operator is
undefined.
 For comparing a column to NULL, use IS NULL or IS
NOT NULL.
NULL
 A value of NULL indicates that the value is unknown
 A value of NULL is different from an empty or zero
value
 No two null values are equal
 Comparisons between two null values, or between a
NULL and any other value, return unknown because the
value of each NULL is unknown.
Syntax
 SELECT column-names FROM table-name WHERE
column-name IS NULL;
 SELECT column-names FROM table-name WHERE
column-name IS NOT NULL;

Query
 Select id from emp where salary IS NULL;
 Select id from emp where salary IS NOT NULL;
IFNULL
 The IFNULL( ) function is available in MySQL, and not
in SQL Server or Oracle
 This function takes two arguments
 If the first argument is not NULL, the function returns
the first argument. Otherwise, the second argument is
returned
 This function is commonly used to replace NULL value
with another value.
Table : Sales_Data
Store_Name Sales
Store A 300
Store B NULL

Query
SELECT SUM (IFNULL(Sales,100)) FROM Sales_Data;

Result:
SUM (IFNULL(Sales,100))
400

This is because NULL has been replaced by 100 via


the IFNULL function. The total then becomes 300 + 100 = 400.
In SQL,
 In SQL, NVL() and COALESCE() perform the same
operation as IFNULL()
 NVL(expression1, expression2)
 COALESCE(expression1, expression2)

Query
 SELECT SUM (NVL(Sales,100)) FROM Sales_Data;
 SELECT SUM (COALESCE(Sales,100)) FROM
Sales_Data;
References
 Silberchatz, Korth, Sudarsan, "Database System and
Concept", sixth edition, Mcgraw, 2013
 Dr. P.S. Deshpande, "SQL & PL/SQL for oracle 10G
Black book", Second edition, Wiley, 2010
Thank You

Вам также может понравиться