数据库原理 Exercises 2

本文最后更新于:2022年5月16日 上午

Database System Concepts Exercises of Chapter 3

Database System Concepts Exercises of Chapter 3

Exercises 3.9 Consider the employee database of Figure \(3.20\), where the primary keys are underlined. Give an expression in SQL for each of the following queries.

  1. Find the names and cities of residence of all employees who work for "First Bank Corporation".

  2. Find the names, street addresses, and cities of residence of all employees who work for "First Bank Corporation" and earn more than \(\$ 10,000\).

  3. Find all employees in the database who do not work for "First Bank Corporation".

  4. Find all employees in the database who earn more than each employee of "Small Bank Corporation".

  5. Assume that the companies may be located in several cities. Find all companies located in every city in which "Small Bank Corporation" is located.

  6. Find the company that has the most employees.

  7. Find those companies whose employees earn a higher salary, on average, than the average salary at "First Bank Corporation".

\[ employee (\underline{employee\_name}, street, city)\\ works (\underline{employee\_name}, company\_name, salary)\\ company (\underline{company\_name}, city)\\ manages ( \underline{ employee\_name, } manager\_name )\\ Figure~~3.20~~Employee~~database~~for~~Exercises~~3.9 \]

My answer:

select e.employee_name, city from employee as e, works as w where w.company_name \(=\) 'First Bank Corporation' and w.employee_name \(=\) e.employee_name

select * from employee where employee_name in (select employee_name from works where company_name = 'First Bank Corporation' and salary \(>10000\) )

select employee_name from works where company_name \(\neq\) 'First Bank Corporation'

select employee_name from works where salary \(>\) all (select salary from works where company_name \(=\) 'Small Bank Corporation')

select S.company_name from company as \(S\) where not exists ((select city from company where company_name \(=\) 'Small Bank Corporation') except (select city from company as \(T\) where S.company_name \(=\) T.company_name ) )

select company_name from works group by company_name having count (distinct employee_name) \(>=\) all (select count (distinct employee_name) from works group by company_name)

select company_name from works group by company_name having avg (salary) \(>\) (select avg (salary) from works where company_name \(=\) 'First Bank Corporation')

Exercises 3.8 Consider the bank database of Figure \(3.19\), where the primary keys are underlined. Construct the following SQL queries for this relational database.

  1. Find all customers of the bank who have an account but not a loan.

  2. Find the names of all customers who live on the same street and in the same city as "Smith".

  3. Find the names of all branches with customers who have an account in the bank and who live in "Harrison".

\[ branch(\underline{branch\_name}, branch\_city, assets)\\ customer (\underline{customer\_name}, customer\_street, customer\_city)\\ loan (\underline{loan\_numbe}r, branch\_name, amount)\\ borrower (\underline{customer\_name}, \underline{loan\_number})\\ account (\underline{account\_number}, branch\_name, balance)\\ depositor (\underline{customer\_name}, \underline{account\_number})\\ Figure~~3.19~~Banking~~database~~for~~Exercises~~3.8~~and~~3.15 \]

My answer:

select customer_name from depositor except (select customer_name from borrower)

select F.customer_name from customer as \(F\) join customer as \(S\) using(customer_street, customer_city) where S.customer_name \(=\) 'Smith'

select distinct branch_name from account natural join depositor natural join customer where customer_city = 'Harrison'

Exercises 3.15 Consider the bank database of Figure 3.19, where the primary keys are underlined. Construct the following SQL queries for this relational database. a. Find all customers who have an account at all the branches located in "Brooklyn". b. Find out the total sum of all loan amounts in the bank. c. Find the names of all branches that have assets greater than those of at least one branch located in "Brooklyn".

My answer:

select distinct S.customer_name

from depositor as S

where not exists(

(select branch_name

from branch

where branch_city = 'Brooklyn')

except

(select R.branch_name

from depositor as T, account as R

where T.account_number = R.account_number

and S.customer_name = T.customer_name))

select sum(amount) as sum_loan

from loan

select branch_name

from branch

where assets >some

(select assets

from branch

where branch_city = 'Brooklyn')