0% found this document useful (0 votes)
294 views

SQL Text Book Exercise Qusetions

This document provides a set of practice exercises involving writing SQL queries on sample databases. The exercises cover topics such as: selecting data, filtering with WHERE clauses, aggregations, joins, subqueries, modifications including INSERT, DELETE and UPDATE statements. The exercises are intended to help students practice and reinforce their SQL skills.

Uploaded by

CSEMohan Raj M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
294 views

SQL Text Book Exercise Qusetions

This document provides a set of practice exercises involving writing SQL queries on sample databases. The exercises cover topics such as: selecting data, filtering with WHERE clauses, aggregations, joins, subqueries, modifications including INSERT, DELETE and UPDATE statements. The exercises are intended to help students practice and reinforce their SQL skills.

Uploaded by

CSEMohan Raj M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Practice Exercises 115

Review Terms

• Data-definition language • Set operations


• Data-manipulation language
° union
• Database schema
• Database instance
° intersect
• Relation schema ° except
• Relation instance • Aggregate functions
• Primary key ° avg, min, max, sum, count
• Foreign key
° group by
° Referencing relation ° having
° Referenced relation • Nested subqueries
• Null value • Set comparisons
• Query language ° {<, <=, >, >=} { some, all }
• SQL query structure
° exists
° select clause ° unique
° from clause • lateral clause
° where clause • with clause
• Multiset relational algebra • Scalar subquery
• as clause • Database modification
• order by clause
° Delete
• Table alias
• Correlation name (correlation vari-
° Insert
able, tuple variable) ° Update

Practice Exercises

3.1 Write the following queries in SQL, using the university schema. (We suggest
you actually run these queries on a database, using the sample data that we
provide on the web site of the book, db-book.com. Instructions for setting up
a database, and loading sample data, are provided on the above web site.)
a. Find the titles of courses in the Comp. Sci. department that have 3 credits.
b. Find the IDs of all students who were taught by an instructor named Ein-
stein; make sure there are no duplicates in the result.
116 Chapter 3 Introduction to SQL

c. Find the highest salary of any instructor.


d. Find all instructors earning the highest salary (there may be more than
one with the same salary).
e. Find the enrollment of each section that was offered in Fall 2017.
f. Find the maximum enrollment, across all sections, in Fall 2017.
g. Find the sections that had the maximum enrollment in Fall 2017.

3.2 Suppose you are given a relation grade points(grade, points) that provides a con-
version from letter grades in the takes relation to numeric scores; for example,
an “A” grade could be specified to correspond to 4 points, an “A−” to 3.7 points,
a “B+” to 3.3 points, a “B” to 3 points, and so on. The grade points earned by a
student for a course offering (section) is defined as the number of credits for the
course multiplied by the numeric points for the grade that the student received.
Given the preceding relation, and our university schema, write each of the
following queries in SQL. You may assume for simplicity that no takes tuple has
the null value for grade.
a. Find the total grade points earned by the student with ID '12345', across
all courses taken by the student.
b. Find the grade point average (GPA) for the above student, that is, the total
grade points divided by the total credits for the associated courses.
c. Find the ID and the grade-point average of each student.
d. Now reconsider your answers to the earlier parts of this exercise under
the assumption that some grades might be null. Explain whether your
solutions still work and, if not, provide versions that handle nulls properly.

3.3 Write the following inserts, deletes, or updates in SQL, using the university
schema.
a. Increase the salary of each instructor in the Comp. Sci. department by
10%.
b. Delete all courses that have never been offered (i.e., do not occur in the
section relation).
c. Insert every student whose tot cred attribute is greater than 100 as an in-
structor in the same department, with a salary of $10,000.

3.4 Consider the insurance database of Figure 3.17, where the primary keys are
underlined. Construct the following SQL queries for this relational database.

a. Find the total number of people who owned cars that were involved in
accidents in 2017.
Practice Exercises 117

person (driver id, name, address)


car (license plate, model, year)
accident (report number, year, location)
owns (driver id, license plate)
participated (report number, license plate, driver id, damage amount)

Figure 3.17 Insurance database

b. Delete all year-2010 cars belonging to the person whose ID is '12345'.

3.5 Suppose that we have a relation marks(ID, score) and we wish to assign grades
to students based on the score as follows: grade F if score < 40, grade C if 40
≤ score < 60, grade B if 60 ≤ score < 80, and grade A if 80 ≤ score. Write SQL
queries to do the following:
a. Display the grade for each student, based on the marks relation.
b. Find the number of students with each grade.
3.6 The SQL like operator is case sensitive (in most systems), but the lower() func-
tion on strings can be used to perform case-insensitive matching. To show how,
write a query that finds departments whose names contain the string “sci” as a
substring, regardless of the case.
3.7 Consider the SQL query
select p.a1
from p, r1, r2
where p.a1 = r1.a1 or p.a1 = r2.a1
Under what conditions does the preceding query select values of p.a1 that are
either in r1 or in r2? Examine carefully the cases where either r1 or r2 may be
empty.
3.8 Consider the bank database of Figure 3.18, where the primary keys are under-
lined. Construct the following SQL queries for this relational database.
a. Find the ID of each customer of the bank who has an account but not a
loan.
b. Find the ID of each customer who lives on the same street and in the same
city as customer '12345'.
c. Find the name of each branch that has at least one customer who has an
account in the bank and who lives in “Harrison”.
118 Chapter 3 Introduction to SQL

branch(branch name, branch city, assets)


customer (ID, customer name, customer street, customer city)
loan (loan number, branch name, amount)
borrower (ID, loan number)
account (account number, branch name, balance )
depositor (ID, account number)

Figure 3.18 Banking database.

3.9 Consider the relational database of Figure 3.19, where the primary keys are
underlined. Give an expression in SQL for each of the following queries.
a. Find the ID, name, and city of residence of each employee who works for
“First Bank Corporation”.
b. Find the ID, name, and city of residence of each employee who works for
“First Bank Corporation” and earns more than $10000.
c. Find the ID of each employee who does not work for “First Bank Corpo-
ration”.
d. Find the ID of each employee who earns more than every employee of
“Small Bank Corporation”.
e. Assume that companies may be located in several cities. Find the name
of each company that is located in every city in which “Small Bank Cor-
poration” is located.
f. Find the name of the company that has the most employees (or compa-
nies, in the case where there is a tie for the most).
g. Find the name of each company whose employees earn a higher salary,
on average, than the average salary at “First Bank Corporation”.

employee (ID, person name, street, city)


works (ID, company name, salary)
company (company name, city)
manages (ID, manager id)

Figure 3.19 Employee database.


Exercises 119

3.10 Consider the relational database of Figure 3.19. Give an expression in SQL for
each of the following:
a. Modify the database so that the employee whose ID is '12345' now lives
in “Newtown”.
b. Give each manager of “First Bank Corporation” a 10 percent raise unless
the salary becomes greater than $100000; in such cases, give only a 3
percent raise.

Exercises

3.11 Write the following queries in SQL, using the university schema.
a. Find the ID and name of each student who has taken at least one Comp.
Sci. course; make sure there are no duplicate names in the result.
b. Find the ID and name of each student who has not taken any course
offered before 2017.
c. For each department, find the maximum salary of instructors in that de-
partment. You may assume that every department has at least one instruc-
tor.
d. Find the lowest, across all departments, of the per-department maximum
salary computed by the preceding query.
3.12 Write the SQL statements using the university schema to perform the following
operations:
a. Create a new course “CS-001”, titled “Weekly Seminar”, with 0 credits.
b. Create a section of this course in Fall 2017, with sec id of 1, and with the
location of this section not yet specified.
c. Enroll every student in the Comp. Sci. department in the above section.
d. Delete enrollments in the above section where the student’s ID is 12345.
e. Delete the course CS-001. What will happen if you run this delete state-
ment without first deleting offerings (sections) of this course?
f. Delete all takes tuples corresponding to any section of any course with
the word “advanced” as a part of the title; ignore case when matching the
word with the title.
3.13 Write SQL DDL corresponding to the schema in Figure 3.17. Make any reason-
able assumptions about data types, and be sure to declare primary and foreign
keys.

You might also like