Oracle Database Sql Certified Associate · Free Practice Question Medium
Question 16
View and examine the following SQL exhibits.
Exhibit: 1
- SELECT last_name, salary, hire_date
- FROM employees
- ORDER BY salary DESC;
Exhibit: 2
- SELECT last_name, salary, hire_date
- FROM employees
- ORDER BY 2 DESC;
Indicate what is true about these queries.
-
A
The second statement returns a syntax error.
-
B
The two statements can be made to produce identical results by adding a column alias for the salary column in the second SQL statement.
-
C
The two statements produce identical results.
-
D
There is no need to specify DESC because the results are sorted in descending order by default.
Reveal correct answer
Correct answer: C
Explanation
Correct answer: The two statements produce identical results.
Analyze the two SQL queries carefully:
Exhibit 1:
- SELECT last_name, salary, hire_date
- FROM employees
- ORDER BY salary DESC;
Orders the result by the salary column in descending order.
Exhibit 2:
- SELECT last_name, salary, hire_date
- FROM employees
- ORDER BY 2 DESC;
Orders the result by the second column in the
SELECTlist, which is salary.ORDER BY 2means order by the second selected column.Adding
DESCmakes it descending order.
Now, let's examine the statements:
The two statements produce identical results.
True
Both sort by the
salarycolumn descending.ORDER BY salary DESCandORDER BY 2 DESCare equivalent here.
The second statement returns a syntax error.
False
Ordering by column position is valid in SQL.
There is no need to specify DESC because the results are sorted in descending order by default.
False
Default order is ascending, so DESC is necessary to sort descending.
The two statements can be made to produce identical results by adding a column alias for the salary column in the second SQL statement.
False
Adding an alias is unnecessary since ordering by column position already works.
A. The second statement does not return a syntax error. Both SQL statements are valid and will execute without any syntax errors.
B. Adding a column alias for the salary column in the second SQL statement will not make the two statements produce identical results. The column alias does not affect the sorting order of the results.
C. The two statements produce identical results because they both retrieve data from the EMPLOYEES table and sort the results by the SALARY column in descending order.
D. There is no need to specify DESC in the ORDER BY clause because the results are sorted in ascending order by default. If you want to sort in descending order, you need to explicitly specify DESC in the ORDER BY clause.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
