従業員テーブルの説明を調べます。

次の要件を調べてください。
1- 各従業員の姓、入社日、勤続年数を表示します。
2. 従業員の勤続年数が 5 年以上 10 年未満の場合は、勤続年数 -5 年以上と表示します。
3. 勤続年数が10年以上15年未満の場合は「勤続10年以上」と表示します。
4. 勤続年数が 15 年以上の従業員の場合は、「勤続 15-*- 年」と表示します。
5. これらの条件がいずれも一致しない場合は、「勤続年数 5 年未満」と表示されます。
6. 結果を hire_date 列で並べ替えます。
すべての要件を満たすステートメントはどれですか?
正解:D
Option D is the correct SQL statement that satisfies all the requirements mentioned. The CASE statement correctly compares the hire_date to date intervals subtracted from the current date (SYSDATE) to determine the number of years of service. This CASE statement is also appropriately ordered to ensure that the first condition matched is the one returned, preventing overlapping of the conditions.
Here is how Option D works according to the requirements:
* It selects the last_name and hire_date from the employees table.
* The CASE statement is used to calculate the number of years of service and to display the appropriate message according to the intervals defined.
* The ORDER BY hire_date clause ensures the results are sorted by the date of hire.
In Option D, the intervals are defined in the correct order, ensuring that the first true condition is the one that is used:
* The first WHEN checks if the hire date is 15 or more years ago.
* The second WHEN checks if it is 10 or more years ago.
* The third WHEN checks if it is 5 or more years ago.
* The ELSE clause covers any hire dates less than 5 years ago.
The correct syntax for the intervals is SYSDATE - hire_date >= TO_YMINTERVAL('15-0') and similarly for the 10 and 5 years intervals.
Options A, B, and C are incorrect because they have various issues, such as incorrect ordering of the CASE statement's conditions, which could lead to incorrect results due to overlapping intervals, or the use of the TO_YMINTERVAL function that may not properly cover the intended date ranges.
References:
* Oracle Documentation on CASE Expressions: SQL Language Reference - CASE Expression
* Oracle Documentation on TO_YMINTERVAL Function: SQL Language Reference - TO_YMINTERVAL
* Oracle Documentation on ORDER BY Clause: SQL Language Reference - ORDER BY Clause Therefore, Option D is the statement that fulfills all the requirements for displaying the number of years of service based on the employee's hire date and ordered by the hire date.