Вы находитесь на странице: 1из 5

--A

Red: Excluded for now, needs more advanced techniques

--1. Who supervises Tellers?


-- who
SELECT FNAME, LNAME
from employee
-- supervises
WHERE EMP_ID IN
( SELECT SUPERIOR_EMP_ID
from employee
-- Tellers
where title like 'Teller');
--2.How many checking accounts were opened at branches in the state of MA?
select count(account_id)
from account
where product_cd in
(select product_cd
from product
where name like 'checking account')
and open_branch_id in
(select branch_id
from branch
where state like 'MA');
--3.What is the total balance of accounts opened by individual customers
--from the state of NH?
select sum(avail_balance)
from account
where cust_id in
(select cust_id
from customer
where cust_type_cd like 'I'
and state like 'NH');
--4.We define anyone who is not a Teller as a managerial employee.
--Name the branches where these employees work.
select name
from branch
where BRANCH_ID in
(select assigned_branch_id
from employee
where title not in 'Teller');
--5.Modify the query above to list branch names, and number of
--managerial employees working at each.
select name,
from branch
where branch_id in
(select assigned_branch_id
from employee
where title *****
select count(emp_id),
from employee
where title not in'Teller';
--6.Find supervisors of employees who work in New Hampshire (NH)
select fname, lname
from employee

where emp_id in
(select superior_emp_id
from employee
where assigned_branch_id in
(select branch_id
from branch
where state like 'NH'));
--7.Which departments had employees start in 2002?
select name
from department
where dept_id in
(select dept_id
from employee
where start_date between '01-01-2002' and '31-12-2002');
--8.List accounts opened by customers from Salem at the branch in the same city.
select account_id
from account
where cust_id in
(select cust_id
from customer
where city like 'Salem'
and
open_branch_id in
(select branch_id
from branch
where city like 'Salem'));
--B Queries in this section are excerpted from the Subqueries 2 Note (SQL 4.2).
--Try them out, then visit the note where you are guided to some of the solution
s.
-- 1.which employee directly reports to the president, but manages no one?
select fname, lname
from employee
where emp_id not in
(select superior_emp_id
from employee
where superior_emp_id is not null)
and
superior_emp_id =
(select emp_id
from employee
where title like 'President');
/* 2. all accounts opened by Tellers at Woburn Branch */
select account_id
from account
where open_branch_id in
(select branch_id
from branch
where name like 'Woburn Branch')
and open_emp_id in
(select emp_id
from employee
where title like 'Teller' or title like 'Head Teller');

/* 3. find department names for tellers */


select name
from department
where dept_id in
(select dept_id
from employee
where title like 'Teller' or title like 'Head Teller');
/* 4. Find all checking accounts opened by Paula Roberts*/
select account_id
from account
where product_cd in
(select product_cd
from product
where name like 'checking account')
and
open_emp_id =
(select emp_id
from employee
where fname like 'Paula' and lname like 'Roberts');
/* 5. Find how many checking accounts were opened by Paula Roberts */
select count(account_id)
from account
where product_cd in
(select product_cd
from product
where name like 'checking account')
and
open_emp_id =
(select emp_id
from employee
where fname like 'Paula' and lname like 'Roberts');
--6. Find how many accounts of each product were opened at Woburn branch?
select product_cd, count(account_id)
from account
where open_branch_id in
(select branch_id
from branch
where name like 'Woburn Branch')
group by product_cd;
/* 7. How many checking accounts opened by Paula Roberts
--have a balance more than 1000 Dollars? */
select count(account_id)
from account
where product_cd in
(select product_cd
from product
where name like 'checking account')
and
open_emp_id =
(select emp_id
from employee
where fname like 'Paula' and lname like 'Roberts')
and

avail_balance > 1000;


--C Some of the queries in this section may have been solved in class.
--The referenced note (SQL 4.2) is same as Subqueries 2.
--1. How many Tellers work in Operations?
select count(emp_id)
from employee
where dept_id in
(select dept_id
from department
where name like 'Operations');
--2. Find employees who work at Quincy Branch
--who started in 2001, 2002 or 2003
select fname, lname
from employee
where start_date between '01-01-01' and '31-01-03'
and assigned_branch_id in
(select branch_id
from branch
where name like 'Quincy Branch');
--3. Total balance for checking accounts opened by
--Michael Smith at Headquarters. Do not use CHK. ref: SQL 4.2 part E
select sum(avail_balance)
from account
where
product_cd in
(select product_cd
from product
where name like 'checking account')
and
open_emp_id in
(select emp_id
from employee
where fname like 'Michael' and lname like 'Smith'
and assigned_branch_id =
(select branch_id
from branch
where name like 'Headquarters'));
--4. Show accounts opened by Tellers. ref SQL 4.2 part C
select account_id
from account
where open_emp_id in
(select emp_id
from employee
where title like 'Teller' or title like 'Head Teller');
--5. Show moneymkt accounts opened by customers from Quincy, Woburn or Salem.
select account_id
from account
where product_cd in
(select product_cd
from product
where name like 'money market account')
and
cust_id in
(select cust_id

from customer
where city in ('Salem', 'Quincy', 'Woburn'));
--6. Show Tellers managed by Head Tellers
select fname, lname, emp_id
from employee
where title like 'Teller'
and
superior_emp_id in
(select emp_id
from employee
where title like 'Head Teller');
--7. Show department names for all Teller managed by the Head Teller
select name
from department
where dept_id in
(select dept_id
from employee
where title like 'Teller'
and
superior_emp_id in
(select emp_id
from employee
where title like 'Head Teller'));

Вам также может понравиться