Best SQL Interview Questions: Q #1) What Is SQL?
Best SQL Interview Questions: Q #1) What Is SQL?
Let's start.
Answer: Structured Query Language is a database tool which is used to create and
access database to support software application.
Answer: The table is a collection of record and its information at a single view.
Answer:
1) DDL (Data Definition Language): It is used to define the database structure such as
tables. It includes three statements such as Create, Alter, and Drop.
OR
2) DML (Data Manipulation Language): These statements are used to manipulate the
data in records. Commonly used DML statements are Insert, Update, and Delete.
The Select statement is used as partial DML statement that is used to select all or
relevant records in the table.
3) DCL (Data Control Language): These statements are used to set privileges such as
Grant and Revoke database access permission to the specific user.
Answer:
The DISTINCT statement is used with the SELECT statement. If the records contain
duplicate values then DISTINCT is used to select different values among duplicate
records.
Syntax:
Answer:
WHERE Clause: This clause is used to define the condition, extract and display only
those records which fulfill the given condition
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition;
GROUP BY Clause: It is used with SELECT statement to group the result of the
executed query using the value specified in it. It matches the value with the column
name in tables and groups the end result accordingly.
Syntax:
SELECT column_name(s)
FROM table_name
GROUP BY column_name;
HAVING clause: This clause is used in association with the GROUP BY clause. It is
applied to each group of result or the entire result as a single group and much similar as
WHERE clause, the only difference is you cannot use it without GROUP BY clause
Syntax:
SELECT column_name(s)
FROM table_name
GROUP BY column_name
HAVING condition;
ORDER BY clause: This clause is to define the order of the query output either in
ascending (ASC) or in descending (DESC) order. Ascending (ASC) is the default one but
descending (DESC) is set explicitly.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
ORDER BY column_name ASC|DESC;
USING clause: USING clause comes in use while working with SQL Joins. It is used to
check equality based on columns when tables are joined. It can be used instead ON
clause in Joins.
Syntax:
SELECT column_name(s)
FROM table_name
JOIN table_name
USING (column_name);
Q #6) Why do we use SQL constraints? Which constraints we can use while
creating a database in SQL?
Answer:
Constraints are used to set the rules for all records in the table. If any constraints get
violated then it can abort the action that caused it.
Constraints are defined while creating the database itself with CREATE TABLE
statement or even after the table is created once with ALTER TABLE statement.
● NOT NULL: That indicates that the column must have some value and
cannot be left null
● UNIQUE: This constraint is used to ensure that each row and column has
unique value and no value is being repeated in any other row or column
● PRIMARY KEY: This constraint is used in association with NOT NULL and
UNIQUE constraints such as on one or the combination of more than one
column to identify the particular record with a unique identity.
● FOREIGN KEY: It is used to ensure the referential integrity of data in the
table and also matches the value in one table with another using Primary
Key
● CHECK: It is used to ensure whether the value in columns fulfills the
specified condition
Answer:
There are 4 major types of joins made to use while working on multiple tables in SQL
databases
● INNER JOIN: It is also known as SIMPLE JOIN which returns all rows from
BOTH tables when it has at least one column matched
Syntax:
SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON column_name1=column_name2;
Example
1 SELECT Employee.Emp_id,
Joining.Joining_Date
2 FROM
Employee
3 INNER JOIN
Joining
4 ON Employee.Emp_id =
Joining.Emp_id
5 ORDER BY
Employee.Emp_id;
There will be 4 records selected. These are the results that you should see
Employee and orders tables where there is a matching customer_id value in both the
Employee and orders tables
LEFT JOIN (LEFT OUTER JOIN): This join returns all rows from a LEFT table and its
matched
Syntax:
SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON column_name1=column_name2;
Example
1 SELECT Employee.Emp_id,
Joining.Joining_Date
2 FROM
Employee
4 ON Employee.Emp_id =
Joining.Emp_id
5 ORDER BY
Employee.Emp_id;
There will be 4 records selected. These are the results that you should see:
RIGHT JOIN (RIGHT OUTER JOIN): This joins returns all rows from the RIGHT table
and its matched rows from a LEFT table.
Syntax:
SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON column_name1=column_name2;
Example
1 SELECT Employee.Emp_id,
Joining.Joining_Date
2 FROM
Employee
5 ORDER BY
Employee.Emp_id;
There will be 4 records selected. These are the results that you should see
FULL JOIN (FULL OUTER JOIN): This joins returns all when there is a match either in
the RIGHT table or in the LEFT table.
Syntax:
SELECT column_name(s)
FROM table_name1
FULL OUTER JOIN table_name2
ON column_name1=column_name2;
Example
1 SELECT Employee.Emp_id,
Joining.Joining_Date
2 FROM
Employee
4 ON Employee.Emp_id =
Joining.Emp_id
5 ORDER BY
Employee.Emp_id;
There will be 8 records selected. These are the results that you should see
Q #8) What are transactions and their controls?
Answer:
In simple words, we can say that a transaction means a group of SQL queries executed
on database records.
Answer:
Properties of the transaction are known as ACID properties, such as
Answer:
SQL Aggregate Functions calculates values from multiple columns in a table and returns
a single value.
Answer:
Scalar Functions are used to return a single value based on the input values.
Answer:
Action and Event are two main components of SQL triggers when certain actions are
performed the event occurs in response to that action.
Syntax:
Answer:
A View can be defined as a virtual table that contains rows and columns with fields from
one or more table.
Syntax:
Answer:
SQL CREATE and REPLACE can be used for updating the view.
Answer:
SQL GRANT and REVOKE commands are used to implement privileges in SQL multiple
user environments. The administrator of the database can grant or revoke privileges to
or from users of database object like SELECT, INSERT, UPDATE, DELETE, ALL etc.
GRANT Command: This command is used provide database access to user apart from
an administrator.
Syntax:
GRANT privilege_name
ON object_name
TO {user_name|PUBLIC|role_name}
[WITH GRANT OPTION];
In above syntax WITH GRANT OPTIONS indicates that the user can grant access to
another user too.
REVOKE command: This command is used provide database deny or remove access
to database objects.
Syntax:
REVOKE privilege_name
ON object_name
FROM {user_name|PUBLIC|role_name};
Answer:
Answer:
SQL Injection is a type of database attack technique where malicious SQL statements
are inserted into an entry field of database such that once it is executed the database is
opened for an attacker. This technique is usually used for attacking Data-Driven
Applications to have access to sensitive data and perform administrative tasks on
databases.
Answer:
SQL Sandbox is the safe place in SQL Server Environment where untrusted scripts are
executed. There are 3 types of SQL sandbox, such as
● Safe Access Sandbox: Here a user can perform SQL operations such as
creating stored procedures, triggers etc. but cannot have access to the
memory and cannot create files.
● External Access Sandbox: User can have access to files without having
a right to manipulate the memory allocation.
● Unsafe Access Sandbox: This contains untrusted codes where a user
can have access to memory.
Answer:
SQL is a structured query language to create and access databases whereas PL/SQL
comes with procedural concepts of programming languages.
Answer:
SQL is a structured query language that is used for manipulating and accessing the
relational database, on the other hand, MySQL itself is a relational database that uses
SQL as the standard database language.
Answer:
NVL function is used to convert the null value to its actual value.
Answer:
The output of Cross Join is called as a Cartesian product. It returns rows combining
each row from the first table with each row of the second table. For Example, if we join
two tables having 15 and 20 columns the Cartesian product of two tables will be
15×20=300 Rows.
Answer:
Query within another query is called as Subquery. A subquery is called inner query
which returns output that is to be used by another query.
Q #24) How many row comparison operators are used while working with a
subquery?
Answer:
There are 3-row comparison operators that are used in subqueries such as IN, ANY and
ALL.
● One table can have only one clustered index but multiple nonclustered
indexes.
● Clustered indexes can be read rapidly rather than non-clustered indexes.
● Clustered indexes store data physically in the table or view and
non-clustered indexes do not store data in the table as it has separate
structure from the data row
Answer:
Answer:
TRUNCATE removes all rows from the table which cannot be retrieved back, DROP
removes the entire table from the database and it cannot be retrieved back.
Q #28) How to write a query to show the details of a student from Students table
whose
Answer:
Answer:
Normalization is used to organize the data in such a manner that data redundancy will
never occur in the database and avoid insert, update and delete anomalies.
● First Normal Form (1NF): It removes all duplicate columns from the table.
Creates a table for related data and identifies unique column values
● First Normal Form (2NF): Follows 1NF and creates and places data
subsets in an individual table and defines the relationship between tables
using the primary key
● Third Normal Form (3NF): Follows 2NF and removes those columns which
are not related through primary key
● Fourth Normal Form (4NF): Follows 3NF and do not define multi-valued
dependencies. 4NF also known as BCNF
Answer:
The relationship can be defined as the connection between more than one tables in the
database.
Answer:
Syntax:
Answer:
Answer:
Triggers may implement data modification logic by using INSERT, UPDATE, and
DELETE statements. These triggers that contain data modification logic and find other
triggers for data modification are called Nested Triggers.
Q #35) What is a Cursor?
Answer:
● Declare Cursor
● Open Cursor
● Retrieve row from the Cursor
● Process the row
● Close Cursor
● Deallocate Cursor
Answer:
Collation is a set of rules that check how the data is sorted by comparing it. Such as
Character data is stored using correct character sequence along with case sensitivity,
type, and accent.
Answer:
● Database Connectivity
● Constraint Check
● Required Application Field and its size
● Data Retrieval and Processing With DML operations
● Stored Procedures
● Functional flow
Answer:
Answer:
● Data Mapping
● Data stored and retrieved
● Use of Black Box techniques such as Equivalence Partitioning and
Boundary Value Analysis (BVA)
Answer: The index can be defined as the way to retrieve the data more quickly. We can
define indexes using CREATE statements.
Syntax:
Syntax:
******************
Answer: To select all the records from the table we need to use the following syntax:
Answer: Join keyword is used to fetch data from related two or more tables. It returns
rows where there is at least one match in both the tables included in the join. Read more
here.
1. Right Join
2. Outer Join
3. Full Join
4. Cross Join
5. Self Join.
Answer: To add another column in the table following command has been used.
Answer: Delete is used to delete a row or rows from a table based on the specified
condition.
Answer: A Primary key is a column whose values uniquely identify every row in a table.
Primary key values can never be reused.
Answer: When a one table’s primary key field is added to related tables in order to
create the common field which relates the two tables, it called a foreign key in other
tables.
Answer: A CHECK constraint is used to limit the values or type of data that can be
stored in a column. They are used to enforce domain integrity.
Q #51) Is it possible for a table to have more than one foreign key?
Answer: Yes, a table can have many foreign keys and only one primary key.
Q #52) What are the possible values for the BOOLEAN data field?
Answer: For a BOOLEAN data field, two values are possible: -1(true) and 0(false).
Answer: A stored procedure is a set of SQL queries which can take input and send back
output.
Answer: An identity column in the SQL automatically generates numeric values. We can
define a start and increment value of the identity column.
Answer: The Trigger allows us to execute a batch of SQL code when a table event
occurs (Insert, update or delete command executed against a specific table)
Example:
Q #59) Write a SQL SELECT query that only returns each name only once from a
table?
Answer: To get each name only once, we need to use the DISTINCT keyword.
Answer: DML stands for Data Manipulation Language. INSERT, UPDATE and DELETE
are DML statements.
DDL stands for Data Definition Language. CREATE, ALTER, DROP, RENAME are DDL
statements.
Q #63) Suppose a Student column has two columns, Name and Marks. How to get
name and marks of the top three students.
Answer: SELECT Name, Marks FROM Student s1 where 3 <= (SELECT COUNT(*)
FROM Students s2 WHERE s1.marks = s2.marks)
Answer: DELETE removes some or all rows from a table based on the condition. It can
be rolled back.
TRUNCATE removes ALL rows from a table by de-allocating the memory pages. The
operation cannot be rolled back
Answer: Generally, these properties are referred to as ACID properties. They are:
1. Atomicity
2. Consistency
3. Isolation
4. Durability.
Answer: It’s an 18 character long pseudo column attached with each row of a table.
Answer: MINUS – returns all distinct rows selected by the first query but not by the
second.
Answer: A transaction is a sequence of code that runs against a database. It takes the
database from one consistent state to another.
Q #70) What is the difference between UNIQUE and PRIMARY KEY constraints?
Answer: A table can have only one PRIMARY KEY whereas there can be any number
of UNIQUE keys.
The primary key cannot contain Null values whereas Unique key can contain Null
values.
Answer: Primary key created on more than one column is called composite primary key.
Answer: A Subquery is a subset of select statements whose return values are used in
filtering conditions of the main query.
Answer: Set of rules that define how data is stored, how case sensitivity and Kana
character can be treated etc.
Answer: Case facilitates if-then-else type of logic in SQL. It evaluates a list of conditions
and returns one of the multiple possible result expressions.
Answer: A temp table is a temporary storage structure to store the data temporarily.
Answer: A view is a virtual table which contains data from one or more tables. Views
restrict data access of the table by selecting only required values and make complex
queries easy.
1. Views restrict access to the data because the view can display selective
columns from the table.
2. Views can be used to make simple queries to retrieve the results of
complicated queries. For example, views can be used to query information
from multiple tables without the user knowing.
Q #83) List the various privileges that a user can grant to another user?
Answer: A table is the basic unit of data storage in the database management system.
Table data is stored in rows and columns.
Q #88) What is the difference between Having clause and Where clause?
Answer: Both specify a search condition but Having clause is used only with the
SELECT statement and typically used with GROUP BY clause.
If GROUP BY clause is not used then Having behaved like WHERE clause only.
Q #89) What is the difference between Local and Global temporary table?
Answer: If defined in inside a compound statement a local temporary table exists only
for the duration of that statement but a global temporary table exists permanently in the
DB but its rows disappear when the connection is closed.