-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunions_queries_script.sql
More file actions
158 lines (115 loc) · 3.92 KB
/
Copy pathunions_queries_script.sql
File metadata and controls
158 lines (115 loc) · 3.92 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
-- =========================================================
-- FILE: unions.sql
-- PROJECT: TechSphere Solutions Database System
-- AUTHOR: Benard Onyango Omoga
-- DATABASE: MySQL
-- DESCRIPTION:
-- Demonstrates UNION, UNION ALL, and practical use cases
-- for combining row-based datasets.
-- =========================================================
-- =========================================================
-- USE DATABASE
-- =========================================================
USE techsphere_solutions;
-- =========================================================
-- SECTION 1: INTRODUCTION TO UNION
-- PURPOSE:
-- UNION combines results vertically (row-wise),
-- unlike JOIN which combines data horizontally.
-- =========================================================
-- =========================================================
-- SECTION 2: BASIC UNION (INCONSISTENT DATA - DEMO)
-- =========================================================
-- Combines unrelated datasets (not best practice, for learning only)
SELECT first_name, last_name
FROM employees
UNION
SELECT role_title, base_salary
FROM job_roles;
-- WHAT IT DOES:
-- Stacks results into one output table
-- Column names are taken from the first SELECT
-- Data types are implicitly converted where possible
-- =========================================================
-- SECTION 3: UNION DISTINCT (DEFAULT BEHAVIOR)
-- =========================================================
-- UNION automatically removes duplicates
SELECT first_name, last_name
FROM employees
UNION
SELECT first_name, last_name
FROM employees;
-- WHAT IT DOES:
-- Removes duplicate rows automatically
-- =========================================================
-- SECTION 4: UNION ALL (KEEPS DUPLICATES)
-- =========================================================
-- UNION ALL returns all records including duplicates
SELECT first_name, last_name
FROM employees
UNION ALL
SELECT first_name, last_name
FROM employees;
-- WHAT IT DOES:
-- Returns duplicate rows as well
-- Faster than UNION because no deduplication step
-- =========================================================
-- SECTION 5: PRACTICAL BUSINESS USE CASE
-- =========================================================
-- Identify employees based on risk categories:
-- 1. High age employees
-- 2. High salary employees
-- 3. Senior experienced staff classification
SELECT
first_name,
last_name,
'Senior Employee' AS category
FROM employees
WHERE TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) > 50
UNION
SELECT
first_name,
last_name,
'Experienced Employee' AS category
FROM employees
WHERE TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) BETWEEN 40 AND 50
UNION
SELECT
e.first_name,
e.last_name,
'High Salary Employee' AS category
FROM employees e
JOIN employee_positions ep
ON e.employee_id = ep.employee_id
JOIN job_roles jr
ON ep.role_id = jr.role_id
WHERE jr.base_salary >= 100000
ORDER BY first_name;
-- WHAT IT DOES:
-- Combines multiple analytical filters into one dataset:
-- - Older employees
-- - Mid-aged experienced employees
-- - High earning employees
-- =========================================================
-- SECTION 6: KEY RULES OF UNION
-- =========================================================
-- RULE 1:
-- Each SELECT must have same number of columns
-- RULE 2:
-- Corresponding columns must have compatible data types
-- RULE 3:
-- Column names come from first SELECT statement
-- RULE 4:
-- UNION removes duplicates, UNION ALL keeps them
-- =========================================================
-- SECTION 7: BUSINESS INSIGHT USE CASE
-- =========================================================
-- Used for:
-- - HR analysis
-- - Risk classification
-- - Employee segmentation
-- - Reporting dashboards
-- - KPI grouping across datasets
-- =========================================================
-- END OF FILE
-- =========================================================