0% found this document useful (0 votes)
14 views8 pages

Day 4 - CASE and NULL

Uploaded by

rajatgogia1992
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views8 pages

Day 4 - CASE and NULL

Uploaded by

rajatgogia1992
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Case Statements and

Handling NULL Values


• Case Expression Syntax:

• In the above screenshot num_value is a column.

• In simple terms whenever you see a column name next to the CASE keyword it is called as a SIMPLE CASE EXPRESSION
as you are locking the logic to only one column which indicates that all the conditions you will be writing will only be on
that specific column.

• Whenever, you don’t see any column name next to the CASE keyword it is called as SEARCHED CASE EXPRESSION which
indicates that you are not locking the logic only to one column you can give the logic on any column as per your
requirement.
• Consider this employees table.

• Let’s suppose I want to increase the salary on the basis of JOB_ID.


1. All the people whose job_id is AD_PRES their salary needs to be increased by 10%
2. All the people whose job_id is IT_PROG their salary needs to be increased by 20%
3. Everyone else’s salary should remain as it is.

• As the question above clearly indicates that the salary has to be increased based on JOB_ID. It means I need to write a
query based on the JOB_ID i.e. on the basis of a single column so we can say that we will be writing a SIMPLE CASE
EXPRESSION.
• Consider this employees table.

• Let’s suppose I want to increase the salary on the basis of JOB_ID.


1. All the people whose job_id is AD_PRES their salary needs to be increased by 10%
2. Employee whose employee_id is 104 need to increase that person’s salary by 20%
3. Everyone else’s salary should remain as it is.

• As the question above clearly indicates that the salary has to be increased based on JOB_ID and Employee_id.It means I
need to write a query based on the JOB_ID and Employee_id i.e. on the basis of multiple column so we can say that we
will be writing a SEARCHED CASE EXPRESSION.
• NULL, In database context, is the total absence
of a value in a certain field and it means that the
field value is unknown.

NULL
• NULL is not same as ZERO value for a numerical
field, text field or space.

• NULL implies that a database field value has not


been stored.
FUNCTIONS TO HANDLE NULL VALUES

• Below are the list of functions available in MySQL that can help you to deal with NULL.

1. ISNULL
2. IFNULL
3. COALESCE
ISNULL:

Consider the below table where you can see a column by the name commission_pct where you see some values as NULL
and there are some real values present in the column.

. If we write the below query

select *, isnull(commission_pct) from employees;

. The output will look something like screenshot on right.

. All the places where there is NULL it will show as 1 which


means TRUE.

. All the places where there is NO NULL it will shows as 0


which means FALSE.
IFNULL:

Let’s suppose you want to replace these NULL with 0 or some other value given or decided by the business you can make use
of IFNULL function.

IFNULL takes two arguments.


1. First argument, is the column on which you would like to apply this function.
2. Second argument, is the value that you need to replace the NULLs with.

When I write the below query you can see that the rows where
it was NULL a new column has been added where the NULL is
showing as zero (as I have written a SELECT statement)
and wherever, there was a value it remained as it is.

If in case you want to change or replace the null in the same


column you can make use of UPDATE statement.

Update table_name
Set column_name = ifnull(column_name,0);
COALESCE:

Unlike IFNULL, Coalesce can take multiple arguments. The thing that we need to keep in mind is the datatype of the column as
all the values or arguments that you provide to replace for NULL for the column should be of the same datatype as the
column.

Consider the below example of the Locations table.

As you can see in the above screenshot the


first row for the state province column the value is NULL. I want to pick the value from city column wherever the state
province is NULL. If in case for that row the city column value is also NULL I want to pick the data from street address column
and put it in state province column.

So, the query will be something like this:

Select *, coalesce(state_province, city, street_address) as NewAddress from locations;

You might also like