Oracle Database SQL (1Z0-071) Practice Questions

Oracle Database SQL (1Z0-071) exam preparation

Oracle Database SQL (1Z0-071) practice questions in English: 180 questions with explanations. The mock exam has 78 questions and 120 minutes.

Oracle Database SQL (1Z0-071) free question 8 / 10 · Domain 6: Reporting Aggregated Data Using Group Functions

In the employees table, department_id is 10 in two rows, 20 in three rows, and 30 in one row. Which is the result of the following SELECT statement? SELECT COUNT() FROM employees GROUP BY department_id;

  1. Three rows, one per department, in no guaranteed order; use ORDER BY to sort them
  2. One row (6): rows are not grouped by a column that is not in the select list
  3. Three rows sorted by department_id in ascending order; GROUP BY also sorts
  4. An error: the GROUP BY column needs to appear in the select list as well
Show the answer and explanation

Answer: A

Why this is correct
The SELECT topic in the SQL Language Reference says that expressions in the GROUP BY clause can contain any columns of the tables in the FROM clause, regardless of whether the columns appear in the select list, and that the GROUP BY clause groups rows but does not guarantee the order of the result set. The query returns three rows, one per department (2, 3, and 1), in no guaranteed order.

Why the other options are wrong
"An error: the GROUP BY column needs to appear in the select list as well" … A GROUP BY expression does not have to appear in the select list. The documentation says GROUP BY expressions can contain any columns in the FROM clause, regardless of whether the columns appear in the select list.
"One row (6): rows are not grouped by a column that is not in the select list" … Grouping is decided by the GROUP BY expressions, not by what is in the select list. Leaving the column out of the select list does not make the whole table one group.
"Three rows sorted by department_id in ascending order; GROUP BY also sorts" … GROUP BY only gathers rows into groups. It does not guarantee the order of the result. If you need an order, specify ORDER BY.

Key takeaway
You can write a query that groups by a column and does not show it, but then you cannot tell which row belongs to which department. Only ORDER BY decides the order of the result.

Go to the Oracle Database SQL (1Z0-071) practice app (10 free questions; purchase unlocks all questions)

See the full practice set