SQL Interview Questions and Frequently Asked Questions 1. The Most Important DDL Statements in SQL Are
SQL Interview Questions and Frequently Asked Questions 1. The Most Important DDL Statements in SQL Are
SQL Interview Questions and Frequently Asked Questions 1. The Most Important DDL Statements in SQL Are
Questions
1. The most important DDL statements in SQL are:
CREATE TABLE - creates a new database table
ALTER TABLE - alters (changes) a database table
DROP TABLE - deletes a database table
CREATE INDEX - creates an index (search key)
DROP INDEX - deletes an index
= Equal
<> or != Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
3. SELECT statements:
4. The SELECT INTO Statement is most often used to create backup copies of
tables or for archiving records.
9. The IN operator may be used if you know the exact value you want to return for
at least one of the columns.
When this clause is used with the DROP command, a parent table can be dropped even
when a child table exists.
DROP TABLE &TABLE NAME; Variable names should start with an alphabet. Here the
table name starts with an '&' symbol.
13. Which system tables contain information on privileges granted and privileges
obtained?
USER_TAB_PRIVS_MADE, USER_TAB_PRIVS_RECD
14. Which system table contains information on constraints on all the tables
created?obtained?
USER_CONSTRAINTS.
16. State true or false. !=, <>, ^= all denote the same operation?
True.
17. State true or false. EXISTS, SOME, ANY are operators in SQL?
True.
20. What is the advantage of specifying WITH GRANT OPTION in the GRANT
command?
The privilege receiver can further grant the privileges he/she has obtained from the owner
to any other user.
START or @.
22. What is the value of comm and sal after executing the following query if the
initial value of ‘sal’ is 10000
UPDATE EMP SET SAL = SAL + 1000, COMM = SAL*0.1;?
23. Which command displays the SQL command in the SQL buffer, and then
executes it?
RUN.
24. What command is used to get back the privileges offered by the GRANT
command?
REVOKE.
26. Which date function is used to find the difference between two dates?
MONTHS_BETWEEN.
LIKE operator.
28. What is the use of the DROP option in the ALTER TABLE command?
IS NULL operator.
30. What are the privileges that can be granted on a table by a user to others?
31. Which function is used to find the largest integer less than or equal to a specific
value?
FLOOR.
32. Which is the subset of SQL commands used to manipulate Oracle Database
structures, including tables?
DESC has two purposes. It is used to describe a schema as well as to retrieve rows from
table in descending order.
Explanation :
The query SELECT * FROM EMP ORDER BY ENAME DESC will display the output
sorted on ENAME in descending order.
34. What command is used to create a table by copying the structure of another
table?
Both will result in deleting all the rows in the table EMP..
1200.
38. What is the parameter substitution symbol used with INSERT INTO command?
&
SQL Injection is when form data contains an SQL escape sequence and injects a new
SQL query to be run.
TRUNCATE commits after deleting entire table i.e., cannot be rolled back. Database
triggers do not fire on TRUNCATE
DELETE allows the filtered deletion. Deleted records can be rolled back or committed.
Database triggers fire on DELETE.
Join is a query, which retrieves related columns or rows from multiple tables.
Self Join - Joining the table with itself.
Equi Join - Joining two tables by equating two common columns.
Non-Equi Join - Joining two tables by equating two common columns.
Outer Join - Joining two tables in such a way that query can also retrieve rows that do not
have corresponding join value in the other table.
Sub-query is a query whose return values are used in filtering conditions of the main
query.
INTERSECT - returns all distinct rows selected by both queries. MINUS - returns all
distinct rows selected by the first query but not by the second. UNION - returns all
distinct rows selected by either query UNION ALL - returns all rows selected by either
query, including all duplicates.
Using ROWID.
CONSTRAINTS
Maintaining data integrity through a set of rules that restrict the values of one or more
columns of the tables based on the values of primary key or unique key of the referenced
table.
SAVEPOINTS are used to subdivide a transaction into smaller parts. It enables rolling
back part of a transaction. Maximum of five save points are allowed.
54. What is difference between CHAR and VARCHAR2? What is the maximum
SIZE allowed for each type?
55. How many LONG columns are allowed in a table? Is it possible to use LONG
columns in WHERE clause or ORDER BY?
Only one LONG column is allowed. It is not possible to use LONG column in WHERE
or ORDER BY clause.
56. What are the pre-requisites to modify datatype of a column and to add a column
with NOT NULL constraint?
59. If unique key constraint on DATE column is created, will it validate the rows
that are inserted with SYSDATE?
Database link is a named path through which a remote database can be accessed.
60. How to access the current value and next value from a sequence? Is it possible to
access the current value in a session before accessing next value?
Sequence name CURRVAL, sequence name NEXTVAL. It is not possible. Only if you
access next value in the session, current value can be accessed.
CYCLE specifies that the sequence continue to generate values after reaching either
maximum or minimum value. After pan-ascending sequence reaches its maximum value,
it generates its minimum value. After a descending sequence reaches its minimum, it
generates its maximum.
NO CYCLE specifies that the sequence cannot generate more values after reaching its
maximum or minimum value.
A View can be updated/deleted/inserted if it has only one base table if the view is based
on columns from one or more tables then insert, update and delete is not possible.
63. If a view on a single base table is manipulated will the changes be reflected on
the base table?
If changes are made to the tables and these tables are the base tables of a view, then the
changes will be reference on the view.
64. Which of the following statements is true about implicit cursors?
1. Implicit cursors are used for SQL statements that are not named.
2. Developers should use implicit cursors with great care.
3. Implicit cursors are used in cursor for loops to handle data processing.
4. Implicit cursors are no longer a feature in Oracle.
66. A developer would like to use referential datatype declaration on a variable. The
variable name is EMPLOYEE_LASTNAME, and the corresponding table and
column is EMPLOYEE, and LNAME, respectively. How would the developer define
this variable using referential datatypes?
1. Use employee.lname%type.
2. Use employee.lname%rowtype.
3. Look up datatype for EMPLOYEE column on LASTNAME table and use that.
4. Declare it to be type LONG.
1. %found
2. %too_many_rows
3. %notfound
4. %rowcount
5. %rowtype
68. If left out, which of the following would cause an infinite loop to occur in a
simple loop?
1. LOOP
2. END LOOP
3. IF-THEN
4. EXIT
1. cursor action_cursor is
2. select name, rate, action
3. into action_record
4. from action_table;
5. There are no errors in this statement.
1. open
2. fetch
3. parse
4. None, cursor for loops handle cursor opening implicitly.
71. What happens when rows are found using a FETCH statement
74. What is the maximum number of handlers processed before the PL/SQL block is
exited when an exception occurs?
1. Only one
2. All that apply
3. All referenced
4. None
77. For which trigger timing can you reference the NEW and OLD qualifiers?
1. Statement and Row 2. Statement only 3. Row only 4. Oracle Forms trigger
1. RAISE_ERROR
2. SQLERRM
3. RAISE_APPLICATION_ERROR
4. RAISE_SERVER_ERROR
82. The CHECK_THEATER trigger of the THEATER table has been disabled.
Which command can you issue to enable this trigger?
1. ALTER TRIGGER check_theater ENABLE;
2. ENABLE TRIGGER check_theater;
3. ALTER TABLE check_theater ENABLE check_theater;
4. ENABLE check_theater;
86. Procedure and Functions are explicitly executed. This is different from a
database trigger. When is a database trigger executed?
87. Which Oracle supplied package can you use to output values and messages from
database triggers, stored procedures and functions within SQL*Plus?
88. What occurs if a procedure or function terminates with failure without being
handled?
1. Any DML statements issued by the construct are still pending and can be committed or
rolled back.
2. Any DML statements issued by the construct are committed
3. Unless a GOTO statement is used to continue processing within the BEGIN section,the
construct terminates.
4. The construct rolls back any DML statements issued and returns the unhandled
exception to the calling environment.
71. BEGIN
72. theater_pck.v_total_seats_sold_overall := theater_pck.get_total_for_year;
73. END;
For this code to be successful, what must be true?
90. A stored function must return a value based on conditions that are determined at
runtime. Therefore, the SELECT statement cannot be hard-coded and must be
created dynamically when the function is executed. Which Oracle supplied package
will enable this feature?
1. DBMS_DDL
2. DBMS_DML
3. DBMS_SYN
4. DBMS_SQL
Method 1:
Will give you a zero if it is a number or greater than zero if not numeric
(actually gives the count of non numeric characters)
Method 2:
select instr(translate('wwww','abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ','XXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXX
XXXXXXXXXXXXXXXXX'),'X') FROM dual;
What are steps required tuning this query to improve its performance?
SELECT a.*
FROM ter.ter_master a
WHERE NOT EXISTS (SELECT b.repno FROM ermast b
WHERE a.repno=b.repno) AND
(a.brepno = 'ALL' or a.repno > a.brepno)
ORDER BY a.repno;
93. What is the difference between Truncate and Delete interms of Referential
Integrity?
DELETE removes one or more records in a table, checking referential Constraints (to
see if there are dependent child records) and firing any DELETE triggers. In the order
you are deleting (child first then parent) There will be no problems.
TRUNCATE removes ALL records in a table. It does not execute any triggers. Also, it
only checks for the existence (and status) of another foreign key Pointing to the
table. If one exists and is enabled, then you will get The following error. This
is true even if you do the child tables first.
ORA-02266: unique/primary keys in table referenced by enabled foreign keys
You should disable the foreign key constraints in the child tables before issuing
the TRUNCATE command, then re-enable them afterwards.
Top
Level: Low
Expected answer : Candidate should mention use of DECLARE statement, a function
must
return a value while a procedure doesn?t have to.
2. What is a mutating table error and how can you get around it?
Level: Intermediate
Expected answer: This happens with triggers. It occurs because the trigger is trying
to update a row it is currently using. The usual fix involves either use of views
or temporary tables so the database is selecting from one while updating the other.
Level: Low
Expected answer: %ROWTYPE allows you to associate a variable with an entire table
row.
The %TYPE associates a variable with a single column type.
4. What packages (if any) has Oracle provided for use by developers?
Level: Intermediate
Expected answer: PL/SQL tables are scalar arrays that can be referenced by a
binary integer. They can be used to hold values for use in later queries
or calculations. In Oracle 8 they will be able to be of the %ROWTYPE designation,
or RECORD.
Level: Low
The DECLARE statement is used in PL/SQL anonymous blocks such as with stand
alone, non-stored PL/SQL procedures. It must come first in a PL/SQL stand alone file if
it is used.
8. What are SQLCODE and SQLERRM and why are they important for PL/SQL
developers?
Level: Intermediate
Expected answer: SQLCODE returns the value of the error number for the last error
encountered. The SQLERRM returns the actual error message for the last error
encountered. They can be used in exception handling to report, or, store in an error log
table, the error that occurred in the code. These are especially useful for the WHEN
OTHERS exception.
Level: Low
Expected answer: Use the %ISOPEN cursor status variable.
Level:Intermediate to high
Expected answer: Use the DBMS_OUTPUT package. Another possible method is to just
use the SHOW ERROR command, but this only shows errors. The DBMS_OUTPUT
package can be used to show intermediate results from loops and the status of variables
as the procedure is executed. The new package UTL_FILE can
also be used.
Level:Intermediate to high
Expected Answer: There are 12 types of triggers in PL/SQL that consist of
combinations of the BEFORE, AFTER, ROW, TABLE, INSERT, UPDATE, DELETE
and
ALL key words:
BEFORE ALL ROW INSERT
AFTER ALL ROW INSERT
BEFORE INSERT
AFTER INSERT etc.
Top
Level: Low
Expected answer: By use of the & symbol. For passing in variables the numbers
1-8 can be used (&1, &2,...,&8) to pass the values after the command into the
SQLPLUS session. To be prompted for a specific variable, place the ampersanded
variable in the code itself:
"select * from dba_tables where owner=&owner_name;" . Use of double
ampersands tells SQLPLUS to resubstitute the value for each subsequent
use of the variable, a single ampersand will cause a reprompt for the
value unless an ACCEPT statement is used to get the value from the user.
2. You want to include a carriage return/linefeed in your output from a SQL script,
how can you do this?
Level: Intermediate
Expected answer: By use of the EXECUTE (short form EXEC) command.
4. How do you execute a host operating system command from within SQL?
Level: Low
Expected answer: By use of the exclamation point "!" (in UNIX and some other OS) or
the HOST (HO) command.
5. You want to use SQL to build SQL, what is this called and give an example
7. You want to group the following set of select returns, what can you group on?
8. What special Oracle feature allows you to specify how the cost based system treats
a SQL statement?
Level: Intermediate to high Expected answer: The COST based system allows the use of
HINTs to control the optimizer path selection. If they can give some example hints such
as FIRST ROWS, ALL ROWS, USING INDEX, STAR, even better.
9. You want to determine the location of identical rows in a table before attempting
to place a unique index on the table, how can this be done?
Level: High Expected answer: Oracle tables always have one guaranteed unique column,
the rowid column. If you use a min/max function against your rowid and then select
against the proposed primary key you can squeeze out the rowids of the duplicate rows
pretty quick. For example: select rowid from emp e where e.rowid > (select min(x.rowid)
from emp x where x.emp_no = e.emp_no); In the situation where multiple columns make
up the proposed key, they must all be used in the where clause.
Level: Low
Expected answer: A Cartesian product is the result of an unrestricted join of two or more
tables. The result set of a three table Cartesian product will have x * y * z number of rows
where x, y, z correspond to the number of rows in each table involved in the join.
11. You are joining a local and a remote table, the network manager complains
about the traffic involved, how can you reduce the network traffic?
Level: High Expected answer: Push the processing of the remote data to the remote
instance by using a view to pre-select the information for the join. This will result in only
the data required for the join being sent across.
Level: Intermediate to high Expected answer: The EXPLAIN PLAN command is a tool
to tune SQL statements. To use it you must have an explain_table generated in the user
you are running the explain plan for. This is created using the utlxplan.sql script. Once
the explain plan table exists you run the explain plan command giving as its argument the
SQL statement to be explained. The explain_plan table is then queried to see the
execution plan of the statement. Explain plans can also be run using tkprof.
15. How do you set the number of lines on a page of output? The width?
Level: Low Expected answer: The SET command in SQLPLUS is used to control the
number of lines generated per page and the width of those lines, for example SET
PAGESIZE 60 LINESIZE 80 will generate reports that are 60 lines long with a line width
of 80 characters. The PAGESIZE and LINESIZE options can be shortened to PAGES and
LINES.
Level: Low
Expected answer: The SET option TERMOUT controls output to the screen. Setting
TERMOUT OFF turns off screen output. This option can be shortened to TERM.
17. How do you prevent Oracle from giving you informational messages during and
after a SQL statement execution?
Level: Low Expected answer: The SET options FEEDBACK and VERIFY can be set to
OFF.