Sei sulla pagina 1di 307

Authors

Neena Kochhar

Debby Kramer

Technical Contributors and Reviewers

Christian Bauwens Debra Bowman Lenny Brunson Jackie Collins Ralf Durben Brian Fry Anthony Holbrook Karlene Jensen Sarah Jones Glenn Maslen Sundar Nagarathnam Sandra Schrick Ulrike Schwinn Rosemarie Truman Jenny Tsai Laura Van Deusen

Publishers

Stephanie Jones

Kimberly Lee

Jennifer Robertson

Mark Turangan

There is often more than one way to achieve any result in SQL. Where possible,

There is often more than one way to achieve any result in SQL. Where possible, the alternatives have been identified in these solutions. The performance benefits, if any, are also mentioned. If you want to analyze any of the statements, refer to SQL*Trace. This facility allows you to see how the SQL statement is being interpreted at the database level.

For more information, see Oracle7 Server SQL Reference, Release 7.3 and Oracle7 Server Administrators Guide.

1.

SQL commands are always held in a buffer. True

 

2.

SQL*Plus commands assist with querying data.

False SQL*Plus commands allow you to format resulting data and control files. Only SQL accesses the database.

3.

Show the structure of the S_DEPT table. Select all information from the S_DEPT table.

SQL> DESCRIBE s_dept

 
 

Name

Null? Type

---------------------------- -------- ----

ID

NOT NULL NUMBER(7) NOT NULL VARCHAR2(25)

NAME

REGION_ID

 

NUMBER(7)

SQL> SELECT

*

 

2

FROM

s_dept;

 

ID NAME

REGION_ID

--------- ------------------------- ---------

 

10

Finance

1

31

Sales

1

32

Sales

2

33

Sales

3

34

Sales

4

35

Sales

5

41

Operations

1

42

Operations

2

43

Operations

3

44

Operations

4

45

Operations

5

50

Administration

1

12 rows selected.

4. Show the structure of the S_CUSTOMER table. Using this table, perform the following actions:

SQL> DESCRIBE s_customer

Name

Type

-------------------------- -------- ----

Null?

ID

NOT NULL NUMBER(7)

NAME

NOT NULL VARCHAR2(50)

PHONE

VARCHAR2(25)

ADDRESS

VARCHAR2(400)

CITY

VARCHAR2(30)

STATE

VARCHAR2(20)

COUNTRY

VARCHAR2(30)

ZIP_CODE

VARCHAR2(75)

CREDIT_RATING

VARCHAR2(9)

SALES_REP_ID

NUMBER(7)

REGION_ID

NUMBER(7)

COMMENTS

VARCHAR2(255)

a. Retrieve all information from the S_CUSTOMER table.

SQL> SELECT

*

2

FROM

s_customer;

Continued

4.—continued

b. Display the name and phone number for each customer.

SQL> SELECT

name, phone

2

FROM

s_customer;

NAME

PHONE

----------------------------------- --------------

Unisports

55-2066101

Simms Atheletics

81-20101

Delhi Sports

91-10351

Womansport

1-206-104-0103

Kam’s Sporting Goods

852-3692888

Sportique

33-2257201

Sweet Rock Sports

234-6036201

Muench Sports

49-527454

Beisbol Si!

809-352689

Futbol Sonora

52-404562

Kuhn’s Sports

42-111292

Hamada Sport

20-1209211

Big John’s Sports Emporium

1-415-555-6281

Ojibway Retail

1-716-555-7171

Sporta Russia

7-3892456

15 rows selected.

Continued

4.—continued

c. Display the phone number and name for each customer, with phone number appearing first.

SQL> SELECT

phone, name

2

FROM

s_customer;

PHONE

NAME

-------------------- -------------------------

55-2066101

Unisports Simms Atheletics Delhi Sports Womansport Kams Sporting Goods Sportique Sweet Rock Sports Muench Sports Beisbol Si! Futbol Sonora Kuhns Sports Hamada Sport Big Johns Sports Emporium Ojibway Retail Sporta Russia

81-20101

91-10351

1-206-104-0103

852-3692888

33-2257201

234-6036201

49-527454

809-352689

52-404562

42-111292

20-1209211

1-415-555-6281

1-716-555-7171

7-3892456

15 rows selected.

 

1.

You cannot order by a column that you have not selected.

False

 

2.

This SELECT statement will execute successfully. True

 

SQL> SELECT

last_name, title, salary Ann_sal

2 FROM

s_emp

3 WHERE

last_name = ’Dancs’;

3.

This SELECT statement will execute successfully. True

 

SQL> SELECT

*

2 FROM

s_emp

3 WHERE

salary*12 = 9600;

4.

There are four coding errors in this statement. Can you identify them?

 

