1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Practice solutions docx

202 284 3
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Practice Solutions
Trường học Unknown University
Chuyên ngành Introduction to Oracle: SQL and PL/SQL
Thể loại practice solutions
Định dạng
Số trang 202
Dung lượng 624,83 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder AĆ4 1.. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder AĆ64.—continued b.. Introduction to Oracle: SQL and

Trang 1

Practice Solutions

A

Trang 3

Practice Solutions AĆ3

Preface

There is often more than one way to achieve any result in SQL Where possible, thealternatives 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 thedatabase level

For more information, see

Oracle7 Server SQL Reference, Release 7.3 and Oracle7 Server Administrators Guide.

Trang 4

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder AĆ4

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) NAME NOT NULL VARCHAR2(25) 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

Trang 5

Practice Solutions AĆ5

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

SQL> DESCRIBE s_customer

Name Null? Type

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)

Trang 6

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder AĆ6

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-0103Kam’s Sporting Goods 852-3692888

15 rows selected

Continued

Trang 7

Practice Solutions AĆ7

Trang 8

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder AĆ8

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

3. This SELECT statement will execute successfully

" True

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

SQL> SELECT id, last_name,

" No SAL column in existence (WHERE clause)

" The ANNUAL SALARY alias cannot include spaces Alias should readANNUAL_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 singlequotation marks The value should read ‘%84’ in line 5

Trang 9

Practice Solutions AĆ9

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 allcompanies represented by sales representative 11 Save your SQL statement to

Continued

Trang 10

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder AĆ10

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.

SQL> SAVE p2q5 REPLACE

Wrote file p2q5

Continued

Trang 11

Practice Solutions AĆ11

5.—continued

d. Retrieve p2q5 into the SQL buffer Order the query results in descending

order by customer number Run your query

-Big John’s Sports Emporium 213 EXCELLENTBeisbol Si! 209 EXCELLENTWomansport 204 EXCELLENT

6. Show the structure of the S_EMP table

SQL> DESCRIBE s_emp

Name Null? Type

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)

Trang 12

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder AĆ12

6.—continued

a. Display the user name for employee 23

SQL> SELECT userid

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; Employees DEPT_ID

-Mark Quick-To-See 10

Audry Ropeburn 50

Carmen Velasquez 50

Continued

Trang 13

Practice Solutions AĆ13

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 changeyour query into a case-insensitive query

SQL> SELECT first_name, last_name

3 WHERE 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

Trang 14

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder AĆ14

6d.—continued

USERID START_DAT

-

-rmenchu 14-MAY-90 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 2 FROM s_emp 3 WHERE salary NOT BETWEEN 1000 AND 2500; LAST_NAME SALARY -

-Smith 940 Patel 795

Newman 750

Markarian 850

Chang 800

Patel 795

Dancs 860

7 rows selected

Continued

Trang 15

Practice Solutions AĆ15

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”,

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 last_name, start_date

3 WHERE start_date LIKE ’%91’;

Continued

Trang 16

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder AĆ16

d. Display the full name of all employees with no manager

SQL> SELECT first_name, last_name

3 WHERE manager_id IS NULL;

FIRST_NAME LAST_NAME

Carmen Velasquez

Trang 17

-Practice Solutions AĆ17

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

-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

Trang 18

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder AĆ18

8.—continued

b. Display all product names and short descriptions for all descriptions

containing the word bicycle

" Results have been formatted

SQL> SELECT name, short_desc

3 WHERE short_desc LIKE ’%bicycle%’

- Grand Prix Bicycle Road bicycle

-Himalaya Bicycle Mountain bicycle

Grand Prix Bicycle Tires Road bicycle tires

Himalaya Tires Mountain bicycle tires

c. Display all short descriptions Compare the results from Exercise 10b Didyour 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 toremove the case-sensitivity

SQL> SELECT short_desc

3 ORDER BY 1;

Continued

Trang 19

Practice Solutions AĆ19

8c.—continued

SHORT_DESC

-Advanced ski boot

Advanced ski pole

Baseball

Batting helmet

Beginner’s ski boot

Beginner’s ski pole

Bicycle helmet

Catcher’s glove

Curling bar

Eighty pound weight

Elbow pads, pair

Fifty pound weight

Infielder’s glove

Intermediate ski boot

Intermediate ski pole

Junior soccer ball

Knee pads, pair

Mountain bicycle

Mountain bicycle tires

One hundred pound weight

Outfielder’s 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

Trang 20

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder AĆ20

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

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

Trang 21

Practice Solutions AĆ21

4. Display the employee number, last name, and salary increased by 15% and

expressed as a whole number

SQL> SELECT id, last_name, ROUND(salary * 1.15)

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

Trang 22

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder AĆ22

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

Trang 23

Practice Solutions AĆ23

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

" Results have been formatted

SQL> SELECT last_name, start_date,

Trang 24

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder AĆ24

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 1991Quick-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 1992Markarian 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 1991Schwartz 09-MAY-91 Eleventh of November 1991

25 rows selected

Trang 25

Practice Solutions AĆ25

7. Display the product name for products that have “ski” in the 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 datethe employee was hired Order your result by the number of months employed.Round the number of months up to the closest whole number

Trang 26

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder AĆ26

Note: Your MONTHS_WORKED may differ from the solution because your

SYSDATE may return a different value

Trang 27

Practice Solutions AĆ27

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,

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

Trang 28

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder AĆ28

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 ’ ||

-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.

Trang 29

Practice Solutions AĆ29

3 FROM s_emp, s_dept

4 WHERE s_emp.dept_id = s_dept.id;

Continued

Trang 30

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder AĆ30

Trang 31

Practice Solutions AĆ31

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

" Results have been formatted

SQL> SELECT e.last_name, d.name, r.name

2 FROM s_emp e, s_dept d, s_region r

3 WHERE e.dept_id = d.id

4 AND r.id = d.region_id

5 AND 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 s_emp.last_name, s_dept.name

2 FROM s_emp, s_dept

3 WHERE s_emp.dept_id = s_dept.id

4 AND s_emp.last_name = ’Smith’;

LAST_NAME NAME

-

-Smith Operations

Trang 32

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder AĆ32

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

SQL> SELECT s_product.name,

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

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

Trang 33

Practice Solutions AĆ33

SQL> SELECT s_customer.id ”Customer ID”,

4 FROM s_customer, s_ord

5 WHERE s_customer.id =

Continued

Trang 34

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder AĆ34

SQL> SELECT w.last_name EMP_NAME, w.id EMP_ID,

3 FROM s_emp w, s_emp m

4 WHERE w.manager_id = m.id;

Continued

Trang 35

Practice Solutions AĆ35

Trang 36

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder AĆ36

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”,

m.id ”Manager ID”

4 WHERE w.manager_id = m.id (+);

Employee Name Employee ID Manager Name Manager ID - - - -Velasquez 1 Ngao 2 Velasquez 1Nagayama 3 Velasquez 1Quick-To-See 4 Velasquez 1Ropeburn 5 Velasquez 1Urguhart 6 Ngao 2Menchu 7 Ngao 2Biri 8 Ngao 2Catchpole 9 Ngao 2Havel 10 Ngao 2Magee 11 Nagayama 3Giljum 12 Nagayama 3Sedeghi 13 Nagayama 3Nguyen 14 Nagayama 3Dumas 15 Nagayama 3Maduro 16 Urguhart 6Nozaki 18 Menchu 7Markarian 21 Biri 8Patel 23 Catchpole 9Smith 17 Urguhart 6Patel 19 Menchu 7Newman 20 Biri 8Chang 22 Catchpole 9Dancs 24 Havel 10Schwartz 25 Havel 10

25 rows selected

Trang 37

Practice Solutions AĆ37

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

SQL> SELECT c.name CUSTOMER, p.id PRODUCT_ID,

3 FROM s_customer c, s_product p, s_item i,

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

Trang 38

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder AĆ38

9.—continued

CUSTOMER PRODUCT_ID QUANTITY - - -Womansport 10011 500Womansport 10013 400Womansport 10021 500Womansport 30326 600Womansport 41010 250Womansport 30433 450Womansport 10023 400Kuhn’s Sports 20106 50Kuhn’s Sports 20201 130Kuhn’s Sports 30421 55Kuhn’s Sports 30321 75Kuhn’s Sports 20108 22Hamada Sport 20510 9Hamada Sport 41080 50Hamada Sport 41100 42Hamada Sport 32861 57Hamada Sport 20512 18Hamada Sport 32779 60Hamada Sport 30321 85Big John’s Sports Emporium 10011 150Big John’s Sports Emporium 30426 500Big John’s Sports Emporium 50418 43Big John’s Sports Emporium 32861 50Big John’s Sports Emporium 30326 1500Big John’s Sports Emporium 10012 600Big John’s Sports Emporium 10022 300

26 rows selected

Trang 39

Practice Solutions AĆ39

Practice 5 Solutions

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

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

SQL> SELECT MAX (total) ”Highest”,

Trang 40

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder AĆ40

Ngày đăng: 21/12/2013, 06:17

TỪ KHÓA LIÊN QUAN

w