SQL Joins and Aggregation 2
SQL Joins and Aggregation 2
AGGREGATION
Organised By: School of IT & Digital Analytics
▪ Join Examples
▪ SQL Aggregation
▪ Aggregation Examples
▪ Conclusion
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
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
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
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
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
300.00 50.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
Performance Optimisation
▪ Efficient queries significantly reduce the load
on your database server, improve response
times, and ensure a smooth user experience.
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.