Restricting and Sorting Data Questions
Restricting and Sorting Data Questions
http://www.tutorialspoint.com/sql_certificate/restricting_and_sorting_data_questions.htm
Copyright tutorialspoint.com
1. Which of the following clause is used to limit the number of rows retrieved from a
SELECT query?
A. LIMIT
B. WHERE
C. AND
D. FROM
Answer: B. The WHERE clause is used to restrict the number of rows returned from a SELECT
query.
2. Choose the database elements whose values can be compared in a WHERE clause of a
SELECT query.
A. Column
B. Sequence
C. Procedure
D. Literal
Answer: A, D. The WHERE clause can be used to compare the values from columns, literals,
arithmetic functions and functions.
3. What are the elements NOT contained in the WHERE clause predicate of the SELECT
query?
A. Comparison operator
B. Comparison condition
C. Column Name
D. Table Name
4. Which of the following values can NOT be returned after evaluation of WHERE clause
condition?
A. UNKNOWN
B. TRUE
C. FALSE
D. NULL
Answer: A. If the result of the condition in WHERE clause is not known, NULL is returned. In all
other scenarios, either TRUE or FALSE is returned.
5. What is the minimum number of WHERE clauses that must be present in a SELECT
query?
A. 1
B. 2
C. 0
D. 3
Answer: C. The WHERE clause is an optional clause in the SELECT query which is only used to
restrict the number of rows.
6. What is the maximum number of WHERE clauses that can be included in a SELECT
query?
A. 1
B. 2
C. 0
D. 3
Answer: A. The WHERE clause is an optional clause in the SELECT query which can be used only
once to restrict the number of rows.
7. Which of the following statements are correct about the WHERE clause?
Answer: C. The WHERE clause must have comparison operator to evaluate the condition. It can
use function as one of the operand. Only one WHERE clause is allowed in a SELECT query.
8. Write a SELECT query to list down unique departments from EMP table?
Answer: B & C. The keyword DISTINCT is used to filter out the duplicate rows from the SELECT
query.
9. Which of the following operations are permitted for date and timestamp columns?
A. Division
B. Addition
C. Subtraction
D. Concatenation
Answer: B, C, and D. Addition, subtraction and Concatenation are the operations permitted for
date and timestamp columns.
10. From the below operators, which one of them holds the highest precedence level?
A. Division /
B. Multiplication
C. Brackets ( )
D. Subtraction
Answer: C. Expressions within the brackets hold the highest precedence level.
Answer: C. The expression sysdate hiredate returns the number of employment days of an
employee with the company.
12. Which of the below statements correctly describle the DUAL table in Oracle?
Answer: B, C, D. The DUAL table in Oracle is owned by SYS and contains one column DUMMY of
type VARCHAR21.
Answer: B. Subtraction between two dates results in numeric difference of days between the two
dates
Answer: B.
Answer: A, D. Use NVL function to provide an alternate value to a column when NULL.
17. What does the selection of columns in a SELECT statement known as?
A. Retrieval
B. Selection
C. Projection
D. Limiting
Answer: C. Projection is the ability to select only the required columns in SELECT statement.
18. What does the restriction of rows returned by a SELECT statement known as
A. Retrieval
B. Projection
C. Restricting
D. Selection
Answer: C. Restricting is the ability to limit the number of rows by putting certain conditions.
19. Which of the following is true about the query given below?
A. All the rows for the column COL1 will be sorted in the Descending order.
B. All the rows for the column COL1 will be sorted in the Ascending order.
C. The query will give an error as there is no WHERE clause in the query.
D. The query will give an error as the ORDER BY clause should include all the columns in the
SELECT clause.
Answer: B. By default, the ORDER BY clause sorts the values in ascending order.
20. Which of the following is true about the SQL query given below?
SELECT col1,col2
FROM tab1
WHERE col1 = 'A'
ORDER BY col2 DESC, col1;
A. It will display the row which has the col1 value as 'A' ordered by the col1 in the descending
order and then col2 in the descending order.
B. The ORDER BY clause will not work as the keyword DESC should be always written in the end
of the ORDER BY clause and not in between as given in the query.
C. The above query will be sorted in descending order on the basis of col2 only and the use of
col1 in the ORDER BY clause will be discarded.
D. It will display the row which has the col1 value as 'A' ordered by the col1 and then followed
by col2 as the execution of the ORDER BY clause happens from the order of columns in the
SELECT statement.
Answer: C. Since the COL1 is already filtered and fixed in the query as a scalar value, no sorting
will happen on the basis of COL1.
A. It executes successfully
B. It gives the required result with the COL2 value as 'B' but no ordering by the columns COL1
and COL2 because ORDER BY clause appears before the WHERE clause.
C. It will display the row which has the COL2 value as 'B' ordered by the COL1, COL2.
D. It throws an error as the ORDER BY clause cannot be written before the WHERE clause in
Oracle.
Answer: D. The ORDER BY clause must appear after the WHERE clause in the SELECT statement
22. Which two clauses of the SELECT statement are necessary for Selection and
Projection?
A. SELECT, FROM
B. ORDER BY, WHERE
C. SELECT, WHERE
D. SELECT, ORDER BY
Answer: C.
23. Which of the following WHERE clauses will NOT fit in the below SELECT query?
A. WHERE DNAME IN ;
B. WHERE DNAME LIKE '%er%';
C. WHERE DNAME BETWEEN 'e' AND 'r';
D. WHERE DNAME CONTAINS 'e%r';
Answer: B. The LIKE operator is used to perform wild card search in SQL queries.
25. Which two of the following conditions are equivalent to each other?
Answer: A, D. The NOT operator can be used to negate the effect of its operand. Therefore
COMMISNULL is equivalent to NOT(COMMISNOTNULL).
A. SELECT, FROM
B. SELECT,FROM,WHERE
C. SELECT,WHERE
D. SELECT,WHERE,ORDER BY
Answer: A. SELECT and FROM are the mandatory clauses in a SELECT query.
27. Which three of the following WHERE clause conditions are equivalent to each other?
Answer: A, C, D. The conditions can be made equivalent with the use of IN, BETWEEN and
relational operators
28. Which of the following is true with respect to the below query?
A. It fetches the employee id, name and job of those employees who have 'ith' appearing
anywhere in their name.
B. It fetches the employee id, name and job of those employees whose name starts with 'ith'.
C. The query throws an error as two expressions for string matching cannot be written together.
D. It fetches the employee id, name and job of those employees whose name starts with any
alphanumeric character followed by 'ith' and any alphanumeric characters after 'ith'.
Answer: D.
A. :
B. ;
C. .
D. /
Answer: B, D. A semicolon ; or backslash / is used to terminate a query in SQL* Plus and SQL
Developer.
30. The employees JAMES and MILLER want to know their department id by querying
the database. Which of the following queries will give the required result?
D. SELECT ename, deptno FROM emp WHERE ename = 'JAMES' OR ename = 'MILLER'
Answer: D. Multiple conditions can be joined using OR clause. Query execution is successful if
either of the two is true.
A. The WHERE can compare values in columns, literal, arithmetic expressions, or functions.
B. The WHERE clause contains column name
C. Column aliases can be used in the WHERE clause.
D. The WHERE clause cannot contain list of values or constants.
Answer: C, D.
A. DD-MON-YY
B. DD-MON-YYYY
C. DD-MM-RR
D. DD-MON-RR
34. You need to display the names of all the employees having the first name as
"GARRY" from the EMPLOYEES table. Which of the following queries will fulfill the
requirement?
A. SELECT first_name FROM employees WHERE first_name LIKE 'GARRY%';
Answer: C. Wild Cards can be used if certain characters of the search string are unknown.
35. You need to display the employee ID of all the employees who contain a letter 's' in
their last name at second position and department ID as 100. Which of the following
queries will fetch the required results?
A. SELECT emp_id FROM employees WHERE dept_id = 100 AND last_name LIKE '%s%';
B. SELECT emp_id FROM employees WHERE dept_id = 100 AND last_name LIKE '%s_';
C. SELECT emp_id FROM employees WHERE dept_id = 100 AND last_name LIKE '_s_%';
D. SELECT emp_id FROM employees WHERE dept_id = 100 AND last_name LIKE '_s%';
A. The first name, last name and the department ID for all the employees who joined in the year
1998 will be displayed.
B. The first name, last name and the department ID for all the employees who joined in the year
2098 will be displayed.
C. No results will be returned.
D. The first name, last name and the department ID for all the employees who joined in the year
1998 between 1st January, 1998 and 31st December, 1998 will be displayed.
Answer: D. The LIKE operator is used to perform wild card search on character and date literals.
37. Which of the following is used to get rows based on a range of values?
A. UNION ALL
B. IN
C. BETWEEN
D. LIKE
Answer: C. The BETWEEN operator is used to retrieve rows based on range of values.
38. You need to display the employee IDs of the employees who have their salaries
between 20000 inclusive and 50000inclusive. Which of the following queries will fetch the
required results?
A. SELECT emp_id FROM employees WHERE salary >=20000 AND salary <=50000;
D. SELECT emp_id FROM employees WHERE salary between 20000 AND 50000;
Answer: A, D. For larger ranges of values, BETWEEN and relational operators are best suited in
the queries. IN operator is not recommended for large range of values.
A. It will display all the employees having last names starting with the alphabets 'B' till 'E'
inclusive of B and exclusive of E.
B. It will throw an error as BETWEEN can only be used for Numbers and not strings.
C. It will display all the employees having last names starting from 'B' and ending with 'E'.
D. It will display all the employees having last names in the range of starting alphabets as 'B'
and 'E' excluding the names starting with 'B' and 'E'.
Answer: A. The BETWEEN operator works with the range of character values also.
A. It will show all the employees who are under the managers having IDs in the range starting
from 100 to 300.
B. It will show all the employees who are under the managers having IDs 100, 200 or 300.
C. It will throw an error as the manager IDs should be put in quotes.
D. It will throw an error as the sorting of manager_id in the WHERE clause conflicts with the
ORDER BY clause.
Answer: B. The IN operator can be used to provide small and limited number of range.
A. BETWEEN
B. LIKE
C. IS NULL
D. IN NOTIN
Answer: D. The IN operator defines a Membership condition which may use a range of values or a
subquery.
42. Which of the following data types can be used within IN operator?
A. VARCHAR2
B. NUMBER
C. DATE
D. ALL
A. SELECT emp_id, last_name, first_name FROM employees WHERE first_name LIKE 'Bryan%'
OR first_name LIKE 'Jason%';
Answer: C, D. The IN operator checks for ANY values defined as membership condition.
44. You need to extract details of those departments whose name contains the string
'_DXX'. Which of the below WHERE clauses could be used in the SELECT statement to
get the required output?
Answer: B.
45. Which statement is true regarding the default behavior of the ORDER BY clause?
Answer: A. The ORDER BY clause does a case sensitive sorting with character values.
46. You need to generate a report of all employees from the EMPLOYEES table based on
the following conditions: 1. The Employee first name should not begin with 'T' or 'N'. 2.
The Employee's salary should be more than 20000. 3. The Employee should have been
hired after 1st January 2010. Which WHERE clause would give the required result?
A. WHERE first_name NOT LIKE 'T%' OR first_name NOT LIKE 'N%' AND salary > 20000 AND
hire_date > '1-JAN-10'
B. WHERE firstn ameNOTLIKE TOR salary > 20000 OR hire_date > '1-JAN-10'
C. WHERE first_name NOT LIKE 'T%' AND first_name NOT LIKE 'N%' AND salary > 20000 AND
hire_date > '1-JAN-10'
D. WHERE firstn ameNOTLIKE ANDsalary > 20000ANDhired ate > 1 JAN 10
Answer: C.
47. Using the EMPLOYEES table, you need to display the names of all employees hired
after January 1, 2013, starting with the freshers. Which query would give the required
result? Chooseallthatapply.
A. SELECT first_name, hire_date FROM employees WHERE hire_date > '01-JAN-13' ORDER BY
2 DESC;
B. SELECT first_name, hire_date FROM employees WHERE hire_date > '01-JAN-13' ORDER BY
first_name DESC;
C. SELECT first_name, hire_date FROM employees WHERE hire_date > '01-JAN-13' ORDER BY
1 DESC;
D. SELECT first_name, hire_date "START DATE" FROM employees WHERE hire_date > '01-
JAN-13' ORDER BY "START DATE" DESC;
Answer: A, D.
48. Using the EMPLOYEES table, you need to find out the names and salaries of all the
employees hired in departments 100 and 101 in the time interval 15th March '12 to
15th October '13. Which two queries would give the required result? Choosetwo.
A. SELECT first_name, salary FROM employees WHERE dept_id IN (100,101) AND hire_date
BETWEEN '15-MAR-12' AND '15-OCT-12';
B. SELECT first_name, salary FROM employees WHERE dept_id = 100 OR dept_id =101 AND
hire_date >='15-MAR-12' OR hire_date <='15-OCT-12';
C. SELECT first_name, salary FROM employees WHERE (dept_id BETWEEN 100 AND 101) AND
(hire_date IN ('15-MAR-12','15-OCT-12'));
D. SELECT first_name, salary FROM employees WHERE (dept_id = 100 OR dept_id =101) AND
(hire_date >='15-MAR-12' AND hire_date <='15-OCT-12');
Answer: A, D.
49. Using the EMPLOYEES table, you issue the following query to generate the names,
current salary and the salary increased after an appraisal by 25%. The increased salary
for all the employees should be above 30000.
The query throws an error ORA-00904. What is the reason for the error?
Answer: C. A column alias cannot be used in WHERE clause conditions but can be used in SELECT
statement and ORDER BY clause.
50. You need to display employee names from the EMPLOYEES table that belong to the
Department id 100 with minimum salary as either 2000 or 4000 and no job_id. You
issue the following query.
Answer: A. The condition salary = 2000ORsalary = 4000 results in FALSE because an employee cannot
held multiple salaries at a time.
51. Which three tasks can be performed using SQL functions built into Oracle Database?
Choosethree.
52. You need to generate a report that displays the IDs of all employees in the
EMPLOYEES table whose salary is at least 25% more than the value 20000. The details
should be displayed in the descending order of the salary. You issue the following
query.
SELECT emp_id
FROM employees
WHERE salary>=20000*0.25
ORDER BY salary*0.25 DESC;
Dates are stored in the default date format dd-mon-rr in the TRAININGS table. Which
three SQL statements would execute successfully? Choosethree.
Answer: A, C, D.
54. Which of the following statements is/are true with respect to the below query?
A. ORDER BY clause should contain only those columns which are in the SELECT statement.
B. The above query will sort the result set in descending order.
C. ORDER BY clause can contain any column in the related table, not necessarily the columns in
the SELECT statement.
D. It throws an error on execution.
Answer: C. The ORDER BY clause can use a column to sort the data which is not selected in the
column list but is contained in the table used in FROM clause.
A. ORDER BY clause should contain only those columns which are in the SELECT statement.
B. The above query will sort the result set in descending order of first names of employees.
C. ORDER BY clause works with column aliases.
D. The SELECT query throws an error on execution because column alias cannot be used in
ORDER BY clause.
Answer: C. The ORDER BY clauses works fine with the column aliases used in SELECT statement.
Answer: A. Numeric position of the column can be used in the ORDER BY clause.
57. You need to list the employees details for different jobs but only one at a time.
Which of the following is an easier way to achieve the same in SQL* Plus?
Answer: C. The &X notation haults the query execution and prompts for user input every time the
query is executed.
58. Which of the following statements is true regarding substitution variables in SQL?
A. The same query can be executed for different values using the substitution variables.
B. Using the substitution variables, one needs to alter the WHERE clause every time.
C. Substitution variables are not supported in Oracle.
D. There is a limitation that a value is supposed to be entered in the substitution variables every
time during the execution of a query.
Answer: A.
A. VARCHAR2
B. DATE
C. NO DATA TYPE
D. NUMBER
Answer: C. Substitution variables do not have the data type of their own but comply with the
column's data type with whom they are used.
A. The value entered in the variables remains constant and the user cannot change the values
after the execution of the query for the first time.
B. The value is stored in the variables after the query executes once.
C. The substitution variables only support NUMBERS.
D. The value stored in the substitution variables usingasingleampersand is used for the first
execution and gets discarded.
Answer: D.
61. Which of the following is a correct syntax for Substitution variables in SQL* Plus?
A. :var
B. $var
C. &var
D. &&var
Answer: C, D.
62. Which of the following Substitution variables will take the entered value once and
then keeps it for the rest of the session?
A. &&var
B. &var
C. :var
D. ::var
Answer: A. A substitution variable with double ampersand repeatedly uses the value once
provided by the user.
Answer: C.
Answer: B, D.
65. Choose the statements which hold true about the query given below.
A. It throws an error as the use of the substitution variable prompt_col is not allowed.
B. It executes successfully but the result set is not sorted.
C. It executes successfully but the variable value entered in the SELECT statement is ignored.
D. It executes successfully and the value of the substitution variable is maintained throughout
the session.
Answer: D. A substitution variable can be used in all the clauses of SQL query.
66. Which of the following commands is used to create and assign a value to a
substitution variable in SQL* Plus?
A. &var
B. &&var
C. SET
D. DEFINE
Answer: D. Use DEFINE command in SQL* Plus to declare a substitution variable in a session.
67. What will be the outcome of the below activity in SQL* Plus?
A. The SELECT query throws error as substitution variables cannot be defined in the session.
B. It prompts the user to enter the value for the variable &eid.
C. It executes successfully with the employee ID substituted as 117.
D. It ignores the DEFINE command because the substitution variable is declared without
ampersand & sign.
Answer: C.
68. What is the command to remove the value of the substitution variable set by the
command DEFINE?
A. UNDEFINE
B. SET OFF
C. DELETE
D. CLEAR
Answer: A. Use UNDEFINE command to delete a substitution variable from the session
69. Which of the following commands is used to check the substitution variables values
before and after execution of an SQL query?
A. DEFINE
B. UNDEFINE
C. SHOW VARIABLE
D. VERIFY
Answer: D.Use VERIFY command in SQL*Plus and SQL Developer to check the substitution of
values using substitution variables.
70. Which of the following are valid operators for the WHERE clause?
A. >=
B. IS NULL
C. !=
D. IS LIKE
Answer: A, B, C.
Answer: C.
72. Which of the below WHERE clause predicates will correctly list the employees from
department 20?
A. WHERE deptno IS 20
B. WHERE deptno 20
C. WHERE deptno=20
D. WHERE 20=deptno
Answer: C, D. The equality operator = is used to compare the operands in the condition for
equality.
73. Write a SELECT query to list the employees whose salary is greater than 1000.
A. SELECT ename, sal FROM emp WHERE sal GREATER THAN 1000
D. SELECT ename, sal FROM emp WHERE sal MORE THAN 1000
Answer: B. The greater than operator > is used to compare the operands in the condition.
74. What would happen when the below query is executed in SQL* Plus?
A. Executes successfully and lists the employees whose 10th part of salary is greater than 10
times his department number
B. Raises error because expressions must be enclosed with parentheses
C. Raises error because WHERE clause cannot evaluate expressions
D. Raises error because WHERE clause cannot use literals
A. WHERE clause cannot refer the column JOB since it doesn't appears in the SELECT column
list
B. Character literal CLERK must be enclosed within single quotes
C. Character literal CLERK must be enclosed within parentheses
D. No error in the query
A. The query lists the employees whose hiredate is atleast 100 days earlier than the current
date
B. The query lists the employees who have worked more than 100 days in the company
C. The query lists the employees whose hiredate is after 100 days in that year
D. The query lists the employees who have spent less than 100 days in the company
77. Which of the following query will display the employees which are hired after 31st
Decemeber, 1982?
78. Which of the following WHERE conditions will list employees who were hired on
current date?
A. WHERE sysdate-hiredate=0
B. WHERE sysdate=hiredate
C. WHERE sysdate-hiredate<1
D. WHERE to_date sysdate, DD MON YYYY = to_date hiredate = DD MON YYYY
Answer: C, D. The condition SYSDATE=HIREDATE will not work because SYSDATE contains
dynamic timestamp component while hiredate is a static value in the database.
79. What of the following are the valid formats of date literals which can be used in
WHERE clause?
A. 24/Mar/95
B. 02-12-1983
C. 19-JUN-2001
D. 31.04.2010