Top SQL Interview Questions and Answers in 2023
Top SQL Interview Questions and Answers in 2023
com
Naveen
44–56 minutes
1. Define database.
A database is an organized collection of structured data that can be
stored, easily accessed, managed, and retrieved digitally from a
remote or local computer system. Databases can be complex and
vast and are built with a fixed design and modeling approach. While
smaller databases can be stored on a file system, large ones are
hosted on computer clusters or cloud storage.
DBMS vs RDBMS
3. What is SQL?
5. What is denormalization?
• Inner Join
• Left Join
• Right Join
• Full Join
• Left (Outer) Join: Use of left join is to retrieve all the records or
rows from the left and the matched ones from the right.
SELECT *
FROM Table_A A
LEFT JOIN Table_B B
ON A.col = B.col;
• Right (Outer) Join: Use of Right join is to retrieve all the records or
rows from the right and the matched ones from the left.
SELECT *
FROM Table_A A
RIGHT JOIN Table_B B
ON A.col = B.col;
• Full (Outer) Join: The use of Full join is to retrieve the records that
have a match either in the left table or the right table.
SELECT *
FROM Table_A A
FULL JOIN Table_B B
ON A.col = B.col;
Ace your next SQL interview with our expert-written SQL Join
Interview Questions.
Code:
Output:
Code:
Output:
Output:
A PRIMARY KEY constraint will automatically have a UNIQUE
constraint. However, unlike a PRIMARY KEY, multiple UNIQUE
constraints are allowed per table.
Here rows and columns are referred to as tuples and attributes, and
the number of columns in a table is referred to as a field. In the
record, fields represent the characteristics and attributes and
contain specific information about the data.
//
CREATE TABLE Employee (
ID int NOT NULL,
Employee_name varchar(255) NOT NULL,
Employee_designation varchar(255),
Employee_Age int,
PRIMARY KEY (ID)
);
The key that can accept only a null value and cannot accept
duplicate values is called a unique key. The role of a unique key is
to make sure that all columns and rows are unique.
The syntax for a unique key will be the same as the primary key.
So, the query using a unique key for the employee table will be:
//
CREATE TABLE Employee (
ID int NOT NULL,
Employee_name varchar(255) NOT NULL,
Employee_designation varchar(255),
Employee_Age int,
UNIQUE(ID)
);
Both primary and unique keys carry unique values but a primary
key cannot have a null value, while a unique key can. In a table,
there cannot be more than one primary key, but there can be
multiple unique keys.
• DDL: DDL is that part of SQL that defines the data structure of the
database in the initial stage when the database is about to be
created. It is mainly used to create and restructure database
objects. Commands in DDL are:
• Create table
• Alter table
• Drop table
• Updating records
• Retrieving the data
Indexes are used to find all rows matching with some columns and
then to skim through only those subsets of the data to find the
matches.
Syntax:
Syntax:
Syntax:
Syntax:
• subtraction (-)
• multiplication (*)
• division (/)
• remainder/modulus (%)
• AND
• ANY
• ISNULL
• EXISTS
• BETWEEN
• IN
• LIKE
• NOT
• OR
• UNIQUE
• OR (|, ^)
• NOT (~)
• % (Wildcard)
• [] (Character(s) matches)
Code:
Output:
Now, we will see the major differences between clustered and non-
clustered indexes:
SQL MySQL
SQL PL/SQL
• Consistency means that the data meets all validity guidelines. The
transaction never leaves the database without finishing its state.
Syntax:
UPPER(‘ string’)
Example:
Output:
DEMO STRING
LOWER(‘STRING’)
Example:
Output:
demo string
Syntax:
Initcap(‘sTRING’)
Example:
Output:
Dataset
Syntax:
CONCAT(‘str1’,’str2’)
Example:
Output:
Data Science
Syntax:
LENGTH(‘String’)
Example:
Output:
11
Since the primary key is unique for each record, this primary field is
added as the AUTO_INCREMENT field so that it is incremented
when a new record is inserted.
Syntax:
• TRUNCATE: This statement deletes all the data from inside a table.
Example:
Output:
Output:
TRUNCATE TABLE
Table_name;
Example:
Output:
Output:
To create and use the table again in its original form, all the
elements associated with the table need to be redefined.
• One-to-one relationship
• Many-to-one relationship
• Many-to-many relationship
41. What are the third-party tools that are used in SQL
Server?
The following is the list of third-party tools that are used in SQL
Server:
• SQL CHECK
• SQL DOC 2
• SQL Backup 5
• SQL Prompt
• Litespeed 5.0
• Snapshot replication
• Transactional replication
• Merge replication
Select SERVERPROPERTY('productversion')
The COALESCE function takes a set of inputs and returns the first
non-null value.
Syntax:
COALESCE(val1,val2,val3,……,nth val)
Example:
SELECT COALESCE(NULL, 1, 2, ‘MYSQL’)
Output:
SQL Server allows the OLEDB provider, which provides the link, to
connect to all databases.
SQL Server Agent plays an important role in the daily work of SQL
Server administrators or DBAs. This is one of the important parts of
SQL Server. The aim of the server agent is to easily implement
tasks using a scheduler engine that enables the tasks to be
performed at scheduled times. SQL Server Agent uses SQL Server
to store scheduled management task information.
• FROM
The FROM clause defines the tables and views from which data
can be interpreted. The tables and views listed must exist at the
time the question is given.
• WHERE
The WHERE clause defines the parameters that are used to limit
the contents of the results table. You can test for basic relationships
or for relationships between a column and a series of columns
using subselects.
• GROUP BY
The GROUP BY clause is commonly used for aggregate functions
to produce a single outcome row for each set of unique values in a
set of columns or expressions.
• ORDER BY
The ORDER BY clause helps in choosing the columns on which the
table’s result should be sorted.
• HAVING
The HAVING clause filters the results of the GROUP BY clause by
using an aggregate function.
Output:
The stuff() function deletes a part of the string and then inserts
another part into the string, starting at a specified position.
Syntax:
Example:
Output:
Python Tutorial
Views are virtual tables used to limit the tables that we want to
display. Views are nothing but the result of an SQL statement that
has a name associated with it. Since views are not physically
present, they take less space to store.
Syntax:
Output:
In SQL, the views are classified into four types. They are:
• Simple View: A view that is based on a single table and does not
have a GROUP BY clause or other features.
• Complex View: A view that is built from several tables and includes
a GROUP BY clause as well as functions.
• Materialized View: A view that saves both the definition and the
details. It builds data replicas by physically preserving them.
Syntax:
Example:
We are going to create a stored procedure that will help us extract
the age of the employees.
create procedure employee_age
as
select e_age from employee
go
Now, we will execute it.
exec employee_age
Output:
Syntax:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column_x=table2.column_y;
Example:
Output:
Now, we will apply Inner Join to both these tables, where the
e_dept column in the employee table is equal to the d_name
column of the department table.
Syntax:
Output:
After applying Inner Join, we have only those records where the
departments match in both tables. As we can see, the matched
departments are Support, Analytics, and Sales.
Views Tables
A view does not hold data A table contains data and stores it
itself in databases
Syntax:
Output:
Self Join in SQL is used for joining a table with itself. Here,
depending on some conditions, each row of the table is joined with
itself and with other rows of the table.
Syntax:
Example:
Example:
6 Amir 22 MP 4,500.00
Output:
ID Name Salary
2 Anand 1,500.00
2 Abhishek 1,500.00
1 Vishal 2,000.00
2 Vishal 1,500.00
3 Vishal 2,000.00
6 Vishal 4,500.00
1 Sayeedul 2,000.00
2 Sayeedul 1,500.00
3 Sayeedul 2,000.00
4 Sayeedul 6,500.00
6 Sayeedul 4,500.00
1 Amir 2,000.00
2 Amir 1,500.00
3 Amir 2,000.00
1 Arpit 2,000.00
2 Arpit 1,500.00
3 Arpit 2,000.00
4 Arpit 6,500.00
5 Arpit 8,500.00
6 Arpit 4,500.00
The Union operator is used to combine the result set of two or more
select statements. For example, the first select statement returns
the fish shown in Image A, and the second statement returns the
fish shown in Image B. The Union operator will then return the
result of the two select statements as shown in Image A U B. If
there is a record present in both tables, then we will get only one of
them in the final result.
Syntax:
Union:
These are the two tables in which we will use the Union operator.
Union:
Output:
The Union All operator gives all the records from both tables
including the duplicates.
Syntax:
Union All:
select * from student_details2
Output:
• To grab and switch to the next row in the result set, use the FETCH
statement.
Syntax:
Output:
Output:
66. How can you copy data from one table into
another table?
Here, we have our employee table.
We have to copy this data into another table. For this purpose, we
can use the INSERT INTO SELECT operator. Before we go ahead
and do that, we will have to create another table that will have the
same structure as the above-given table.
Syntax:
Output:
Let us create an employee table where the column names are ID,
NAME, DEPARTMENT, and EMAIL. Below are the SQL scripts for
generating the sample data:
69. Can you identify the employee who has the third-
highest salary from the given employee table (with
salary-related data)?
Consider the following employee table. In the table, Sabid has the
third-highest salary (60,000).
Name Salary
Tarun 70,000
Sabid 60,000
Adarsh 30,000
Vaibhav 80,000
Below is a simple query to find out the employee who has the third-
highest salary. The functions RANK, DENSE RANK, and ROW
NUMBER are used to obtain the increasing integer value by
imposing the ORDER BY clause in the SELECT statement, based
on the ordering of the rows. The ORDER BY clause is necessary
when RANK, DENSE RANK, or ROW NUMBER functions are
used. On the other hand, the PARTITION BY clause is optional.
WITH CTE AS
(
SELECT Name, Salary, RN = ROW_NUMBER() OVER (ORDER
BY Salary DESC) FROM EMPLOYEE
)
SELECT Name, Salary FROM CTE WHERE RN =3
Output:
Name Salary
Tarun 50,000
Tarun 60,000
Sabid 70,000
Adarsh 80,000
Vaibhav 90,000
Output:
Department Total
Marketing 70,000
Production 130,000
Testing 150,000
Now, let us see the output when we apply HAVING to the above
query.
Department Total
Production 130,000
Testing 150,000
The white box testing method mainly deals with the internal
structure of a particular database, where users hide specification
details. The white box testing method involves the following:
• As the coding error can be detected by testing the white box, it can
eliminate internal errors.
• Mapping details
72. How can you create empty tables with the same
structure as another table?