SQL Interview Questions
SQL is an English like language consisting of commands to store, retrieve, maintain & regulate access to your database.
SQL*Plus is an application that recognizes & executes SQL commands & specialized SQL*Plus commands that can customize reports, provide help & edit facility & maintain system variables.
JOIN is the form of SELECT command that combines info from two or more tables.
Types of Joins are Simple (Equijoin & Non-Equijoin), Outer & Self join.
Equijoin returns rows from two or more tables joined together based upon a equality condition in the WHERE clause.
Non-Equijoin returns rows from two or more tables based upon a relationship other than the equality condition in the WHERE clause.
Outer Join combines two or more tables returning those rows from one table that have no direct match in the other table.
Self Join joins a table to itself as though it were two separate tables.
Union is the product of two or more tables.
Intersect is the product of two tables listing only the matching rows.
Minus is the product of two tables listing only the non-matching rows.
Order of SQL statement execution
Where clause, Group By clause, Having clause, Order By clause & Select.
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)
What will be the output of the following query?
SELECT REPLACE(TRANSLATE(LTRIM(RTRIM(‘!! ATHEN !!’,’!’), ‘!’), ‘AN’, ‘**’),’*’,’TROUBLE’) FROM DUAL;?
What is the difference between TRUNCATE and DELETE commands?
TRUNCATE is a DDL command whereas DELETE is a DML command. Hence DELETE operation can be rolled back, but TRUNCATE operation cannot be rolled back. WHERE clause can be used with DELETE and not with TRUNCATE.
Which command displays the SQL command in the SQL buffer, and then executes it?
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.
What is the sub-query?
Sub-query is a query whose return values are used in filtering conditions of the main query.
What is correlated sub-query?
Correlated sub-query is a sub-query, which has reference to the main query.
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.
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.
Which is the subset of SQL commands used to manipulate Oracle Database structures, including tables?
Data Definition Language (DDL).
What command is used to create a table by copying the structure of another table?
CREATE TABLE .. AS SELECT command
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.
TRUNCATE TABLE EMP;
DELETE FROM EMP;
Will the outputs of the above two commands differ?
Both will result in deleting all the rows in the table EMP..
What is the output of the following query SELECT TRUNC(1234.5678,-2) FROM DUAL;?
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.
Why is it better to use an integrity constraint to validate data in a table than to use a stored procedure ?
Because an integrity constraint is automatically checked while data is inserted into a table. A stored has to be specifically invoked.