SQL Interview Questions and Answers
Structured Query Language (SQL) developed by IBM in 1970s, It is used to communicate with Databases.
Most of the Databases Management Systems like Oracle, Sybase, MS SQL Server, MySQL, IBM-DB2 etc… using SQL for communication databases.
SQL knowledge is mandatory for Database developers as well as Testers.
1) What are most important DDL Commands in SQL?
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
2) What are the Operators used in SELECT statements?
<> 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) How to INSERT Values into Tables?
INSERT INTO table_name VALUES (value1, value2,….)
INSERT INTO table_name (column1, column2,…) VALUES (value1, value2,….)
4) How to Update a Column Name?
UPDATE table_name SET column_name = new_value WHERE column_name = some_value
5) How to Delete Columns, Rows?
Delete a particular column:
DELETE FROM table_name WHERE column_name = some_value
Delete All Rows:
DELETE FROM table_name or DELETE * FROM table_name
6) Give an usage for BETWEEN … AND?
SELECT column_name FROM table_name WHERE column_name BETWEEN value1 AND value2 The values can be numbers, text, or dates.
7) What is the use of CASCADE CONSTRAINTS?
When this clause is used with the DROP command, a parent table can be dropped even when a child table exists.
8) Why does the following command give a compilation error?
DROP TABLE &TABLE NAME;
Variable names should start with an alphabet. Here the table name starts with an ‘&’ symbol.
9) Which system tables contain information on privileges granted and privileges obtained?
10) Which system table contains information on constraints on all the tables created?obtained?
11) State true or false. EXISTS, SOME, ANY are operators in SQL?
12) What does the following query do?
SELECT SAL + NVL(COMM,0) FROM EMP;?
This displays the total salary of all employees. The null values in the commission column will be replaced by 0 and added to salary.
13) 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.
14) Which command executes the contents of a specified file?
START or @.
15) Which command displays the SQL command in the SQL buffer, and then executes it?
16) What command is used to get back the privileges offered by the GRANT command?
17) Which date function is used to find the difference between two dates?
18) What operator performs pattern matching?
19) What is the use of the DROP option in the ALTER TABLE command?
It is used to drop constraints specified on the table.
20) What operator tests column for the absence of data?
IS NULL operator.
21) What are the privileges that can be granted on a table by a user to others?
Insert, update, delete, select, references, index, execute, alter, all.
22) Which function is used to find the largest integer less than or equal to a specific value?
23) Which is the subset of SQL commands used to manipulate Oracle Database structures, including tables?
Data Definition Language (DDL).
24) What is the use of DESC in SQL?
DESC has two purposes. It is used to describe a schema as well as to retrieve rows from table in descending order.
The query SELECT * FROM EMP ORDER BY ENAME DESC will display the output sorted on ENAME in descending order.
25) What command is used to create a table by copying the structure of another table?
CREATE TABLE .. AS SELECTcommand
To copy only the structure, the WHERE clause of the SELECT command should contain a FALSE statement as in the following.
CREATE TABLE NEWTABLE AS SELECT * FROM EXISTINGTABLE WHERE 1=2;
If the WHERE condition is true, then all the rows or rows satisfying the condition will be copied to the new table.
26) What is the output of the following query SELECT TRUNC(1234.5678,-2) FROM DUAL;?
27) What are the wildcards used for pattern matching.?
_ for single character substitution
% for multi-character substitution.
28) What’s an SQL injection?
SQL Injection is when form data contains an SQL escape sequence and injects a new SQL query to be run.
29) What is difference between TRUNCATE & DELETE?
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.
30) What is a join? Explain the different types of joins?
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.
31) What is the sub-query?
Sub-query is a query whose return values are used in filtering conditions of the main query.
32) What is correlated sub-query?
Correlated sub-query is a sub-query, which has reference to the main query.
33) Explain CONNECT BY PRIOR?
Retrieves rows in hierarchical order eg. select empno, ename from emp where.
34) Difference between SUBSTR and INSTR?
INSTR (String1, String2 (n, (m)),
INSTR returns the position of the m-th occurrence of the string 2 in string1. The search begins from nth position of string1.
SUBSTR (String1 n, m)
SUBSTR returns a character string of size m in string1, starting from n-th position of string1.
35) Explain UNION, MINUS, UNION ALL and INTERSECT?
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.
36) What is ROWID?
ROWID is a pseudo column attached to each row of a table. It is 18 characters long, blockno, rownumber are the components of ROWID.
37) What is the fastest way of accessing a row in a table?
Using ROWID. CONSTRAINTS
38) What is an integrity constraint?
Integrity constraint is a rule that restricts values to a column in a table.
39) What is referential integrity constraint?
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.
Follow me on social media: