0% found this document useful (0 votes)
105 views45 pages

Final Book

The document is a guide titled 'SQL Projects on Banking and Finance' authored by Dr. Eyo Eyo, aimed at teaching SQL through practical business scenarios relevant to the banking and finance sector. It includes various business scenarios that banks may encounter, such as customer account balances, loan details, and transaction analysis, along with solutions provided at the end. The book is designed for a wide audience, from beginners to professionals, and emphasizes the importance of SQL in managing data effectively.

Uploaded by

abeltherealfm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views45 pages

Final Book

The document is a guide titled 'SQL Projects on Banking and Finance' authored by Dr. Eyo Eyo, aimed at teaching SQL through practical business scenarios relevant to the banking and finance sector. It includes various business scenarios that banks may encounter, such as customer account balances, loan details, and transaction analysis, along with solutions provided at the end. The book is designed for a wide audience, from beginners to professionals, and emphasizes the importance of SQL in managing data effectively.

Uploaded by

abeltherealfm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Y

SQL PROJECTS ON COP


EE
FR

BANKING
&
FINANCE
(WITH SOLUTIONS)

Eyowhite
SQL PROJECTS ON

BANKING
&
FINANCE

Dr. Eyo Eyo

Website: www.eyowhite.com
Twitter (X): twitter.com/eyowhite3
www.eyowhite.com

Copyright © 2025 Eyo Eyo

All rights reserved.

No part of this book shall be reproduced, stored in a retrieval system, or


transmitted by any means, electronic, mechanical, photocopying, recording, or
otherwise, without written permission from the publisher. No patent liability is
assumed with respect to the use of the information contained herein. Although
every precaution has been taken in the preparation of this book, the author
assumes no responsibility for errors or omissions. Nor is any liability assumed
for damages resulting from the use of the information contained herein.
Database Diagram
SQL Projects on Banking and Finance

Table of Contents
About the Author ......................................................................................................... 2
Important information ................................................................................................ 3
Business Scenarios ........................................................................................................ 5

1|Page
SQL Projects on Banking and Finance

About the Author

Dr. Eyo Eyo (PhD) is an IT Business Analyst and formerly a University Lecturer. He is an

accomplished researcher in the fields of machine learning, data analysis, business analysis,

and engineering.

Eyo holds a Doctor of Philosophy (PhD) degree, which stands as a testament to his dedication

to advancing knowledge in data science and engineering. His academic journey has been

marked by a relentless pursuit of excellence, resulting in numerous scholarly achievements

and contributions that have enriched his discipline.

Eyo’s commitment to data analytics and artificial intelligence goes beyond the classroom, as

he continually seeks innovative ways to engage learners and foster a deeper understanding

of complex subjects.

In the realm of research, Eyo has made significant strides. His work in machine learning and

data analysis has led to groundbreaking insights and practical applications, contributing to

the advancement of knowledge through numerous published works.

As you delve into the pages of Eyo's book, “SQL Projects on Banking and Finance”, you will

discover the depth of his expertise and the invaluable insights he brings to the world of

Structured Query Language and data analysis in general.

2|Page
Important information

Welcome to "SQL Projects on Banking and Finance". This book is designed to be

your comprehensive guide to mastering the art of SQL using practical business

scenarios. Whether you're a beginner looking to build a strong foundation or an

experienced practitioner aiming to sharpen your skills.

SQL (Structured Query Language) is the backbone of managing and manipulating

data in modern databases, making it an essential skill for anyone working with data-

driven applications.

This book covers the commands used in Data Query language (DQL) in most parts.

Other categories of SQL commands namely, Data Manipulation Language (DML),

Data Definition Language (DDL) and Data Control Language (DCL) will be covered in

a separate book.

Solutions to the problems in this book are given at the end of this book (before the

Appendix).

NOTE: The commands employed in the "sample" solutions primarily align with SQL
Server conventions, although suggestions are provided on how the syntax might be
adapted to suit other frequently used SQL platforms such as MySQL, Oracle,
PostgreSQL, etc.

Database used in this book.

The database used in this book is based solely on Banking and Finance as the title

suggests. Access to the database should have already been acquired when the book

was downloaded. The Database Diagram given above (after the copyright page) can

help in understanding the relationships between the tables.

3|Page
SQL Server installation and database restoration

In the Appendix, you'll find comprehensive instructions for installing SQL Server 2019

(or later) on Windows 10 and later versions, as well as the step-by-step process on

how to restore a database using the Microsoft AdventureWorks database as an

example.

Who This Book Is For

This "SQL Projects on Banking and Finance" assumes some fundamental

knowledge of SQL at the least. Nevertheless, it is intended for a wide audience,

including beginners, students, educators and database professionals and anyone who

uses SQL in their everyday lives and business.

Feedback

We value your feedback and suggestions. If you have any comments, questions, or

ideas for improvement, please don't hesitate to reach out on any of the following

platforms:

• Twitter: twitter.com/Eyowhite3

• Website: eyowhite.com/contact

Your input will help us enhance future editions of this book.

Thank you for choosing " SQL Projects on Banking and Finance ". We hope this book

serves as a valuable resource in your journey to become a proficient SQL practitioner.

Happy querying!

[Eyo Eyo]

4|Page
Business Scenarios

1. Business Scenario Q1

Customer Account Balances Overview


The bank management wants to have a comprehensive view of all customers along

with their account details and current balances. This information is crucial for

identifying high-value customers, understanding the distribution of account balances,

and planning targeted marketing campaigns.

