SQL Interview Questions
SQL Interview Questions
SQL Interview Questions
DDL commands are used to define or alter the structure of the database.
DML commands are used for managing data present in the database.
SELECT: To select specific data from a database
INSERT: To insert new records into a table
UPDATE: To update existing records
DELETE: To delete existing records from a table
What are the different DCL commands in SQL?
DCL commands are used to create roles, grant permission, and control access to
the database objects.
TCL commands are used to manage the changes made by DML statements.
1 CREATE VIEW view_name AS SELECT column_name1, column_name2 FROM table_name WHERE CONDITION;
The SQL SELECT DISTINCT query is used to return only unique values. It
eliminates all the duplicated values.
What is a Join?
Join is a query, which retrieves related columns or rows from multiple tables.
INNER JOIN
LEFT JOIN
RIGHT JOIN
OUTER JOIN
SQL constraints are the set of rules that enforced some restriction while
inserting, deleting or updating of data in the databases.
Some of the constraints in SQL are – Primary Key, Foreign Key, Unique Key,
SQL Not Null, Default, Check and Index constraint.
The short answer is no, a table is not allowed to contain multiple primary keys
but it allows to have one composite primary key consisting of two or more
columns.
Composite PRIMARY KEY is a primary key created on more than one column
(combination of multiple fields) in a table.
A FOREIGN KEY is a key used to link two tables together. A FOREIGN KEY in a
table is linked with the PRIMARY KEY of another table.
There should be only one PRIMARY KEY in a table whereas there can be any
number of UNIQUE Keys.
PRIMARY KEY doesn’t allow NULL values whereas Unique key allows NULL
values.
A field with a NULL value is a field with no value. A NULL value is different from a
zero value or a field that contains spaces. A field with a NULL value is one that
has been left blank during record creation. Assume, there is a field in a table is
optional and it is possible to insert a record without adding a value to the optional
field then the field will be saved with a NULL value.
What is the difference between NULL value, Zero, and Blank space?
As I mentioned earlier, Null value is field with no value which is different from
zero value and blank space.
Null value is a field with no value.
Zero is a number
Blank space is the value we provide. The ASCII value of space is CHAR(32).
A field with a NULL value is a field with no value. NULL value cannot be
compared with other NULL values. Hence, It is not possible to test
for NULL values with comparison operators, such as =, <, or <>. For this, we
have to use the IS NULL and IS NOT NULL operators.
NOT NULL constraint is used to ensure that the value in the filed cannot be a
NULL
A CHECK constraint is used to limit the value that is accepted by one or more
columns.
E.g. ‘Age’ field should contain only the value greater than 18.
CREATE TABLE EMP_DETAILS(EmpID int NOT NULL, NAME VARCHAR (30) NOT NULL, Age INT CHECK (AGE
1
> 18), PRIMARY KEY (EmpID));
DELETE TRUNCATE
Delete statement is used to delete rows from a Truncate statement is used to delete all the
table. It can be rolled back. rows from the table and free the space
containing the table. It cant be rolled back.
We can use WHERE condition in DELETE We cant use WHERE condition in TRUNCATE
statement and can delete required rows statement. So we cant delete required rows
alone
We can delete specific rows using DELETE We can only delete all the rows at a time using
TRUNCATE
Delete maintains log and performance is slower Truncate maintains minimal log and
than Truncate performance wise faster
We need DELETE permission on Table to use We need at least ALTER permission on the
DELETE command table to use TRUNCATE command
What is the difference between Union and Union All command?
This is one of the tricky SQL Interview Questions. Interviewer may ask you this
question in another way as what are the advantages of Union All over Union.
Both Union and Union All concatenate the result of two tables but the way these
two queries handle duplicates are different.
Union: It omits duplicate records and returns only distinct result set of two or
more select statements.
Union All: It returns all the rows including duplicates in the result set of different
select statements.
Performance wise Union All is faster than Union, Since Union All doesn’t remove
duplicates. Union query checks the duplicate values which consumes some time
to remove the duplicate records.
Assume: Table1 has 10 records, Table2 has 10 records. Last record from both
the tables are same.
2 UNION
2 UNION ALL
Data type of all the columns in the two tables should be same.
Write the command to remove all Players named Sachin from the
Players table.
DELETE from Players WHERE Player_Name = ‘Sachin’;
By using the DISTINCT keyword, we could get each name only once.
In SQL, there is a built-in function called GetDate() which helps to return the
current date.
SELECT GetDate();
Write an SQL Query to find list of Employee_Name start with ‘E’ from
the below table
Write SQL SELECT query that returns the FirstName and LastName
from Employee_Details table.
4 major types of Joins are used 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 matching column.
Syntax:
SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON column_name1=column_name2;
For Example,
In this example, we have a table Employee with the following data:
The second table’s name is Joining.
RIGHT JOIN (RIGHT OUTER JOIN): This joins returns all rows from the RIGHT table and
its matched rows from the LEFT table.
Syntax:
SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON column_name1=column_name2;
For Example,
In this example, we have a table Employee with the following data:
E0012 2016/04/18
E0013 2016/04/19
E0014 2016/05/01
FULL JOIN (FULL OUTER JOIN): This joins returns all results 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;
For Example,
In this example, we have a table Employee with the following data:
Write an SQL query to fetch the EmpId and FullName of all the
employees working under Manager with id – ‘986’.
Ans. We can use the EmployeeDetails table to fetch the employee details
with a where clause for the manager-
FROM EmployeeDetails
Write an SQL query to fetch the different projects available from the
EmployeeSalary table.
SELECT DISTINCT(Project)
FROM EmployeeSalary;
FROM EmployeeSalary
Write an SQL query to find the maximum, minimum, and average salary
of the employees.
Ans. We can use the aggregate function of SQL to fetch the max, min, and
average values-
SELECT Max(Salary),
Min(Salary),
AVG(Salary)
FROM EmployeeSalary;
Write an SQL query to find the employee id whose salary lies in the
range of 9000 and 15000.
Ans. Here, we can use the ‘Between’ operator with a where clause.
SELECT EmpId, Salary
FROM EmployeeSalary
Write an SQL query to fetch all the employees who either live in
California or work under a manager with ManagerId – 321.
Ans. This interview question requires us to satisfy either of the conditions –
employees living in ‘California’ and working under Manager with ManagerId
‘321’. So, we will use the OR operator here-
SELECT EmpId, City, ManagerId
FROM EmployeeDetails
WHERE City='California' OR ManagerId='321';
Write an SQL query to fetch all those employees who work on Project
other than P1.
Ans. Here, we can use the NOT operator to fetch the rows which are not
satisfying the given condition.
SELECT EmpId
FROM EmployeeSalary
SELECT EmpId
FROM EmployeeSalary
Write an SQL query to fetch the EmpIds that are present in both the
tables – ‘EmployeeDetails’ and ‘EmployeeSalary.
Ans. Using sub query-
SELECT EmpId FROM
EmployeeDetails
where EmpId IN
EmployeeDetails