SQL> SELECT

id, last_name,

2

salary x 12 ANNUAL SALARY

3

FROM

s_emp

4

WHERE

sal > 3000

5

AND

start_date LIKE %84;

No SAL column in existence (WHERE clause). The ANNUAL SALARY alias cannot include spaces. Alias should read ANNUAL_SALARY or be enclosed by quotation marks. The multiplication operator is *, not x as shown in line 2.

All values when using the LIKE operator should be enclosed within single quotation marks. The value should read ‘%84’ in line 5.

5. Use the S_CUSTOMER table to perform the following actions.

a. Create a query to display the name, customer number, and credit rating for all companies represented by sales representative 11. Save your SQL statement to a file named p2q5.

SQL> SELECT

name, id, credit_rating

2

FROM

s_customer

3

WHERE

sales_rep_id = 11

4

SQL> SAVE p2q5

Created file p2q5

b. Run your query in the file p2q5.

SQL> START p2q5

ID CREDIT_RA

------------------------------ --------- ---------

204 EXCELLENT 209 EXCELLENT 213 EXCELLENT 214 POOR

NAME

Womansport Beisbol Si! Big Johns Sports Emporium Ojibway Retail

Continued

5.—continued

c. Load p2q5 into the SQL buffer. Name the column headings Company, Company ID, and Rating. Rerun your query. Resave your query as p2q5.

Solution file: p2q5.sql

SQL> GET p2q5

1 SELECT

2 FROM

name, id, credit_rating

s_customer sales_rep_id = 11

SQL> 1 SELECT name Company, id Company ID, SQL> i credit_rating RatingSQL> RUN

3* WHERE

1

SELECT

name Company, id Company ID,

2

credit_rating Rating

3

FROM

s_customer

4* WHERE

sales_rep_id = 11

Company

------------------------------ ---------- ---------

Womansport Beisbol Si! Big Johns Sports Emporium Ojibway Retail

SQL> SAVE p2q5 REPLACE

Wrote file p2q5

Company ID Rating

204 EXCELLENT 209 EXCELLENT 213 EXCELLENT 214 POOR

Continued

5.—continued

d. Retrieve p2q5 into the SQL buffer. Order the query results in descending order by customer number. Run your query.

SQL> GET p2q5

1

SELECT

name Company, id Company ID,

2

credit_rating Rating

3

FROM

s_customer

4* WHERE

sales_rep_id = 11

SQL> i ORDER BY 2 DESC SQL> /

Company

------------------------------ ---------- ---------

Ojibway Retail Big Johns Sports Emporium Beisbol Si! Womansport

Company ID Rating

214 POOR 213 EXCELLENT 209 EXCELLENT 204 EXCELLENT

6. Show the structure of the S_EMP table.

SQL> DESCRIBE s_emp

Name

Type

-------------------------- -------- ----

Null?

ID

NOT NULL NUMBER(7)

LAST_NAME

NOT NULL VARCHAR2(25)

FIRST_NAME

VARCHAR2(25)

USERID

NOT NULL VARCHAR2(8)

START_DATE

DATE

COMMENTS

VARCHAR2(255)

MANAGER_ID

NUMBER(7)

TITLE

VARCHAR2(25)

DEPT_ID

NUMBER(7)

SALARY

NUMBER(11,2)

COMMISSION_PCT

NUMBER(4,2)

6.—continued

a. Display the user name for employee 23.

SQL> SELECT

userid

2 FROM

s_emp

3 WHERE

id = 23;

USERID

--------

rpatel

b. Display the first name, last name, and department number of the employees in departments 10 and 50 in alphabetical order of last name. Merge the first name and last name together, and title the column Employees.

SQL> SELECT

first_name||’ ’||last_name

2

Employees, dept_id

3

FROM s_emp

4

WHERE

dept_id IN (10,50)

5

ORDER BY last_name;

DEPT_ID

------------------------------------ ---------

Employees

Mark Quick-To-See

10

Audry Ropeburn

50

Carmen Velasquez

50

Continued

6.—continued

c. Display all employees whose last names contain an “s”.

This solution is not quite complete because names that contain a capital S do not appear in this list. In a later lesson, you will see how to change your query into a case-insensitive query.

SQL> SELECT

2 FROM

3 WHERE

first_name, last_name

s_emp

last_name like %s%;

FIRST_NAME

------------------------- ------------------------

LAST_NAME

Carmen

Velasquez

Andre

Dumas

Bela

Dancs

d. Display the user names and start date of employees hired between May 5, 1990 and May 26, 1991. Order the query results by start date ascending order.

SQL> SELECT

userid, start_date

2

FROM s_emp

3

WHERE

start_date BETWEEN 05-may-90

