0% found this document useful (0 votes)
2 views3 pages

17 March Assignment Case When

The document contains notes and SQL queries related to Unix and database operations, focusing on the use of CASE statements for conditional logic in SQL. It includes examples for categorizing employee salaries, updating records based on conditions, and aggregating data by gender and department. Upcoming topics mentioned are indexing, views, and various functions.

Uploaded by

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

17 March Assignment Case When

The document contains notes and SQL queries related to Unix and database operations, focusing on the use of CASE statements for conditional logic in SQL. It includes examples for categorizing employee salaries, updating records based on conditions, and aggregating data by gender and department. Upcoming topics mentioned are indexing, views, and various functions.

Uploaded by

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

1> class notes and assignments of unix on sharepoints

2> webminal.org
3> numbering filename

============
CASE WHEN#############

select e.*,
case
when sal < 90000 then 'low'
when sal < 800000 then 'avg'
else 'high'
end status

from employee e;

select e.*,
case
when country ='IN' then 'class_1'
when country ='US' then 'class_2'
else 'class_3'
end class

from employee e;

select e.*,
decode(country,'IN','class1','US','class2','class3') class
from employee e;

===============

1> TO SHOW THE STATUS OR FLAG


sel e.*,
case
when <condition> then <res>

when <condition> then <res>

when <condition> then <res>

else <default>

end as Status

from table e

2> FOR CONDITION BASED CALCULATIONS

SELECT *, CASE
WHEN SALARY < 4500 THEN (SALARY + SALARY * 25/100)
WHEN SALARY < 5000 THEN (SALARY + SALARY * 10/100)
END AS INCREMENT FROM CUSTOMERS;

3> FOR CONDITION BASED SORT (CASE STATMENT IN ORDER BY CLAUSE)


4> CONDITION BASED GROUPING WHERE KEY WILL BE NEWLY ADDED CONDITION
BASED COLUMN LIKE STATUS

4.1> Condition based (summation, counting, min,max, avg..) aggragation


5> CONDITION BASED UPDATION
UPDATE CUSTOMER
SET SALARY =
CASE WHEN SALARY < 4500 THEN (SALARY + SALARY * 25/100)
WHEN SALARY < 5000 THEN (SALARY + SALARY * 10/100)
END AS INCREMENT FROM CUSTOMERS;
6> (CASE WHILE INSERT)used for condition based value calculation of another column
based on existing column conditions.

=========================

t1
Cat1
AVG
GOOD
AZ
BAD
NEO
AVG
AVG
NEO
CAT1

sort it in order of NEO > Cat1> BAD> AVG >Good >

select *
from table
order by
case when t1='NEO' then 1
when t1='Cat1' then 2
when t1='BAD' then 3
when t1='AVG' then 4
when t1='Good' then 5
else 6
end
==============================
dept gender....
IT F abc...
IT M
IT F
IT M
IT M
CS M
CS M

o/p
IT F 1
IT M 3
CS F 0
CS M 2

SELECT DEPT,GENDER,COUNT(1)
FROM DEPT TABLE
GROUP BY DEPT,GENDER;

o/p
dept female_cnt male_cnt
IT 1 3
CS 0 2

select dept ,count(case when gender ='M' THAN 1 end) AS male_cnt


,count(case when gender = 'F' then 1 end) as fem_cnt
from emp group by dept
=====================================
Upcoming topics
index
view
functions (string, decimal, date)

---

You might also like