0% found this document useful (0 votes)
0 views3 pages

Advanced PostgreSQL CTE Examples

The document provides advanced PostgreSQL Common Table Expressions (CTE) examples for banking tables. It includes queries for summarizing account types by branch, ranking branches by total balance, identifying branches without 'Current' accounts, aggregating balances and account counts by account type, showing branches with above-average balances, and identifying customers with multiple account types. Each example demonstrates the use of CTEs to efficiently retrieve and analyze banking data.

Uploaded by

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

Advanced PostgreSQL CTE Examples

The document provides advanced PostgreSQL Common Table Expressions (CTE) examples for banking tables. It includes queries for summarizing account types by branch, ranking branches by total balance, identifying branches without 'Current' accounts, aggregating balances and account counts by account type, showing branches with above-average balances, and identifying customers with multiple account types. Each example demonstrates the use of CTEs to efficiently retrieve and analyze banking data.

Uploaded by

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

Advanced PostgreSQL CTE Examples for Banking Tables

1. Branch-wise Account Type Summary


Show how many accounts of each type exist in every branch.

WITH AccountTypeCounts AS (
SELECT
BB.BNAME,
AT.TYPE_NAME,
COUNT(*) AS TOTAL_ACCOUNTS
FROM ACCOUNT A
JOIN BANK_BRANCH BB ON A.BCODE = BB.BCODE
JOIN ACCOUNT_TYPE AT ON A.TYPE_ID = AT.TYPE_ID
GROUP BY BB.BNAME, AT.TYPE_NAME
)
SELECT *
FROM AccountTypeCounts
ORDER BY BNAME, TYPE_NAME;

2. Top 3 Branches by Total Balance


Find the top 3 branches ranked by the sum of their account balances.

WITH BranchBalances AS (
SELECT
BB.BNAME,
SUM(A.BALANCE) AS TOTAL_BALANCE
FROM BANK_BRANCH BB
LEFT JOIN ACCOUNT A ON BB.BCODE = A.BCODE
GROUP BY BB.BNAME
),
RankedBranches AS (
SELECT *,
RANK() OVER (ORDER BY TOTAL_BALANCE DESC) AS BAL_RANK
FROM BranchBalances
)
SELECT *
FROM RankedBranches
WHERE BAL_RANK <= 3;

3. Branches with No 'Current' Accounts


List all branches that do not have any accounts of type 'Current'.

WITH BranchWithCurrent AS (
SELECT DISTINCT A.BCODE
FROM ACCOUNT A
JOIN ACCOUNT_TYPE AT ON A.TYPE_ID = AT.TYPE_ID
WHERE AT.TYPE_NAME = 'Current'
)
SELECT BB.BNAME
FROM BANK_BRANCH BB
LEFT JOIN BranchWithCurrent BWC ON BB.BCODE = BWC.BCODE
WHERE BWC.BCODE IS NULL;

4. Total Balance and Account Count Per Account Type


Aggregate balances and account counts grouped by account type.

WITH TypeSummary AS (
SELECT
AT.TYPE_NAME,
COUNT(A.ACC_NO) AS TOTAL_ACCOUNTS,
SUM(A.BALANCE) AS TOTAL_BALANCE
FROM ACCOUNT A
JOIN ACCOUNT_TYPE AT ON A.TYPE_ID = AT.TYPE_ID
GROUP BY AT.TYPE_NAME
)
SELECT * FROM TypeSummary;

5. Branches Where Average Balance > Overall Average


Show branches whose average account balance is above the global average.

WITH BranchAvg AS (
SELECT
BB.BNAME,
AVG(A.BALANCE) AS BRANCH_AVG
FROM BANK_BRANCH BB
JOIN ACCOUNT A ON BB.BCODE = A.BCODE
GROUP BY BB.BNAME
),
OverallAvg AS (
SELECT AVG(BALANCE) AS ALL_AVG FROM ACCOUNT
)
SELECT BA.*
FROM BranchAvg BA, OverallAvg OA
WHERE BA.BRANCH_AVG > OA.ALL_AVG;

6. Customers Holding Multiple Account Types


Identify customers who hold more than one type of account.

WITH TypeCountPerCustomer AS (
SELECT
CUST_NAME,
COUNT(DISTINCT TYPE_ID) AS TYPE_COUNT
FROM ACCOUNT
GROUP BY CUST_NAME
)
SELECT *
FROM TypeCountPerCustomer
WHERE TYPE_COUNT > 1;

You might also like