Académique Documents
Professionnel Documents
Culture Documents
Solutions 3-1
Chapter 3
The Relational Model 2: SQL
Solutions
Answers to Review Questions
Note: Answers to odd-numbered review questions are found in Appendix D of the textbook.
1.
To create a table in SQL, use a CREATE TABLE command. The word TABLE is followed by the name of the
table to be created and then by the names and data types of the columns (fields) that make up the table. The
data types you can use are INTEGER (large negative and positive whole numbers), SMALLINT (whole
numbers from -32,768 to 32,767), DECIMAL (numbers that have a decimal part), CHAR (alphanumeric
strings), and DATE (date values).
2.
The WHERE clause lets you specify a condition. Only those rows on which a condition is satisfied will be
included in the query results. The comparison operators you can include in a WHERE clause are: = (equal to),
> (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to), and < > (not equal to).
3.
A compound condition is formed by connecting two or more simple conditions using one or both of the
following operators: AND and OR. You can also precede a single condition with the NOT operator to negate a
condition. When you connect simple conditions using the AND operator, all the simple conditions must be true
for the compound condition to be true. When you connect simple conditions using the OR operator, the
compound condition will be true whenever any of the simple conditions are true.
4.
A computed field is one whose values are calculated from other fields. To use a computed field in an SQL
query, type the desired calculation. To assign a name to a computed field, follow the calculation with the word
AS and then the desired name.
5.
To use the LIKE or IN operators in an SQL query, include them in the WHERE clause. A character string
containing one or more wildcards is included after the word LIKE. The word IN is followed by a list of values.
6.
To sort data, use the ORDER BY clause, which consists of the words ORDER BY followed by the sort key. If
there is more than one sort key, the major key is listed first. To sort data in descending order, follow the sort
key with the word DESC.
7.
Use an SQL built-in function (COUNT, SUM, AVG, MAX, and MIN) by including it in the SELECT clause
followed by the name of the field to which it applies.
8.
A subquery is a query that is included in another query. The subquery is evaluated before the query that
contains it.
9.
To group data in SQL, include the words GROUP BY followed by the field or fields on which the data is to be
grouped in the query results. If you group data, you only can include the fields on which you are grouping or
statistics in the SELECT clause.
10.
To join tables in SQL, use a SELECT command that lists two or more tables to be joined in the FROM clause,
and then specify the matching columns in the WHERE clause.
11.
To qualify the name of a field in an SQL query, precede the field with the name of the table to which it belongs,
followed by a period. It is necessary to qualify a field if the field name occurs in more than one of the tables
listed in the FROM clause.
12.
To take the union of two tables, create a SQL query for each table and then place the word UNION between the
queries. The two tables involved in a union must have the same structure, or be union compatible; in other
words, they must have the same number of fields and their corresponding fields must have the same data types.
Solutions 3-2
13.
The update commands in SQL are INSERT, which inserts new rows in a table; UPDATE, which changes all the
rows in a table that satisfy some condition; and DELETE, which deletes all the rows in a table that satisfy some
condition.
14.
Create a query to select the columns from the desired table. After the SELECT clause, include an INTO clause
followed by the name of the table to be created.
Solutions 3-3
WHERE RepNum='35';
10.
SELECT PartNum, Description, OnHand * Price
FROM Part
WHERE Class='HW';
11.
SELECT *
FROM Part
ORDER BY Description;
12.
SELECT *
FROM Part
ORDER BY Class, PartNum;
13.
SELECT Class, SUM(OnHand)
FROM Part
GROUP BY Class;
14.
SELECT PartNum, Description, OnHand, Warehouse, Price
INTO SportingGoods
FROM Part
WHERE Class='SG';
15.
UPDATE SportingGoods
SET Description='Fitness Gym'
WHERE PartNum='BV06';
16.
DELETE
FROM SportingGoods
WHERE Price>1000;
Solutions 3-4
Solutions 3-5
GROUP BY Type
ORDER BY Type;
16.
SELECT BookCode, Title, Book.PublisherCode, PublisherName
FROM Book, Publisher
WHERE Book.PublisherCode=Publisher.PublisherCode;
17.
SELECT Title, Price
FROM Book, Publisher
WHERE Book.PublisherCode=Publisher.PublisherCode
AND PublisherName='Taunton Press';
18.
SELECT Title, BookCode
FROM Book, Publisher
WHERE Book.PublisherCode=Publisher.PublisherCode
AND PublisherName='Putnam Publishing Group'
AND Price>15;
19.
SELECT BookCode, Title, PublisherCode, Price
INTO Fiction
FROM Book
WHERE Type='FIC';
20.
UPDATE Fiction
SET Price=14.50
WHERE Price=14.00;
21.
DELETE
FROM Fiction
WHERE PublisherCode=VB;
Solutions 3-6
Solutions 3-7