forked from AlexTheAnalyst/MySQL-YouTube-Series
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Intermediate - Case Statements.sql
109 lines (46 loc) · 1.7 KB
/
Intermediate - Case Statements.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
-- Case Statements
-- A Case Statement allows you to add logic to your Select Statement, sort of like an if else statement in other programming languages or even things like Excel
SELECT *
FROM employee_demographics;
SELECT first_name,
last_name,
CASE
WHEN age <= 30 THEN 'Young'
END
FROM employee_demographics;
--
SELECT first_name,
last_name,
CASE
WHEN age <= 30 THEN 'Young'
WHEN age BETWEEN 31 AND 50 THEN 'Old'
WHEN age >= 50 THEN "On Death's Door"
END
FROM employee_demographics;
-- Poor Jerry
-- Now we don't just have to do simple labels like we did, we can also perform calculations
-- Let's look at giving bonuses to employees
SELECT *
FROM employee_salary;
-- Pawnee Council sent out a memo of their bonus and pay increase structure so we need to follow it
-- Basically if they make less than 45k then they get a 5% raise - very generous
-- if they make more than 45k they get a 7% raise
-- they get a bonus of 10% if they work for the Finance Department
SELECT first_name, last_name, salary,
CASE
WHEN salary > 45000 THEN salary + (salary * 0.05)
WHEN salary < 45000 THEN salary + (salary * 0.07)
END AS new_salary
FROM employee_salary;
-- Unfortunately Pawnee Council was extremely specific in their wording and Jerry was not included in the pay increases. Maybe Next Year.
-- Now we need to also account for Bonuses, let's make a new column
SELECT first_name, last_name, salary,
CASE
WHEN salary > 45000 THEN salary + (salary * 0.05)
WHEN salary < 45000 THEN salary + (salary * 0.07)
END AS new_salary,
CASE
WHEN dept_id = 6 THEN salary * .10
END AS Bonus
FROM employee_salary;
-- as you can see Ben is the only one who get's a bonus