Skip to content

Commit 6baac10

Browse files
committed
understanding case when then end keywords
1 parent b96b8d7 commit 6baac10

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

as_keyword.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@ select * from film; -- here we can change our sql statement accordingly
88

99
select * from newFilm;
1010

11+
12+
create table newFilm2
13+
AS
14+
select film_id, title, release_year from film;
15+
16+
select * from newFilm2;

case_when_then_end_keywords.sql

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use sakila;
2+
3+
show tables;
4+
5+
select * from customer;
6+
7+
-- all these keywords must be used together
8+
-- so its kind of basically giving the classification to the column values
9+
10+
select
11+
customer_id,
12+
first_name,
13+
last_name,
14+
CASE
15+
WHEN address_id > 100 THEN 'Address is greather than 100'
16+
WHEN address_id < 100 THEN 'Address is less than 100'
17+
WHEN address_id = 100 THEN 'Address is equal to 100'
18+
END as address_id_range
19+
from customer; -- now according to the value of the address_id the column will show this kind of output
20+
21+
22+
-- we can also use this in where clause accordingly
23+
select * from city;
24+
25+
select * from city
26+
where country_id =
27+
(
28+
CASE
29+
WHEN country_id in (82,87, 101, 60) then country_id
30+
WHEN country_id NOT IN (82, 87, 101, 60) then 90
31+
END
32+
); -- here all those country_id follows the first case there result will be shown
33+
34+
35+
-- in order by clause also
36+
select * from city
37+
order by
38+
(
39+
CASE
40+
WHEN country_id in (82,87, 101, 60) then city
41+
WHEN country_id NOT IN (82, 87, 101, 60) then country_id
42+
END
43+
);

0 commit comments

Comments
 (0)