115 SQL Interview Questions and Answers
115 SQL Interview Questions and Answers
Q3. What do you mean by DBMS? What are its different types?
A Database Management System (DBMS) is a software application that interacts with the user,
applications, and the database itself to capture and analyze data. A database is a structured
collection of data.
A DBMS allows a user to interact with the database. The data stored in the database can be
modified, retrieved and deleted and can be of any type like strings, numbers, images, etc.
There are two types of DBMS:
• Relational Database Management System: The data is stored in relations (tables). Example –
MySQL.
• Non-Relational Database Management System: There is no concept of relations, tuples and
attributes. Example – MongoDB
Let’s move to the next question in this SQL Interview Questions.
Q7. What are some common clauses used with SELECT query in SQL?
The following are some frequent SQL clauses used in conjunction with a SELECT query:
WHERE clause: In SQL, the WHERE clause is used to filter records that are required depending
on certain criteria. ORDER BY clause: The ORDER BY clause in SQL is used to sort data in
ascending (ASC) or descending (DESC) order depending on specified field(s) (DESC). GROUP BY
clause: GROUP BY clause in SQL is used to group entries with identical data and may be used
with aggregation methods to obtain summarised database results. HAVING clause in SQL is
used to filter records in combination with the GROUP BY clause. It is different from WHERE, since
the WHERE clause cannot filter aggregated records.
Q13. How to create empty tables with the same structure as another
table?
To create empty tables: Using the INTO operator to fetch the records of one table into a new
table while setting a WHERE clause to false for all entries, it is possible to create empty tables
with the same structure. As a result, SQL creates a new table with a duplicate structure to accept
the fetched entries, but nothing is stored into the new table since the WHERE clause is active.
Q14. How to create empty tables with the same structure as another
table?
To create empty tables: Using the INTO operator to fetch the records of one table into a new
table while setting a WHERE clause to false for all entries, it is possible to create empty tables
with the same structure. As a result, SQL creates a new table with a duplicate structure to accept
the fetched entries, but nothing is stored into the new table since the WHERE clause is active.
Q19. Write the SQL query to get the third maximum salary of an
employee from a table named employees.
Employee table
SELECT * FROM(
SELECT employee_name, salary, DENSE_RANK()
OVER(ORDER BY salary DESC)r FROM Employee)
WHERE r=&n;
To find 3rd highest salary set n = 3
Q25. What are some common clauses used with SELECT query in SQL?
The following are some frequent SQL clauses used in conjunction with a SELECT query:
WHERE clause: In SQL, the WHERE clause is used to filter records that are required depending on
certain criteria. ORDER BY clause: The ORDER BY clause in SQL is used to sort data in ascending
(ASC) or descending (DESC) order depending on specified field(s) (DESC). GROUP BY clause:
GROUP BY clause in SQL is used to group entries with identical data and may be used with
aggregation methods to obtain summarised database results. HAVING clause in SQL is used to
filter records in combination with the GROUP BY clause. It is different from WHERE, since the
WHERE clause cannot filter aggregated records.
ELSE result
END;
The following SQL query removes the duplicate ids from the table: DELETE FROM table WHERE
ID IN ( SELECT ID, COUNT(ID) FROM table GROUP BY ID HAVING COUNT (ID) > 1);
SELECT * FROM(
SELECT employee_name, salary, DENSE_RANK()
OVER(ORDER BY salary DESC)r FROM Employee)
WHERE r=&n;
To find to the 2nd highest salary set n = 2
To find 3rd highest salary set n = 3 and so on.
Q64. How to create empty tables with the same structure as another
table?
To create empty tables:
Using the INTO operator to fetch the records of one table into a new table while setting a WHERE
clause to false for all entries, it is possible to create empty tables with the same structure. As a
result, SQL creates a new table with a duplicate structure to accept the fetched entries, but
nothing is stored into the new table since the WHERE clause is active.
Q70. What is the default ordering of data using the ORDER BY clause?
How could it be changed?
The ORDER BY clause in MySQL can be used without the ASC or DESC modifiers. The sort order is
preset to ASC or ascending order when this attribute is absent from the ORDER BY clause.
Q72. What are the syntax and use of the COALESCE function?
From a succession of expressions, the COALESCE function returns the first non-NULL value. The
expressions are evaluated in the order that they are supplied, and the function’s result is the
first non-null value. Only if all of the inputs are null does the COALESCE method return NULL.
Q77. What is the difference between cross join and natural join?
The cross join produces the cross product or Cartesian product of two tables whereas the
natural join is based on all the columns having the same name and data types in both the tables.
Q81. Write a SQL query to find the names of employees that begin with
‘A’?
To display name of the employees that begin with ‘A’, type in the below command:
SELECT * FROM Table_name WHERE EmpName like 'A%'
Q85. How can you insert NULL values in a column while inserting the
data?
NULL values in SQL can be inserted in the following ways:
• Implicitly by omitting column from column list.
• Explicitly by specifying NULL keyword in the VALUES clause
Q100. Name the operator which is used in the query for pattern
matching?
LIKE operator is used for pattern matching, and it can be used as -.
1. % – It matches zero or more characters.
For example- select * from students where studentname like ‘a%’
_ (Underscore) – it matches exactly one character. For example- select * from student where
studentname like ‘abc_’
Q114. What are the different authentication modes in SQL Server? How
can it be changed?
Windows mode and Mixed Mode – SQL and Windows. You can go to the below steps to change
authentication mode in SQL Server:
• Click Start> Programs> Microsoft SQL Server and click SQL Enterprise Manager to run SQL
Enterprise Manager from the Microsoft SQL Server program group.
• Then select the server from the Tools menu.
• Select SQL Server Configuration Properties, and choose the Security page.