Lab 9
Lab 9
Lab 09
Advanced SQL Operations: Insert into Select and Views
I. Topic Overview:
The students will solve problems to familiarize themselves advanced SQL operations, such
as Insert into Select and Views. The INSERT INTO SELECT statement copies data from
one table and inserts it into another table. In SQL, a view is a virtual table based on the
result-set of an SQL statement. There are 2 main tasks which contain some sub-tasks.
V. Activity Detail
a. Hour: 1
Discussion:
Explain Insert Into Select queries.
Problem Task:
i. Task 1
b. Hour: 2
Discussion:
Check students have completed Tasks 1. Explain Views to the students. Later,
check students can create virtual Tables. Help students solve the challenging
tasks.
Problem Task:
i. Task 2
Page 1 of 3
Department of Computer Science & Engineering
Course: Database Management Systems Lab (CSE331)
Credit Hour:1 Semester: SUMMER 2024
Use a Text editor such as Note Pad to type and save your program.
Copy and Paste your program from the Text editor to the command line. If the program works,
save the program. Otherwise, fix the error and save it.
Save your text file regularly.
We will use the same data as Lab 05. So if you have it with you, simply copy the folder to udrive>data. Then go to command
window, login and use db name which is “Bank”. If you don’t have it with you, you can borrow it from a friend!
Task 1
The INSERT INTO SELECT statement copies data from one table and inserts it into another
table. This is useful in creating backup tables or summary tables. INSERT INTO SELECT
requires that data types in source and target tables to match. The following syntax is used to copy
data from a table into another table. The first syntax is for copying all data; the second one is for
copying specific data.
1. Create new table “account2”. Account 2 should have the same columns and data type as
account table.
2. Insert all account information into account2 if the balance is greater than $500.
3. Create new table “branch_balance”. This table will have the following attributes:
branch_name:varchar(15) and total_balance:int.
4. Insert the branch name and the total available balance in each branch in the table
branch_balance.
5. Create a table “customer_loan”. The table should have the following attributes:
customer_name(varchar(20)), loan_amount(int).
Page 2 of 3
Department of Computer Science & Engineering
Course: Database Management Systems Lab (CSE331)
Credit Hour:1 Semester: SUMMER 2024
6. Insert the loan amount taken by the customers in the customer_loan table. [Note: You can
add SQL functions, WHERE, and JOIN statements to the “Insert Into Select” query and
present the data as if the data were coming from one single]
Task 2
In SQL, a view is a virtual table based on the result-set of an SQL statement. Views will always
show the updated data, i.e. if the data in the underlying table changes, same change is reflected in
the view. The database engine recreates the data, using the view's SQL statement, every time a
user queries a view. This is one of the major differences between a copied table and a view in SQL
as the data in the table will not change even if the data in the table the data was copied from
changes. You can add SQL functions, WHERE, and JOIN statements to a view and present the
data as if the data were coming from one single table. The following syntax is used to create views.
We can query a view using a “select” query just as a table.
1. Create a view branch_balance_view that will contain branch names and the total balance
amount in the branch.
2. Retrieve the data from branch_balance_view. Retrieve the data from
branch_balance_view and branch_balance. Check the difference(if any).
3. A new account was opened in Perryridge branch. The account number is A-300 and the
opening balance is $200. Insert this data in the account table.
4. Retrieve the data from branch_balance_view and branch_balance. Check the difference.
Page 3 of 3