SQL Interview Questions and Answers
SQL Interview Questions and Answers
The following are the most popular and useful SQL interview questions and answers
for fresher and experienced candidates. These questions are created specifically to
familiarise you with the types of questions you might encounter during your SQL
interview. According to our experiences, good interviewers rarely plan to ask any
specific topic during the interview. Instead, questioning usually begins with a basic
understanding of the subject, and based on your responses, further discussion
happened.
1) What is SQL?
SQL stands for the Structured Query Language. It is the standard language used to
maintain the relational database and perform many different data manipulation
operations on the data. SQL was initially invented in 1970. It is a database language
used for database creation, deletion, fetching and modifying rows, etc. sometimes, it
is pronounced as 'sequel.' We can also use it to handle organized data comprised of
entities (variables) and relations between different entities of the data.
46.2M
856
HTML Tutorial
o Data definition language (DDL): It defines the data structure that consists of
commands like CREATE, ALTER, DROP, etc.
o Data manipulation language (DML): It is used to manipulate existing data in the
database. The commands in this category are SELECT, UPDATE, INSERT, etc.
o Data control language (DCL): It controls access to the data stored in the database.
The commands in this category include GRANT and REVOKE.
o Transaction Control Language (TCL): It is used to deal with the transaction
operations in the database. The commands in this category are COMMIT, ROLLBACK,
SET TRANSACTION, SAVEPOINT, etc.
Example
Example
GRANT: It enables system administrators to assign privileges and roles to the specific
user accounts to perform specific tasks on the database.
REVOKE: It enables system administrators to revoke privileges and roles from the user
accounts so that they cannot use the previously assigned permission on the database.
Example
Fields are the components to provide the structure for the table. It stores the same
category of data in the same data type. A table contains a fixed number of columns
but can have any number of rows known as the record. It is also called a column in the
table of the database. It represents the attribute or characteristics of the entity in the
record.
Example
Table: Student
1. CONSTRAINT constraint_name]
2. FOREIGN KEY [foreign_key_name] (col_name, ...)
3. REFERENCES parent_tbl_name (col_name,...)
The primary key act as a unique identifier for The unique key is also a unique identifier for records when the
each record in the table. primary key is not present in the table.
We cannot store NULL values in the primary We can store NULL value in the unique key column, but only one
key column. NULL is allowed.
We cannot change or delete the primary key We can modify the unique key column values.
column values.
o Software
o Data
o Procedures
o Database Languages
o Query Processor
o Database Manager
o Database Engine
o Reporting
There are some rules of database normalization, which is commonly known as Normal
From, and they are:
Using these steps, the redundancy, anomalies, inconsistency of the data in the
database can be removed.
19) What is the primary use of Normalization?
Normalization is mainly used to add, delete or modify a field that can be made in a
single table. The primary use of Normalization is to remove redundancy and remove
the insert, delete and update distractions. Normalization breaks the table into small
partitions and then links them using different relationships to avoid the chances of
redundancy.
The occurrence of redundant terms in the database causes the waste of space in the
disk.
Due to redundant terms, inconsistency may also occur. If any change is made in the
data of one table but not made in the same data of another table, then inconsistency
will occur. This inconsistency will lead to the maintenance problem and effects the
ACID properties as well.
The primary use of a view is to implement the security mechanism. It is the searchable
object where we can use a query to search the view as we use for the table. It only
shows the data returned by the query that was declared when the view was created.
o Unique Index
o Clustered Index
o Non-Clustered Index
o Bit-Map Index
o Normal Index
o Composite Index
o B-Tree Index
o Function-Based Index
Example
Suppose we want to make a Phone column as a unique index. We can do this like
below:
The purpose of creating a non-clustered index is for searching the data. Its best
example is a book where the content is written in one place, and the index is at a
different place. We can create 0 to 249 non-clustered indexes in each table. The non-
clustered indexing improves the performance of the queries which use keys without
assigning the primary key.
30) What are the differences between SQL, MySQL, and SQL
Server?
The following comparison chart explains their main differences:
SQL or Structured Query Language MySQL is the popular database SQL Server is an RDBMS database
is useful for managing our management system used for system mainly developed for the
relational databases. It is used to managing the relational database. It Windows system to store, retrieve,
query and operate the database. is a fast, scalable, and easy-to-use and access data requested by the
database. developer.
SQL first appeared in 1974. MySQL first appeared on May 23, SQL Server first appeared on April
1995. 24, 1989.
SQL was developed by IBM MySQL was developed by Oracle SQL Server was developed by
Corporation. Corporation. Microsoft Company.
SQL is a query language for MySQL is database software that SQL Server is also a software that
managing databases. uses SQL language to conduct with uses SQL language to conduct with
the database. the database.
SQL has no variables. MySQL can use variables constraints SQL Server can use variables
and data types. constraints and data types.
SQL is a programming language, MySQL is software, so it gets SQL Server is also software, so it
so that it does not get any updates. frequent updation. gets frequent updation.
Its commands are always fixed and
remain the same.
SQL PL/SQL
SQL is a database structured query language PL/SQL or Procedural Language/Structured Query Language is
used to communicate with relational a dialect of SQL used to enhance the capabilities of SQL. Oracle
databases. It was developed by IBM Corporation developed it in the early 90's. It uses SQL as its
Corporations and first appeared in 1974. database language.
SQL has no variables. PL/SQL can use variables constraints and data types.
SQL can execute only a single query at a time. PL/SQL can execute a whole block of code at once.
SQL query can be embedded in PL/SQL. PL/SQL cannot be embedded in SQL as SQL does not support
any programming language and keywords.
SQL can directly interact with the database PL/SQL cannot directly interact with the database server.
server.
SQL is like the source of data that we need to PL/SQL provides a platform where SQL data will be shown.
display.
Clustered indexes store the data information and the Non-clustered indexes stores only the information,
data itself. and then it will refer you to the data stored in clustered
data.
There can only be one clustered index per table. There can be one or more non-clustered indexes in a
table.
A clustered index determines how data is stored It creates a logical ordering of data rows and uses
physically in the table. Therefore, reading from a pointers for accessing the physical data files. Therefore,
clustered index is faster. reading from a clustered index is slower.
A clustered index always contains an index id of 0. A non-clustered index always contains an index id>0.
Indexing is a method to get the requested data very fast. There are mainly two types
of indexes in SQL, clustered index and non-clustered index. The differences between
these two indexes are very important from an SQL performance perspective. The
following comparison chart explains their main differences:
35) Which are joins in SQL? Name the most commonly used
SQL joins?
SQL joins are used to retrieve data from multiple tables into a meaningful result set. It
is performed whenever you need to fetch records from two or more tables. They are
used with SELECT statement and join conditions.
o INNER JOIN
o SELF JOIN
o LEFT OUTER JOIN
o RIGHT OUTER JOIN
o FULL OUTER JOIN
o CROSS JOIN
1. SELECT column_lists
2. FROM table1
3. INNER JOIN table2 ON join_condition1
4. INNER JOIN table3 ON join_condition2
5. ...;
1. SELECT colum_lists
2. FROM table1
3. RIGHT JOIN table2
4. ON join_condition;
1. SELECT colum_lists
2. FROM table1
3. LEFT JOIN table2
4. ON join_condition;
SQL triggers have two main components one is action, and another is an event. When
certain actions are taken, an event occurs as a result of those actions.
We use the CREATE TRIGGER statement for creating a trigger in SQL. Here is the syntax:
A SELF JOIN is required when we want to combine data with other data in the same
table itself. It is often very useful to convert a hierarchical structure to a flat structure.
1. SELECT column_lists
2. FROM table1 AS T1, table1 AS T2
3. WHERE join_conditions;
Example
If we want to get retrieve the student_id and name from the table where student_id is
equal, and course_id is not equal, it can be done by using the self-join:
A. UNION: It combines two or more results from multiple SELECT queries into a single
result set. It has a default feature to remove the duplicate rows from the tables. The
following syntax illustrates the Union operator:
B. UNION ALL: This operator is similar to the Union operator, but it does not remove
the duplicate rows from the output of the SELECT statements. The following syntax
illustrates the UNION ALL operator:
1. SELECT columns FROM table1
2. UNION ALL
3. SELECT columns FROM table2;
C. INTERSECT: This operator returns the common records from two or more SELECT
statements. It always retrieves unique records and arranges them in ascending order
by default. Here, the number of columns and data types should be the same. The
following syntax illustrates the INTERSECT operator:
D. MINUS: This operator returns the records from the first query, which is not found
in the second query. It does not return duplicate values. The following syntax illustrates
the MINUS operator:
This operator is used to selects the range of data It is a logical operator to determine whether or not a specific
between two values. The values can be value exists within a set of values. This operator reduces the
numbers, text, and dates as well. use of multiple OR conditions with the query.
It returns records whose column value lies in It compares the specified column's value and returns the
between the defined range. records when the match exists in the set of values.
The following syntax illustrates this operator: The following syntax illustrates this operator:
SELECT * FROM table_name SELECT * FROM table_name
WHERE column_name BETWEEN 'value1' AND WHERE column_name IN ('value1','value 2');
'value2';
Column Level Constraints: These constraints are only applied to a single column and
limit the type of data that can be stored in that column.
Table Level Constraints: These constraints are applied to the entire table and limit
the type of data that can be entered.
46) How to write an SQL query to find students' names start with
'A'?
We can write the following query to get the student details whose name starts with A:
47) Write the SQL query to get the third maximum salary of an
employee from a table named employees.
The following query is the simplest way to get the third maximum salary of an
employee:
Here is the demo example that shows how to get the third maximum salary of an
employee.
The following are the alternative way to get the third-highest salary of an employee:
B. Using Subquery
1. SELECT salary
2. FROM
3. (SELECT salary
4. FROM employees
5. ORDER BY salary DESC
6. LIMIT 3) AS Temp
7. ORDER BY salary LIMIT 1;
1) The delete statement removes single or multiple The truncate command deletes the whole contents of
rows from an existing table depending on the an existing table without the table itself. It preserves
specified condition. the table structure or schema.
3) We can use the WHERE clause in the DELETE We cannot use the WHERE clause with TRUNCATE.
command.
4) DELETE statement is used to delete a row from TRUNCATE statement is used to remove all the
a table. rows from a table.
5) DELETE is slower because it maintained the log. TRUNCATE statement is faster than DELETE
statement as it deletes entire data at a time without
maintaining transaction logs.
6) You can roll back data after using the DELETE It is not possible to roll back after using the
statement. TRUNCATE statement.
7) DELETE query takes more space. TRUNCATE query occupies less space.
The ACID property is an acronym for Atomicity, Consistency, Isolation, and Durability.
Atomicity: It ensures that all statements or operations within the transaction unit must
be executed successfully. If one part of the transaction fails, the entire transaction fails,
and the database state is left unchanged. Its main features are COMMIT, ROLLBACK,
and AUTO-COMMIT.
Consistency: This property ensures that the data must meet all validation rules. In
simple words, we can say that the database changes state only when a transaction will
be committed successfully. It also protects data from crashes.
Isolation: This property guarantees that the concurrent property of execution in the
transaction unit must be operated independently. It also ensures that statements are
transparent to each other. The main goal of providing isolation is to control
concurrency in a database.
Durability: This property guarantees that once a transaction has been committed, it
persists permanently even if the system crashes, power loss, or failed.
o A function should have a name, and the name cannot begin with a special character
such as @, $, #, or other similar characters.
o Functions can only work with the SELECT statements.
o Every time a function is called, it compiles.
o Functions must return value or result.
o Functions are always used with input parameters.
o User-Defined Function: Functions created by a user based on their needs are termed
user-defined functions.
o System Defined Function: Functions whose definition is defined by the system are
termed system-defined functions. They are built-in database functions.
LOWER: This function is used to converts a given character into lowercase. The
following example will return the 'STEPHEN' as 'stephen':
UPPER: This function is used to converts a given character into uppercase. The
following example will return the 'stephen' as 'STEPHEN':
INITCAP: This function is used to converts given character values to uppercase for the
initials of each word. It means every first letter of the word is converted into uppercase,
and the rest is in lower case. The following example will return the 'hello stephen' as
'Hello Stephen':
A) CONCAT: This function is used to join two or more values together. It always
appends the second string into the end of the first string. For example:
Output: Information-technology
B) SUBSTR: It is used to return the portion of the string from a specified start point to
an endpoint. For example:
C) LENGTH: This function returns the string's length in numerical value, including the
blank spaces. For example:
Output: 16
D) INSTR: This function finds the exact numeric position of a specified character or
word in a given string. For example:
Output: 7
E) LPAD: It returns the padding of the left-side character value for right-justified value.
For example:
Output: ***200
F) RPAD: It returns the padding of the right-side character value for left-justified value.
For example:
Output: 200***
G) TRIM: This function is used to remove all the defined characters from the
beginning, end, or both. It also trimmed extra spaces. For example:
Output: BCDCB
Input: SELECT REPLACE ( 'It is the best coffee at the famous coffee shop.', 'coffee',
'tea');
56) What are the syntax and use of the COALESCE function?
The COALESCE() function evaluates the arguments in sequence and returns the first
NON-NULL value in a specified number of expressions. If it evaluates arguments as
NULL or not found any NON-NULL value, it returns the NULL result.
Example:
Suppose we have a table 'customer' containing eight records in which the name
column has some duplicate values.
If we want to get the name column without any duplicate values, the DISTINCT
keyword is required. Executing the below command will return a name column with
unique values.
We have taken a customer table in the previous example. Now, we will demonstrate
the ORDER BY clause on them as well.
In the below output, we can see that the first query will sort the table data in ascending
order based on the name column. However, if we run the second query by specifying
the DESC keyword, the table's order is changed in descending order.
Answer: No. The above query does not return the output because we cannot use the
WHERE clause to restrict the groups. We need to use the HAVING clause instead of
the WHERE clause to get the correct output.
WHERE HAVING
It does not allow to work with aggregate functions. It can work with aggregate functions.
This clause can be used with the SELECT, UPDATE, and DELETE This clause can only be used with the SELECT
statements. statement.
SQL provides seven (7) aggregate functions, which are given below:
o AVG(): This function is used to returns the average value from specified columns.
o COUNT(): This function is used to returns the number of table rows, including rows
with null values.
o MAX(): This function is used to returns the largest value among the group.
o MIN(): This function is used to returns the smallest value among the group.
o SUM(): This function is used to returns the total summed values(non-null) of the
specified column.
o FIRST(): This function is used to returns the first value of an expression.
o LAST(): This function is used to returns the last value of an expression.
The DENSE_RANK function assigns a unique rank for each row within a partition as
per the specified column value without any gaps. It always specifies ranking in
consecutive order. If the two rows are assigned the same rank, this function will assign
it with the same rank, and the next rank being the next sequential number. For
example, if we have 3 records at rank 4, the next rank listed would be ranked 5.