Docsity

COMPUTER 100 SQL Chapter04, Exercises of Information and Communications Technology (ICT)

Toggle sidebar
bg1
A Guide to SQL, Ninth Edition Solutions 4-1
Chapter 4: Single-Table Queries
Solutions
Answers to Review Questions
1. The basic form of the SELECT command is SELECT-FROM. Specify the columns to be listed
after the word SELECT (or type * to select all columns), and then specify the table name that
contains these columns after the word FROM. Optionally, you can include conditions after the word
WHERE.
2. Simple conditions are written in the form: column name, comparison operator, column name or
value. Simple conditions can involve any of the comparison operators: =, >, >=, <, <=, or < > or !=
(not equal to).
3. You can form compound conditions by combining simple conditions, using the operators AND,
OR, or NOT.
4. Use the BETWEEN operator.
5. Use arithmetic operators and write the computation in place of a column name. You can assign a
name to the computation by following the computation with the word AS and then the desired
name.
6. To check for a value in a character column that is similar to a particular string of characters, use
the LIKE clause. The percent (%) wildcard represents any collection of characters. The underscore
(_) wildcard represents any single character.
7. In Oracle, the percent (%) wildcard represents any collection of characters. The underscore (_)
wildcard represents any single character.
8. Use the IN clause.
9. Use the ORDER BY clause to sort data.
10. List the sort keys in order of importance in the ORDER BY clause. The more important key is
called the major key or the primary sort key. The less important key is called the minor key or the
secondary sort key.
11. To sort in descending order, follow the sort key with DESC.
12. The aggregate functions are COUNT, SUM, AVG, MAX, and MIN.
13. To avoid duplicates, precede the column name with the DISTINCT operator.
14. One SQL query that is placed inside another. It is evaluated before the outer query.
15. Use the GROUP BY clause.
16. Use the HAVING clause.
17. Use the phrase IS NULL in the WHERE clause.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download COMPUTER 100 SQL Chapter04 and more Exercises Information and Communications Technology (ICT) in PDF only on Docsity!

Chapter 4: Single-Table Queries

Solutions

Answers to Review Questions

1. The basic form of the SELECT command is SELECT-FROM. Specify the columns to be listed

after the word SELECT (or type * to select all columns), and then specify the table name that

contains these columns after the word FROM. Optionally, you can include conditions after the word

WHERE.

2. Simple conditions are written in the form: column name, comparison operator, column name or

value. Simple conditions can involve any of the comparison operators: =, >, >=, <, <=, or < > or !=

(not equal to).

3. You can form compound conditions by combining simple conditions, using the operators AND,

OR, or NOT.

4. Use the BETWEEN operator.

5. Use arithmetic operators and write the computation in place of a column name. You can assign a

name to the computation by following the computation with the word AS and then the desired

name.

6. To check for a value in a character column that is similar to a particular string of characters, use

the LIKE clause. The percent (%) wildcard represents any collection of characters. The underscore

(_) wildcard represents any single character.

7. In Oracle, the percent (%) wildcard represents any collection of characters. The underscore (_)

wildcard represents any single character.

8. Use the IN clause.

9. Use the ORDER BY clause to sort data.

10. List the sort keys in order of importance in the ORDER BY clause. The more important key is

called the major key or the primary sort key. The less important key is called the minor key or the

secondary sort key.

11. To sort in descending order, follow the sort key with DESC.

12. The aggregate functions are COUNT, SUM, AVG, MAX, and MIN.

13. To avoid duplicates, precede the column name with the DISTINCT operator.

14. One SQL query that is placed inside another. It is evaluated before the outer query.

15. Use the GROUP BY clause.

16. Use the HAVING clause.

17. Use the phrase IS NULL in the WHERE clause.

18. [Critical Thinking]

SELECT CUSTOMER_NAME, CITY

FROM CUSTOMER

WHERE CITY LIKE '[CG]%';

Answers to TAL Distributors Exercises

SELECT ITEM_NUM, DESCRIPTION, PRICE FROM ITEM;

SELECT * FROM ORDERS;

SELECT ITEM_NUM, DESCRIPTION, ON_HAND

FROM ITEM

WHERE ON_HAND >= 20

AND ON_HAND <= 40;

or SELECT ITEM_NUM, DESCRIPTION, ON_HAND FROM ITEM WHERE ON_HAND BETWEEN 20 AND 40;

SELECT ITEM_NUM, DESCRIPTION, ON_HAND * PRICE AS ON_HAND_VALUE FROM ITEM WHERE CATEGORY = 'TOY';

SELECT ITEM_NUM, DESCRIPTION, ON_HAND * PRICE AS ON_HAND_VALUE

FROM ITEM

WHERE ON_HAND * PRICE >= 1500;

SELECT ITEM_NUM, DESCRIPTION

FROM ITEM

WHERE CATEGORY IN ('GME', 'PZL');

In Oracle and SQL Server: SELECT CUSTOMER_NUM, CUSTOMER_NAME FROM CUSTOMER WHERE CUSTOMER_NAME LIKE 'C%'; In Access: SELECT CUSTOMER_NUM, CUSTOMER_NAME FROM CUSTOMER WHERE CUSTOMER_NAME LIKE ā€œC*ā€';

SELECT COUNT(*)

FROM CUSTOMER

WHERE BALANCE > CREDIT_LIMIT;

SELECT SUM(BALANCE)

FROM CUSTOMER

WHERE REP_NUM = '15'

AND BALANCE < CREDIT_LIMIT;

SELECT ITEM_NUM, DESCRIPTION, ON_HAND

FROM ITEM

WHERE ON_HAND >

(SELECT AVG(ON_HAND)

FROM ITEM);

SELECT MIN(PRICE)

FROM ITEM;

SELECT ITEM_NUM, DESCRIPTION, PRICE

FROM ITEM

WHERE PRICE =

(SELECT MIN(PRICE)

FROM ITEM);

SELECT REP_NUM, SUM(BALANCE)

FROM CUSTOMER

GROUP BY REP_NUM

ORDER BY REP_NUM;

SELECT REP_NUM, SUM(BALANCE)

FROM CUSTOMER

GROUP BY REP_NUM

HAVING SUM(BALANCE) > 5000

ORDER BY REP_NUM;

SELECT ITEM_NUM

WHERE STATE <> 'MA';

Or SELECT LAST_NAME FROM GUIDE WHERE NOT STATE = 'MA';

SELECT TRIP_NAME FROM TRIP WHERE TYPE = 'Biking';

SELECT TRIP_NAME FROM TRIP WHERE SEASON = 'Summer';

SELECT TRIP_NAME

FROM TRIP

WHERE TYPE = 'Hiking' AND DISTANCE >10;

SELECT CUSTOMER_NUM, LAST_NAME, FIRST_NAME FROM CUSTOMER WHERE STATE IN ('NJ', 'NY', 'PA');

SELECT CUSTOMER_NUM, LAST_NAME, FIRST_NAME FROM CUSTOMER WHERE STATE IN ('NJ', 'NY', 'PA') ORDER BY STATE DESC, LAST_NAME;

SELECT RESERVATION_ID, TRIP_ID, CUSTOMER_NUM, (TRIP_PRICE+OTHER_FEES)*NUM_PERSONS AS

TOTAL_PRICE

FROM RESERVATION

WHERE NUM_PERSONS > 4;

In Oracle and SQL Server: SELECT TRIP_NAME FROM TRIP WHERE TRIP_NAME LIKE '%Pond%'; In Access: SELECT TRIP_NAME FROM TRIP WHERE TRIP_NAME LIKE 'Pond';

In Oracle and SQL Server SELECT LAST_NAME, FIRST_NAME FROM GUIDE WHERE HIRE_DATE < '6/10/2013'; In Access SELECT LAST_NAME, FIRST_NAME FROM GUIDE WHERE HIRE_DATE < #6/10/2013#;

SELECT TYPE, AVG(DISTANCE), AVG(MAX_GRP_SIZE)

FROM TRIP

GROUP BY TYPE

ORDER BY TYPE;

SELECT DISTINCT(SEASON)

FROM TRIP;

SELECT RESERVATION_ID

FROM RESERVATION

WHERE TRIP_ID IN

(SELECT TRIP_ID

FROM TRIP

WHERE TYPE = 'Paddling');

SELECT MAX(DISTANCE) FROM TRIP WHERE TYPE = 'Biking';

SELECT TRIP_ID, SUM(TRIP_PRICE) FROM RESERVATION GROUP BY TRIP_ID HAVING COUNT(*) > 1 ORDER BY TRIP_ID;

Answers to Solmaris Condominium Group Exercises

SELECT OWNER_NUM, LAST_NAME, FIRST_NAME FROM OWNER;

SELECT * FROM LOCATION;

SELECT LAST_NAME, FIRST_NAME FROM OWNER WHERE CITY = 'Bowton';

SELECT LAST_NAME, FIRST_NAME FROM OWNER WHERE CITY <> 'Bowton'; or SELECT LAST_NAME, FIRST_NAME FROM OWNER WHERE NOT (CITY = 'Bowton');

SELECT LOCATION_NUM, UNIT_NUM

FROM CONDO_UNIT

WHERE SQR_FT <= 1200;

SELECT LOCATION_NUM, UNIT_NUM

FROM CONDO_UNIT

WHERE BDRMS = 3;

SELECT UNIT_NUM

FROM CONDO_UNIT

WHERE BDRMS = 2

AND LOCATION_NUM = 2;

SELECT LOCATION_NUM, UNIT_NUM, SQR_FT, CONDO_FEE

FROM CONDO_UNIT

ORDER BY SQR_FT, CONDO_FEE;

SELECT COUNT(CONDO_ID)

FROM CONDO_UNIT

WHERE BDRMS = 1

GROUP BY LOCATION_NUM

ORDER BY LOCATION_NUM;

SELECT SUM(CONDO_FEE)

FROM CONDO_UNIT;

  1. [Critical Thinking] SELECT OWNER_NUM, LAST_NAME FROM OWNER WHERE STATE IN ('FL', 'GA', 'SC'); Or SELECT OWNER_NUM, LAST_NAME FROM OWNER WHERE STATE = 'FL' OR STATE = 'GA' OR STATE = 'SC';
  2. [Critical Thinking] In Oracle and SQL Server SELECT * FROM SERVICE_REQUEST WHERE DESCRIPTION LIKE '%pantry%'; In Access SELECT * FROM SERVICE_REQUEST WHERE DESCRIPTION LIKE 'pantry';