Branch and Account Type
Branch and Account Type
ChatGPT said:
Sure! Here are several realistic SQL examples involving BANK_BRANCH, ACCOUNT, and an
additional dimension: ACCOUNT_TYPE. These examples will help you practice common SQL
tasks like JOIN, GROUP BY, COUNT, and AGGREGATE FUNCTIONS.
sql
CopyEdit
BNAME VARCHAR(100),
CITY VARCHAR(50)
);
TYPE_NAME VARCHAR(50)
);
CUST_NAME VARCHAR(100),
BCODE VARCHAR(10),
TYPE_ID INT,
);
sql
CopyEdit
-- Branches
-- Account Types
-- Accounts
✅ Example Queries
sql
CopyEdit
FROM BANK_BRANCH BB
GROUP BY BB.BNAME;
sql
CopyEdit
FROM BANK_BRANCH BB
GROUP BY BB.BNAME;
sql
CopyEdit
FROM ACCOUNT A
sql
CopyEdit
SELECT BB.BNAME
FROM BANK_BRANCH BB
LEFT JOIN ACCOUNT A ON BB.BCODE = A.BCODE
sql
CopyEdit
FROM ACCOUNT A
GROUP BY AT.TYPE_NAME;
sql
CopyEdit
FROM ACCOUNT A
GROUP BY BB.BNAME;
sql
CopyEdit
FROM ACCOUNT A
You said:
yes please
ChatGPT said:
Great! Let's dive into advanced SQL examples using BANK_BRANCH, ACCOUNT, and
ACCOUNT_TYPE. We'll add functionality around:
2. 📊 Window Functions
4. 🔄 Transaction Simulation
sql
CopyEdit
WITH RankedCustomers AS (
SELECT
A.CUST_NAME,
BB.BNAME,
A.BALANCE,
FROM ACCOUNT A
WHERE rnk = 1;
📈 2. Window Function: Running Total of Balance per Branch
sql
CopyEdit
SELECT
A.CUST_NAME,
BB.BNAME,
A.BALANCE,
FROM ACCOUNT A
sql
CopyEdit
DELIMITER //
BEGIN
UPDATE ACCOUNT
END //
DELIMITER ;
💡 Usage:
sql
CopyEdit
sql
CopyEdit
DELIMITER //
BEGIN
FROM ACCOUNT
UPDATE ACCOUNT
ELSE
END IF;
END //
DELIMITER ;
💡 Usage:
sql
CopyEdit
sql
CopyEdit
START TRANSACTION;
UPDATE ACCOUNT SET BALANCE = BALANCE - 1000 WHERE ACC_NO = 1001; -- Debit from Alice
UPDATE ACCOUNT SET BALANCE = BALANCE + 1000 WHERE ACC_NO = 1002; -- Credit to Bob
-- Optional condition
COMMIT;
sql
CopyEdit
WITH BranchTypeBalances AS (
SELECT
BB.BNAME,
AT.TYPE_NAME,
A.BALANCE
FROM ACCOUNT A
FROM BranchTypeBalances
Would you like me to help you generate test data for bulk simulations or build a
report/dashboard query for a bank manager view (e.g., total deposits, customer count, top N
balances per city)?
You said:
yes please
ChatGPT said:
Perfect! Let's create a Bank Manager Dashboard SQL Report that includes:
Sections:
2. 👥 Total Customers
3. 💰 Total Deposits
sql
CopyEdit
2. 👥 Total Customers
sql
CopyEdit
sql
CopyEdit
sql
CopyEdit
FROM ACCOUNT
LIMIT 3;
5. 📊 Average Balance per Branch
sql
CopyEdit
FROM ACCOUNT A
GROUP BY BB.BNAME
sql
CopyEdit
FROM ACCOUNT A
GROUP BY AT.TYPE_NAME
sql
CopyEdit
WITH CityBranchSums AS (
SELECT
BB.CITY,
BB.BNAME,
SUM(A.BALANCE) AS TOTAL_BALANCE,
RANK() OVER (PARTITION BY BB.CITY ORDER BY SUM(A.BALANCE) DESC) AS rnk
FROM ACCOUNT A
FROM CityBranchSums
WHERE rnk = 1;
sql
CopyEdit
DELIMITER $$
BEGIN
ACC_NO,
CUST_NAME,
BALANCE,
BCODE,
TYPE_ID
)
VALUES (
1000 + i,
CONCAT('Customer_', i),
ELSE 'B003'
END,
FLOOR(1 + RAND() * 3)
);
SET i = i + 1;
END WHILE;
END$$
DELIMITER ;
CALL GenerateAccounts();
📦 Final Tip
You can wrap the dashboard queries in a stored procedure or view to run them in one go and
refresh periodically.
You said:
ChatGPT said:
Your document is ready! You can download it using the link below:
You said:
yes
ChatGPT said:
Your PDF is ready! You can download it using the link below:
You said:
ChatGPT said:
It seems like I can’t do more advanced data analysis right now. Please try again later.
However, I can still give you a complete .docx or .pdf file with everything we've discussed on this
page. Would you like me to retry generating both the full Word and PDF files once the tool is
available again, or would you prefer a downloadable .txt file version as a fallback right now?
You said:
yes please
ChatGPT said:
It seems like I can’t do more advanced data analysis right now. Please try again later.
Once the tools are available again, I can generate and provide downloadable .docx and .pdf
versions of the full page content for you. If you'd like, I can paste the complete text content here
right now so you can copy and save it manually. Would you like me to do that?
You said:
yes
ChatGPT said:
Here is the complete text content from this page, formatted for your convenience. You can copy
and paste this into a Word or text editor and save it manually.
🔧 1. Table Definitions
sql
CopyEdit
BNAME VARCHAR(100),
CITY VARCHAR(50)
);
TYPE_NAME VARCHAR(50)
);
CUST_NAME VARCHAR(100),
TYPE_ID INT,
);
📥 2. Sample Data
sql
CopyEdit
-- Branches
-- Account Types
-- Accounts
sql
CopyEdit
FROM BANK_BRANCH BB
GROUP BY BB.BNAME;
FROM BANK_BRANCH BB
GROUP BY BB.BNAME;
FROM ACCOUNT A
SELECT BB.BNAME
FROM BANK_BRANCH BB
FROM ACCOUNT A
GROUP BY AT.TYPE_NAME;
FROM ACCOUNT A
GROUP BY BB.BNAME;
FROM ACCOUNT A
sql
CopyEdit
WITH RankedCustomers AS (
SELECT A.CUST_NAME, BB.BNAME, A.BALANCE,
FROM ACCOUNT A
sql
CopyEdit
FROM ACCOUNT A
sql
CopyEdit
DELIMITER //
BEGIN
END //
DELIMITER ;
CopyEdit
DELIMITER //
BEGIN
ELSE
END IF;
END //
DELIMITER ;
sql
CopyEdit
START TRANSACTION;
COMMIT;
sql
CopyEdit
-- Total Branches
-- Total Customers
-- Total Deposits
SELECT CUST_NAME, ACC_NO, BALANCE FROM ACCOUNT ORDER BY BALANCE DESC LIMIT 3;
GROUP BY AT.TYPE_NAME;
WITH CityBranchSums AS (
sql
CopyEdit
DELIMITER $$
BEGIN
ACC_NO,
CUST_NAME,
BALANCE,
BCODE,
TYPE_ID
VALUES (
1000 + i,
CONCAT('Customer_', i),
ELSE 'B003'
END,
FLOOR(1 + RAND() * 3)
);
SET i = i + 1;
END WHILE;
END$$
DELIMITER ;
CALL GenerateAccounts();
📝 7. Notes
You can wrap the dashboard queries in a view or stored procedure for automation.
Modify SQL syntax as per your DBMS (MySQL, PostgreSQL, Oracle, SQL Server).
The RANK() and SUM() OVER() window functions enable analytical reports.
Let me know when you'd like a downloadable version again, or if you'd like this published as a
web or app-based dashboard!
Tools