1Z0-071 Aggregating Data with Group Functions 2 — Questions and Answers
Question 1: What is the result of using COUNT(column_name) when some rows have NULL in that column?
- It counts all rows including NULLs
- It returns NULL
- It counts only non-NULL rows (Correct answer)
- It raises an ORA error
Correct answer: It counts only non-NULL rows
COUNT(column_name) excludes NULL values, counting only rows where the column has a non-NULL value.
Question 2: Which query correctly uses HAVING to show departments with more than 5 employees?
- SELECT dept_id, COUNT(*) FROM emp WHERE COUNT(*) > 5 GROUP BY dept_id
- SELECT dept_id, COUNT(*) FROM emp GROUP BY dept_id HAVING COUNT(*) > 5 (Correct answer)
- SELECT dept_id, COUNT(*) FROM emp HAVING COUNT(*) > 5
- SELECT dept_id, COUNT(*) FROM emp GROUP BY dept_id WHERE COUNT(*) > 5
Correct answer: SELECT dept_id, COUNT(*) FROM emp GROUP BY dept_id HAVING COUNT(*) > 5
HAVING must follow GROUP BY and can reference aggregate functions like COUNT(*) to filter groups.
Question 3: What does the MIN() function return when applied to a VARCHAR2 column?
- An error, because MIN() only works on numbers
- The alphabetically first value (Correct answer)
- The shortest string
- The last inserted value
Correct answer: The alphabetically first value
MIN() on a VARCHAR2 column returns the alphabetically (or collation-order) lowest string value.
Question 4: Which of the following is a valid use of group functions in Oracle?
- SELECT SUM(salary) FROM employees WHERE SUM(salary) > 10000
- SELECT SUM(salary) FROM employees HAVING SUM(salary) > 10000 (Correct answer)
- SELECT department_id, SUM(salary) FROM employees HAVING department_id = 10
- SELECT SUM(salary), department_id FROM employees GROUP BY SUM(salary)
Correct answer: SELECT SUM(salary) FROM employees HAVING SUM(salary) > 10000
A group function like SUM() can be used in HAVING but not in WHERE without a subquery.
Question 5: How do you find the number of distinct job titles in the EMPLOYEES table?
- SELECT COUNT(job_id) FROM employees
- SELECT COUNT(*) FROM employees
- SELECT COUNT(DISTINCT job_id) FROM employees (Correct answer)
- SELECT DISTINCT COUNT(job_id) FROM employees
Correct answer: SELECT COUNT(DISTINCT job_id) FROM employees
COUNT(DISTINCT column) counts unique non-NULL values of that column.
Question 6: Which clause determines how rows are divided into groups before aggregation?
- ORDER BY
- PARTITION
- GROUP BY (Correct answer)
- CLUSTER BY
Correct answer: GROUP BY
GROUP BY divides the rows of a result set into groups for aggregation by group functions.
What is the result of using COUNT(column_name) when some rows have NULL in that column?