-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_relational_schema_and_joins.sql.sql
More file actions
91 lines (80 loc) · 3.32 KB
/
Copy path03_relational_schema_and_joins.sql.sql
File metadata and controls
91 lines (80 loc) · 3.32 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
/*
====================================================================
Title: Relational Database Architecture & Multi-Table Joins
Author: Sean Rivas
Tools: MySQL Workbench
Description: Focuses on relational database design, mapping primary-to-foreign
key relationships, self-referencing employee hierarchies,
and multi-tier inner/left joins.
Key Techniques: Inner Joins, Left Joins, Recursive Self-Joins,
Pattern Matching (RLIKE), Unique Value Distinct Filtering
====================================================================
*/
USE banking;
-- ====================================================================
-- SECTION 1: PRODUCT CATALOG & CATEGORY MAPPING
-- ====================================================================
-- Objective: Join product catalogs to their high-level product types to audit inventory categorizations
SELECT
p.NAME AS product_name,
pt.NAME AS product_type
FROM product AS p
INNER JOIN product_type AS pt
ON p.product_type_cd = pt.product_type_cd;
-- ====================================================================
-- SECTION 2: BRANCH OPERATIONS & EMPLOYEE ASSIGNMENTS
-- ====================================================================
-- Objective: Map employees to their assigned operational bank branches to review staffing distributions
SELECT
b.NAME AS branch_name,
b.CITY AS city,
e.LAST_NAME AS last_name,
e.TITLE AS title
FROM branch AS b
INNER JOIN employee AS e
ON b.BRANCH_ID = e.ASSIGNED_BRANCH_ID;
-- Objective: Audit distinct employee job titles across the organization to understand structural roles
SELECT DISTINCT
TITLE AS job_title
FROM employee;
-- ====================================================================
-- SECTION 3: ORGANIZATIONAL HIERARCHIES (SELF-JOINS)
-- ====================================================================
-- Objective: Model reporting structures by linking employees to their respective managers/supervisors
SELECT
e.LAST_NAME AS employee_last_name,
e.TITLE AS employee_title,
m.LAST_NAME AS manager_last_name,
m.TITLE AS manager_title
FROM employee AS e
LEFT JOIN employee AS m
ON e.SUPERIOR_EMP_ID = m.EMP_ID;
-- ====================================================================
-- SECTION 4: MULTI-TABLE COMPLEX JOINS & ACCOUNT AUDITING
-- ====================================================================
-- Objective: Trace account balances back to specific financial products and individual account holders
SELECT
p.NAME AS product_name,
a.AVAIL_BALANCE AS available_balance,
i.LAST_NAME AS customer_last_name
FROM account AS a
INNER JOIN product AS p
ON a.PRODUCT_CD = p.PRODUCT_CD
LEFT JOIN customer AS c
ON a.CUST_ID = c.CUST_ID
LEFT JOIN individual AS i
ON c.CUST_ID = i.CUST_ID;
-- Objective: Filter transaction audit logs for specific account holders using regular expression pattern matching (LastName starting with 'T')
SELECT
ac.TXN_ID AS transaction_id,
ac.AMOUNT AS transaction_amount,
ac.TXN_DATE AS transaction_date,
i.LAST_NAME AS customer_last_name
FROM acc_transaction AS ac
INNER JOIN account AS a
ON ac.ACCOUNT_ID = a.ACCOUNT_ID
INNER JOIN customer AS c
ON a.CUST_ID = c.CUST_ID
INNER JOIN individual AS i
ON c.CUST_ID = i.CUST_ID
WHERE i.LAST_NAME RLIKE '^T';