4

AND 26-may-91

5

ORDER BY start_date;

Continued

6d.—continued

START_DAT

-------- --------- rmenchu 14-MAY-90

USERID

cmagee

14-MAY-90

rpatel

17-OCT-90

echang

30-NOV-90

murguhar 18-JAN-91

anozaki 09-FEB-91 ysedeghi 18-FEB-91

mhavel

27-FEB-91

bdancs

17-MAR-91

sschwart 09-MAY-91

amarkari 26-MAY-91

11 rows selected.

7. Use the S_EMP table to perform the following actions.

a. Write a query to show the last name and salary of all employees who are not making between 1000 and 2500 per month.

SQL> SELECT

last_name, salary

s_emp

2 FROM

3 WHERE salary NOT BETWEEN 1000 AND 2500;

SALARY

------------------------- ---------

LAST_NAME

Smith

940

Patel

795

Newman

750

Markarian

850

Chang

800

Patel

795

Dancs

860

7 rows selected.

Continued

7.—continued

b. List the last name and salary of employees who earn more than 1350 who are in department 31, 42, or 50. Label the last name column Employee Name, and label the salary column Monthly Salary.

SQL> SELECT

last_name Employee Name,

2

salary Monthly Salary

3

FROM

s_emp

4

WHERE

salary > 1350

5

AND

dept_id IN (31, 42, 50);

Employee Name

------------------------- --------------

Monthly Salary

Velasquez

2500

Nagayama

1400

Ropeburn

1550

Magee

1400

c. Display the last name and start date of every employee who was hired in

1991.

SQL> SELECT

2 FROM

3 WHERE

last_name, start_date

s_emp

start_date LIKE %91;

Continued

7c.—continued

LAST_NAME

------------------------- ---------

START_DAT

Nagayama

17-JUN-91

Urguhart

18-JAN-91

Havel

27-FEB-91

Sedeghi

18-FEB-91

Dumas

09-OCT-91

Nozaki

09-FEB-91

Patel

06-AUG-91

Newman

21-JUL-91

Markarian

26-MAY-91

Dancs

17-MAR-91

Schwartz

09-MAY-91

11 rows selected.

d.

Display the full name of all employees with no manager.

SQL> SELECT

first_name, last_name

2 FROM

s_emp

3 WHERE

manager_id IS NULL;

FIRST_NAME

LAST_NAME

------------------------- -------------------------

Carmen

Velasquez

8. Show the structure of the S_PRODUCT table.

SQL> DESCRIBE s_product

Name

Null?

Type

------------------------- -------- ----

ID

NOT NULL NUMBER(7)

NAME

NOT NULL VARCHAR2(50)

SHORT_DESC

VARCHAR2(255)

LONGTEXT_ID

NUMBER(7)

IMAGE_ID

NUMBER(7)

SUGGESTED_WHLSL_PRICE

NUMBER(11,2)

WHLSL_UNITS

VARCHAR2(25)

a. Alphabetically display all products having a name beginning with Pro.

SQL> SELECT

name

2 FROM

s_product

3 WHERE

name LIKE Pro%;

NAME

-------------------------------------------------

-

Pro Curling Bar Pro Ski Boot Pro Ski Pole Prostar 10 Pound Weight Prostar 100 Pound Weight Prostar 20 Pound Weight Prostar 50 Pound Weight Prostar 80 Pound Weight

8 rows selected.

Continued

8.—continued

b. Display all product names and short descriptions for all descriptions containing the word bicycle.

Results have been formatted.

SQL> SELECT

2 FROM

3 WHERE

name, short_desc

s_product

short_desc LIKE %bicycle%

NAME

------------------------- --------------------

Grand Prix Bicycle Himalaya Bicycle

Grand Prix Bicycle Tires Road bicycle tires

Himalaya Tires

SHORT_DESC

Road bicycle Mountain bicycle

Mountain bicycle tires

c. Display all short descriptions. Compare the results from Exercise 10b. Did your response in Exercise 10b return all descriptions containing “bicycle”?

No, not all words containing “bicycle” were returned because the WHERE clause is case-sensitive. In a later lesson, you will see how to remove the case-sensitivity.

SQL> SELECT

short_desc

2 FROM s_product

3 ORDER BY 1;

Continued

8c.—continued

SHORT_DESC

---------------------------------------------

Advanced ski boot Advanced ski pole Baseball Batting helmet Beginners ski boot Beginners ski pole Bicycle helmet Catchers glove Curling bar Eighty pound weight Elbow pads, pair Fifty pound weight Infielders glove Intermediate ski boot Intermediate ski pole Junior soccer ball Knee pads, pair Mountain bicycle Mountain bicycle tires One hundred pound weight Outfielders glove Road bicycle Road bicycle tires Straight bar Ten pound weight Thirty inch bat Thirty-six inch bat Thirty-two inch bat Tire pump Twenty pound weight Water bottle World cup net World cup soccer ball

