Discovering Outer Joins in SQL | Insights from a RealLife Banking Case Study
Note: All SQL queries used in the Case study is tested on Oracle,SQLServer,MySQL and PostgreSQL.
1.List all customers along with their account details and transaction details, even if they have no accounts.
SELECT c.CustomerID, c.Name, a.AccountID, a.AccountType, a.Balance,
t.TransactionID, t.TransactionType, t.Amount, t.tDate
FROM Customers c
LEFT JOIN Accounts a
ON c.CustomerID = a.CustomerID
LEFT JOIN Transactions t
ON a.AccountID = t.AccountID;
Explanation
This query returns a full list of all customers and their account and transaction details, even if the customer has no accounts or any transactions. LEFT JOIN (outer join) allows the retrieval of all customers, regardless of any matching account in the accounts table. If the customer has no accounts, then all account and transaction fields will be returned NULL. The second LEFT JOIN joins the accounts table to the transactions table so that even if there are no transactions on an account, those fields will also return NULL.
It supports MySQL, PostgreSQL, SQL Server, and Oracle.
2.Find customers who have not made any transactions in the last year.
Oracle
SELECT c.CustomerID, c.Name
FROM Customers c
LEFT JOIN Accounts a ON c.CustomerID = a.CustomerID
LEFT JOIN Transactions t ON a.AccountID = t.AccountID
AND
t.tDate >= SYSDATE - INTERVAL '1' YEAR
WHERE t.TransactionID IS NULL;
Explanation
This query finds customers who have not made any transactions in the last year. specifically using Oracle SQL syntax It's doing two LEFT JOIN operations.
The first join links Customers to Accounts this would bring all the customers even those without accounts.
This second LEFT JOIN joins Accounts to the Transactions table but only for transactions which have happened within the last year t.tDate >= SYSDATE - INTERVAL '1' YEAR;.
The WHERE t.TransactionID IS NULL is the key to this question. That will clear all customers that have performed at least some transaction in the last year. This will be left with the customers either who have no transactions or who haven't made any transactions in the last year. It returns the list of inactive customers-in the case that they may have accounts-and hence can be used to find dormant accounts or customers not having recently engaged in financial activity.
__________
SQLServer
__________
SELECT c.CustomerID, c.Name
FROM Customers c
LEFT JOIN Accounts a ON c.CustomerID = a.CustomerID
LEFT JOIN Transactions t ON a.AccountID = t.AccountID
AND
t.tDate >= DATEADD(YEAR, -1, GETDATE())
WHERE t.TransactionID IS NULL;
Explanation
That SQL Server query gets the customers who made no transactions last year. It employs two LEFT JOIN statements: a first join connects the Customers table to the Accounts table, so that all customers are returned even though they may have made no accounts. The second LEFT JOIN joins Accounts table with the Transactions table but only includes transactions made inside the last year, so here t.tDate >= DATEADD(YEAR,-1,GETDATE()). In SQL Server, GETDATE() is a function that returns the current date, and the function DATEADD(YEAR,-1,GETDATE()) subtracts one year from the current date.
The WHERE t.TransactionID IS NULL condition excludes customers who have successfully created some transaction in the last year. It refers to those accounts that haven't been accessed for the past year. A query like this one is helpful in finding inactive customers because it would naturally include accounts with people who have accounts but have not made transactions within the last year. Thus, the list will show individuals with a dormant account or those who haven't recently touched the system.
__________
PostgreSQL
__________
SELECT c.CustomerID, c.Name FROM Customers c
LEFT JOIN Accounts a ON c.CustomerID = a.CustomerID
LEFT JOIN Transactions t ON a.AccountID = t.AccountID
AND t.tDate >= CURRENT_DATE - INTERVAL '1 year'
WHERE t.TransactionID IS NULL;
Explanation
This query uses LEFT JOIN to identify the customers who have not made a transaction over the past year. It starts by doing a LEFT JOIN of the Customers table and the Accounts table, including even those without accounts. It continues with another LEFT JOIN between Accounts and Transactions filtering tDate to show only transactions within the last year. t.tDate >= CURRENT_DATE - INTERVAL '1 year'. The important clause of the statement is WHERE t.TransactionID IS NULL: the link excludes all customers that have transacted with in the last year, to leave those who have not.
__________
MySQL
__________
SELECT c.CustomerID, c.Name
FROM Customers c
LEFT JOIN Accounts a ON c.CustomerID = a.CustomerID
LEFT JOIN Transactions t ON a.AccountID = t.AccountID
AND
t.tDate >= CURDATE() - INTERVAL 1 YEAR
WHERE t.TransactionID IS NULL;
3.Display accounts that do not have any associated transactions.
SELECT a.AccountID, a.AccountType, a.Balance
FROM Accounts a
LEFT JOIN
Transactions t ON a.AccountID = t.AccountID
WHERE t.TransactionID IS NULL;
Explanation
This query uses a LEFT JOIN an outer join which includes all the accounts that have no transactions associated with them. A LEFT JOIN is performed from the Transactions table using the AccountID field as the key, from the Accounts table. A LEFT JOIN assures you that all the rows in the Accounts table will be returned, even if there are no matching rows in the Transactions table.
What is the important part of the query is WHERE condition t.TransactionID IS NULL, These will only include accounts that have no corresponding transaction in the table. As the LEFT JOIN includes all accounts and fills in NULL in case there were no corresponding transactions this will be the efficient selection only for the accounts with no transactions.
This query is compatible for Oracle,MySQL,SQLServer,postgreSQL.