Top 50 SQL Interview Questions
Top 50 SQL Interview Questions
SQL INTERVIEW
QUESTIONS
Q 1. What is SQL?
Q 2. What is a database?
Ans: Some common differences between SQL and PL/SQL are as shown
below:
SQL PL/SQL
SQL is a query execution or PL/SQL is a complete
commanding language. programming language
SQL tells databases, what to do? PL/SQL tells databases how to do.
LIKE: operator namepattern: exact value extracted from the pattern to get
related data in
result set.
The required query is:
Ans :
1. CHAR Datatype:
It is a datatype in SQL which is used to store character string of fixed length
specified.
If the length of the string is less than set or fixed-length then it is padded with
extra blank spaces so that its length became equal to the set length
when PAD_CHAR_TO_FULL_LENGTH SQL mode is enabled.
The storage size of the CHAR datatype is n bytes(set length). We should use
this datatype when we expect the data values in a column are of the same
length.
Example:
Consider the Query:
OUTPUT:
LENGTH(Gender)
6
6
2. VARCHAR Datatype:
It is a datatype in SQL which is used to store character string of variable length
but a maximum of the set length specified.
If the length of the string is less than set or fixed-length then it will store as it
is without padded with extra blank spaces.
The storage size of the VARCHAR datatype is equal to the actual length of the
entered string in bytes.
We should use this datatype when we expect the data values in a column are
of variable length.
©Topperworld
Example:
Consider the Query:
OUTPUT:
LENGTH(Name)
5
4
Views in SQL are a kind of virtual table. A view also has rows and columns as
they are on a real table in the database.
We can create a view by selecting fields from one or more tables present in
the database. A View can either have all the rows of a table or specific rows
based on certain conditions.
The CREATE VIEW statement of SQL is used for creating views.
Basic Syntax:
FROM table_name
condition;
Ans : A Foreign key is a field that can uniquely identify each row in another
table. And this constraint is used to specify a field as a Foreign key. That is this
field points to the primary key of another table. This usually creates a kind of
link between the two tables.
Consider the two tables as shown below:
Orders:
O_ID ORDER_NO C_ID
1 2253 3
2 3325 3
3 4521 2
4 8532 1
Customers:
C_ID NAME ADDRESS
1 RAMESH DELHI
2 SURESH NOIDA
3 DHARMESH GURGAON
As we can see clearly, that the field C_ID in the Orders table is the primary key
in the Customers’ table, i.e. it uniquely identifies each row in the Customers
table.
Syntax:
(
O_ID int NOT NULL,
int,
Ans : An SQL Join statement is used to combine data or rows from two or
more tables based on a common field between them. Different types of Joins
are:
➢ INNER JOIN: The INNER JOIN keyword selects all rows from both tables as
long as the condition is satisfied. This keyword will create the result set by
combining all rows from both the tables where the condition satisfies i.e.
the value of the common field will be the same.
➢ LEFT JOIN: This join returns all the rows of the table on the left side of the
join and matching rows for the table on the right side of the join. For the
rows for which there is no matching row on the right side, the result set
will be null. LEFT JOIN is also known as LEFT OUTER JOIN.
➢ RIGHT JOIN: RIGHT JOIN is similar to LEFT JOIN. This join returns all the
rows of the table on the right side of the join and matching rows for the
table on the left side of the join. For the rows for which there is no
matching row on the left side, the result set will contain null. RIGHT JOIN
is also known as RIGHT OUTER JOIN.
➢ FULL JOIN: FULL JOIN creates the result set by combining the results of
both LEFT JOIN and RIGHT JOIN. The result set will contain all the rows
from both tables. For the rows for which there is no matching, the result
set will contain NULL values.
⚫ Insertion Time: It refers to the time taken to find the appropriate space
and insert new data.
Ans : An SQL query is used to retrieve the required data from the database.
However, there may be multiple SQL queries that yield the same results but
with different levels of efficiency.
An inefficient query can drain the database resources, reduce the database
speed or result in a loss of service for other users. So it is very important to
optimize the query to obtain the best database performance.
Ans: Generally, there are three types of operators that are used in SQL.
1. Arithmetic Operators
2. Comparison Operators
3. Logical Operators
Logical operators are those operators that take two expressions as operands
and return TRUE or False as output. While working with complex SQL
statements and queries, comparison operators come in handy and these
operators work in the same way as logic gates do.
Q 17. What are local and global variables and their differences?
Ans :
⚫ Global Variable:
Global variables are variables that are defined outside of functions. These
variables have global scope, so they can be used by any function without
passing them to the function as parameters.
⚫ Local Variable:
Local variables are variables that are defined within functions. They have
local scope, which means that they can only be used within the functions
that define them.
The scope remains throughout the The scope is limited and remains within
program. the function only in which they are
declared.
Any change in global variable Any change in the local variable does not
affects the whole program, affect other functions of the program.
wherever it is being used.
Global variables are stored in the Local variables are stored in a stack in
data segment of memory. memory.
We cannot declare many variables We can declare various variables with the
with the same name. same name but in other functions.
Ans :
A table can have only one A table can have multiple non-clustered
clustered index. indexes.
The clustered index has an The non-Clustered index does not have
inherent ability to store data the inherent ability to store data on the
on the disk. disk.
In the Clustered index, the In the Non-Clustered index, the index key
Clustered key defines the order defines the order of data within the
of data within the table. index.
CLUSTERED INDEX NON-CLUSTERED INDEX
Ans : Set Operations in SQL eliminate duplicate tuples and can be applied
only to the relations which are union compatible. Set Operations available in
SQL are :
➢ Set Union
➢ Set Intersection
➢ Set Difference
•
UNION Operation: This operation includes all the tuples which are present
in either of the relations.
For example: To find all the customers who have a loan or an account or both
in a bank.
SELECT CustomerName FROM Depositor
UNION
SELECT CustomerName FROM Borrower ;
Ans : Aliases are the temporary names given to a table or column for the
purpose of a particular SQL query.
It is used when the name of a column or table is used other than its original
name, but the modified name is only temporary.
➢ Aliases are created to make table or column names more readable.
➢ The renaming is just a temporary change and the table name does not
change in the original database.
➢ Aliases are useful when table or column names are big or not very
readable.
➢ These are preferred when there is more than one table involved in a
query.
Q 25. What is the difference between TRUNCATE and DROP
statements?
Ans :
Ans : For doing operations on data SQL has many built-in functions, they
are categorized into two categories and further sub-categorized into seven
different functions under each category. The categories are:
Aggregate functions:
These functions are used to do operations from the values of the column and
a single value is returned.
Scalar functions:
These functions are based on user input, these too return a single value.
Ans : In SQL, the AND & OR operators are used for filtering the data and
getting precise results based on conditions.
The AND and OR operators are used with the WHERE clause.
These two operators are called conjunctive operators.
AND Operator: This operator displays only those records where both
conditions condition 1 and condition 2 evaluate to True.
OR Operator: This operator displays the records where either one of the
conditions condition 1 and condition 2 evaluates to True. That is, either
condition1 is True or condition2 is True.
Q 28. Why do we use Commit and Rollback commands?
Ans :
COMMIT ROLLBACK
The transaction can not undo changes Transaction reaches its previous
after COMMIT execution. state after ROLLBACK.
Ans : SQL injection is a technique used to exploit user data through web
page inputs by injecting SQL commands as statements. Basically, these
statements can be used to manipulate the application’s web server by
malicious users.
➢ SQL injection is a code injection technique that might destroy your
database.
➢ SQL injection is one of the most common web hacking techniques.
➢ SQL injection is the placement of malicious code in SQL statements, via
web page input.
SELECT column(s),
CAOLESCE(expression_1,. ,expression_n)
table_name;
ISNULL(): The ISNULL function has different uses in SQL Server and MySQL.
In SQL Server, ISNULL() function is used to replace NULL values.
Syntax:
value_to_replace)
table_name;
Q 36. What is Case WHEN in SQL?
case_value
WHEN when_value THEN statement_list
[ELSE statement_list]
CASE
syntax: 2
CASE
WHEN search_condition THEN statement_list
[ELSE statement_list]
CASE
Q 37. The difference between NVL and NVL2 functions?
Ans : These functions work with any data type and pertain to the use of null
values in the expression list. These are all single-row functions i.e. provide
one result per row.
NVL(expr1, expr2): In SQL, NVL() converts a null value to an actual value.
Data types that can be used are date, character, and number. Data types
must match with each other. i.e. expr1 and expr2 must be of the same data
type.
NVL2(expr1, expr2, expr3): The NVL2 function examines the first
expression. If the first expression is not null, then the NVL2 function returns
the second expression. If the first expression is null, then the third
expression is returned i.e. If expr1 is not null, NVL2 returns expr2. If expr1 is
null, NVL2 returns expr3. The argument expr1 can have any data type.
Ans :
DISTINCT is useful in certain circumstances, but it has drawbacks that it can
increase the load on the query engine to perform the sort (since it needs to
compare the result set to itself to remove duplicates). We can remove
duplicate entries using the following options:
➢ Remove duplicates using row numbers.
➢ Remove duplicates using self-Join.
➢ Remove duplicates using group by.
Ans : Livelock occurs when two or more processes continually repeat the
same interaction in response to changes in the other processes without
doing any useful work.
These processes are not in the waiting state, and they are running
concurrently.
This is different from a deadlock because in a deadlock all processes are in
the waiting state.
Ans : In SQL Server the data dictionary is a set of database tables used to
store information about a database’s definition.
One can use these data dictionaries to check the constraints on an already
existing table and to change them(if possible).
Ans : CURRENT_DATE returns to the current date. This function returns the
same value if it is executed more than once in a single statement, which
means that the value is fixed, even if there is a long delay between fetching
rows in a cursor.
Syntax:
CURRENT_DATE
or
DATE
Q 43. What is ETL in SQL?
These are three database functions that are incorporated into one tool to
pull data out from one database and put data into another database.
Ans : A trigger can also contain INSERT, UPDATE, and DELETE logic within
itself, so when the trigger is fired because of data modification it can also
cause another data modification, thereby firing another trigger.
A trigger that contains data modification logic within itself is called a nested
trigger.
Q 44. How to find the available constraint information in the table?
Ans : In SQL Server the data dictionary is a set of database tables used to
store information about a database’s definition.
One can use these data dictionaries to check the constraints on an already
existing table and to change them(if possible).
Q 45. How can you fetch common records from two tables?
Ans : The below statement could be used to get data from multiple tables,
so, we need to use join to get data from multiple tables.
Syntax :
tablename2.columnnmae
FROM tablenmae1
JOIN tablename2
tablename2.columnnmae
Ans : In SQL, zero or blank space can be compared with another zero or
blank space. whereas one null may not be equal to another null. null means
data might not be provided or there is no data.
Q 47. What is the need for group functions in SQL?
Ans : A transaction is a single logical unit of work that accesses and possibly
modifies the contents of a database. Transactions access data using read-
and-write operations. In order to maintain consistency in a database, before
and after the transaction, certain properties are followed. These are
called ACID properties. ACID (Atomicity, Consistency, Isolation, Durability) is
a set of properties that guarantee that database transactions are processed
reliably.
Ans : The ORDER BY statement in SQL is used to sort the fetched data in
either ascending or descending according to one or more columns.
➢ By default ORDER BY sorts the data in ascending order.
➢ We can use the keyword DESC to sort the data in descending order and
the keyword ASC to sort in ascending order.
Q 50. Explain SQL Having statement?
➢ Our Vision
❖ Our vision is to create a world where every college student can easily
access high-quality educational content, connect with peers, and achieve
their academic goals.
❖ We believe that education should be accessible, affordable, and engaging,
and that's exactly what we strive to offer through our platform.
➢ Unleash Your Potential
❖ Education is not just about textbooks and lectures; it's also about forming
connections and growing together.
❖ TopperWorld encourages you to engage with your fellow students, ask
questions, and share your knowledge.
❖ We believe that collaborative learning is the key to academic success.
Published by
www.skyappzacademy.com