33 rows selected.

1. Single row functions work on many rows to produce a single result.

False Single row functions work for each row selected by the query and return one result per row.

2. You can use all of the arithmetic operators on date values.

False Additional and subtraction are the only operators that can be used.

3. What is the name of the pseudo column that holds the current date?

SYSDATE

4. Display the employee number, last name, and salary increased by 15% and expressed as a whole number.

SQL> SELECT

2

FROM

id, last_name, ROUND(salary * 1.15) s_emp;

ID LAST_NAME

ROUND(SALARY*1.15)

------ ------------ ------------------

1

Velasquez

2875

2

Ngao

1668

3

Nagayama

1610

4

Quick-To-See

1668

5

Ropeburn

1783

6

Urguhart

1380

7

Menchu

1438

8

Biri

1265

9

Catchpole

1495

10

Havel

1503

11

Magee

1610

12

Giljum

1714

13

Sedeghi

1742

14

Nguyen

1754

15

Dumas

1668

16

Maduro

1610

17

Smith

1081

18

Nozaki

1380

19

Patel

914

20

Newman

863

21

Markarian

978

22

Chang

920

23

Patel

914

24

Dancs

989

25

Schwartz

1265

25 rows selected.

5. Display the employee last name and title in parentheses for all employees. The report should look like the output below.

SQL> SELECT

last_name||(||INITCAP(title)||)

2

EMPLOYEE

3

FROM s_emp

4

ORDER BY last_name;

EMPLOYEE

----------------------------------------------------

Biri(Warehouse Manager) Catchpole(Warehouse Manager) Chang(Stock Clerk) Dancs(Stock Clerk) Dumas(Sales Representative) Giljum(Sales Representative) Havel(Warehouse Manager) Maduro(Stock Clerk) Magee(Sales Representative) Markarian(Stock Clerk) Menchu(Warehouse Manager) Nagayama(Vp, Sales) Newman(Stock Clerk) Ngao(Vp, Operations) Nguyen(Sales Representative) Nozaki(Stock Clerk) Patel(Stock Clerk) Patel(Stock Clerk) Quick-To-See(Vp, Finance) Ropeburn(Vp, Administration) Schwartz(Stock Clerk) Sedeghi(Sales Representative) Smith(Stock Clerk) Urguhart(Warehouse Manager) Velasquez(President)

25 rows selected.

6. Display each employee’s last name, hire date, and salary review date, which is the first Monday after six months of service. Format the dates to appear in the format similar to Eighth of May 1992.

Results have been formatted.

 

SQL> SELECT

last_name, start_date,

2

TO_CHAR(NEXT_DAY(ADD_MONTHS(start_date,6),

3

MONDAY),

4

fmDdspth ofMonth YYYY) REVIEW FROM

s_emp;

5

Continued

 

6.—continued

LAST_NAME

------------ --------- ------------------------------

START_DAT REVIEW

Velasquez

03-MAR-90 Tenth of September 1990

Ngao

08-MAR-90 Tenth of September 1990

Nagayama

17-JUN-91 Twenty-Third of December 1991

Quick-To-See 07-APR-90 Eighth of October 1990

Ropeburn

04-MAR-90 Tenth of September 1990

Urguhart

18-JAN-91 Twenty-Second of July 1991

Menchu

14-MAY-90 Nineteenth of November 1990

Biri

07-APR-90 Eighth of October 1990

Catchpole

09-FEB-92 Tenth of August 1992

Havel

27-FEB-91 Second of September 1991

Magee

14-MAY-90 Nineteenth of November 1990

Giljum

18-JAN-92 Twentieth of July 1992

Sedeghi

18-FEB-91 Nineteenth of August 1991

Nguyen

22-JAN-92 Twenty-Seventh of July 1992

Dumas

09-OCT-91 Thirteenth of April 1992

Maduro

07-FEB-92 Tenth of August 1992

Smith

08-MAR-90 Tenth of September 1990

Nozaki

09-FEB-91 Twelfth of August 1991

Patel

06-AUG-91 Tenth of February 1992

Newman

21-JUL-91 Twenty-Seventh of January 1992

Markarian

26-MAY-91 Second of December 1991

Chang

30-NOV-90 Third of June 1991

Patel

17-OCT-90 Twenty-Second of April 1991

Dancs

17-MAR-91 Twenty-Third of September 1991

Schwartz

09-MAY-91 Eleventh of November 1991

25 rows selected.

7. Display the product name for products that have “ski” in the name.

SQL> SELECT

name

2 FROM

s_product

3 WHERE

LOWER(name) LIKE %ski%;

NAME

--------------------

Ace Ski Boot Pro Ski Boot Bunny Ski Pole Ace Ski Pole Pro Ski Pole

8. For each employee, calculate the number of months between today and the date the employee was hired. Order your result by the number of months employed. Round the number of months up to the closest whole number.

 

SQL> SELECT

last_name,

2

ROUND(MONTHS_BETWEEN(SYSDATE, start_date))

3

MONTHS_WORKED

4

FROM s_emp

5

ORDER BY MONTHS_BETWEEN(SYSDATE, start_date);

Continued

 

8.—continued

LAST_NAME

------------ -------------

MONTHS_WORKED

Catchpole

46

Maduro

46

Nguyen

47

Giljum

47

Dumas

50

Patel

52

Newman

53

Nagayama

54

Markarian

55

Schwartz

55

Dancs

57

Havel

58

Sedeghi

58

Nozaki

58

Urguhart

59

Chang

61

Patel

62

Menchu

67

Magee

67

Quick-To-See

68

Biri

68

Ngao

69

Smith

69

Ropeburn

69

Velasquez

69

25 rows selected.

Note:

Your MONTHS_WORKED may differ from the solution because your SYSDATE may return a different value.

9. Display the last name for all employees and the day of the week they started. Order the results by the day of the week starting with Monday.

Results have been formatted.

SQL> SELECT

last_name, start_date,

2

TO_CHAR(start_date, DAY) DAY

3

FROM s_emp

4

ORDER BY TO_CHAR(start_date-1, d);

LAST_NAME

------------ --------- ----------

START_DAT DAY

Nagayama

17-JUN-91 MONDAY

Menchu

14-MAY-90 MONDAY

Sedeghi

18-FEB-91 MONDAY

Magee

14-MAY-90 MONDAY

Patel

06-AUG-91 TUESDAY

Havel

27-FEB-91 WEDNESDAY

Patel

17-OCT-90 WEDNESDAY

Nguyen

22-JAN-92 WEDNESDAY

Dumas

09-OCT-91 WEDNESDAY

Ngao

08-MAR-90 THURSDAY

Smith

08-MAR-90 THURSDAY

Schwartz

09-MAY-91 THURSDAY

Urguhart

18-JAN-91 FRIDAY

Chang

30-NOV-90 FRIDAY

Maduro

07-FEB-92 FRIDAY

Velasquez

03-MAR-90 SATURDAY

Quick-To-See 07-APR-90 SATURDAY

Biri

07-APR-90 SATURDAY

Nozaki

09-FEB-91 SATURDAY

Giljum

18-JAN-92 SATURDAY

Ropeburn

04-MAR-90 SUNDAY

Newman

21-JUL-91 SUNDAY

Dancs

17-MAR-91 SUNDAY

Markarian

26-MAY-91 SUNDAY

Catchpole

09-FEB-92 SUNDAY

25 rows selected.

10. Write a query that produces the following for each employee:

<employee name> earns <salary> monthly but wants<3 times salary>. For example: ALLEN earns $1,100 monthly but wants $3,300.

SQL> SELECT

UPPER(last_name)|| earns ||

2

TO_CHAR(salary, fm$99,999)

3

|| monthly but wants ||

4

TO_CHAR(salary*3, fm$99,999)

5

||.’ ”Dream Salaries

s_emp;

6

FROM

Dream Salaries

---------------------------------------------------

VELASQUEZ earns $2,500 monthly but wants $7,500. NGAO earns $1,450 monthly but wants $4,350. NAGAYAMA earns $1400 monthly but wants $4,200. QUICK-TO-SEE earns $,1450 monthly but wants $4,350. ROPEBURN earns $1,550 monthly but wants $4,650. URGUHART earns $1,200 monthly but wants $3,600. MENCHU earns $1,250 monthly but wants $3,750. BIRI earns $1,100 monthly but wants $3,300. CATCHPOLE earns $1,300 monthly but wants $3,900. HAVEL earns $1,307 monthly but wants $3,921. MAGEE earns $1,400 monthly but wants $4,200. GILJUM earns $1,490 monthly but wants $4,470. SEDEGHI earns $1,515 monthly but wants $4,545. NGUYEN earns $1,525 monthly but wants $4,575. DUMAS earns $1,450 monthly but wants $4,350. MADURO earns $1,400 monthly but wants $4,200. NOZAKI earns $1,200 monthly but wants $3,600. MARKARIAN earns $850 monthly but wants $2,550. PATEL earns $795 monthly but wants $2,385. SMITH earns $1,000 monthly but wants $3,000. PATEL earns $795 monthly but wants $2,385. NEWMAN earns $750 monthly but wants $2,250. CHANG earns $800 monthly but wants $2,400. DANCS earns $860 monthly but wants $2,580. SCHWARTZ earns $1,100 monthly but wants $3,300.

Use the S_EMP, S_DEPT, S_CUSTOMER, S_REGION, S_ORD, S_ITEM, and S_PRODUCT tables to complete the following exercises.

1. Write a report containing each employee’s last name, department number, and name of their department.

 

SQL> SELECT

s_emp.last_name, s_emp.dept_id, s_dept.name

2

3

FROM

s_emp, s_dept

4

WHERE

s_emp.dept_id = s_dept.id;

Continued

 

1.—continued

LAST_NAME

------------ ------- --------------------

DEPT_ID NAME

Velasquez

50 Administration

Ngao

41 Operations

Nagayama

31 Sales

Quick-To-See

10 Finance

Ropeburn

50 Administration

Urguhart

41 Operations

Menchu

42 Operations

Biri

43 Operations

Catchpole

44 Operations

Havel

45 Operations

Magee

31 Sales

Giljum

32 Sales

Sedeghi

33 Sales

Nguyen

34 Sales

Dumas

35 Sales

Maduro

41 Operations

Smith

41 Operations

Nozaki

42 Operations

Patel

42 Operations

Newman

43 Operations

Markarian

43 Operations

Chang

44 Operations

Patel

34 Sales

Dancs

45 Operations

Schwartz

45 Operations

25 rows selected.

2. Write a query to display the last name, department name, and region name of all employees who earn a commission.

Results have been formatted.

SQL> SELECT

2 FROM

3 WHERE

4 AND

5 AND

e.last_name, d.name, r.name

s_emp e, s_dept d, s_region r

e.dept_id = d.id

r.id = d.region_id

e.commission_pct IS NOT NULL;

LAST_NAME

------------ -------------- --------------------

NAME

NAME

Magee

Sales

North America

Giljum

Sales

South America

Sedeghi

Sales

Africa / Middle East

Nguyen

Sales

Asia

Dumas

Sales

Europe

3. Display the employee name and department name for Smith.

SQL> SELECT

2 FROM

3 WHERE

4 AND

s_emp.last_name, s_dept.name

s_emp, s_dept

s_emp.dept_id = s_dept.id

s_emp.last_name = Smith;

LAST_NAME

------------ ----------------------

Smith

NAME

Operations

4. Display the product name, product number, and quantity ordered of all items in order number 101. Label the quantity column ORDERED.

SQL> SELECT

s_product.name,

2

s_product.id,

3

s_item.quantity ORDERED

4

FROM

s_product, s_item

5

WHERE

s_product.id = s_item.product_id

6

AND

s_item.ord_id = 101;

NAME

ID ORDERED

--------------------------------- ----- -------

Grand Prix Bicycle Tires

30421

15

Pro Curling Bar

40422

30

Prostar 10 Pound Weight

41010

20

Prostar 100 Pound Weight

41100

35

Major League Baseball

50169

40

Griffey Glove

50417

27

Cabrera Bat

50530

50

7 rows selected.

5. Display the customer number and the last name of their sales representative. Order the list by last name.

 

SQL> SELECT

s_customer.id, s_emp.last_name

2

FROM

s_emp, s_customer

3

WHERE

s_emp.id = s_customer.sales_rep_id

4

ORDER BY s_emp.last_name;

Continued

 

5.—continued

ID LAST_NAME

----- ---------------

205

Dumas

206

Dumas

208

Dumas

211

Dumas

215

Dumas

201

Giljum

210

Giljum

204

Magee

214

Magee

209

Magee

213

Magee

202

Nguyen

203

Nguyen

212

Sedeghi

14 rows selected.

6. Display the customer number, customer name, and order number of all customers and their orders. Display the customer number and name, even if they have not placed an order.

 

SQL> SELECT

s_customer.id Customer ID,

2

s_customer.name Customer Name,

3

s_ord.id Order ID

4

FROM

s_customer, s_ord

5

WHERE

s_customer.id =

6

s_ord.customer_id (+);

Continued

 

6.—continued

Order ID

----------- ------------------------------ ----------

Customer ID Customer Name

201

Unisports

97

202

Simms Atheletics

98

203

Delhi Sports

99

204

Womansport

100

204

Womansport

111

205

Kams Sporting Goods

101

206

Sportique

102

207

Sweet Rock Sports

208

Muench Sports

103

208

Muench Sports

104

209

Beisbol Si!

105

210

Futbol Sonora

106

210

Futbol Sonora

112

211

Kuhns Sports

107

212

Hamada Sport

108

