Using The Group Functions Questions
Using The Group Functions Questions
http://www.tutorialspoint.com/sql_certificate/using_the_group_functions_questions.htm
Copyright tutorialspoint.com
GROUP BY department_id ;
GROUP BY department_id ;
B. The COUNT(*) function counts the number of rows with duplicates and NULL values
C. The COUNT(DISTINCT) function counts the number of distinct rows
D. COUNT(*) is equivalent to COUNT(ALL)
Answer: B. The COUNT(*) counts the number of rows including duplicates and NULLs. Use
DISTINCT and ALL keyword to restrict duplicate and NULL values.
5. What are the appropriate data types accepted by GROUP BY functions?
A. Nested Tables
B. NUMBER
C. CLOB
D. DATE
Answer: B. The data types for the functions with an argument may be CHAR, VARCHAR2,
NUMBER or DATE.
6. A table T_COUNT has 12 number values as 1, 2, 3, 32, 1, 1, null, 24, 12, null, 32, null.
Predict the output of the below query.
SELECT COUNT (*) FROM t_count;
A. 12
B. 6
C. 9
D. Throws exception because COUNT function doesn't works with NULL values
Answer: A. The COUNT(*) counts the number of rows including duplicates and NULLs. Use
DISTINCT and ALL keyword to restrict duplicate and NULL values.
7. A table T_COUNT has 12 number values as 1, 2, 3, 32, 1, 1, null, 24, 12, null, 32, null.
Predict the output of the below query.
SELECT COUNT (num) FROM t_count;
A. 12
B. 6
C. 9
D. Throws exception because COUNT function doesn't works with NULL values
Answer: C. COUNT (column) ignores the NULL values but counts the duplicates.
8. A table T_COUNT has 12 number values as 1, 2, 3, 32, 1, 1, null, 24, 12, null, 32, null.
Predict the output of the below query.
SELECT COUNT (ALL num) FROM t_count;
A. 12
B. 6
C. 9
D. Throws exception because COUNT function doesn't works with NULL values
Answer: C. COUNT(ALL column) ignores the NULL values but counts the duplicates.
9. A table T_COUNT has 12 number values as 1, 2, 3, 32, 1, 1, null, 24, 12, null, 32, null.
Predict the output of the below query.
A. 12
B. 6
C. 9
D. Throws exception because COUNT function doesn't works with NULL values
Answer: B. COUNT (DISTINCT column) counts the distinct not null values.
10. What happens when the below query is executed in SQL* Plus?
SELECT COUNT() FROM dual;
EMPLOYEE_ID
NOT NULL NUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOT NULL VARCHAR2(25)
EMAIL
NOT NULL VARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOT NULL DATE
JOB_ID
NOT NULL VARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
SELECT department_id , SUM(salary )
FROM employees
GROUP BY department_id ;
D. SELECT category, AVG(retail-cost) Profit FROM books GROUP BY category HAVING profit
> 8.56;
B. ADD
C. TOTAL
D. SUM
Answer: D. SUM function is used to get the addition of numeric values.
25. Which of the following SELECT statements lists the highest retail price of all books
in the Family category?
A. SELECT MAX(retail) FROM books WHERE category = 'FAMILY';
B. SELECT MAX(retail) FROM books HAVING category = 'FAMILY';
C. SELECT retail FROM books WHERE category = 'FAMILY' HAVING MAX(retail);
D. None of the above
Answer: A. Since the category FAMILY has to be restricted before grouping, table rows must be
filtered using WHERE clause and not HAVING clause.
26. Which of the following functions can be used to include NULL values in calculations?
A. SUM
B. NVL
C. MAX
D. MIN
Answer: B.NVL is a general function to provide alternate values to the NULL values. It can really
make a difference in arithmetic calculations using AVG, STDDEV and VARIANCE group functions.
27. Which of the following is not a valid statement?
A. You must enter the ALL keyword in a group function to include all duplicate values.
B. The AVG function can be used to find the average calculated difference between two dates.
C. The MIN and MAX functions can be used on VARCHAR2 columns.
D. All of the above
Answer: A. The ALL keyword counts duplicates but ignores NULLs. Duplicates are also included
with '*' and column name specification.
28. Which of the following SQL statements determines how many total customers were
referred by other customers?
A. SELECT customer#, SUM(referred) FROM customers GROUP BY customer#;
B. SELECT COUNT(referred) FROM customers;
C. SELECT COUNT(*) FROM customers;
D. SELECT COUNT(*) FROM customers WHERE referred IS NULL;
Answer: B. Considering all customers as one group, COUNT(referred) will count only those who
are referred by someone. COUNT(referred) will ignore NULL values of the column.
29. Determine the correct order of execution of following clauses in a SELECT
statement.
1.SELECT
2.FROM
3.WHERE
4.GROUP BY
5.HAVING
6.ORDER BY
A. 2-3-4-5-1-6
B. 1-2-3-4-5-6
C. 6-5-4-3-2-1
D. 5-4-2-3-1-6
Answer: A. Processing order starts from FROM clause to get the table names, then restricting
rows using WHERE clause, grouping them using GROUP BY clause, restricting groups using HAVING
clause. ORDER BY clause is the last one to be processed to sort the final data set.
30. Which of the below clauses is used to group a set of rows based on a column or set
of columns?
A. HAVING
B. WHERE
C. GROUP BY
D. GROUPING
Answer: C. GROUP BY clause forms the groups of the data based on the column list specified.
31. Which of the following group functions can be used for population variance and
population standard deviation problems?
A. VAR_POP
B. STDDEV_POP
C. VARIANCE
D. STDDEV_SASMP
Answer: A, B.
32. Select the positions in a SELECT query where a group function can appear.
A. SELECT statement
B. WHERE clause
C. ORDER BY clause
D. GROUP BY clause
Answer: A, C, D. Group functions can appear in SELECT, ORDER BY and HAVING clause. Oracle
raises exception if group functions are used in WHERE or GROUP BY clauses.
33. Examine the structure of the EMPLOYEES table as given. Which query will return the
minimum salary in each department?
SQL> DESC employees
Name
Null?
Type
----------------------- -------- ---------------EMPLOYEE_ID
NOT NULL NUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOT NULL VARCHAR2(25)
EMAIL
NOT NULL VARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOT NULL DATE
JOB_ID
NOT NULL VARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
GROUP BY department_id ;
GROUP BY salary ;
GROUP BY employee_id ;
Answer: B. MIN function returns the minimum salary in a group formed by department.
34. Examine the structure for the table EMPLOYEES and Interpret the output of the
below query
SQL> DESC employees
Name
Null?
Type
----------------------- -------- ---------------EMPLOYEE_ID
NOT NULL NUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOT NULL VARCHAR2(25)
EMAIL
NOT NULL VARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOT NULL DATE
JOB_ID
NOT NULL VARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
SELECT COUNT(*), COUNT(all comm) FROM employees ;
A. It throws error because only one aggregate function can be used in a query.
B. It throws error because GROUP BY clause is missing.
C. It executes successfully and returns same values for both.
D. It executes successfully where COUNT(*) including NULLs and COUNT(all comm) excluding
NULLs.
Answer: D.
35. Which of the following are true about group functions?
A. You can use group functions in any clause of a SELECT statement.
B. You can use group functions only in the column list of the select clause and in the WHERE
clause of a SELECT statement.
C. You can mix single row columns with group functions in the column list of a SELECT
statement by grouping on the single row columns.
D. You can pass column names, expressions, constants, or functions as parameter to an group
function.
Answer: C. Group functions can be nested only to a depth of two. Group functions can be nested
inside single-row functions (AVG embedded in a TO_CHAR function). In addition, single-row
functions can be nested inside group functions.
36. Examine the structure of the table EMPLOYEES as given. You want to create a
A. WHERE
B. SELECT
C. ORDER BY
D. GROUP BY
Answer: D. GROUP BY clause must contain all the columns appearing in the SELECT statement. It
raises error because JOB is not a selected column. It should have used DEPARTMENT_ID in placed
of JOB.
39. Examine the table structure as given.
SQL> DESC employees
Name
Null?
Type
----------------------- -------- ---------------EMPLOYEE_ID
NOT NULL NUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOT NULL VARCHAR2(25)
EMAIL
NOT NULL VARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOT NULL DATE
JOB_ID
NOT NULL VARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
Which of the below SELECT query will display the maximum and minimum salary earned by each
job category?
A. SELECT job, MAX(salary ), MIN (salary ) FROM employees
GROUP BY department_id ;
GROUP BY job;
SELECT department_id
FROM employees
WHERE hiredate > '01-JAN-1985'
AND COUNT(*) > 2
GROUP by department_id
HAVING SUM (salary ) > 1000;
A. It executes successfully and lists the count of employees under each job category but ignores
the HAVING clause since "salary " is not in GROUP BY clause.
B. It throws error because HAVING clause is invalid.
C. It throws error because "salary " is not included in the GROUP BY clause.
D. It executes successfully and lists the count of employees under each category having sum of
salary greater than 5000.
Answer: D. The HAVING clause restricts the group results. COUNT function is used for counting
while SUM is used for adding the numeric values.
42. What is true of using group functions on columns that contain NULL values?
A. Group functions on columns ignore NULL values.
B. Group functions on columns returning dates include NULL values.
C. Group functions on columns returning numbers include NULL values.
D. Group functions on columns cannot be accurately used on columns that contain NULL values.
Answer: A. Except COUNT function, all the group functions ignore NULL values.
43. Which of the following statetments are true about the usage of GROUP BY columns
in a subquery?
A. Subqueries can contain GROUP BY and ORDER BY clauses.
B. Subqueries cannot contain GROUP BY and ORDER BY clauses.
C. Subqueries can contain ORDER BY but not the GROUP BY clause.
D. Subqueries cannot contain ORDER BY but can have GROUP BY clause.
Answer: A. Like the primary query, a subquery can contain a GROUP BY as well as ORDER BY
clause.
Examine the table structure as given and answer the questions 44 to 49 that follow.
SQL> DESC employees
Name
Null?
Type
----------------------- -------- ---------------EMPLOYEE_ID
NOT NULL NUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOT NULL VARCHAR2(25)
EMAIL
NOT NULL VARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOT NULL DATE
JOB_ID
NOT NULL VARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
A. It executes successfully.
B. It gives an error because the HAVING clause is not valid.
C. It gives an error because the GROUP BY expression is not valid.
D. It gives an error because aggregate functions cannot be nested in SELECT statement.
Answer: B. The HAVING clause doesn't allows nesting of aggregate functions.
45. Predict the output of the below query
SELECT avg(salary ), department_id
FROM employees
GROUP BY department_id ;
A. It gives error because an aggregate function cannot appear just after SELECT clause.
B. It gives error because GROUP BY clause is invalid.
C. It executes without errors but produces no output.
D. It executes successfully and gives average salary in each department.
Answer: D. Group functions can be used in any sequence (before or after the group by columns)
in a SELECT query.
46. Predict the output of the below query
SELECT lower(job),avg(salary )
FROM employees
GROUP BY upper(job);
GROUP BY department_id ;
A. It executes successfully and displays average salary of departments higher than 10.
B. It throws error because non aggregated column cannot be used in HAVING clause.
C. It executes successfully but displays wrong result for the departments.
D. It throws error because HAVING clause must be placed before GROUP BY clause.
Answer: A. GROUP BY expressions can be used in HAVING clause to filter out the groups from the
final data set.
49. Predict the output of the below query
SELECT department_id , AVG (salary )
FROM employees
GROUP BY department_id
HAVING (department_id >10 and AVG(salary )>2000);
B. MIN
C. SUM
D. COUNT
Answer: B, D. The group function AVG and SUM can be used with numeric data only.
51. Which of the following statements are true?
A. AVG and SUM can be used only with numeric data types.
B. STDDEV and VARIANCE can be used only with numeric data types.
C. MAX can be used with LONG data type.
D. MAX and MIN cannot be used with LOB or LONG data types.
Answer: A, B, D. The group functions AVG,SUM, VARIANCE and STDDEV can be used with
numeric data only. None of the group functions can be used with LONG data type.
52. Examine the table structure as given.
SQL> DESC employees
Name
Null?
Type
----------------------- -------- ---------------EMPLOYEE_ID
NOT NULL NUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOT NULL VARCHAR2(25)
EMAIL
NOT NULL VARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOT NULL DATE
JOB_ID
NOT NULL VARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
Query - 2
SELECT avg(nvl(comm,0))
FROM employees ;
A. It throws error because GROUP BY column list doesn't matches with SELECT column list.
B. It executes successfully and produces average salary of a job category in each department.
C. It executes successfully and produces average salary for a department in each job category.
D. It throws error because GROUP BY and ORDER BY clause have different list of columns.
Answer: B. Though GROUP BY clause implicitly sorts the groups, the GROUP BY and ORDER BY
clauses can be used together in a query.
57. Which clause should you use to exclude group results in a query using group
functions?
A. WHERE
B. HAVING
C. GROUP BY
D. ORDER BY
Answer: B. HAVING clause is used to restrict the groups.
Examine the table structure as given and answer the questions 58 and 59 that follow.
SQL> DESC employees
Name
Null?
Type
----------------------- -------- ---------------EMPLOYEE_ID
NOT NULL NUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOT NULL VARCHAR2(25)
EMAIL
NOT NULL VARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOT NULL DATE
JOB_ID
NOT NULL VARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
A. It executes successfully.
B. It throws error because HAVING clause precedes the GROUP BY clause.
C. It throws error because HAVING clause uses the aggregate function.
D. It executes but no results are displayed because HAVING clause precedes the GROUP BY
clause.
Answer: A. HAVING clause can precede the GROUP BY clause but it is processed only after the
group results are calculated.
59. Predict the outcome of the below query
A. It returns an error because the BETWEEN operator cannot be used in the HAVING clause.
B. It returns an error because WHERE and HAVING clauses cannot be used in the same SELECT
statement.
C. It returns an error because WHERE and HAVING clauses cannot be used to apply conditions
on the same column.
D. It executes successfully.
Answer: D. The WHERE clause restricts the number of rows participating in group clause
processing.
60. Which statements are true regarding the WHERE and HAVING clauses in a SELECT
statement?
A. The HAVING clause can be used with group functions in subqueries.
B. The WHERE clause can be used to exclude rows after dividing them into groups.
C. The WHERE clause can be used to exclude rows before dividing them into groups.
D. The WHERE and HAVING clauses can be used in the same statement only if they are applied
to different columns in the table.
Answer: A, C. WHERE and HAVING clause can be used together in a query. WHERE excludes the
rows before group processing while HAVING restricts the groups.
Examine the table structure as given and answer the questions 61 and 62 that follow.
SQL> DESC employees
Name
Null?
Type
----------------------- -------- ---------------EMPLOYEE_ID
NOT NULL NUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOT NULL VARCHAR2(25)
EMAIL
NOT NULL VARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOT NULL DATE
JOB_ID
NOT NULL VARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
A. It throws an error because the aggregate functions used in HAVING clause must be in SELECT
list.
B. It throws an error because the HAVING clause appears before GROUP BY clause.
C. It displays the departments whose average salary is greater than the minimum salary of the
department.
D. It displays the departments whose average salary is greater than the minimum salary of the
organization.
Answer: C. Group functions can be used by HAVING clause to filter the groups.
62. Interpret the output of the below query.
SELECT SUM(AVG(LENGTH(first_name )))
FROM employees
GROUP BY department_id ;
A. 1
B. 0
C. NULL
D. Throws error because group functions cannot be applied on DUAL table.
Answer: A. The DUAL table contains single column DUMMY of type CHAR(1) whose value is 'X'.
Based on the below scenario, answer the question from 67 to 74.
An organization has 14 employees who work on fixed salary of 1000. The company recruits 5 new
employees whose salary is not yet fixed by the payroll department. However, during the month
end processing, the HR payroll department generates several reports to reconcile the financial
data of the organization. Examine the table structure as given.
SQL> DESC employees
Name
Null?
Type
----------------------- -------- ---------------EMPLOYEE_ID
NOT NULL NUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOT NULL VARCHAR2(25)
EMAIL
NOT NULL VARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOT NULL DATE
JOB_ID
NOT NULL VARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
A. NULL
B. 14000
C. 19000
D. 0
Answer: B. The SUM function adds the salaries of the employees.
68. What is the output of the below query?
SELECT AVG (salary ) FROM employees ;
A. 1000
B. 736.84
C. NULL
D. 0
Answer: A. The AVG (salary ) function calculates the average of salaries and ignoring the NULL
values. In this case, AVG(salary)=(14*1000)/14=1000.
69. What is the output of the below query?
SELECT AVG (nvl(salary ,0)) FROM employees ;
A. 1000
B. NULL
C. 736.84
D. 0
Answer: C. The AVG(NVL(salary ,0)) gives an alternate value to the NULLs and enables them to
participate in average calculation. In this case, (14*1000)/19 = 736.84.
70. What is the output of the below query?
SELECT VARIANCE (salary ) FROM employees ;
A. 1000
B. 0
C. NULL
D. 204678.36
Answer: B. The VARIANCE (salary ) calculates the variance of salary column values ignoring
NULLs.
71. What is the output of the below query?
SELECT VARIANCE (nvl(salary ,0)) FROM employees ;
A. 1000
B. 0
C. NULL
D. 204678.36
Answer: D. The VARIANCE (NL(salary ,0)) calculates the variance of salary column values
including NULLs.
72. What is the output of the below query?
SELECT STDDEV (salary ) FROM employees ;
A. 1
B. 1000
C. 0
D. NULL
Answer: C. The STDDEV (salary ) calculates the standard deviation of salary column values
ignoring NULLs.
73. What is the output of the below query?
SELECT STDDEV (nvl(salary ,0)) FROM employees ;
A. 0
B. 452.41
C. 1000
D. NULL
Answer: B. The STDDEV (nvl(salary ,0)) calculates the standard deviation of salary column values
including NULLs.
74. What is the output of the below query?
select count(*),count(salary ) from employees ;
A. 19,19
B. 14,19
C. 19,14
D. 14,14
Answer: C. COUNT(*) includes NULLs while COUNT(salary ) ignores NULL values.
Which of the below query will give the department who have more than 5 employees working in it?
A. SELECT department_id
FROM employees
B. SELECT department_id
FROM employees
C. SELECT department_id
FROM employees
GROUP BY employee_id
D. SELECT department_id
FROM employees
GROUP BY department_id
Answer: D.
76. Which of the following are true about the CUBE extension of GROUP BY?
A. Enables performing multiple GROUP BY clauses with a single query.
B. Performs aggregations for all possible combinations of columns included.
C. Performs increasing levels of cumulative subtotals, based on the provided column list.
D. None of the above
Answer: B. CUBE, ROLLUP are the GROUP BY extensions used for OLAP processing. CUBE
aggregates the results whenever a new permutation of column is formed.
Use the following SELECT statement to answer below questions 77 to 82:
1
2
3
4
5
77. Which line of the SELECT statement is used to restrict the number of records the
query processes?
A. 1
B. 3
C. 4
D. 5
Answer: B. WHERE clause is used to restrict the rows before the groups are formed.
78. Which line of the SELECT statement is used to restrict groups displayed in the query
results?
A. 1
B. 3
C. 4
D. 5
Answer: D. HAVING is used to restrict the group results after the group processing is over.
79. Which line of the SELECT statement is used to group data stored in the database?
A. 1
B. 3
C. 4
D. 5
Answer: C. GROUP BY clause uses the group by columns to group the data in the table.
80. Which clause must be included for the query to execute successfully?
A. 1
B. 3
C. 4
D. 5
Answer: C. Because the SELECT clause contains the CUSTOMER# column, it is mandatory to have
GROUP BY clause with the CUSTOMER# column.
81. What is the purpose of using COUNT(*) in the SELECT query?
A. The number of records in the specified tables
B. The number of orders placed by each customer
C. The number of NULL values in the specified tables
D. The number of customers who have placed an order
Answer: B. It counts the number of rows processing under a group. In this case, group is formed
by the customer and COUNT(*) counts the orders placed by each customer.
82. Which of the following functions can be used to determine the earliest ship date for
all orders recently processed by JustLee Books?
A. COUNT function
B. MAX function
C. MIN function
D. STDDEV function
Answer: C. MIN function is used to retrieve the least value of the column. When used with date
columns, it fetches the minimum date from the column.
83. Which of the following is not a valid SELECT statement?
A. SELECT STDDEV(retail) FROM books;
B. SELECT AVG(SUM(retail)) FROM orders NATURAL JOIN orderitems NATURAL JOIN books
GROUP BY customer#;
What is the best explanation as to why this SQL statement will NOT execute?
SELECT department_id "Department", AVG (salary)"Average"
FROM employees
GROUP BY Department;
A. Salaries cannot be averaged as not all the numbers will divide evenly.
89. Which of the below query will display the number of distinct job categories working
in each department?
A. SELECT department_id , COUNT(DISTINCT job) FROM employees
GROUP BY job;
GROUP BY employee_id ;
GROUP BY department_id ;
GROUP BY department_id ;
A. It executes successfully.
B. It throws error because ORDER BY clause is invalid.
C. It throws error because GROUP BY clause is invalid.
D. It throws error because GROUP BY and ORDER BY clause cannot be used together.
Answer: A. ORDER BY clause can use the group functions for sorting.