2. Business Scenario Q2

High-Value Customers Identification


The bank management wants to identify all customers who have a balance greater

than $5,000 in their accounts. This information is critical for understanding the high-

value customer segment, offering them tailored financial products, and providing them

with premium customer services.

3. Business Scenario Q3

Transactions in 2022

The bank management wants to analyse all transactions made in the year 2022 to

understand customer behaviour, transaction volumes, and financial flows during that

period. This analysis will help in identifying trends, detecting anomalies, and planning

future strategies.

5|Page
4. Business Scenario Q4

Monthly Deposit Summary

The bank management wants to calculate the total amount deposited in all accounts

for the month of May 2022. This information is essential for monitoring cash inflows,

assessing the bank's liquidity position, and planning for future financial needs.

5. Business Scenario Q5

Customer Loan Details

The bank management wants to retrieve the details of all loans taken by a customer

with ID “C0768”. This information is crucial for understanding the customer's borrowing

behaviour, managing their credit risk, and providing them with tailored loan products.

6. Business Scenario Q6

Branch-Specific Employee List

The bank management wants to retrieve a list of all employees working in a branch

having an ID “B0851”. This information is useful for branch managers to understand

their team composition, manage human resources effectively, and plan for staffing

needs.

7. Business Scenario Q7

Total Credit Cards Issued

The bank management wants to determine the total number of credit cards issued by

the bank. This information is important for understanding the bank's reach in the credit

card market, evaluating the success of their credit card products, and planning future

marketing campaigns.
6|Page
8. Business Scenario Q8

Average Interest Rate for Loans

The bank management wants to calculate the average interest rate for all loans. This

information is essential for assessing the overall cost of borrowing for customers,

comparing it with industry benchmarks, and making decisions about future loan

product offerings and interest rate adjustments.

9. Business Scenario Q9

Active Customers in 2020

The bank management wants to identify and retrieve the details of all customers who

have made at least one transaction in the year 2020. This information is valuable for

understanding customer activity, identifying engaged customers, and planning

targeted marketing and customer retention strategies.

10. Business Scenario Q10

Inactive Accounts Between 2019 and 2023

The bank management wants to identify all accounts that have had no transactions

between the years 2019 and 2023. This information is important for understanding

long-term account inactivity, identifying dormant accounts, and planning strategies to

reactivate these accounts.

7|Page
11. Business Scenario Q11

Total Loan Payments in 2015

The bank management wants to calculate the total amount of payments made towards

loans in the year 2015. This information is essential for understanding the cash flow

related to loan repayments, assessing the bank's financial performance for that year,

and making strategic decisions based on loan repayment trends.

12. Business Scenario Q12

Customer Investments in Mutual Funds

The bank management wants to retrieve the details of all investments made by

customers in mutual funds. This information is valuable for understanding the

investment preferences of customers, assessing the performance of mutual fund

products, and planning targeted investment offerings.

13. Business Scenario Q13

Transaction Count by Account Type

The bank management wants to find the total number of transactions for each account

type (Checking, Savings, Credit). This information is important for understanding the

transaction activity across different types of accounts, identifying popular account

types, and making strategic decisions related to product offerings and customer

engagement.

8|Page
14. Business Scenario Q14

Employee Count by Branch

The bank management wants to list all branches along with the number of employees

working in each branch. This information is essential for understanding branch staffing

levels, identifying branches that may need additional staffing, and optimising human

resource allocation.

15. Business Scenario Q15

Total Outstanding Loan Amount by Customer

The bank management wants to calculate the total outstanding loan amount for each

customer. This information is crucial for assessing individual customer debt levels,

managing credit risk, and making informed decisions about loan approvals and

customer credit limits.

16. Business Scenario Q16

Customers with Multiple Account Types

The bank management wants to retrieve the details of all customers who have more

than one type of account. This information is important for understanding customer

engagement, identifying cross-selling opportunities, and analysing the diversity of

customer portfolios.

9|Page
17. Business Scenario Q17

Total Number of Loans Approved in 2017

The bank management wants to find the total number of loans approved in the year

2017. This information is essential for assessing the bank's lending activity for that

year, understanding market demand, and planning future loan offerings and strategies.

18. Business Scenario Q18

Average Balance of Savings Accounts

The bank management wants to calculate the average balance of all savings accounts.

This information is important for understanding the typical balance held by customers

in savings accounts, assessing the bank's liquidity, and making informed decisions

about interest rates and savings account products.

19. Business Scenario Q19

Customers with Stock Investments

The bank management wants to retrieve the details of all customers who have

investments in stocks. This information is valuable for understanding customer

investment behaviour, identifying customers interested in equity markets, and planning

targeted marketing campaigns for stock-related financial products.

20. Business Scenario Q20

Total Interest Earned on Loans in 2012

The bank management wants to calculate the total interest earned on all loans in the

year 2012. This information is crucial for understanding the revenue generated from

10 | P a g e
loan interest during that period, evaluating the profitability of the bank's lending

activities, and making informed financial planning and strategic decisions.

21. Business Scenario Q21

Total Number of Deposits in a Specific Branch

The bank management wants to calculate the total number of deposit transactions

made in a branch with ID “B0036”. This information is essential for understanding the

deposit activity within a branch, assessing branch performance, and planning resource

allocation.

22. Business Scenario Q22

Employees Hired in 2018

The bank management wants to retrieve the details of all employees who were hired

in the year 2018. This information is important for understanding hiring trends,

analysing employee retention, and planning future hiring strategies.

23.Business Scenario Q23

Total Amount of Investments Made by All Customers

The bank management wants to calculate the total amount of investments made by all

customers. This information is crucial for understanding the overall investment activity,

evaluating the bank's investment product performance, and making strategic decisions

regarding investment offerings.

11 | P a g e
24. Business Scenario Q24

Customers with Multiple Loans

The bank management wants to retrieve the details of all customers who have more

than one loan. This information is important for understanding customer borrowing

behaviour, identifying high-risk customers, and providing targeted financial services.

25. Business Scenario Q25

Accounts with Low Balances

The bank management wants to list all accounts that have a balance less than $500.

This information is important for identifying accounts that may require attention, such

as those at risk of becoming inactive or needing additional financial products and

services to encourage higher balances.

26. Business Scenario Q26

Total Amount of Withdrawals in January 2023

The bank management wants to calculate the total amount of withdrawals made in

January 2023. This information is essential for understanding cash outflows, assessing

liquidity needs, and planning for financial management and customer service

strategies.

27.Business Scenario Q27

Customers Making Payments Using Bank Transfers

The bank management wants to retrieve the details of all customers who have made

payments using bank transfers. This information is important for understanding

12 | P a g e
customer payment preferences, identifying trends in payment methods, and planning

targeted services and promotions for bank transfer users.

28. Business Scenario Q28

Total Number of Credit Card Transactions in 2022

The bank management wants to find the total number of credit card transactions made

in the year 2022. This information is important for understanding the usage and

popularity of credit cards among customers, assessing transaction volumes, and

planning marketing strategies for credit card products.

29. Business Scenario Q29

Average Credit Limit of Credit Cards

The bank management wants to calculate the average credit limit of all credit cards.

This information is essential for understanding the distribution of credit limits among

customers, assessing the bank's credit exposure, and making informed decisions

about credit card policies and offerings.

30. Business Scenario Q30

Customers with Bond Investments

The bank management wants to retrieve the details of all customers who have

investments in bonds. This information is valuable for understanding customer

investment preferences, identifying potential opportunities for targeted marketing of

bond-related financial products, and analysing the popularity of bonds among

customers.

13 | P a g e
31. Business Scenario Q31

Total Number of Loans Approved by Loan Type

The bank management wants to calculate the total number of loans approved for each

loan type (Personal, Mortgage, Auto, Student). This information is crucial for

understanding the distribution of loan approvals across different types, evaluating the

demand for various loan products, and making informed decisions about future loan

offerings.

32.Business Scenario Q32

List of Employees Working as Loan Officers

The bank management wants to list all employees who work as loan officers. This

information is essential for understanding the workforce composition, managing

human resources, and planning targeted training and development programs for loan

officers.

33.Business Scenario Q33

Total Number of Accounts Opened in 2014

The bank management wants to find the total number of accounts that were opened

in the year 2014. This information is important for understanding the growth in the

customer base during that year, evaluating the success of marketing campaigns, and

making informed decisions about future strategies to attract new customers.

14 | P a g e
34. Business Scenario Q34

Average Transaction Amount by Transaction Type

The bank management wants to calculate the average transaction amount for each

transaction type. This information is essential for understanding customer behaviour,

identifying transaction trends, and making informed decisions about fee structures and

service offerings.

ADVANCED SCENARIOS

35.Business Scenario Q35

Identify High-Value Customers

The bank management wants to identify high-value customers, defined as the top 10%

of customers based on their total account balances and investments. This information

is crucial for targeted marketing, offering premium services, and personalised financial

products to these valuable customers.

36. Business Scenario Q36

Customer Segmentation

The bank management wants to categorise customers into segments (e.g., low,

medium, high value) based on their account balances, transaction frequency, and

investment amounts. This information is crucial for targeted marketing, personalised

service offerings, and enhancing customer satisfaction.

15 | P a g e
37. Business Scenario Q37

Account Activity Summary

The bank management wants to retrieve a summary of account activity for each

customer. This summary should include the total deposits, total withdrawals, and the

current balance for each customer's accounts. This information is crucial for

understanding customer behaviour, monitoring account health, and providing

personalised financial advice.

38.Business Scenario Q38

Customer Investment Profile

The bank management wants to list customers along with their total investment

amounts and the types of investments they hold. This information is crucial for

understanding customer investment behaviour, identifying potential high-value clients,

and tailoring investment products to meet customer needs.

39. Business Scenario Q39

Monthly Transaction Volume

The bank management wants to calculate the total transaction volume for each month

from 2011 to 2023, broken down by transaction type. This information is essential for

understanding transaction patterns, planning for resource allocation, and identifying

peak transaction periods.

16 | P a g e
40. Business Scenario Q40

Loan Repayment Status

The bank management wants to list all loans along with their repayment status. This

should include the total amount repaid and the outstanding balance for each loan. This

information is crucial for monitoring loan performance, identifying potential defaults,

and managing credit risk.

41. Business Scenario Q41

Analyse Customer Investment Trends

The bank management wants to analyse customer investment trends, Year-over-Year

Growth in Investments: This includes understanding how customers are investing over

time, identifying popular investment types, and tracking the total investment amounts.

This information is crucial for developing investment products, marketing strategies,

and providing personalised investment advice.

42. Business Scenario Q42

Credit Card Expiry Notification

The bank management wants to identify credit cards that are set to expire in the next

three months (from January 2022) and list their holders. This information is crucial for

customer service, allowing the bank to notify customers about the impending expiration

