Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110.

Similar presentations


Presentation on theme: "SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110."— Presentation transcript:

1 SQL1-ch4 群組函數與聚總資料

2 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110

3 群組函數  群組函數 (group function)  聚總函數 (aggregate function)  每一組資料列只會有一個結果

4 練習  找出員工資料表中,最資深、資淺員工的 聘用日期。

5 群組函數  群組函數與空值

6 Q39/80( 可略 ) View the Exhibit and examine the description of the PRODUCT_INFORMATION table. Which SQL statement would retrieve from the table the number of products having LIST_PRICE as NULL? 6

7 A. SELECT COUNT(list_price) FROM product_information WHERE list_price IS NULL; B. SELECT COUNT(list_price) FROM product_information WHERE list_price = NULL; C. SELECT COUNT(NVL(list_price, 0)) FROM product_information WHERE list_price IS NULL; D. SELECT COUNT(DISTINCT list_price) FROM product_information WHERE list_price IS NULL; the number of products having LIST_PRICE as NULL? 7

8 資料群組  建立資料群組

9 Q33/80 Evaluate the following SQL statements: Statement 1: SELECT MAX(unit_price*quantity) “Maximum Order” FROM order_items; Statement 2: SELECT MAX(unit_price*quantity) “Maximum Order” FROM order_items GROUP BY order_id; Which statements are true regarding the output of these SQL statements? (Choose all that apply.) 9

10 A. Statement 1 would return only one row of output. B. Both the statements would give the same output. C. Statement 2 would return multiple rows of output. D. Statement 1 would not return any row because the GROUP BY clause is missing. E. Both statements would ignore NULL values for the UNIT_PRICE and QUANTITY columns. 10 SELECT MAX(unit_price*quantity) “Maximum Order” FROM order_items; SELECT MAX(unit_price*quantity) “Maximum Order” FROM order_items GROUP BY order_id;

11 資料群組  Group by  必須放在 where 子句之後  須包含欄位名稱或運算式  不可用欄位別名  可搭配單列函數使用  Select 子句中沒有使用聚總函數的欄位,須 使用 Group by 分群。

12 資料群組  依多個欄位分組

13 練習  找出每個部門中,最資深、資淺員工的聘 用日期。

14 Q110/140 Which two statements are true regarding the GROUP BY clause in a SQL statement? (Choose two.) A. You can use column alias in the GROUP BY clause. B. Using the WHERE clause after the GROUP BY clause excludes the rows after creating groups. C. The GROUP BY clause is mandatory( 強制的 ) if you are using an aggregate function in the SELECT clause. D. Using the WHERE clause before the GROUP BY clause excludes the rows before creating groups. E. If the SELECT clause has an aggregate function, then those individual columns without an aggregate function in the SELECT clause should be included in the GROUP BY clause. (P4-17)

15 群組篩選  不能在 where 中使用群組函數

16 群組篩選  群組條件的篩選,須使用 Having 子句

17 Q95/140 Which three statements are true regarding group functions( 群組函數 )? (Choose three.) A. They can be used on columns or expressions. B. They can be passed as an argument to another group function. C. They can be used only with a SQL statement that has the GROUP BY clause. D. They can be used on only one column in the SELECT clause of a SQL statement. E. They can be used along with the single-row function in the SELECT clause of a SQL statement. (P4-10)

18 Q10/140 Which three statements are true regarding the WHERE and HAVING clauses in a SQL statement? (Choose three.) A. The HAVING clause conditions can have aggregate functions. B. The HAVING clause conditions can use aliases for the columns. C. WHERE and HAVING clauses cannot be used together in a SQL statement. D. The WHERE clause is used to exclude rows before the grouping of data. E. The HAVING clause is used to exclude one or more aggregated results after grouping data.

19 Q59/140( 可略 ) View the Exhibit and examine the description of the PRODUCT_INFORMATION table. Which SQL statement would retrieve from the table the number of products having LIST_PRICE as NULL? A. SELECT COUNT(list_price) FROM product_information WHERE list_price IS NULL; B. SELECT COUNT(list_price) FROM product_information WHERE list_price = NULL;

20 C. SELECT COUNT(NVL(list_price, 0)) FROM product_information WHERE list_price IS NULL; D. SELECT COUNT(DISTINCT list_price) FROM product_information WHERE list_price IS NULL;


Download ppt "SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110."

Similar presentations


Ads by Google