1 / 30

SQL1-ch5

SQL1-ch5. 顯示多個表格的資料. 題號. 80 題: 34 、 57 、 71 、 72 140 題: 18 、 25 、 62 、 97 、 115 、 131. 表格合併. 內部合併 :只列出兩表格相符欄位資料 自然合併 -Natural Join 合併 -Join using 、 on 外部合併 :可列出不符合欄位資料 Left outer join Right outer join. 表格合併. 自然合併 (Natural Join) 兩表格中具有 相同名稱 、 型態 的資料欄位 若欄位名稱相同,但型態不同則回傳錯誤. 使用 Using 子句.

denali
Download Presentation

SQL1-ch5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SQL1-ch5 顯示多個表格的資料

  2. 題號 • 80題:34、57、71、72 • 140題:18、25、62、97、115、131

  3. 表格合併 • 內部合併:只列出兩表格相符欄位資料 • 自然合併-Natural Join • 合併-Join using、on • 外部合併:可列出不符合欄位資料 • Left outer join • Right outer join

  4. 表格合併 • 自然合併(Natural Join) • 兩表格中具有相同名稱、型態的資料欄位 • 若欄位名稱相同,但型態不同則回傳錯誤

  5. 使用Using子句 • Using可指定欲合併的欄位(名稱須相同) • Natural join 須改成 Join … Using … • 欄位不可加上表格名稱或別名

  6. Q34/80 View the Exhibit and examine the structure of the ORDERS and ORDER_ITEMS tables. Evaluate the following SQL statement: SELECT oi.order_id, product_id, order_date FROM order_itemsoiJOIN orders o USING(order_id); Which statement is true regarding the execution of this SQL statement?

  7. Q34表格合併(SQL1 ch5) SELECT oi.order_id, product_id, order_date FROM order_itemsoi JOIN orders o USING(order_id); A. The statement would not execute because table aliases are not allowed in the JOIN clause. B. The statement would not execute because the table alias prefix is not used in the USING clause. C. The statement would not execute because all the columns in the SELECT clause are not prefixed with table aliases. D. The statement would not execute because the column part of the USING clause cannot have a qualifier in the SELECT list.(P5-11)

  8. 使用On子句 • 使用On可描述合併的條件(欄位名可不同) • From tableAjointableBon (條件) • 若欄位名稱相同,須加上表格名稱或別名

  9. 使用On子句 • On子句的變化

  10. 練習 • 查出每位員工的姓名、部門名稱與城市。 • 姓名(last_name)在會員表格(employees)中 • 部門名稱(department_name)在部門表格(departments)中 • 城市(city)在位置表格(locations)中

  11. Q71/80 View the Exhibit and examine the structure of the EMP and SALGRADE tables. You want to display the names of all employees whose salaries belong to GRADE 5. Which SQL statements give the required output? (Choose all that apply)

  12. A. SELECT ename FROM emp JOIN salgrade USING (sal BETWEEN losal AND hisal) AND grade = 5; B. SELECT ename FROM emp e JOIN salgrade s ON (e.sal BETWEEN s.losal AND s.hisal AND s.grade = 5); C. SELECT ename FROM emp e JOIN salgrade s ON (e.sal BETWEEN s.losal AND s.hisal) AND s.grade = 5; D. SELECT ename FROM emp e JOIN salgrade s ON (e.sal BETWEEN s.losal AND s.hisal) WHERE s.grade=5; E. SELECT ename FROM emp e JOIN salgrade s WHERE e.sal BETWEEN s.losal AND s.hisal AND s.grade = 5;

  13. 外部合併 • 希望可顯示出未符合的資料

  14. Q57/80 View the Exhibit and examine the description of the ORDER_ITEMS and PRODUCT_INFORMATION tables. The ORDER_ITEM table has records pertaining to details for each product in an order. The PRODUCT_INFORMATION table has records for all the products available for ordering. Evaluate the following SQL statement: SELECT oi.order_id, pi.product_id FROM order_itemsoi RIGHT OUTER JOIN product_informationpi ON (oi.product_id=pi.product_id); Which statement is true regarding the output of this SQL statement?

  15. SELECT oi.order_id, pi.product_id FROM order_itemsoi RIGHT OUTER JOIN product_information pi ON (oi.product_id=pi.product_id); A. The query would return the ORDER_ID and PRODUCT_ID for only those products that are ordered. B. The query would return the ORDER_ID and PRODUCT_ID for the products that are ordered as well as for the products that have never been ordered. C. The query would return the ORDER_ID and PRODUCT_ID for the products that are ordered but not listed in the PRODUCT_INFORMATION table. D. The query would return the ORDER_ID and PRODUCT_ID for those products that are ordered as well as for the products that have never been ordered, and for the products that are not listed in the PRODUCT_INFORMATION table.

  16. Q72/80 View the Exhibit and examine the structure of the DEPARTMENTS and LOCATIONS tables. You want to display all the cities and the corresponding departments in them, if any. Which query would give you the required output?

  17. A. SELECT location_id LOC, city, department_idDEPT FROM locations LEFT OUTER JOIN departments USING (location_id); B. SELECT location_id LOC, city, department_idDEPT FROM locations RIGHT OUTER JOIN departments USING (location_id); C. SELECT l.location_id LOC, l.city, d.department_idDEPT FROM locations l LEFT OUTER JOIN departments d USING (location_id); D. SELECT l.location_id LOC, l.city, d.department_idDEPT FROM locations l FULL OUTER JOIN departments d USING (location_id);

  18. Q25/140(可略) View the Exhibit and examine the structure of the ORDER_ITEMS and ORDERS tables. You are asked to retrieve the ORDER_ID, PRODUCT_ID, and total price (UNIT_PRICE multiplied by QUANTITY), where the total price is greater than 50,000. You executed the following SQL statement: SELECT order_id, product_id, unit_price*quantity "Total Price" FROM order_items WHERE unit_price*quantity > 50000 NATURAL JOIN orders; Which statement is true regarding the execution of the statement?

  19. SELECT order_id, product_id, unit_price*quantity "Total Price" FROM order_items WHERE unit_price*quantity > 50000 NATURAL JOIN orders; A. The statement would execute and provide the desired result. B. The statement would not execute because the ON keyword is missing in the NATURAL JOIN clause. C. The statement would not execute because the WHERE clause is before the NATURAL JOIN clause. D. The statement would not execute because the USING keyword is missing in the NATURAL JOIN clause.

  20. Q62/140(可略) View the Exhibit and examine the description of the EMPLOYEES and DEPARTMENTS tables. You want to display the LAST_NAME for the employees, LAST_NAME for the manager of the employees, and the DEPARTMENT_NAME for the employees having 100 as MANAGER_ID. The following SQL statement was written: SELECT m.last_name "Manager", e.last_name "Employee", department_name "Department" FROM employees m JOIN employees e ON (m.employee_id = e.manager_id) WHERE e.manager_id=100 JOIN departments d ON (e.department_id = d.department_id);

  21. Which statement is true regarding the output of this SQL statement? A. The statement would provide the desired results. B. The statement would not execute because the ON clause is written twice. C. The statement would not execute because the WHERE clause is wrongly placed. D. The statement would not execute because the self join uses the ON clause instead of the USING clause.

  22. Q131/140 View the Exhibit and examine the description of the DEPARTMENTS and EMPLOYEES tables. To retrieve data for all the employees for their EMPLOYEE_ID, FIRST_NAME, and DEPARTMENT NAME, the following SQL statement was written: SELECT employee_id, first_name, department_name FROM employees NATURAL JOIN departments; The desired output is not obtained after executing the above SQL statement. What could be the reason for this?

  23. SELECT employee_id, first_name, department_name FROM employees NATURAL JOIN departments; A. The NATURAL JOIN clause is missing the USING clause. B. The table prefix is missing for the column names in the SELECT clause. C. The DEPARTMENTS table is not used before the EMPLOYEES table in the FROM clause. D. The EMPLOYEES and DEPARTMENTS tables have more than one column with the same column name and data type.

  24. Q115/140 Which two statements are true regarding the types of table joins available in Oracle Database? (Choose two.) A. You can use the JOIN clause to join only two tables. B. You can explicitly(明確的) provide the join condition with a NATURAL JOIN. C. You can use the USING clause to join tables on more than one column. D. You can use the ON clause to specify multiple conditions while joining tables.

  25. Q97/140 View the Exhibit and examine the structure of the PRODUCT_INFORMATION and INVENTORIES tables. You have a requirement from the supplies department to give a list containing PRODUCT_ID, SUPPLIER_ID, and QUANTITY_ON_HAND(庫存量) for all the products wherein QUANTITY_ON_HAND is less than five. Which two SQL statements can accomplish the task? (Choose two.) A. SELECT product_id, quantity_on_hand , supplier_id FROM product_information NATURAL JOIN inventories AND quantity_on_hand < 5;

  26. B. SELECT i.product_id, i.quantity_on_hand , i.supplier_id FROM product_information pi JOIN inventories i USING (product_id) AND quantity_on_hand < 5; C. SELECT i.product_id, i.quantity_on_hand , pi.supplier_id FROM product_information pi JOIN inventories i ON (pi.product_id=i.product_id) WHERE quantity_on_hand < 5; D. SELECT i.product_id, i.quantity_on_hand , i.supplier_id FROM product_information pi JOIN inventories i ON (pi.product_id=i.product_id) AND quantity_on_hand < 5;

  27. Q18/140 View the Exhibit and examine the structure for the ORDERS and ORDER_ITEMS tables. You want to display ORDER_ID, PRODUCT_ID, and TOTAL (UNIT_PRICE multiplied by QUANTITY) for all the orders placed in the last seven days. Which query would you execute? A. SELECT order_id, product_id, unit_price*quantity"TOTAL" FROM order_itemsoi JOIN orders o ON (o.order_id=oi.order_id) WHERE o.order_date>=SYSDATE-7;

  28. B. SELECT o.order_id,oi.product_id, oi.unit_price*oi.quantity "TOTAL" FROM order_itemsoi JOIN orders o USING (order_id) WHERE o.order_date>=SYSDATE-7; C. SELECT o.order_id, oi.product_id,oi.unit_price*oi.quantity "TOTAL" FROM order_itemsoi JOIN orders o WHERE o.order_date>=SYSDATE-7 ON (o.order_id=oi.order_id); D. SELECT o.order_id, oi.product_id, oi.unit_price*oi.quantity "TOTAL" FROM order_itemsoi JOIN orders o ON (o.order_id=oi.order_id) WHERE o.order_date>=SYSDATE-7;

More Related