of their credit cards and facilitate timely renewals or replacements.

17 | P a g e
43. Business Scenario Q43

Customer Investment Portfolio Analysis

The bank management wants to analyse the investment portfolios of their customers.

This involves identifying the types of investments each customer has, the total amount

invested by each customer, and the distribution of investment types. This information

is crucial for understanding customer investment behaviour, tailoring investment

products, and providing personalised financial advice.

44. Business Scenario Q44

The bank management wants to analyse the performance of each branch. This

includes calculating the total number of accounts, total loan amounts, total transaction

amounts, and total number of employees for each branch. This information is crucial

for evaluating branch performance, identifying high-performing branches, and making

informed decisions about resource allocation and branch management.

18 | P a g e
SOLUTIONS
1 --Q1.
2 SELECT
3 c.CustomerID,
4 c.FirstName,
5 c.LastName,
6 c.Gender,
7 c.DateOfBirth,
8 c.Email,
9 c.PhoneNumber,
10 a.AccountID,
11 a.AccountType,
12 a.Balance,
13 a.OpenDate
14 FROM
15 FB.Customers c
16 JOIN
17 FB.Accounts a
18 ON
19 c.CustomerID = a.CustomerID;
20
21
22 --Q2.
23 SELECT
24 c.CustomerID,
25 c.FirstName,
26 c.LastName,
27 c.Gender,
28 c.DateOfBirth,
29 c.Email,
30 c.PhoneNumber,
31 a.AccountID,
32 a.AccountType,
33 a.Balance,
34 a.OpenDate
35 FROM
36 FB.Customers c
37 JOIN
38 FB.Accounts a
39 ON
40 c.CustomerID = a.CustomerID
41 WHERE
42 a.Balance > 5000;
43
44 --Q3.
45 SELECT
46 t.TransactionID,
47 t.AccountID,
48 t.TransactionType,
49 t.Amount,
50 t.TransactionDate
51 FROM
52 FB.Transactions t
53 WHERE
SOLUTIONS
54 t.TransactionDate BETWEEN '2022-01-01' AND '2022-12-31';
55
56 --Q4.
57 SELECT
58 SUM(t.Amount) AS TotalDeposits
59 FROM
60 FB.Transactions t
61 WHERE
62 t.TransactionType = 'Deposit'
63 AND t.TransactionDate >= '2022-05-01'
64 AND t.TransactionDate < '2022-06-01';
65
66 --Q5.
67 SELECT
68 l.LoanID,
69 l.CustomerID,
70 l.LoanType,
71 l.LoanAmount,
72 l.InterestRate,
73 l.LoanDate
74 FROM
75 FB.Loans l
76 WHERE
77 l.CustomerID = 'C0768';
78
79 --Q6.
80 SELECT
81 e.EmployeeID,
82 e.FirstName,
83 e.LastName,
84 e.Position,
85 e.HireDate,
86 e.PhoneNumber,
87 e.BranchID
88 FROM
89 FB.Employees e
90 WHERE
91 e.BranchID = 'B0851';
92
93
94 --Q7.
95 SELECT
96 COUNT(cc.CardID) AS TotalCreditCardsIssued
97 FROM
98 FB.Credit_cards cc;
99
100 --Q8.
101 SELECT
102 AVG(l.InterestRate) AS AverageInterestRate
103 FROM
104 FB.Loans l;
105
106 --Q9.
SOLUTIONS
107 SELECT DISTINCT
108 c.CustomerID,
109 c.FirstName,
110 c.LastName,
111 c.Gender,
112 c.DateOfBirth,
113 c.Email,
114 c.PhoneNumber
115 FROM
116 FB.Customers c
117 JOIN
118 FB.Accounts a
119 ON
120 c.CustomerID = a.CustomerID
121 JOIN
122 FB.Transactions t
123 ON
124 a.AccountID = t.AccountID
125 WHERE
126 t.TransactionDate >= '2020-01-01'
127 AND t.TransactionDate < '2021-01-01';
128
129 --Q10.
130 SELECT
131 a.AccountID,
132 a.CustomerID,
133 a.AccountType,
134 a.Balance,
135 a.OpenDate
136 FROM
137 FB.Accounts a
138 WHERE
139 a.AccountID NOT IN (
140 SELECT
141 t.AccountID
142 FROM
143 FB.Transactions t
144 WHERE
145 t.TransactionDate >= '2019-01-01'
146 AND t.TransactionDate < '2024-01-01'
147 );
148
149 --Q11.
150 SELECT
151 SUM(p.Amount) AS TotalLoanPayments2015
152 FROM
153 FB.Payments p
154 WHERE
155 p.PaymentDate >= '2015-01-01'
156 AND p.PaymentDate < '2016-01-01';
157
158 --Q12.
159 SELECT
SOLUTIONS
160 i. InvestmentID,
161 i.CustomerID,
162 c.FirstName,
163 c.LastName,
164 i.InvestmentType,
165 i.Amount,
166 i.InvestmentDate
167 FROM
168 FB.Investments i
169 JOIN
170 FB.Customers c
171 ON
172 i.CustomerID = c.CustomerID
173 WHERE
174 i.InvestmentType = 'Mutual Funds';
175
176 --13.
177 SELECT
178 a.AccountType,
179 COUNT(t.TransactionID) AS TotalTransactions
180 FROM
181 FB.Transactions t
182 JOIN
183 FB.Accounts a
184 ON
185 t.AccountID = a.AccountID
186 GROUP BY
187 a.AccountType;
188
189
190 --Q14.
191 SELECT
192 b.BranchID,
193 b.BranchName,
194 COUNT(e.EmployeeID) AS NumberOfEmployees
195 FROM
196 FB.Branches b
197 JOIN
198 FB.Employees e
199 ON
200 b.BranchID = e.BranchID
201 GROUP BY
202 b.BranchID,
203 b.BranchName;
204
205 --Q15.
206 SELECT
207 l.CustomerID,
208 c.FirstName,
209 c.LastName,
210 SUM(l.LoanAmount) AS TotalOutstandingLoanAmount
211 FROM
212 FB.Loans l
SOLUTIONS
213 JOIN
214 FB.Customers c
215 ON
216 l.CustomerID = c.CustomerID
217 GROUP BY
218 l.CustomerID,
219 c.FirstName,
220 c.LastName;
221
222 --Q16.
223 SELECT DISTINCT
224 c.CustomerID,
225 c.FirstName,
226 c.LastName,
227 c.Gender,
228 c.DateOfBirth,
229 c.Email,
230 c.PhoneNumber
231 FROM
232 FB.Customers c
233 JOIN
234 FB.Accounts a
235 ON
236 c.CustomerID = a.CustomerID
237 WHERE
238 c.CustomerID IN (
239 SELECT
240 CustomerID
241 FROM
242 FB.Accounts
243 GROUP BY
244 CustomerID
245 HAVING
246 COUNT(DISTINCT AccountType) > 1
247 );
248
249
250 --Q17.
251 SELECT
252 COUNT(l.LoanID) AS TotalLoansApproved2017
253 FROM
254 FB.Loans l
255 WHERE
256 l.LoanDate >= '2017-01-01'
257 AND l.LoanDate < '2018-01-01';
258
259
260 --Q18.
261 SELECT
262 AVG(a.Balance) AS AverageSavingsBalance
263 FROM
264 FB.Accounts a
265 WHERE
SOLUTIONS
266 a.AccountType = 'Savings';
267
268
269 --Q19.
270 SELECT DISTINCT
271 c.CustomerID,
272 c.FirstName,
273 c.LastName,
274 c.Gender,
275 c.DateOfBirth,
276 c.Email,
277 c.PhoneNumber
278 FROM
279 FB.Customers c
280 JOIN
281 FB.Investments i
282 ON
283 c.CustomerID = i.CustomerID
284 WHERE
285 i.InvestmentType = 'Stocks';
286
287
288 --Q20.
289 SELECT
290 SUM(l.LoanAmount * l.InterestRate / 100) AS TotalInterestEarned2012
291 FROM
292 FB.Loans l
293 WHERE
294 l.LoanDate >= '2012-01-01'
295 AND l.LoanDate < '2013-01-01';
296
297 --Q21.
298 SELECT
299 COUNT(t.TransactionID) AS TotalDeposits
300 FROM
301 FB.Transactions t
302 JOIN
303 FB.Accounts a
304 ON
305 t.AccountID = a.AccountID
306 JOIN
307 FB.Employees e
308 ON
309 a.CustomerID = e.EmployeeID
310 JOIN
311 FB.Branches b
312 ON
313 e.BranchID = b.BranchID
314 WHERE
315 t.TransactionType = 'Deposit'
316 AND b.BranchID = 'B0036';
317
318
SOLUTIONS
319
320 --Q22.
321 SELECT
322 e.EmployeeID,
323 e.FirstName,
324 e.LastName,
325 e.Position,
326 e.HireDate,
327 e.PhoneNumber,
328 e.BranchID
329 FROM
330 FB.Employees e
331 WHERE
332 e.HireDate >= '2018-01-01'
333 AND e.HireDate < '2019-01-01';
334
335 --Q23.
336 SELECT
337 SUM(i.Amount) AS TotalInvestments
338 FROM
339 FB.Investments i;
340
341 --Q24.
342 SELECT DISTINCT
343 c.CustomerID,
344 c.FirstName,
345 c.LastName,
346 c.Gender,
347 c.DateOfBirth,
348 c.Email,
349 c.PhoneNumber
350 FROM
351 FB.Customers c
352 JOIN
353 (SELECT
354 CustomerID
355 FROM
356 FB.Loans
357 GROUP BY
358 CustomerID
359 HAVING
360 COUNT(LoanID) > 1) l
361 ON
362 c.CustomerID = l.CustomerID;
363
364 --Q25.
365 SELECT
366 a.AccountID,
367 a.CustomerID,
368 a.AccountType,
369 a.Balance,
370 a.OpenDate
371 FROM
SOLUTIONS
372 FB.Accounts a
373 WHERE
374 a.Balance < 500;
375
376 --Q26.
377 SELECT
378 SUM(t.Amount) AS TotalWithdrawalsJanuary2023
379 FROM
380 FB.Transactions t
381 WHERE
382 t.TransactionType = 'Withdrawal'
383 AND t.TransactionDate >= '2023-01-01'
384 AND t.TransactionDate < '2023-02-01';
385
386 --Q27.
387 SELECT DISTINCT
388 c.CustomerID,
389 c.FirstName,
390 c.LastName,
391 c.Gender,
392 c.DateOfBirth,
393 c.Email,
394 c.PhoneNumber
395 FROM
396 FB.Customers c
397 JOIN
398 FB.Loans l
399 ON
400 c.CustomerID = l.CustomerID
401 JOIN
402 FB.Payments p
403 ON
404 l.LoanID = p.LoanID
405 WHERE
406 p.PaymentMethod = 'Bank Transfer';
407
408 --Q28.
409 SELECT
410 COUNT(t.TransactionID) AS TotalCreditCardTransactions2022
411 FROM
412 FB.Transactions t
413 JOIN
414 FB.Credit_cards cc
415 ON
416 t.AccountID = cc.CardID
417 WHERE
418 t.TransactionDate >= '2022-01-01'
419 AND t.TransactionDate < '2023-01-01';
420
421 --Q29.
422 SELECT
423 AVG(cc.CreditLimit) AS AverageCreditLimit
424 FROM
SOLUTIONS
425 FB.Credit_cards cc;
426
427 --Q30.
428 SELECT DISTINCT
429 c.CustomerID,
430 c.FirstName,
431 c.LastName,
432 c.Gender,
433 c.DateOfBirth,
434 c.Email,
435 c.PhoneNumber
436 FROM
437 FB.Customers c
438 JOIN
439 FB.Investments i
440 ON
441 c.CustomerID = i.CustomerID
442 WHERE
443 i.InvestmentType = 'Bonds';
444
445 --Q31.
446 SELECT
447 l.LoanType,
448 COUNT(l.LoanID) AS TotalLoansApproved
449 FROM
450 FB.Loans l
451 GROUP BY
452 l.LoanType;
453
454 --Q32.
455 SELECT
456 e.EmployeeID,
457 e.FirstName,
458 e.LastName,
459 e.Position,
460 e.HireDate,
461 e.PhoneNumber,
462 e.BranchID
463 FROM
464 FB.Employees e
465 WHERE
466 e.Position = 'Loan Officer';
467
468
469 --Q33.
470 SELECT
471 COUNT(a.AccountID) AS TotalAccountsOpened2014
472 FROM
473 FB.Accounts a
474 WHERE
475 a.OpenDate >= '2014-01-01'
476 AND a.OpenDate < '2015-01-01';
477
SOLUTIONS
478
479 --Q34.
480 SELECT
481 t.TransactionType,
482 AVG(t.Amount) AS AverageTransactionAmount
483 FROM
484 FB.Transactions t
485 GROUP BY
486 t.TransactionType;
487
488
489 --Q35.
490 WITH CustomerBalances AS (
491 SELECT Top 10
492 a.CustomerID,
493 SUM(a.Balance) AS TotalBalance
494 FROM
495 FB.Accounts a
496 GROUP BY
497 a.CustomerID
498 ), CustomerInvestments AS (
499 SELECT
500 i.CustomerID,
501 SUM(i.Amount) AS TotalInvestments
502 FROM
503 FB.Investments i
504 GROUP BY
505 i.CustomerID
506 ), CustomerTotalValue AS (
507 SELECT
508 cb.CustomerID,
509 (cb.TotalBalance + COALESCE(ci.TotalInvestments, 0)) AS TotalValue
510 FROM
511 CustomerBalances cb
512 LEFT JOIN
513 CustomerInvestments ci
514 ON
515 cb.CustomerID = ci.CustomerID
516 )
517 SELECT
518 c.CustomerID,
519 c.FirstName,
520 c.LastName,
521 c.Gender,
522 c.DateOfBirth,
523 c.Email,
524 c.PhoneNumber,
525 ct.TotalValue
526 FROM
527 FB.Customers c
528 JOIN
529 CustomerTotalValue ct
530 ON
SOLUTIONS
531 c.CustomerID = ct.CustomerID
532 ORDER BY
533 ct.TotalValue DESC
534
535 --Q36.
536 WITH CustomerBalances AS (
537 SELECT
538 a.CustomerID,
539 SUM(a.Balance) AS TotalBalance
540 FROM
541 FB.Accounts a
542 GROUP BY
543 a.CustomerID
544 ), CustomerTransactions AS (
545 SELECT
546 a.CustomerID,
547 COUNT(t.TransactionID) AS TransactionCount
548 FROM
549 FB.Transactions t
550 JOIN
551 FB.Accounts a
552 ON
553 t.AccountID = a.AccountID
554 GROUP BY
555 a.CustomerID
556 ), CustomerInvestments AS (
557 SELECT
558 i.CustomerID,
559 SUM(i.Amount) AS TotalInvestments
560 FROM
561 FB.Investments i
562 GROUP BY
563 i.CustomerID
564 ), CustomerMetrics AS (
565 SELECT
566 cb.CustomerID,
567 cb.TotalBalance,
568 COALESCE(ct.TransactionCount, 0) AS TransactionCount,
569 COALESCE(ci.TotalInvestments, 0) AS TotalInvestments
570 FROM
571 CustomerBalances cb
572 LEFT JOIN
573 CustomerTransactions ct
574 ON
575 cb.CustomerID = ct.CustomerID
576 LEFT JOIN
577 CustomerInvestments ci
578 ON
579 cb.CustomerID = ci.CustomerID
580 )
581 SELECT
582 cm.CustomerID,
583 cm.TotalBalance,
SOLUTIONS
584 cm.TransactionCount,
585 cm.TotalInvestments,
586 CASE
587 WHEN cm.TotalBalance >= 100000 OR cm.TotalInvestments >= 50000 THEN
'High Value'
588 WHEN cm.TotalBalance >= 50000 OR cm.TotalInvestments >= 25000 THEN
'Medium Value'
589 ELSE 'Low Value'
590 END AS CustomerSegment
591 FROM
592 CustomerMetrics cm;
593
594
595 --Q37.
596 -- Calculate Total Deposits and Total Withdrawals for Each Account
597 WITH TotalDeposits AS (
598 SELECT
599 t.AccountID,
600 SUM(t.Amount) AS TotalDepositAmount
601 FROM
602 FB.Transactions t
603 WHERE
604 t.TransactionType = 'Deposit'
605 GROUP BY
606 t.AccountID
607 ), TotalWithdrawals AS (
608 SELECT
609 t.AccountID,
610 SUM(t.Amount) AS TotalWithdrawalAmount
611 FROM
612 FB.Transactions t
613 WHERE
614 t.TransactionType = 'Withdrawal'
615 GROUP BY
616 t.AccountID
617 )
618
619 -- Combine Deposits and Withdrawals with Current Balance for Each Customer
620 SELECT
621 c.CustomerID,
622 c.FirstName,
623 c.LastName,
624 COALESCE(SUM(td.TotalDepositAmount), 0) AS TotalDeposits,
625 COALESCE(SUM(tw.TotalWithdrawalAmount), 0) AS TotalWithdrawals,
626 SUM(a.Balance) AS CurrentBalance
627 FROM
628 FB.Customers c
629 LEFT JOIN
630 FB.Accounts a
631 ON
632 c.CustomerID = a.CustomerID
633 LEFT JOIN
634 TotalDeposits td
SOLUTIONS
635 ON
636 a.AccountID = td.AccountID
637 LEFT JOIN
638 TotalWithdrawals tw
639 ON
640 a.AccountID = tw.AccountID
641 GROUP BY
642 c.CustomerID,
643 c.FirstName,
644 c.LastName
645 ORDER BY
646 c.CustomerID;
647
648
649 --Q38.
650 SELECT
651 c.CustomerID,
652 c.FirstName,
653 c.LastName,
654 SUM(i.Amount) AS TotalInvestmentAmount,
655 STRING_AGG(i.InvestmentType, ', ') AS InvestmentTypes
656 FROM
657 FB.Customers c
658 JOIN
659 FB.Investments i
660 ON
661 c.CustomerID = i.CustomerID
662 GROUP BY
663 c.CustomerID,
664 c.FirstName,
665 c.LastName
666 ORDER BY
667 TotalInvestmentAmount DESC;
668
669
670 --Q39.
671 SELECT
672 YEAR(t.TransactionDate) AS Year,
673 MONTH(t.TransactionDate) AS Month,
674 t.TransactionType,
675 SUM(t.Amount) AS TotalTransactionVolume
676 FROM
677 FB.Transactions t
678 WHERE
679 t.TransactionDate >= '2011-01-01'
680 AND t.TransactionDate < '2024-01-01'
681 GROUP BY
682 YEAR(t.TransactionDate),
683 MONTH(t.TransactionDate),
684 t.TransactionType
685 ORDER BY
686 Year,
687 Month,
SOLUTIONS
688 t.TransactionType;
689
690
691 --Q40.
692 WITH TotalPayments AS (
693 SELECT
694 p.LoanID,
695 SUM(p.Amount) AS TotalRepaid
696 FROM
697 FB.Payments p
698 GROUP BY
699 p.LoanID
700 )
701 SELECT
702 l.LoanID,
703 l.CustomerID,
704 l.LoanAmount,
705 COALESCE(tp.TotalRepaid, 0) AS TotalRepaid,
706 (l.LoanAmount - COALESCE(tp.TotalRepaid, 0)) AS OutstandingBalance,
707 CASE
708 WHEN COALESCE(tp.TotalRepaid, 0) >= l.LoanAmount THEN 'Fully Paid'
709 WHEN COALESCE(tp.TotalRepaid, 0) = 0 THEN 'Not Started'
710 ELSE 'In Progress'
711 END AS RepaymentStatus
712 FROM
713 FB.Loans l
714 LEFT JOIN
715 TotalPayments tp
716 ON
717 l.LoanID = tp.LoanID
718 ORDER BY
719 l.LoanID;
720
721
722 --Q41.
723 WITH YearlyInvestments AS (
724 SELECT
725 YEAR(i.InvestmentDate) AS Year,
726 SUM(i.Amount) AS TotalInvestmentAmount
727 FROM
728 FB.Investments i
729 GROUP BY
730 YEAR(i.InvestmentDate)
731 )
732 SELECT
733 y1.Year,
734 y1.TotalInvestmentAmount,
735 (y1.TotalInvestmentAmount - y2.TotalInvestmentAmount) /
y2.TotalInvestmentAmount * 100 AS YoYGrowth
736 FROM
737 YearlyInvestments y1
738 LEFT JOIN
739 YearlyInvestments y2
SOLUTIONS
740 ON
741 y1.Year = y2.Year + 1
742 ORDER BY
743 y1.Year;
744
745
746 --Q42.
747 SELECT
748 c.CustomerID,
749 c.FirstName,
750 c.LastName,
751 c.Email,
752 cc.CardID,
753 cc.CardNumber,
754 cc.ExpirationDate
755 FROM
756 FB.Customers c
757 JOIN
758 FB.Credit_cards cc
759 ON
760 c.CustomerID = cc.CustomerID
761 WHERE
762 cc.ExpirationDate BETWEEN '2022-01-01' AND DATEADD(MONTH, 3,
'2022-01-01')
763 ORDER BY
764 cc.ExpirationDate;
765
766
767 --Q43.
768 WITH CustomerInvestments AS (
769 SELECT
770 i.CustomerID,
771 i.InvestmentType,
772 SUM(i.Amount) AS TotalInvestmentAmount
773 FROM
774 FB.Investments i
775 GROUP BY
776 i.CustomerID,
777 i.InvestmentType
778 )
779 SELECT
780 c.CustomerID,
781 c.FirstName,
782 c.LastName,
783 ci.InvestmentType,
784 ci.TotalInvestmentAmount,
785 a.Balance AS AccountBalance
786 FROM
787 FB.Customers c
788 JOIN
789 CustomerInvestments ci
790 ON
791 c.CustomerID = ci.CustomerID
SOLUTIONS
792 JOIN
793 FB.Accounts a
794 ON
795 c.CustomerID = a.CustomerID
796 ORDER BY
797 c.CustomerID,
798 ci.InvestmentType;
799
800 --Q44.
801 WITH BranchAccounts AS (
802 SELECT
803 a.BranchID,
804 COUNT(a.AccountID) AS TotalAccounts,
805 SUM(a.Balance) AS TotalBalance
806 FROM
807 FB.Accounts a
808 GROUP BY
809 a.BranchID
810 ), BranchLoans AS (
811 SELECT
812 l.BranchID,
813 SUM(l.LoanAmount) AS TotalLoanAmount
814 FROM
815 FB.Loans l
816 GROUP BY
817 l.BranchID
818 ), BranchTransactions AS (
819 SELECT
820 a.BranchID,
821 SUM(t.Amount) AS TotalTransactionAmount
822 FROM
823 FB.Transactions t
824 JOIN
825 FB.Accounts a
826 ON
827 t.AccountID = a.AccountID
828 GROUP BY
829 a.BranchID
830 )
831 SELECT
832 b.BranchID,
833 b.BranchName,
834 COALESCE(ba.TotalAccounts, 0) AS TotalAccounts,
835 COALESCE(ba.TotalBalance, 0) AS TotalBalance,
836 COALESCE(bl.TotalLoanAmount, 0) AS TotalLoanAmount,
837 COALESCE(bt.TotalTransactionAmount, 0) AS TotalTransactionAmount,
838 COUNT(e.EmpID) AS TotalEmployees
839 FROM
840 FB.Branches b
841 LEFT JOIN
842 BranchAccounts ba
843 ON
844 b.BranchID = ba.BranchID
SOLUTIONS
845 LEFT JOIN
846 BranchLoans bl
847 ON
848 b.BranchID = bl.BranchID
849 LEFT JOIN
850 BranchTransactions bt
851 ON
852 b.BranchID = bt.BranchID
853 LEFT JOIN
854 FB.Employees e
855 ON
856 b.BranchID = e.BranchID
857 GROUP BY
858 b.BranchID,
859 b.BranchName,
860 ba.TotalAccounts,
861 ba.TotalBalance,
862 bl.TotalLoanAmount,
863 bt.TotalTransactionAmount
864 ORDER BY
865 b.BranchName;
866
Appendix

