You are on page 1of 6

Indian Institute of Information Technology Sri City

Database Management Systems LAB-06


TOPIC : Derived tables, views and joins

1. Derived tables

Example1:
select branch_name,avg_balance
from (
select branch_name,avg(balance) as avg_balance
from account
group by branch_name
) as derived_table
Where avg_balance >=700;

Example2:
select avg(balance1)
from (
select sum(balance) as balance1
from account group by branch_name
) as t1;

2. Views

A view is a virtual table based on the result-set of an SQL statement. A view contains rows
and columns, just like a real table. The fields in a view are fields from one or more real
tables in the database.

a. Create
● create view v as <query expression>
● create view v as select branch_name, amount from loan;
b. Delete
drop view v;
c. Update
i. Create or replace view v as <query expression>
1. create view v as select * from loan;
2. create or replace view v as select account_number,balance from account;
ii. Insert into v values(__,__,__)

A view is said to be updatable(that is inserts,updates or deletes can be applied on view) if


following conditions are all satisfied:

● The from clause has only one database relation


● The select clause contains only attribute names of the relation and does not have any
expressions, aggregates or distinct specification
● Any attribute not listed in the select clause can be set null
● The query does not have a group by or having clause

1. Insert into v values(“A-100”,50000);


2. update v set balance = balance + 1000;

Practice questions: [Run these queries and attach screen shot] -> These are ungraded question

1. Find the names of all branches with customers who have an account in the bank
and who live in “Pittsfield”, using exactly one join
+------------------+
| branch_name |
+------------------+
| Redwood |
+------------------+

2. Display name and balance of the customers whose balance is 700 and above.
+----------------------+------------+
| customer_name | balance |
+----------------------+------ ----+
| Johnson | 900.00 |
| Smith | 700.00 |
| Jones | 750.00 |
| Lindsay | 700.00 |
+---------------------+-----------+
3. Find the total loan amount taken by ‘Smith’
+-------------+
| total_loan |
+-------------+
| 2900 |
+-------------+
4. Find the branch cities that occurred more than once in the branch table
+-------------+-------+
| branch_city | count |
+-------------+-----------+
| Brooklyn | 2 |
| Horseneck | 3 |
+-------------+-------+
5. Find the names of customers( along with branch name and city) who have account at
banks, present in the same (branch) city
+---------------+-------------+-------------+
| customer_name | branch_name | branch_city |
+---------------+-------------+-------------+
| Johnson | Brighton | Brooklyn |
| Jones | Brighton | Brooklyn |
| Johnson | Downtown | Brooklyn |
| Smith | Mianus | Horseneck |
| Hayes | Perryridge | Horseneck |
| Turner | Round Hill | Horseneck |
+---------------+-------------+-------------+

6. Display all customer cities and total loan amount taken by all customers from each of
those cities
(loan_amount 1000$ can be considered for both customers of L-17)
+---------------+------------+
| customer_city | total_loan |
+---------------+------------+
| Harrison | 2500 |
| Pittsfield | 1300 |
| Princeton | 1000 |
| Rye | 3400 |
| Brooklyn | NULL |
| Woodside | NULL |
| Stamford | NULL |
| Palo Alto | NULL |
+---------------+------------+
7. Display total balance amount of each customer in customer table( display null for those
who do not have account)
+---------------+---------------+
| customer_name | total_balance |
+---------------+---------------+
| Adams | NULL |
| Brooks | NULL |
| Curry | NULL |
| Glenn | NULL |
| Green | NULL |
| Hayes | 400.00 |
| Johnson | 1400.00 |
| Jones | 750.00 |
| Lindsay | 700.00 |
| Smith | 700.00 |
| Turner | 350.00 |
| Williams | NULL |
+---------------+---------------+

8. Display total loan amount of each customer in customer table( display null for those
who did not take loan)
+---------------+------------+
| customer_name | total_loan |
+---------------+------------+
| Adams | 1300 |
| Brooks | NULL |
| Curry | 500 |
| Glenn | NULL |
| Green | NULL |
| Hayes | 1500 |
| Johnson | NULL |
| Jones | 1000 |
| Lindsay | NULL |
| Smith | 2900 |
| Turner | NULL |
| Williams | 1000 |
+---------------+------------+

9. Create a view that displays customer_name,account_number and loannumber(null if


there is no data for any of the column)
+---------------+----------------+-------------+
| customer_name | account_number | loan_number |
+---------------+----------------+-------------+
| Adams | NULL | L-16 |
| Brooks | NULL | NULL |
| Curry | NULL | L-93 |
| Glenn | NULL | NULL |
| Green | NULL | NULL |
| Hayes | A-102 | L-15 |
| Johnson | A-101 | NULL |
| Johnson | A-201 | NULL |
| Jones | A-217 | L-17 |
| Lindsay | A-222 | NULL |
| Smith | A-215 | L-11 |
| Smith | A-215 | L-23 |
| Turner | A-305 | NULL |
| Williams | NULL | L-17 |

10. Try creating and inserting into view for each of the conditions mentioned above for
views,under which you can’t insert data into views.

__________________________________________________________________________________

Assignment-1 Questions:

Use the employee.csv file for below questions. Use only nested queries to solve the below questions.

1. Write a query to display the employee name and date of joining for all employees in the same
department as Mark. Exclude Mark.
2. Create a query to display the employee number and name for all employees who earn more than
the average salary. Sort the results in descending order of salary.
3. Write a query to display the employee number and name for all employees who work in a
department with any employee whose name contains a “N”.
4. Display the employee name, department number, and job title for all employees whose place is
India.
5. Write an SQL query to show the second highest salary from the employee table.

Assignment-2 Question.
Please use the following schemas and create the following tables. Then write queries for the following
questions using the concept of nested queries.

Sailors(sid: integer, sname: string, rating: integer, age: real);


Boats(bid: integer, bname: string, color: string);
Reserves(sid: integer, bid: integer, day: date).

1. Find all information of sailors who have reserved boat number 101.
2. Find the name of the boat reserved by Bob.
3. Find the names of sailors who have reserved a red boat, and list in the order of age.
4. Find the names of sailors who have reserved at least one boat.
5. Find the ids and names of sailors who have reserved two different boats on the same day.
6. Find the ids of sailors who have reserved a red boat or a green boat.
7. Find the name and the age of the youngest sailor.
8. Count the number of different sailor names.
9. Find the average age of sailors for each rating level.
10. Find the average age of sailors for each rating level that has at least two sailors.

You might also like