-
Notifications
You must be signed in to change notification settings - Fork 0
/
CASES _Intermediate_SQL.sql
50 lines (44 loc) · 1.45 KB
/
CASES _Intermediate_SQL.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
/*
TOPIC: CASE STATEMENT
*/
SELECT FirstName, LastName, Age,
CASE
WHEN Age > 30 THEN 'Old'
ELSE 'Young'
END
FROM EmployeeDemographics
WHERE Age IS NOT NULL
ORDER BY Age
--- THE CASE STATEMENTS IS LIKE AN IF FUNCTION, YOU SET CONDITIONS TO BE MET ACCORDINGLY
--- AND THE FIRST CONDITION IS ALWAYS MET FIRST REGARDLESS IF THE SECOND CONDITION HAS THE SAME CONDITION WITH A DIFFERENT OUTCOME
--- THE OUTCOME OF THE FIRST CONDITION WILL BE SET/MET FIRST
SELECT FirstName, LastName, Age,
CASE
WHEN Age > 30 THEN 'Old'
WHEN Age BETWEEN 27 AND 30 THEN 'Young'
ELSE 'Baby'
END
FROM EmployeeDemographics
WHERE Age IS NOT NULL
ORDER BY Age
----- Testing the first condition with similar outcomes and showing which one will be returned first
SELECT FirstName, LastName, Age,
CASE
WHEN Age = 38 THEN 'Stanley'
WHEN Age > 30 THEN 'Old'
ELSE 'Baby'
END
FROM EmployeeDemographics
WHERE Age IS NOT NULL
ORDER BY Age
------ /SALARY RAISE FOR EMPLOYEES DEPENDING BY THE JOB TITLE/----
SELECT FirstName, LastName, JobTitle, Salary,
CASE
WHEN JobTitle = 'Salesman' THEN Salary + (Salary * .10)
WHEN JobTitle = 'Accountant' THEN Salary + (Salary * .05)
WHEN JobTitle = 'HR' THEN Salary + (Salary * .000001)
ELSE Salary + (Salary * .03)
END AS SalaryAfterRaise
FROM EmployeeDemographics
JOIN EmployeeSalary
ON EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID