SQL Equi Join for Banking Matching Customer ,Account and Transaction Data
Note: All SQL queries used in the Case study is tested on Oracle,SQLServer,MySQL and PostgreSQL.
1.List all customers and the account types they hold, using explicit equality conditions in the join.
SELECT c.CustomerID, c.Name, a.AccountID, a.AccountType
FROM Customers c
JOIN
Accounts a ON c.CustomerID = a.CustomerID;
Explanation
This returns a list of all customers and the types of accounts they hold using a fair join process that explicitly includes both customers and accounts under the condition of equality. As it happened, the equi-join was performed on the word JOIN with the condition specified as ON c.CustomerID = a.CustomerID so it will only return those rows where CustomerID matches in both tables with the result set.This equality condition links a customer with their account, and the queries return the customer's information CustomerID, Name from the customers table along with the data of that account, whose ID and type can be accessed from the accounts table.
These queries work equivalently in Oracle, MySQL, PostgreSQL, and SQL Server because the syntax of simple joins based on explicit equality conditions is uniform across all these databases.
2. Find customers who have made deposits in their checking accounts.
SELECT c.CustomerID, c.Name, a.AccountID, a.AccountType, t.TransactionID, t.TransactionType, t.Amount
FROM Customers c, Accounts a, Transactions t
WHERE c.CustomerID = a.CustomerID
AND a.AccountID = t.AccountID
AND
a.AccountType = 'Checking'
AND t.TransactionType = 'Deposit';
Explanation
This SQL query brings in customer information, checking accounts and deposits into those accounts. It utilizes an inner join of three tables: Customers, Accounts and Transactions. It will select a few columns like CustomerID and Name in the Customers table, AccountID and AccountType in the Accounts table and TransactionID, TransactionType and Amount in the Transactions table. The conditions in the WHERE clause will make sure that only related data gets retrieved.
This joins the customers with the accounts through CustomerID meaning that only accounts of specific customers are taken into account. The second condition links the accounts and transactions tables through AccountID hence transactions will be associated with the correct accounts. Moreover it filters results since only accounts of type "Checking" and type "Deposit" for transactions should be included.Thus the search results will include names of all the customers who have deposited money in their checking accounts along with all the crucial transaction and account information.
3.Show customer and transaction details where the transaction amount is exactly equal to the account balance.
SELECT Customers.Name, Transactions.TransactionID, Transactions.Amount, Transactions.tDate
FROM Customers, Accounts, Transactions
WHERE Customers.CustomerID = Accounts.CustomerID
AND
Accounts.AccountID = Transactions.AccountID
AND
Transactions.Amount = Accounts.Balance;
Explanation
It picks up four particular details customer name, transaction ID, transaction amount and transaction date. To come up with this data it first joins the Customers table with the Accounts table by comparing the CustomerID to ensure that only the accounts connected to certain customers are taken into account.
Then it joins the Accounts table to the Transactions table where it equates the AccountID which would ensure only those transactions related to those accounts would be fetched.
Also, there is an important condition the query would satisfy itself by fetching only those transactions where the transaction amount equals the account balance.This means that the results will be returned where in the transaction amount in the transaction of that particular customer matches the current balance in his account.In the output that you will end up seeing here you have the customer's name the ID of the transaction the amount of the transaction and the date when this transaction occurred.
The query serves to help you get a more explicit view of customers who have an account balance equal to their transaction amount and all sorts of related information.
This query is compatible for Oracle,MySQL,SQLServer,postgreSQL.