0% found this document useful (0 votes)
24 views21 pages

SQL Joins and Aggregation 2

Uploaded by

michealtegha000
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)
24 views21 pages

SQL Joins and Aggregation 2

Uploaded by

michealtegha000
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/ 21

SQL JOINS AND

AGGREGATION
Organised By: School of IT & Digital Analytics

Trainer: Data Visualisation Team,


Access Bank Data Management Office
OUTLINE
▪ Introduction

▪ What are SQL Joins?

▪ Types of SQL Joins

▪ Join Examples

▪ SQL Aggregation

▪ Aggregation Examples

▪ Practical Considerations when Writing SQL Queries

▪ Conclusion

▪ Questions & Answers

Access Bank Data Management Office 2


INTRODUCTION

▪ Transactions, customer interactions, Joins and aggregation are important


and financial records make up vast SQL features for:
datasets
▪ combining data from multiple tables
▪ These need to be managed, and
analyzed, and utilized for better
▪ summarizing data for analysis
decision-making.

Access Bank Data Management Office 3


WHAT ARE SQL Combining Data Essential for Data
Across Tables Analysis
JOINS? ▪ SQL joins merge rows ▪ Facilitate
from multiple tables. comprehensive data
examination.
▪ They rely on related
columns to connect ▪ Crucial for handling &
data. consolidating dispersed
data sets.

Access Bank Data Management Office 4


TYPES OF SQL JOINS
Type of Join Description

Returns rows with common/matching values in


INNER JOIN
both tables.

Returns all rows from the left table, and the


LEFT JOIN
matching rows from the right table.

Returns all rows from the right table, and the


RIGHT JOIN
matching rows from the left table.

Returns all rows when there is a match in either


FULL OUTER left or right table.
JOIN That is, it combines the results of both LEFT and
RIGHT joins.

Access Bank Data Management Office 5


JOIN EXAMPLES

Access Bank Data Management Office 6


Customers Table Accounts Table
CustomerID Name City AccountID CustomerID Balance

1 John Doe New York 1001 1 5000.00

Inner Join 2

3
Jane Smith

Mike Johnson
Los Angeles

Chicago
1002

1003
2

4
3000.00

1500.00
Query:
SELECT Customers.Name,
Accounts.Balance
FROM Customers INNER JOIN
INNER JOIN Accounts
ON Customers.CustomerID
Result
= Accounts.CustomerID;
Name Balance

John Doe 5000.00

Jane Smith 3000.00

Access Bank Data Management Office 7


Customers Table Accounts Table
CustomerID Name City AccountID CustomerID Balance

1 John Doe New York 1001 1 5000.00

Left Join 2

3
Jane Smith

Mike Johnson
Los Angeles

Chicago
1002

1003
2

4
3000.00

1500.00
Query:
SELECT Customers.Name,
Accounts.Balance
FROM Customers LEFT JOIN
LEFT JOIN Accounts
ON Customers.CustomerID
Result
= Accounts.CustomerID;
Name Balance

John Doe 5000.00

Jane Smith 3000.00

Mike Johnson NULL

Access Bank Data Management Office 8


SQL AGGREGATION
Essence of SQL Aggregation:

• Perform calculations across multiple rows in a table

• Returns a singular resultant value

Common Aggregation Functions

• COUNT: Tallies total number of rows

• SUM: Adds up values in a column

• AVG: Computes average value

• MAX: Finds the highest value

• MIN: Identifies the lowest value

Access Bank Data Management Office 9


AGGREGATION
EXAMPLES

Access Bank Data Management Office 10


Transactions Table
TransactionID AccountNumber Amount TransactionDate

1 1001 200.00 2024-01-01 10:00:00

2 1001 150.00 2024-01-02 14:30:00

Count 3

4
1002

1003
300.00

50.00
2024-01-03 09:00:00

2024-01-04 16:45:00

Query:
SELECT COUNT(*) COUNT
AS TotalTransactions
Result
FROM Transactions;
TotalTransactions

Access Bank Data Management Office 11


Transactions Table
TransactionID AccountNumber Amount TransactionDate

1 1001 200.00 2024-01-01 10:00:00

2 1001 150.00 2024-01-02 14:30:00

Sum 3

4
1002

1003
300.00

50.00
2024-01-03 09:00:00

2024-01-04 16:45:00

Query:
SELECT SUM(Amount) SUM
AS TotalAmount
Result
FROM Transactions;
TotalAmount

700.00

Access Bank Data Management Office 12


Transactions Table
TransactionID AccountNumber Amount TransactionDate

1 1001 200.00 2024-01-01 10:00:00

2 1001 150.00 2024-01-02 14:30:00

Avg 3

4
1002

1003
300.00

50.00
2024-01-03 09:00:00

2024-01-04 16:45:00

Query:
SELECT AVG(Amount) AS AVG
AverageAmount
Result
FROM Transactions;
AverageAmount

175.00

Access Bank Data Management Office 13


Transactions Table
TransactionID AccountNumber Amount TransactionDate

1 1001 200.00 2024-01-01 10:00:00

2 1001 150.00 2024-01-02 14:30:00

Max and Min 3 1002 300.00 2024-01-03 09:00:00

4 1003 50.00 2024-01-04 16:45:00


Query:
SELECT MAX(Amount) AS MAX & MIN
MaxAmount, MIN(Amount
) AS MinAmount Result
FROM Transactions; MaxAmount MinAmount

300.00 50.00

Access Bank Data Management Office 14


Customers Table Accounts Table
CustomerID Name City AccountID CustomerID Balance

Combining Joins 1 John Doe New York 1001 1 5000.00

and Aggregation 2

3
Jane Smith

Mike Johnson
Los Angeles

Chicago
1002

1003
2

4
3000.00

1500.00
Query:
SELECT Customers.Name, SUM(
Accounts.Balance) AS TotalB
LEFT JOIN, SUM & GROUP BY
alance
FROM Customers
LEFT JOIN Accounts Result
ON Customers.CustomerID = A
Name TotalBalance
ccounts.CustomerID
GROUP BY Customers.Name; John Doe 5000.00

Jane Smith 3000.00

Mike Johnson NULL

Access Bank Data Management Office 15


Error Handling
▪ NULL Values: Handle NULL values properly to
avoid unexpected results.
▪ Data Type Mismatches: Ensure that columns
used in joins and aggregations are of
compatible data types.
PRACTICAL
Documentation and Maintenance
CONSIDERATIONS ▪ Commenting: Comment on complex queries
WHEN WRITING for clarity and future reference.
▪ Modular Queries: Break down complex
SQL QUERIES queries using CTEs or views for better long-
term maintenance.

Performance Optimisation
▪ Efficient queries significantly reduce the load
on your database server, improve response
times, and ensure a smooth user experience.

Access Bank Data Management Office 16


Query Optimisation

Avoid SELECT * Filter Data Early Use EXISTS instead of Order Joins
✓Select only the columns ✓Filter data at the IN Correctly
you need to avoid beginning of the query ✓For subqueries that ✓Join smaller tables first
processing extra data. using the WHERE clause return many rows, using to reduce the amount of
to reduce rows EXISTS can be more data processed in
processed in subsequent efficient than IN, subsequent joins.
operations. especially for larger
datasets.

Access Bank Data Management Office 17


CONCLUSION
• SQL joins and aggregation functions are
powerful tools for combining and analyzing data
• By understanding how to use these techniques,
you can gain valuable insights to enhance
decision-making across diverse use case
scenarios

Access Bank Data Management Office 18


Questions &
Answers

Access Bank Data Management Office


Additional Resources
▪ Further reading and practice on advanced
joins and aggregation
▪ Intermediate SQL tutorial on joins &
aggregation functions with practical examples
▪ Beginner friendly 4-hour video on SQL basics,
joins, and aggregations
▪ SQL joins cheat sheet

Access Bank Data Management Office 20


Thank You!
▪ We appreciate your attendance.

▪ Email us for data analytics related


enquiries via:
DMO_DataVisualisation

Access Bank Data Management Office 21

You might also like