SQL Interview Questions
SQL Interview Questions
There is given sql interview questions and answers that has been asked in many companies.
For PL/SQL interview questions, visit our next page.
1) What is SQL?
SQL stands for the Structured Query Language. SQL is a standard query language used for
maintaining the relational database and perform many different operations of data
manipulation on the data. SQL initially was invented in 1970. It is a database language used
for database creation, deletion, fetching rows and modifying rows, etc. sometimes it is
pronounced as 'sequel.'
1. Data definition language (DDL):DDL is used to define the data structure it consists of
the commands like CREATE, ALTER, DROP, etc.
2. Data manipulation language (DML):DML is used to manipulate already existing data
in the database. The commands in this category are SELECT, UPDATE, INSERT, etc.
3. Data control language (DCL):DCL is used to control access to data in the database
and includes commands such as GRANT, REVOKE.
A table contains a specified number of the column called fields but can have any number of
rows which is known as the record. So, the columns in the table of the database are known
as the fields and they represent the attribute or characteristics of the entity in the record.
Primary key carries unique value but the field of the primary key cannot be Null on the
other hand unique key also carry unique value but it can have a single Null value field.
The Database is also called a structured form of data. Due to this structured format, you
can access data very easily.
Database management system is an interface between the database and the user. It makes
the data retrieval, data access easier.
Without the database management system, it would be far more difficult for the user to
access the data of the database.
RDBMS is one of the most often used databases due to its easy accessibility and supports
regarding complex queries.
There are some rules of database normalization which commonly known as Normal From
and they are:
Using these steps, the redundancy, anomalies, inconsistency of the data in the database can
be removed.
o The occurrence of redundant terms in the database which causes the waste of the
space in the disk.
o Due to redundant terms inconsistency may also occur id any change will be made in
the data of one table but not made in the same data of another table then
inconsistency will take place, which will lead to the maintenance problem and effects
the ACID properties as well.
1. Arithmetic operators: addition (+), subtraction (-), multiplication (*), division (/),
etc.
2. Logical operators: ALL, AND, ANY, ISNULL, EXISTS, BETWEEN, IN, LIKE, NOT, OR,
UNIQUE.
3. Comparison operator: =, !=, <>, <, >, <=, >=, !<, !>
o Unique Index
o Clustered Index
o NonClustered Index
o Bit-Map index
o Normal index
o Composite index
o B-tree index
o function based index
27) What is the unique Index?
Unique Index:
For creating a unique index, the user has to check the data in the column because the
unique indexes are used when any column of the table has unique values. This indexing
does not allow the field to have duplicate values if the column is unique indexed. A unique
index can be applied automatically when a primary key is defined.
The clustered index is used to reorder the physical order of the table and search based on
the key values. Each table can have only one clustered index. The Clustered index is the
only index which has been automatically created when the primary key is generated. If
moderate data modification needed to be done in the table then clustered indexes are
preferred.
The reason to create non-clustered index is searching the data. We well know that clustered
indexes are created automatically primary keys are generated, but non-clustered indexes
are created when multiple joins conditions and various filters are used in the query. Non-
Clustered Index does not alter the physical order of the table and maintains logical order of
data. Each table can have 999 non-clustered indexes.
MySQL is available for free as it is open source whereas SQL server is not an open source
software.
31) What is the difference between SQL and PL/SQL?
SQL or Structured Query Language is a language which is used to communicate with a
relational database. It provides a way to manipulate and create databases. On the other
hand, PL/SQL is a dialect of SQL which is used to enhance the capabilities of SQL. It was
developed by Oracle Corporation in the early 90's. It adds procedural features of
programming languages in SQL.
In SQL single query is being executed at once whereas in PL/SQL a whole block of code is
executed at once.
SQL is like the source of data that we need to display on the other hand PL/SQL provides a
platform where the SQL the SQL data will be shown.
SQL statement can be embedded in PL/SQL, but PL/SQL statement cannot be embedded in
SQL as SQL do not support any programming language and keywords.
1. One table can have only one clustered index, but it can have many non-clustered
index. (Approximately 250).
2. A clustered index determines how data is stored physically in the table. Clustered
index stores data in the cluster, related data is stored together, so that retrieval of
data becomes simple.
3. Clustered indexes store the data information and the data itself whereas non-
clustered index stores only the information, and then it will refer you to the data
stored in clustered data.
4. Reading from a clustered index is much faster than reading from non-clustered index
from the same table.
5. Clustered index sort and store data row in the table or view based on their key
value, while non-cluster has a structure separate from the data row.
34) What is the SQL query to display the current date?
There is a built-in function in SQL called GetDate() which is used to return the current
timestamp.
1. Theta join
2. Natural join
3. Equijoin
Inner join returns rows when there is at least one match of rows between the tables. INNER
JOIN keyword joins the matching records from two tables.
INNER JOIN
Right Join is used to retrieve rows which are common between the tables and all rows of a
Right-hand side table. It returns all the rows from the right-hand side table even though
there are no matches in the left-hand side table.
RIGHT JOIN
39) What is Left Join in SQL?
Left Join:
The left join is used to retrieve rows which are common between the tables and all rows of
the Left-hand side table. It returns all the rows from the Left-hand side table even though
there are no matches on the Right-hand side table.
LEFT JOIN
Full join return rows when there are matching rows in any one of the tables. This means it
returns all the rows from the left-hand side table and all the rows from the right-hand side
table.
FULL OUTER JOIN
The IN condition operator is used to check for values contained in a specific set of values. IN
operator is used when we have more than one value to choose.
46) Write an SQL query to find names of employee start with 'A'?
1. SELECT * FROM Employees WHERE EmpName like 'A%'
Atomicity: it requires that each transaction is all or nothing. It means if one part of the
transaction fails, the entire transaction fails and the database state is left unchanged.
Consistency: the consistency property ensure that the data must meet all validation rules.
In simple words you can say that your transaction never leaves your database without
completing its state.
Isolation: this property ensure that the concurrent property of execution should not be
met. The main goal of providing isolation is concurrency control.
Durability: durability simply means that once a transaction has been committed, it will
remain so, come what may even power loss, crashes or errors.
50) What is the difference between NULL value, zero and blank
space?
Ans: A NULL value is not the same as zero or a blank space. A NULL value is a value which
is 'unavailable, unassigned, unknown or not applicable.' On the other hand, zero is a
number, and a blank space is treated as a character.
The NULL value can be treated as unknown and missing value as well, but zero and blank
spaces are different from the NULL value.
Case manipulation function can be used in almost every part of the SQL statement.
Case manipulation functions are mostly used when you need to search for data, and you
don?t have any idea that the data you are looking for is in lower case or upper case.
One or more than one characters and words should be passed into the function, and then
the function will perform its operation on those words.
Instead of NVL() function MySQL have IFNULL() and SQL Server have ISNULL() function.
58) What are the syntax and use of the COALESCE function?
The syntax of COALESCE function:
1. COALESCE(exp1, exp2, .... expn)
The COALESCE function is used to return the first non-null expression given in the
parameter list.