213

Big Johns Sports Emporium

109

214

Ojibway Retail

110

215

Sporta Russia

18 rows selected.

7. Display all employees by last name name and employee number along with their manager’s last name and manager number.

 

SQL> SELECT

w.last_name EMP_NAME, w.id EMP_ID,

2

m.last_name MGR_NAME, m.id MGR_ID

3

FROM

s_emp w, s_emp m

4

WHERE

w.manager_id = m.id;

Continued

 

7.—continued

MGR_ID

------------ ---------- ------------ ----------

EMP_NAME

EMP_ID MGR_NAME

Ngao

2 Velasquez

1

Nagayama

3 Velasquez

1

Quick-To-See

4 Velasquez

1

Ropeburn

5 Velasquez

1

Urguhart

6 Ngao

2

Menchu

7 Ngao

2

Biri

8 Ngao

2

Catchpole

9 Ngao

2

Havel

10 Ngao

2

Magee

11 Nagayama

3

Giljum

12 Nagayama

3

Sedeghi

13 Nagayama

3

Nguyen

14 Nagayama

3

Dumas

15 Nagayama

3

Maduro

16 Urguhart

6

Smith

17 Urguhart

6

Nozaki

18 Menchu

7

Patel

19 Menchu

7

Newman

20 Biri

8

Markarian

21 Biri

8

Chang

22 Catchpole

9

Patel

23 Catchpole

9

Dancs

24 Havel

10

Schwartz

25 Havel

10

24 rows selected.

8. Modify the solution to exercise 7 to also display Velasquez, who has no manager.

SQL> SELECT

w.last_name Employee Name, w.id Employee ID,

2

m.last_name Manager Name, m.id Manager ID

3

FROM

s_emp w, s_emp m

4

WHERE

w.manager_id = m.id (+);

Manager ID

--------------- ----------- --------------- ----------

Employee Name

Employee ID Manager Name

Velasquez

1

Ngao

2 Velasquez

1

Nagayama

3 Velasquez

1

Quick-To-See

4 Velasquez

1

Ropeburn

5 Velasquez

1

Urguhart

6 Ngao

2

Menchu

7 Ngao

2

Biri

8 Ngao

2

Catchpole

9 Ngao

2

Havel

10 Ngao

2

Magee

11 Nagayama

3

Giljum

12 Nagayama

3

Sedeghi

13 Nagayama

3

Nguyen

14 Nagayama

3

Dumas

15 Nagayama

3

Maduro

16 Urguhart

6

Nozaki

18 Menchu

7

Markarian

21 Biri

8

Patel

23 Catchpole

9

Smith

17 Urguhart

6

Patel

19 Menchu

7

Newman

20 Biri

8

Chang

22 Catchpole

9

Dancs

24 Havel

10

Schwartz

25 Havel

10

25 rows selected.

9. Display all customers and the product number and quantities they ordered for those customers whose order totaled more than 100,000.

 

SQL> SELECT

c.name CUSTOMER, p.id PRODUCT_ID,

2

i.quantity

3

FROM

s_customer c, s_product p, s_item i,

4

s_ord o

5

WHERE

c.id = o.customer_id

6

AND

o.id = i.ord_id

7

AND

i.product_id = p.id

8

AND

o.total > 100000;

Continued

 

9.—continued

CUSTOMER

------------------------------ ---------- ----------

QUANTITY

PRODUCT_ID

Womansport

10011

500

Womansport

10013

400

Womansport

10021

500

Womansport

30326

600

Womansport

41010

250

Womansport

30433

450

Womansport

10023

400

Kuhns Sports

20106

50

Kuhns Sports

20201

130

Kuhns Sports

30421

55

Kuhns Sports

30321

75

Kuhns Sports

20108

22

Hamada Sport

20510

9

Hamada Sport

41080

50

Hamada Sport

41100

42

Hamada Sport

32861

57

Hamada Sport

20512

18

Hamada Sport

32779

60

Hamada Sport

30321

85

Big Johns Sports Emporium

10011

150

Big Johns Sports Emporium

30426

500

Big Johns Sports Emporium

50418

43

Big Johns Sports Emporium

32861

50

Big Johns Sports Emporium

30326

1500

Big Johns Sports Emporium

10012

600

Big Johns Sports Emporium

10022

300

26 rows selected.

1. Determine the validity of the following statements. Circle either True or False.

a. Group functions work across many rows to produce one result.

True

b. Group functions include nulls in calculations.

False Group functions ignore null values. If you want to include null values, use the NVL function.

c. Use the HAVING clause to exclude rows from a group calculation.

False The WHERE clause restricts rows prior to inclusion in a group calculation.

d. Use the HAVING clause to exclude groups of rows from the display.

True

2. Display the highest and lowest order totals in the S_ORD. Label the columns Highest and Lowest, respectively.

SQL> SELECT

MAX (total) Highest,

2

MIN (total) Lowest

3

FROM

s_ord;

Lowest

---------- ----------

377

Highest

1020935

3. Write a query to display the minimum and maximum salary for each job type ordered alphabetically.

 

SQL> SELECT

title JOB, MAX(salary) MAXIMUM,

2

MIN(salary) MINIMUM

3

FROM

s_emp

4

GROUP BY title;

Continued

 

3.—continued

JOB

------------------------- ---------- ----------

MAXIMUM

MINIMUM

President

2500

2500

Sales Representative

1525

1400

Stock Clerk

1400

750

VP, Administration

1550

1550

VP, Finance

1450

1450

VP, Operations

1450

1450

VP, Sales

1400

1400

Warehouse Manager

1307

1100

8 rows selected.

4. Determine the number of managers without listing them.

SQL> SELECT

COUNT(DISTINCT(manager_id))

2

Number of Managers

3

FROM

s_emp;

Number of Managers

------------------

8

5. Display the number of line items in each order under each order number, labeled Number of Items.

 

SQL> SELECT

ord_id, COUNT(*) NUMBER OF ITEMS

2

FROM s_item

3

GROUP BY ord_id;

Continued

 

5.—continued

ORD_ID NUMBER OF ITEMS

---------- ---------------

97

2

98

1

99

4

100

7

101

7

102

2

103

2

104

4

105

3

106

6

107

5

108

7

109

7

110

2

111

2

112

1

16 rows selected.

6. Display the manager number and the salary of the lowest paid employee for that manager. Exclude any groups where the minimum salary is less than 1000. Sort the output by salary.

 

SQL> SELECT

manager_id,

2

MIN(salary) LOWEST PAID EMPLOYEE

3

FROM s_emp

4

GROUP BY manager_id

5

HAVING

MIN(salary) >= 1000

6

ORDER BY 2;

Continued

 

6.—continued

MANAGER_ID LOWEST PAID EMPLOYEE

---------- --------------------

2

1100

1

1400

3

1400

2500

7. What is the difference between the highest and lowest salaries?

SQL> SELECT

2

FROM

MAX(salary) - MIN (salary) DIFFERENCE s_emp;

DIFFERENCE

----------

1750

If you have time, complete the following exercises.

8. Display the product number and number of times it was ordered, labeled Times Ordered. Only show those products that have been ordered at least three times. Order the data by the number of products ordered.

SQL> SELECT

product_id,

2

COUNT(ord_id) TIMES ORDERED

3

FROM s_item

4

GROUP BY product_id

5

HAVING

COUNT(ord_id) >= 3

6

ORDER BY COUNT(ord_id);

PRODUCT_ID

TIMES ORDERED

----------

--------------

20106

3

20108

3

20201

3

20510

3

50273

3

30421

3

20512

3

30321

4

8 rows selected.

9. Retrieve the region number, region name, and the number of departments within each region.

 

SQL> SELECT

r.id, r.name, COUNT(d.id) # OF DEPT

2

FROM

s_dept d, s_region r

3

WHERE

d.region_id = r.id

4

GROUP BY r.id, r.name;

Continued

 

9.—continued

ID

NAME

# OF DEPT

-----

--------------------------- ----------------

1

North America

4

2

South America

2

3

Africa / Middle East

2

4

Asia

2

5

Europe

2

10. Display the order number and total item count for each order of 100 or more items. For example, if order number 99 contains quantity 30 of one item, and quantity 75 of another item, then the total item count for that order is 105.

SQL> SELECT

ord_id,

2

SUM(quantity) TOTAL ITEM COUNT

3

FROM s_item

4

GROUP BY ord_id

5

HAVING

SUM(quantity) >= 100;

ORD_ID TOTAL ITEM COUNT ---------- -----------------

97

1050

99

165

100

3100

101

217

102

145

106

392

107

332

108

321

109

3143

9 rows selected.

11. Display the customer name and the number of orders for each customer.

SQL> SELECT

2 FROM

c.name, COUNT(o.id) NUMBER OF ORDERS

s_customer c, s_ord o

c.id = o.customer_id

3 WHERE

4 GROUP BY c.name;

NAME

NUMBER OF ORDERS

-------------------------------- -----------------

Beisbol Si!

1

Big Johns Sports Emporium

1

Delhi Sports

1

Futbol Sonora

2

Hamada Sport

1

Kams Sporting Goods

1

Kuhns Sports

1

Muench Sports

2

Simms Athletics

1

Ojibway Retail

1

Sportique

1

Unisports

1

Womansport

2