-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemporary_tables.sql
More file actions
244 lines (181 loc) · 5.9 KB
/
Copy pathtemporary_tables.sql
File metadata and controls
244 lines (181 loc) · 5.9 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
-- =========================================================
-- FILE: temporary_tables.sql
-- PROJECT: TechSphere Solutions Database System
-- AUTHOR: Benard Onyango Omoga
-- DATABASE: MySQL
-- DESCRIPTION:
-- Demonstrates Temporary Tables in MySQL
-- using the TechSphere database schema.
-- =========================================================
-- =========================================================
-- USE DATABASE
-- =========================================================
USE techsphere_solutions;
-- =========================================================
-- SECTION 1: INTRODUCTION TO TEMPORARY TABLES
-- PURPOSE:
-- Temporary tables store intermediate results
-- during the current database session only.
-- =========================================================
-- =========================================================
-- SECTION 2: CREATING A TEMPORARY TABLE MANUALLY
-- PURPOSE:
-- Create a temporary table structure first,
-- then insert records manually.
-- =========================================================
CREATE TEMPORARY TABLE temp_project_assignments
(
first_name VARCHAR(50),
last_name VARCHAR(50),
project_name VARCHAR(100)
);
-- WHAT IT DOES:
-- Creates a temporary table named:
-- temp_project_assignments
-- This table exists only during
-- the current MySQL session.
-- View temporary table
SELECT *
FROM temp_project_assignments;
-- =========================================================
-- SECTION 3: INSERTING DATA INTO TEMPORARY TABLE
-- PURPOSE:
-- Add records into temporary table
-- =========================================================
INSERT INTO temp_project_assignments
VALUES
('Benard', 'Omoga', 'AI Dashboard System');
-- View inserted data
SELECT *
FROM temp_project_assignments;
-- WHAT IT DOES:
-- Inserts temporary project assignment data
-- into memory-only table.
-- =========================================================
-- SECTION 4: CREATING TEMP TABLE FROM QUERY
-- PURPOSE:
-- Faster and preferred method for temp tables
-- =========================================================
CREATE TEMPORARY TABLE high_salary_roles
SELECT
role_id,
role_title,
base_salary
FROM job_roles
WHERE base_salary > 100000;
-- WHAT IT DOES:
-- Creates a temporary table directly
-- from query results.
-- Stores high-paying job roles only.
-- =========================================================
-- SECTION 5: VIEW TEMP TABLE CONTENT
-- PURPOSE:
-- Display temporary table records
-- =========================================================
SELECT *
FROM high_salary_roles;
-- WHAT IT DOES:
-- Displays all high-paying job roles
-- stored inside the temporary table.
-- =========================================================
-- SECTION 6: TEMP TABLE WITH EMPLOYEE ANALYTICS
-- PURPOSE:
-- Store joined analytical data temporarily
-- =========================================================
CREATE TEMPORARY TABLE employee_salary_analysis
SELECT
e.employee_id,
CONCAT(e.first_name, ' ', e.last_name) AS employee_name,
d.department_name,
jr.role_title,
jr.base_salary
FROM employees e
JOIN departments d
ON e.department_id = d.department_id
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 >= 90000;
-- WHAT IT DOES:
-- Creates temporary analytical dataset
-- containing:
-- - Employee names
-- - Departments
-- - Job roles
-- - Salaries
-- Only employees earning 90,000+
-- are included.
-- =========================================================
-- SECTION 7: QUERYING TEMP ANALYTICS TABLE
-- PURPOSE:
-- Perform further analysis on temp data
-- =========================================================
SELECT *
FROM employee_salary_analysis
ORDER BY base_salary DESC;
-- WHAT IT DOES:
-- Displays employees sorted
-- from highest salary to lowest.
-- =========================================================
-- SECTION 8: BUSINESS ANALYTICS EXAMPLE
-- PURPOSE:
-- Temporary reporting dataset for management
-- =========================================================
CREATE TEMPORARY TABLE department_salary_summary
SELECT
d.department_name,
COUNT(e.employee_id) AS total_employees,
AVG(jr.base_salary) AS average_salary,
SUM(jr.base_salary) AS total_department_salary
FROM employees e
JOIN departments d
ON e.department_id = d.department_id
JOIN employee_positions ep
ON e.employee_id = ep.employee_id
JOIN job_roles jr
ON ep.role_id = jr.role_id
GROUP BY d.department_name;
-- View summary report
SELECT *
FROM department_salary_summary;
-- WHAT IT DOES:
-- Creates temporary department salary report
-- for business analytics and HR review.
-- =========================================================
-- KEY CONCEPT SUMMARY
-- =========================================================
-- TEMPORARY TABLE:
-- A table visible only during current session
-- CREATE TEMPORARY TABLE:
-- Creates temporary in-memory table
-- BENEFITS:
-- - Simplifies complex queries
-- - Stores intermediate calculations
-- - Improves query organization
-- - Useful for analytics and ETL workflows
-- =========================================================
-- TEMP TABLE VS CTE
-- =========================================================
-- TEMP TABLE:
-- - Exists for entire session
-- - Can be queried multiple times
-- - Stores physical temporary data
-- CTE:
-- - Exists only during query execution
-- - Better for readability
-- - Does not persist after query
-- =========================================================
-- BUSINESS USE CASES
-- =========================================================
-- Temporary tables are commonly used for:
-- - Payroll analysis
-- - Dashboard preparation
-- - ETL processing
-- - Intermediate reporting
-- - Data transformation
-- - Large analytical workflows
-- =========================================================
-- END OF FILE
-- =========================================================
```