-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample_queries_scripts.sql
More file actions
129 lines (107 loc) · 3.66 KB
/
Copy pathsample_queries_scripts.sql
File metadata and controls
129 lines (107 loc) · 3.66 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
-- =========================================================
-- FILE: queries.sql
-- PROJECT: TechSphere Solutions Database System
-- AUTHOR: Benard Onyango Omoga
-- DATABASE: MySQL
-- DESCRIPTION:
-- Collection of SQL validation and relational
-- analysis queries for the TechSphere Solutions
-- database management system.
-- =========================================================
-- =========================================================
-- USE DATABASE
-- =========================================================
USE techsphere_solutions;
-- =========================================================
-- SECTION 1: DATA VALIDATION QUERIES
-- PURPOSE:
-- Verify that records were inserted correctly
-- into all database tables.
-- =========================================================
-- ---------------------------------------------------------
-- Query 1: View all departments
-- PURPOSE:
-- Display all department records stored in the
-- departments table.
-- ---------------------------------------------------------
SELECT *
FROM departments;
-- ---------------------------------------------------------
-- Query 2: View all employees
-- PURPOSE:
-- Display all employee records including personal
-- and employment details.
-- ---------------------------------------------------------
SELECT *
FROM employees;
-- ---------------------------------------------------------
-- Query 3: View all job roles
-- PURPOSE:
-- Display all available company job roles and
-- their associated salaries.
-- ---------------------------------------------------------
SELECT *
FROM job_roles;
-- ---------------------------------------------------------
-- Query 4: View all employee positions
-- PURPOSE:
-- Display employee-role assignment records.
-- ---------------------------------------------------------
SELECT *
FROM employee_positions;
-- =========================================================
-- SECTION 2: RELATIONAL ANALYSIS QUERIES
-- PURPOSE:
-- Demonstrate SQL JOIN operations and analytical
-- queries across multiple related tables.
-- =========================================================
-- ---------------------------------------------------------
-- Query 5: Retrieve employees with departments
-- PURPOSE:
-- Combine employee data with department names
-- using a LEFT JOIN relationship.
-- ---------------------------------------------------------
SELECT
e.employee_id,
e.first_name,
e.last_name,
d.department_name
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.department_id;
-- ---------------------------------------------------------
-- Query 6: Retrieve employees with job roles
-- PURPOSE:
-- Display employee names together with their
-- assigned job roles and salaries.
-- ---------------------------------------------------------
SELECT
e.first_name,
e.last_name,
jr.role_title,
jr.base_salary
FROM employee_positions ep
JOIN employees e
ON ep.employee_id = e.employee_id
JOIN job_roles jr
ON ep.role_id = jr.role_id;
-- ---------------------------------------------------------
-- Query 7: Calculate average salary by department
-- PURPOSE:
-- Analyze departmental salary distribution using
-- aggregate functions and GROUP BY.
-- ---------------------------------------------------------
SELECT
d.department_name,
AVG(jr.base_salary) AS average_salary
FROM employee_positions ep
JOIN employees e
ON ep.employee_id = e.employee_id
JOIN departments d
ON e.department_id = d.department_id
JOIN job_roles jr
ON ep.role_id = jr.role_id
GROUP BY d.department_name;
-- =========================================================
-- END OF FILE
-- =========================================================