SQL Server Installation Guide

Phase 1: Installing SQL Server 2019 on a Windows 10 operating system.

1. To set up SQL Server 2019, obtain the necessary files by clicking the provided link:

https://www.microsoft.com/en-gb/sql-server/sql-server-downloads

2. Select "Download Now" for the developer edition.

3. After the file has finished downloading, double-click on it to initiate the installation.

4. In the window that appears, choose the "Basic" installation type.

18 | P a g e
5. Press "Next," agree to the Terms and Conditions, and then click "Install."

6. After the installation is finished, you will receive a link to download SQL Server

Management Studio. If you don't spot the link, please click on this provided link:

https://aka.ms/ssmsfullsetup

7. Download SQL Server Management Studio and proceed to install it.

Phase 2: AdventureWorks (2019 or 2022) Database

1. Upon the successful installation of SQL Server 2019, you'll require a database for practice.

Please follow the link below to download the AdventureWorks2019 or 2022 Database:

https://github.com/Microsoft/sql-server-samples/releases/tag/adventureworks

2. On the webpage, locate and select the highlighted option to download the

AdventureWorks2019.bak file or AdventureWorks2022.bak file.

19 | P a g e
3. Navigate to the folder where the AdventureWorks2012.bak file has been downloaded and

proceed to make a copy of the file.

4. Paste the file into the Backup folder within your freshly installed SQL system, which should

be situated in a location resembling the one described below:

C:\Program Files\Microsoft SQL Server\MSSQL11.SQLSERVERBI\MSSQL\Backup

5. Next, open SQL Server Management Studio from either the Programs Menu or the

Applications Desktop (Windows 8).

6. Now click on the following link to restore the database on SQL Server:

https://learn.microsoft.com/en-us/sql/samples/adventureworks-install-configure?view=sql-

server-ver16&tabs=ssms

20 | P a g e
Phase 3: AdventureWorks Data Warehouse Version (2019 or 2022) Database

1. The following link allows you to download the AdventureWorksDW2019.bak or

AdventureWorksDW2022.bak versions: https://learn.microsoft.com/en-

us/sql/samples/adventureworks-install-configure?view=sql-server-ver16&tabs=ssms

2. After downloading the file, following similar steps as in Phase 2 to restore the database.

Phase 4: WideWorldImporters Database:

1. The link to download the WideWorldImporters database is: https://github.com/Microsoft/sql-

server-samples/releases/tag/wide-world-importers-v1.0

2. Ensure to following the steps as outlined in Phase 2 to restore the database.

21 | P a g e
22 | P a g e

You might also like