1. Which of the following is NOT a GROUP BY function?
Answer: C. NVL is a general function used to provide alternate value to the NULL values. The functions MAX, MIN and AVG can be used as GROUP BY functions.
2. Which of the following functions can be used without GROUP BY clause in SELECT query?
Answer: A, B, C, D. All the listed group functions can be used in a query provided no other columns are selected in the SELECT query.
3. Which of the following SELECT query returns the department number with maximum salary compensated to an employee? (Consider 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)
SELECT department_id , max(salary ) FROM employees ;
SELECT department_id , max(salary ) FROM employees GROUP BY department_id ;
SELECT max(salary ) FROM employees GROUP BY department_id ;
SELECT max(salary ) FROM employees ;
Answer: B. The MAX function can be used to return the maximum salary in a department where each group is formed by a department.
4. Which of the following statements are true about the COUNT function?
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?
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;
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;
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;
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.
SELECT COUNT (DISTINCT num) FROM t_count;
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;
Answer: C. COUNT function requires minimum one argument which can be either the column with [ALL | DISTINCT] modifier or '*'.
11. Here are few statements about VARIANCE function in SQL.
i. The function accepts multiple numeric inputs and returns variance of all the values
ii. The function accepts a number column and returns variance of all column values including NULLs
iii. The function accepts a number column and returns variance of all column values excluding NULLs
Chose the correct combination from the below options.Answer: C. The VARIANCE function accepts single numeric argument as the column name and returns variance of all the column values considering NULLs.
12. Which of the following is NOT a GROUP BY extensions in SQL?
Answer: A. GROUPING SETS operations can be used to perform multiple GROUP BY aggregations with a single query.
13. Select the correct statements about the below query. Consider 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)
SELECT department_id , SUM(salary ) FROM employees GROUP BY department_id ;
Answer: A. SUM is a group function which calculates the sum of salaries of a group of employees working in a department.
14. Which clause is used to filter the query output based on aggregated results using a group by function?
Answer: D. HAVING Clause is used for restricting group results. You use the HAVING clause to specify the groups that are to be displayed, thus further restricting the groups on the basis of aggregate information. The HAVING clause can precede the GROUP BY clause, but it is recommended that you place the GROUP BY clause first because it is more logical. Groups are formed and group functions are calculated before the HAVING clause is applied to the groups in the SELECT list.
15. Examine the given table structure and predict the outcome of the following 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(*) FROM employees WHERE comm = NULL;
Answer: B. Excluding out NULLs using WHERE condition is a way to direct the query to ignore NULLs. But here the usage of IS NULL operator is wrong. The condition should be 'WHERE comm IS NULL'.
16. Which of the following statements is true about the group functions?
Answer: C. The AVG function can be only used with numeric values. Other functions which have such restriction are SUM, STDDEV and VARIANCE.
17. Which of the following is a valid SELECT statement?
SELECT AVG(retail-cost) FROM books GROUP BY category;
SELECT category, AVG(retail-cost) FROM books;
SELECT category, AVG(retail-cost) FROM books WHERE AVG(retail-cost) > 8.56 GROUP BY category;
SELECT category, AVG(retail-cost) Profit FROM books GROUP BY category HAVING profit > 8.56;
Answer: A. Column aliases cannot be used in GROUP BY or HAVING clause.
18. Which of the following statements is correct?
Answer: D. Though Oracle doesn't raise error if HAVING clause precedes the GROUP BY clause but it is processed only after the GROUP BY clause is processed and group are ready to be filtered.
19. Which of the following is not a valid SQL statement?
SELECT MIN(pubdate) FROM books GROUP BY category HAVING pubid = 4;
SELECT MIN(pubdate) FROM books WHERE category = 'COOKING';
SELECT COUNT(*) FROM orders WHERE customer# = 1005;
SELECT MAX(COUNT(customer#)) FROM orders GROUP BY customer#;
Answer: A.
20. Which of the following statements is correct?
Answer: C, D. The WHERE clause restricts the rows before they are grouped and processed while HAVING clause restricts the groups.
21. Which of the following is a valid SQL statement?
SELECT customer#, order#, MAX(shipdate-orderdate) FROM orders GROUP BY customer# WHERE customer# = 1001;
SELECT customer#, COUNT(order#) FROM orders GROUP BY customer#;
SELECT customer#, COUNT(order#) FROM orders GROUP BY COUNT(order#);
SELECT customer#, COUNT(order#) FROM orders GROUP BY order#;
Answer: B. The GROUP BY clause must contain all the columns except the one which is used inside the group function.
22. Which of the following SELECT statements lists only the book with the largest profit?
SELECT title, MAX(retail-cost) FROM books GROUP BY title;
SELECT title, MAX(retail-cost) FROM books GROUP BY title HAVING MAX(retail-cost);
SELECT title, MAX(retail-cost) FROM books;
Answer: A.
23. Which of the following statement(s) is/are correct?
1. A group function can be nested inside a group function.
2. A group function can be nested inside a single-row function.
3. A single-row function can be nested inside a group function.
Answer: A, B, 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.
24. Which of the following functions is used to calculate the total value stored in a specified column?
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?
SELECT MAX(retail) FROM books WHERE category = 'FAMILY';
SELECT MAX(retail) FROM books HAVING category = 'FAMILY';
SELECT retail FROM books WHERE category = 'FAMILY' HAVING MAX(retail);
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?
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?
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?
SELECT customer#, SUM(referred) FROM customers GROUP BY customer#;
SELECT COUNT(referred) FROM customers;
SELECT COUNT(*) FROM customers;
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
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?
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?
Answer: A, B.
32. Select the positions in a SELECT query where a group function can appear.
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)
SELECT department_id , MIN (salary ) from EMPLOYEES ;
SELECT department_id , MIN (salary ) from EMPLOYEES GROUP BY department_id ;
SELECT department_id , MIN (salary ) from EMPLOYEES GROUP BY salary ;
SELECT department_id , MIN (salary ) from EMPLOYEES 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 ;
Answer: D.
35. Which of the following are true about group functions?
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 "emp_dept_sales" view by executing the following SQL statements.
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)
CREATE VIEW emp_dept_sales AS SELECT d.department_name , sum(e.salary ) FROM employees e, departments d where e.department_id =d.department_id GROUP by d.department_name ;
Which statement is true regarding the execution of the above statement?
Answer: D. Rules for Performing DML Operations on a View. You cannot add data through a view if the view includes group functions or a GROUP BY clause or DISTINCT keyword. The pseudo column ROWNUM keyword Columns defined by expressions NOT NULL columns in the base tables that are not selected by the view.
37. Which of the following statements are true regarding views?
Answer: C, D. Rules for Performing DML Operations on a View. You cannot add data through a view if the view includes group functions or a GROUP BY clause or DISTINCT keyword. The pseudo column ROWNUM keyword Columns defined by expressions NOT NULL columns in the base tables that are not selected by the view.
38. Examine the table structure as given.
SQL> DESC departments Name Null? Type ----------------------- -------- ---------------- DEPARTMENT_ID NOT NULL NUMBER(4) DEPARTMENT_NAME NOT NULL VARCHAR2(30) MANAGER_ID NUMBER(6) LOCATION_ID NUMBER(4)
Which clause in the below SQL query generates error?
SELECT department_id , avg(salary ) FROM departments WHERE upper(job) in ('SALES','CLERK') GROUP BY job ORDER BY department_id ;
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?
SELECT job, MAX(salary ), MIN (salary ) FROM employees GROUP BY department_id ;
SELECT job, MAX(salary ), MIN (salary ) FROM employees GROUP BY job;
SELECT job, MAX(salary ), MIN (salary ) FROM employees ;
Answer: B. More than one group function can appear in the SELECT statement.
40. Consider 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)
Examine the error in the below query.
SELECT department_id FROM employees WHERE hiredate > '01-JAN-1985' AND COUNT(*) > 2 GROUP by department_id HAVING SUM (salary ) > 1000;
Answer: D. Group functions cannot be used in WHERE clause. The can appear in SELECT, HAVING and ORDER BY clause.
41. 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)
Predict the outcome of the below query
SELECT job, COUNT(employee_id ),sum(salary ) FROM employees GROUP BY job HAVING SUM (salary ) > 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?
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?
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)
44. Predict the outcome of the below query
SELECT avg(max(salary )) FROM employees GROUP BY department_id HAVING avg(max(salary ))>100;
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 ;
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);
Answer: D. The function LOWER, being a single row function must be specified in the GROUP BY clause to base the grouping of EMPLOYEES data.
47. Which of the below query executes successfully?
SELECT employee_id , COUNT(hiredate-sysdate) FROM employees ;
SELECT AVG(salary ), MAX(salary ) FROM employees ;
SELECT AVG(salary ), MAX(salary ) FROM employees GROUP BY department_id ;
SELECT AVG(hiredate) FROM employees ;
Answer: B, C. The first query operates of the whole EMPLOYEES data while the second one processes the data in groups of department.
48. Identify the error in the below SELECT statement.
SELECT department_id , AVG (salary ) FROM employees GROUP BY department_id HAVING department_id > 10;
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);
Answer: C. The HAVING clause can impose multiple conditions joined using AND or OR operator filter the groups.
50. Which of the following group functions can be used with DATE values?
Answer: B, D. The group function AVG and SUM can be used with numeric data only.
51. Which of the following statements are true?
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)
Identify the error in the below query.
SELECT department_id , avg(salary ), count(hiredate) FROM employees GROUP BY department_id ;
Answer: D.
53. Which of the following group function can be used with LOB data types?
Answer: D. No aggregate function can be used with LOB data types.
54. 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)
Predict the output of the below two queries
Query - 1
SELECT avg(comm) FROM employees ;
Query - 2
SELECT avg(nvl(comm,0)) FROM employees ;
Answer: B. The AVG function ignores NULL values while calculating the average of numeric data. AVG(column) will calculate average for only non null values. However, if NVL is used to substitute NULLs with a zero, all the values will be considered.
55. Choose the correct statements about the GROUP BY clause.
Answer: D. As per the processing sequence, the GROUP BY clause must appear after the WHERE clause in a SELECT query.
56. 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)
Predict the outcome of the below query
SELECT department_id ,avg(salary ) FROM employees GROUP BY department_id , job ORDER BY department_id ;
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?
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)
58. Predict the outcome of the below query
SELECT department_id ,avg(salary ) FROM employees HAVING avg(salary )>2000 GROUP BY department_id ORDER BY department_id
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
SELECT department_id , COUNT(first_name ) FROM employees WHERE job IN ('SALESMAN','CLERK','MANAGER','ANALYST') GROUP BY department_id HAVING AVG(salary ) BETWEEN 2000 AND 3000;
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?
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)
61. Predict the outcome of the below query.
SELECT department_id , avg(salary ) FROM employees HAVING avg(salary ) > min(salary ) GROUP BY department_id ;
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 ;
Answer: A. Group functions can be used with single row or general functions in the SELECT query.
63. Up to how many levels, the group functions can be nested?
Answer: B. Group functions can be nested maximum up to 2 levels. However, single row functions can be nested up to any number of levels.
64. What is the limit of number of groups within the groups created by GROUP BY clause?
Answer: D. There is no limit to the number of groups and subgroups that can be formed.
65. Choose the correct statements about the HAVING clause.
Answer: A, C. HAVING clause can only appear in a query if GROUP BY clause is present, but vice versa is not true.
66. What is the output of the below query.
SELECT count(*) FROM dual GROUP BY dummy;
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)
67. What is the output of the below query?
SELECT SUM (salary ) FROM employees ;
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 ;
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 ;
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 ;
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 ;
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 ;
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 ;
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 ;Answer: C. COUNT(*) includes NULLs while COUNT(salary ) ignores NULL values.
75. 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 query will give the department who have more than 5 employees working in it?
SELECT department_id FROM employees WHERE COUNT(*) > 5 GROUP BY department_id ;
SELECT department_id FROM employees HAVING COUNT(*) > 5;
SELECT department_id FROM employees GROUP BY employee_id HAVING COUNT(*) > 5;
SELECT department_id FROM employees GROUP BY department_id HAVING COUNT(*) > 5;
Answer: D.
76. Which of the following are true about the CUBE extension of GROUP BY?
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 SELECT customer#, COUNT(*) 2 FROM customers JOIN orders USING (customer#) 3 WHERE orderdate > '02-APR-09' 4 GROUP BY customer# 5 HAVING COUNT(*) > 2;
77. Which line of the SELECT statement is used to restrict the number of records the query processes?
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?
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?
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?
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?
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?
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?
SELECT STDDEV(retail) FROM books;
SELECT AVG(SUM(retail)) FROM orders NATURAL JOIN orderitems NATURAL JOIN books GROUP BY customer#;
SELECT order#, TO_CHAR(SUM(retail),'999.99') FROM orderitems JOIN books USING (isbn) GROUP BY order#;
SELECT title, VARIANCE(retail-cost) FROM books GROUP BY pubid;
Answer: D. The GROUP BY clause must specify a column or set of columns contained in the SELECT clause. Here PUBID is not contained in the SELECT clause, hence the query is not valid.
84. Which of the below statements are true about the nesting of group functions?
Answer: A, C, D. In an expression containing nested functions, the innermost function is executed first whose result is fed into the next function moving in outwards direction. Single row functions can be well used with group functions which can be maximum nested up to 2 levels.
85. What are the statistical group functions in Oracle?
Answer: B, C. VARIANCE and STATS are the statistical group functions available in Oracle SQL.
86. If the SELECT list contains a column and a group functions, which of the following clause must be mandatorily included?
Answer: C. GROUP BY clause should necessarily contain the column or set of columns contained in the SELECT clause.
87. 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)
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;
Answer: B. Neither GROUP BY clause nor HAVING clause works with column alias.
88. Which of the following data types are compatible with AVG, SUM, VARIANCE, and STDDEV functions?
Answer: A. The functions AVG, SUM, VARIANCE and STDDEV mandatorily work with numeric data type only.
Examine the table structure as given below and answer the questions 89 and 90 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)
89. Which of the below query will display the number of distinct job categories working in each department?
SELECT department_id , COUNT(DISTINCT job) FROM employees GROUP BY job;
SELECT department_id , COUNT(job) FROM employees GROUP BY employee_id ;
SELECT department_id , COUNT(job) FROM employees GROUP BY department_id ;
SELECT department_id , COUNT(DISTINCT job) FROM employees GROUP BY department_id ;
Answer: D. Use DISTINCT modifier to filter out the duplicates.
90. Evaluate this SQL statement:
SELECT employee_id , first_name , department_id , SUM(salary ) FROM employees WHERE salary > 1000 GROUP BY department_id , employee_id , first_name ORDER BY hiredate;
Why will this statement cause an error?
Answer: D. All the columns appearing in SELECT and ORDER BY clause must be included in the GROUP BY clause.
91. Which of the following statements is true about the GROUP BY clause?
Answer: A. Using a WHERE clause, you can exclude rows before dividing them into groups.
92. 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)
Interpret the outcome of the below query.
SELECT department_id , MIN (hiredate) FROM employees GROUP by department_id ;
Answer: C. The query returns the earliest hired employee in each department.
93. Which statement about group functions is true?
Answer: A. All the group functions except COUNT(*), ignore NULL values. It is because they process the values directly contained in a specific column.
94. Which of the following clauses represent valid uses of group functions?
Answer: B, C, D. Group functions can appear in SELECT, HAVING and ORDER BY clauses only.
95. Which of the following statements are true about the GROUP BY clause?
Answer: B. The grouping of data is based on the sequence of columns appearing in the GROUP BY clause.
96. What is difference between WHERE clause and HAVING clause?
Answer: A, B, D. WHERE clause restricts the rows before grouping but HAVING restricts the groups.
97. 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)
Predict the outcome of the below query.
SELECT department_id ,job,count(*) FROM employees GROUP BY department_id ,job ORDER BY department_id ,count(*);
Answer: A. ORDER BY clause can use the group functions for sorting.