Tài liệu Practice solutions docx

202 283 3
Tài liệu Practice solutions docx

Đ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

Practice Solutions A Introduction to Oracle: SQL and PL/SQL Using Procedure BuilderAĆ2 Practice Solutions AĆ3 Preface 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. Introduction to Oracle: SQL and PL/SQL Using Procedure BuilderAĆ4 Practice 1 Solutions 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. Practice Solutions AĆ5 Practice 1 Solutions continued 4. Show the structure of the S_CUSTOMER table. Using this table, perform the following 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) 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 Introduction to Oracle: SQL and PL/SQL Using Procedure BuilderAĆ6 Practice 1 Solutions 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 Practice Solutions AĆ7 Practice 1 Solutions 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 81-20101 Simms Atheletics 91-10351 Delhi Sports 1-206-104-0103 Womansport 852-3692888 Kam’s Sporting Goods 33-2257201 Sportique 234-6036201 Sweet Rock Sports 49-527454 Muench Sports 809-352689 Beisbol Si! 52-404562 Futbol Sonora 42-111292 Kuhn’s Sports 20-1209211 Hamada Sport 1-415-555-6281 Big John’s Sports Emporium 1-716-555-7171 Ojibway Retail 7-3892456 Sporta Russia 15 rows selected. Introduction to Oracle: SQL and PL/SQL Using Procedure BuilderAĆ8 Practice 2 Solutions 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. Practice Solutions AĆ9 Practice 2 Solutions continued 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 NAME ID CREDIT_RA ------------------------------ --------- --------- Womansport 204 EXCELLENT Beisbol Si! 209 EXCELLENT Big John’s Sports Emporium 213 EXCELLENT Ojibway Retail 214 POOR Continued Introduction to Oracle: SQL and PL/SQL Using Procedure BuilderAĆ10 Practice 2 Solutions 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 name, id, credit_rating 2 FROM s_customer 3* WHERE sales_rep_id = 11 SQL> 1 SELECT name ”Company”, id ”Company ID”, SQL> i credit_rating ”Rating” SQL> RUN 1 SELECT name ”Company”, id ”Company ID”, 2 credit_rating ”Rating” 3 FROM s_customer 4* WHERE sales_rep_id = 11 Company Company ID Rating ------------------------------ ---------- --------- Womansport 204 EXCELLENT Beisbol Si! 209 EXCELLENT Big John’s Sports Emporium 213 EXCELLENT Ojibway Retail 214 POOR SQL> SAVE p2q5 REPLACE Wrote file p2q5 Continued [...]... Practice 4 Solutions 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 2 3 FROM 4 WHERE s_emp.last_name, s_emp.dept_id, s_dept.name s_emp, s_dept s_emp.dept_id = s_dept.id; Continued Practice Solutions AĆ29 Practice 4 Solutions. .. Order the query results by start date ascending order SQL> 2 3 4 5 SELECT FROM WHERE userid, start_date s_emp start_date BETWEEN ’05-may-90’ AND ’26-may-91’ ORDER BY start_date; Continued Practice Solutions AĆ13 Practice 2 Solutions continued 6d.—continued USERID -rmenchu cmagee rpatel echang murguhar anozaki ysedeghi mhavel bdancs sschwart amarkari START_DAT 14-MAY-90 14-MAY-90 17-OCT-90 30-NOV-90... 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 Practice Solutions AĆ15 Practice 2 Solutions continued 7c.—continued LAST_NAME Nagayama Urguhart Havel Sedeghi Dumas Nozaki Patel Newman Markarian Dancs Schwartz START_DAT 17-JUN-91 18-JAN-91 27-FEB-91... 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 Practice Solutions AĆ17 Practice 2 Solutions 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... 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 Practice Solutions AĆ19 Practice 3 Solutions 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... 1610 Giljum 1714 Sedeghi 1742 Nguyen 1754 Dumas 1668 Maduro 1610 Smith 1081 Nozaki 1380 Patel 914 Newman 863 Markarian 978 Chang 920 Patel 914 Dancs 989 Schwartz 1265 25 rows selected Practice Solutions AĆ21 Practice 3 Solutions 5 continued Display the employee last name and title in parentheses for all employees The report should look like the output below SQL> SELECT 2 3 FROM 4 ORDER BY last_name||’(’||INITCAP(title)||’)’... " Results have been formatted SQL> SELECT last_name, start_date, 2 TO_CHAR(NEXT_DAY(ADD_MONTHS(start_date,6), 3 ’MONDAY’), 4 ’fmDdspth ”of” Month YYYY’) REVIEW 5 FROM s_emp; Continued Practice Solutions AĆ23 Practice 3 Solutions continued 6.—continued LAST_NAME -Velasquez Ngao Nagayama Quick-To-See Ropeburn Urguhart Menchu Biri Catchpole Havel Magee Giljum Sedeghi Nguyen Dumas Maduro Smith Nozaki... 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 Practice Solutions AĆ25 Practice 3 Solutions 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... WEDNESDAY WEDNESDAY WEDNESDAY WEDNESDAY THURSDAY THURSDAY THURSDAY FRIDAY FRIDAY FRIDAY SATURDAY SATURDAY SATURDAY SATURDAY SATURDAY SUNDAY SUNDAY SUNDAY SUNDAY SUNDAY 25 rows selected Practice Solutions AĆ27 Practice 3 Solutions 10 continued Write a query that produces the following for each employee: earns monthly but wants For example: ALLEN earns $1,100 monthly... -ID LAST_NAME FIRST_NAME USERID START_DATE COMMENTS MANAGER_ID TITLE DEPT_ID SALARY COMMISSION_PCT Practice Solutions Null? -NOT NULL NOT NULL Type -NUMBER(7) VARCHAR2(25) VARCHAR2(25) NOT NULL VARCHAR2(8) DATE VARCHAR2(255) NUMBER(7) VARCHAR2(25) NUMBER(7) NUMBER(11,2) NUMBER(4,2) AĆ11 Practice 2 Solutions continued 6.—continued a Display the user name for employee 23 SQL> SELECT 2 FROM 3 WHERE . Practice Solutions A Introduction to Oracle: SQL and PL/SQL Using Procedure BuilderAĆ2 Practice Solutions AĆ3 Preface There. Operations 4 45 Operations 5 50 Administration 1 12 rows selected. Practice Solutions AĆ5 Practice 1 Solutions continued 4. Show the structure of the S_CUSTOMER

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

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan