Chapter 4 Sample Questions
Chapter 4 Sample Questions
1. Find all accounts where the balance is stored as a DECIMAL data type.
2. SELECT account_number, balance
3. FROM account
4. WHERE balance IS NOT NULL;
5. List the columns from the branch table and their data types.
6. DESCRIBE branch;
7. Find the names of customers with email addresses stored as VARCHAR data
type.
8. SELECT customer_name
9. FROM customer
10. WHERE email IS NOT NULL;
2. Integrity Constraints
4. Find customers who have an account but do not have a loan, ensuring referential
integrity is maintained.
5. SELECT c.customer_name
6. FROM customer c
7. LEFT JOIN depositor d ON c.customer_name = d.customer_name
8. LEFT JOIN borrower b ON c.customer_name = b.customer_name
9. WHERE b.customer_name IS NULL;
10. Update a branch's assets while ensuring the branch_name is unique.
11. UPDATE branch
12. SET assets = 60000
13. WHERE branch_name = 'Dhaka Branch';
14. Ensure that a customer cannot have duplicate account entries (enforce primary
key constraint).
15. ALTER TABLE depositor
16. ADD CONSTRAINT pk_depositor PRIMARY KEY (customer_name,
account_number);
3. Authorization
4. Embedded SQL
10. Write an embedded SQL statement to fetch the balance of an account for a given
customer in a programming language (e.g., Java, C++).
11. EXEC SQL SELECT balance INTO :balance FROM account WHERE
account_number = :account_number;
12. Write an embedded SQL query to update the loan amount for a specific loan in
the system.
13. EXEC SQL UPDATE loan
14. SET amount = :new_amount
15. WHERE loan_number = :loan_number;
16. Create an embedded SQL statement to insert a new customer into the customer
table.
17. EXEC SQL INSERT INTO customer (customer_name, customer_street,
customer_city)
18. VALUES (:customer_name, :customer_street, :customer_city);
5. Dynamic SQL
13. Write a dynamic SQL query to retrieve the account balance for a customer
where the column name is passed as a parameter (e.g., balance or amount).
14. EXECUTE IMMEDIATE 'SELECT ' || :column_name || ' FROM account WHERE
account_number = :account_number';
15. Create a dynamic SQL query to insert a new record into a table, where the table
name and values are provided dynamically.
16. EXECUTE IMMEDIATE 'INSERT INTO ' || :table_name || ' (column1,
column2) VALUES (:value1, :value2)';
17. Write a dynamic SQL query to select customers from a specific branch (branch
name passed as a parameter).
18. EXECUTE IMMEDIATE 'SELECT customer_name FROM customer WHERE
branch_name = :branch_name';
16. Create a stored procedure to update the balance of a customer's account based
on their account number.
17. CREATE PROCEDURE update_balance(IN acc_number INT, IN new_balance
DECIMAL)
18. BEGIN
19. UPDATE account
20. SET balance = new_balance
21. WHERE account_number = acc_number;
22. END;
23. Create a function to calculate the total balance of accounts in a given branch.
24. CREATE FUNCTION total_balance(branch VARCHAR(50))
25. RETURNS DECIMAL(15,2)
26. BEGIN
27. DECLARE total DECIMAL(15,2);
28. SELECT SUM(balance) INTO total FROM account WHERE branch_name =
branch;
29. RETURN total;
30. END;
31. Write a stored procedure to delete a customer from the customer table, ensuring
they do not have any linked accounts.
32. CREATE PROCEDURE delete_customer(IN customer_name VARCHAR(50))
33. BEGIN
34. IF NOT EXISTS (SELECT * FROM depositor WHERE customer_name =
customer_name) THEN
35. DELETE FROM customer WHERE customer_name = customer_name;
36. END IF;
37. END;
7. Recursive Queries
22. Use a CASE expression to categorize loan amounts into different ranges (e.g.,
'Small', 'Medium', 'Large').
23. SELECT loan_number, amount,
24. CASE
25. WHEN amount < 50000 THEN 'Small'
26. WHEN amount BETWEEN 50000 AND 100000 THEN 'Medium'
27. ELSE 'Large'
28. END AS loan_category
29. FROM loan;
30. Create a VIEW to list all customers with their corresponding loan status (based on
amount).
31. CREATE VIEW customer_loan_status AS
32. SELECT c.customer_name, l.amount,
33. CASE
34. WHEN l.amount < 50000 THEN 'Low'
35. WHEN l.amount BETWEEN 50000 AND 100000 THEN 'Moderate'
36. ELSE 'High'
37. END AS loan_status
38. FROM customer c
39. LEFT JOIN borrower b ON c.customer_name = b.customer_name
40. LEFT JOIN loan l ON b.loan_number = l.loan_number;
41. Use RANK() window function to rank customers based on their total loan
amounts per branch.
42. SELECT branch_name, customer_name, SUM(amount) AS
total_loan_amount,
43. RANK() OVER (PARTITION BY branch_name ORDER BY SUM(amount)
DESC) AS loan_rank
44. FROM borrower b
45. JOIN loan l ON b.loan_number = l.loan_number
46. GROUP BY branch_name, customer_name;