P S T U: Atuakhali Cience AND Echnology Niversity
P S T U: Atuakhali Cience AND Echnology Niversity
UNIVERSITY
Faculty of Computer Science and Engineering
Lab Work - 04
Course Title: Database System Sessional
Course Code: CCE-224
Submitted to:
Dr. Md. Samsuzzaman
Professor
Department of Computer and Communication Engineering
Faculty of Computer Science and Engineering
Patuakhali Science and Technology University, Dumki, Patuakhali,
Bangladesh
Submitted by:
Name : Md.Tasnif Rahman
ID : 1802014
Reg : 08424
CHAPTER-03
a. Find the names of all students who have taken at least one Comp.
Sci. course; make sure there are no duplicate names in the result.
b. Find the IDs and names of all students who have not taken any
course offering before Spring 2009.
c. For each department, find themaximumsalary of instructors in that
department. Youmay assume that every department has at least one
instructor.
d. Find the lowest, across all departments, of the per-epartmentmaximum
salary computed by the preceding query.
Answer:
a. SQL query:
select name
from student natural join takes natural join course
where course.dept = ’Comp. Sci.’
b. SQL query:
select id, name
from student
except
select id, name
from student natural join takes
where year < 2009
Since the except operator eliminates duplicates, there is
no need
to use a select distinct clause, although doing so would
not affect
correctness of the query.
c. SQL query:
select dept, max(salary)
from instructor
group by dept
d. SQL query:
select min(maxsalary)
from (select dept, max(salary) as maxsalary
from instructor
group by dept)
Answer:
a. SQL query:
insert into course
values (’CS-001’, ’Weekly Seminar’, ’Comp. Sci.’, 0)
b. SQL query:
insert into section
values (’CS-001’, 1, ’Autumn’, 2009, null, null, null)
Note that the building, roomnumber and slot were not specified in
the question, and we have set them to null. The same effect would
be obtained if they were specified to default to null, and we simply
omitted values for these attributes in the above insert statement.
(Many database systems implicitly set the default value to null, even
if not explicitly specified.)
c. SQL query:
insert into takes
select id, ’CS-001’, 1, ’Autumn’, 2009, null
from student
where dept name = ’Comp. Sci.’
d. SQL query:
delete from takes
where course id= ’CS-001’ and section id = 1 and
year = 2009 and semester = ’Autumn’ and
id in (select id
from student
where name = ’Chavez’)
Note that if there is more than one student named Chavez, all such
students would have their enrollments deleted. If we had used =
instead of in, an error would have resulted if there were more than
one student named Chavez.
e. SQL query:
delete from takes
where course id = ’CS-001’
delete from section
where course id = ’CS-001’
delete from course
where course id = ’CS-001’
If we try to delete the course directly, there will be a foreign key violation
because section has a foriegn key reference to course; similarly,
we have to delete corresponding tuples from takes before deleting
sections, since there is a foreign key reference from takes to section.
As a result of the foreign key violation, the transaction that performs
the delete would be rolled back.
f. SQL query:
delete from takes
where course id in
(select course id
from course
where lower(title) like ’%database%’)
Answer:
a. SQL query:
person (driver id, name, address)
car (license, model, year)
accident (report number, date, location)
owns (driver id, license)
participated (report number, license, driver id, damage
amount)
Figure 3.18 Insurance database for Exercises 3.4 and 3.14.
create table person
(driver id varchar(50),
name varchar(50),
address varchar(50),
primary key (driver id))
b. SQL query:
create table car
(license varchar(50),
model varchar(50),
year integer,
primary key (license))
c. SQL query:
create table accident
(report number integer,
date date,
location varchar(50),
primary key (report number))
d. SQL query:
create table owns
(driver id varchar(50),
license varchar(50),
primary key (driver id,license)
foriegn key (driver id) references person
foriegn key (license) references car)
e. SQL query:
create table participated
(report number integer,
license varchar(50),
driver id varchar(50),
damage amountinteger,
primary key (report number,license)
foriegn key (license) references car
foriegn key (report number) references accident))
a. SQL query:
select count (*)
from accident
where exists
(select *
from participated, owns, person
where owns.driver id = person.driver id
and person.name = ’John Smith’
and owns.license = participated.license
and accident.report number = participated.report number)
The query can be written in other ways too; for example without a
subquery, by using a join and selecting count( distinct report
number)
to get a count of number of accidents involving the car.
b. SQL query:
update participated
set damage amount = 3000
where report number = “AR2197” and
license = “AABB2000”)
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.
branch(branch name, branch city, assets)
customer (customer name, customer street, customer city)
loan (loan number, branch name, amount)
borrower (customer name, loan number)
account (account number, branch name, balance )
depositor (customer name, account number)
Figure 3.19 Banking database for Exercises 3.8 and 3.15.
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”.
Answer:
a. SQL query:
with branchcount as
(select count(*)
branch
where branch city = ’Brooklyn’)
select customer name
from customer c
where branchcount =
(select count(distinct branch name)
from (customer natural join depositor natural join
account
natural join branch) as d
where d.customer name = c.customer name)
There are other ways of writing this query, for example by first
finding customers who do not have an account at some branch
in
Brooklyn, and then removing these customers from the set of
all
customers by using an except clause.
b. SQL query:
select sum(amount)
from loan
c. SQL query:
employee (employee name, street, city)
works (employee name, company name, salary)
company (company name, city)
manages (employee name, manager name)
Figure 3.20 Employee database for Exercises 3.9, 3.10, 3.16, 3.17, and
3.20.
select branch name
from branch
where assets > some
(select assets
from branch
where branch city = ’Brooklyn’)
The keyword any could be used in place of some above.
Answer:
a. Find the names of all employees who work for First Bank
Corporation.
select employee name
from works
where company name = ’First Bank Corporation’
b. Find all employees in the database who live in the same cities as the
companies for which they work.
select e.employee name
from employee e, works w, company c
where e.employee name = w.employee name and e.city
= c.city and
w.company name = c.company name
c. Find all employees in the database who live in the same cities and
on the same streets as do their managers.
select P.employee name
from employee P, employee R, manages M
where P.employee name = M.employee name and
M.manager name = R.employee name and
P.street = R.street and P.city = R.city
d. Find all employees who earn more than the average salary of all
employees of their company.
select employee name
from works T
where salary > (select avg (salary)
from works S
where T.company name = S.company name)
The primary key constraint on works ensures that each personworks
for at most one company.
c. Delete all tuples in the works relation for employees of Small Bank
Corporation.
delete from works
where company name = ’Small Bank Corporation’
3.18 List two reasons why null values might be introduced into the
database.
Answer:
• “null” signifies an unknown value.
• “null” is also used when a value does not exist.
3.19 Show that, in SQL, <> all is identical to not in.
Answer:
Let the set S denote the result of an SQL subquery. We compare
(x <> all S) with (x not in S). If a particular value x1 satisfies (x1 <> all S)
then for all elements y of S x1 6= y. Thus x1 is not a member of S and must
satisfy (x1 not in S). Similarly, suppose there is a particular value x2 which
satisfies (x2 not in S). It cannot be equal to any element w belonging to S,
and hence (x2 <> all S) will be satisfied. Therefore the two expressions are
equivalent.
3.25 The SQL query to find the names of those departments whose
budget is higher than that of Philosophy in the University schema is
as follows:
SELECT d2.dept_name, d2.budget
For each student who has retaken a course at least twice (i.e., the student
has taken the course at least three times), show the course ID and the
student’s ID.
Answer:
select distinct course_id, ID
from takes
order by course_id
Answer:
select distinct ID
from (
select course_id, ID
from takes
group by ID
having count(course_id) > 2
3.28 The SQL query to find the ID and name of those instructors
who teach every course taught in his or her department is as
follows:
SELECT name, ID
COUNT(*) FROM instructor
INNER JOIN teaches ON instructor.id=teaches.ID
INNER JOIN course ON teaches.course_id=course.course_id
AS count1
WHERE count1= SELECT COUNT(*) FROM course AND
instructor.dept_name=course.dept_name
ORDER BY name
Here, the INNERJOIN in all three table perform because there is a relationship
between instructor and teaches using primary key ID and between teaches and
course, the relationship exists with primary key course_id. So, after inner join, the
course_id is counted and after that it is compared with the number of courses in
course.
3.29SQL Query:
The SQL query to find the ID and name of each History student whose name starts
with the letter ‘D’ and who has not taken at least five Music courses is as follows:
FROM
WHERE
Here, the LIKE operator is used to identify the students whose names begin with
‘D’.
3.31The SQL query to find the ID and name of each instructor who
has never given an A grade in any course he or she ha taught is as
follows:
SELECT ID, name
FROM instructor
SELECT ID
);
3.34 The SQL query to find the number of students in each section in
the University schema is as follows:
SELECT s.course_id, s.sec_id, s.year, s.semester, count(id)
AS num