0% found this document useful (0 votes)
14 views4 pages

Chapter 4 Sample Questions

The document outlines various SQL operations including data type queries, integrity constraints, authorization commands, embedded SQL, dynamic SQL, functions, recursive queries, and advanced SQL features. It provides specific SQL statements for tasks such as selecting account balances, updating branch assets, granting permissions, creating stored procedures, and categorizing loan amounts. The content serves as a comprehensive guide for performing database operations and maintaining data integrity in SQL.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Chapter 4 Sample Questions

The document outlines various SQL operations including data type queries, integrity constraints, authorization commands, embedded SQL, dynamic SQL, functions, recursive queries, and advanced SQL features. It provides specific SQL statements for tasks such as selecting account balances, updating branch assets, granting permissions, creating stored procedures, and categorizing loan amounts. The content serves as a comprehensive guide for performing database operations and maintaining data integrity in SQL.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

SQL Data Types and Schemas

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

7. Grant SELECT permission on the customer table to a user called


customer_user.
8. GRANT SELECT ON customer TO customer_user;
9. Revoke INSERT permission on the loan table from a user called loan_admin.
10. REVOKE INSERT ON loan FROM loan_admin;
11. Find the roles assigned to a specific user (e.g., bank_admin).
12. SELECT role_name
13. FROM user_roles
14. WHERE username = 'bank_admin';

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';

6. Functions and Procedural Constructs

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

19. Find the hierarchical structure of branches (parent-child relationship in the


branch table).
20. WITH RECURSIVE branch_hierarchy AS (
21. SELECT branch_name, parent_branch_name
22. FROM branch
23. WHERE parent_branch_name IS NULL
24. UNION ALL
25. SELECT b.branch_name, b.parent_branch_name
26. FROM branch b
27. JOIN branch_hierarchy bh ON b.parent_branch_name =
bh.branch_name
28. )
29. SELECT * FROM branch_hierarchy;
30. Retrieve all the ancestors of a specific loan, assuming a hierarchical loan
structure.
31. WITH RECURSIVE loan_ancestors AS (
32. SELECT loan_number, parent_loan_number
33. FROM loan
34. WHERE loan_number = 2001
35. UNION ALL
36. SELECT l.loan_number, l.parent_loan_number
37. FROM loan l
38. JOIN loan_ancestors la ON l.loan_number = la.parent_loan_number
39. )
40. SELECT * FROM loan_ancestors;
41. Find the total loan amount recursively for customers who have taken out
multiple loans across different branches.
42. WITH RECURSIVE total_loans AS (
43. SELECT customer_name, amount FROM loan
44. WHERE customer_name = 'Abdullah Hasan'
45. UNION ALL
46. SELECT l.customer_name, l.amount FROM loan l
47. JOIN total_loans tl ON l.customer_name = tl.customer_name
48. )
49. SELECT SUM(amount) FROM total_loans;

8. Advanced SQL Features

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;

Let me know if you'd like further explanation or more questions!

You might also like