Database Interview Question
Database Interview Question
INDEX
Q2.
What is the difference between DELETE and TRUNCATE statement?
Q3.
Why do we use CASE Statement in SQL? Give example
Q4.
What is the difference between LEFT, RIGHT, FULL outer join and INNER
join?
Q5.
What is the difference between DISTINCT and GROUP BY?
Q6.
What are the rules to follow when using UNION operator?
Q7.
What are aggregate functions? Name and explain different types of
aggregate functions in SQL?
Q8.
AVG: Calculate the average from the given set of values.
Q9.
Write the SQL query to get the third maximum salary of an employee from
a table named employees.
Q10.
Can we use aggregate function as window function? If yes then how do
we do it?
Q11.
How can you convert a text into date format? Consider the given text as
“31-01-2021“.
Q12.
Extract a substring from the name column starting from the third position
up to 4 characters.
Q13.
What are subqueries? Where can we use them?
Q14. Is it good to have the same subquery multiple times in your query? If no
then how can you solve this?
1
Q15. Difference between WHERE and HAVING clause
Q17. What are steps you would take to tune a SQL query?
Q18. What is the difference between primary key, unique key and foreign key
Q22. Which function can be used to fetch yesterdays date? Provide example.
Q27. Write a query to fetch the EmpFname from the EmployeeInfo table in
upper case and use the ALIAS name as EmpName.
Q28. Write a query to fetch the number of employees working in the department
‘HR
Q30. Write a query to retrieve the first four characters of EmpLname from the
EmployeeInfo table.
Q31. Write a query to fetch only the place name(string before brackets) from
the Address column of EmployeeInfo table.
Q32. Write a query to create a new table which consists of data and structure
copied from the other table.
Q33. Write q query to find all the employees whose salary is between 50000 to
100000.
Q34. Write a query to find the names of employees that begin with ‘S’
2
column as “FullName”. The first name and the last name must be
separated with space.
Q38. Write a query to fetch all the records from the EmployeeInfo table ordered
by EmpLname in descending order and Department in the ascending
order.
Q39. Write a query to fetch details of employees whose EmpLname ends with
an alphabet ‘A’ and contains five alphabets.
Q40. Write a query to fetch details of all employees excluding the employees
with first names, “Sanjay” and “Sonia” from the EmployeeInfo table.
Q42. Write a query to fetch all employees who also hold the managerial
position.
Q44. Write a query to calculate the even and odd records from a table.
Q45. Write a SQL query to retrieve employee details from EmployeeInfo table
who have a date of joining in the EmployeePosition table.
Q46. Write a query to retrieve two minimum and maximum salaries from the
EmployeePosition table.
Q47. Write a query to find the Nth highest salary from the table without using
TOP/limit keyword.
Q49. Write a query to retrieve the list of employees working in the same
department.
Q50. Write a query to retrieve the last 3 records from the EmployeeInfo table.
3
Q51. Write a query to find the third-highest salary from the EmpPosition table
Q52. Write a query to display the first and the last record from the
EmployeeInfo table.
Q54. Write a query to retrieve Departments who have less than 2 employees
working in it.
Q55. Write a query to retrieve EmpPostion along with total salaries paid for
each of them.
Q56. Write a query to fetch 50% records from the EmployeeInfo table.
Q58. Write a query to select last record from the table Student.
Q60. Write a query to access the first Nth records from the table student.
Where N=4
Q61. Write a query to find all even rows from the table Student.
Q62. From the above given tables write a query to find all the students name in
which students got less than 90 marks.
Q63. Write a query to create a new table name Student_marks from the given
table Student.
Q64. Write a query to show the 3rd highest marks from the Student table.
Q66. Write a query to show the record of the three highest marks from the
student table.
Q67. Write a query to fetch the stu_Name and Stu_Marks of those students
whose age is 20.
Q69. Write a query to show all the record of those students whose marks is
greater than 82 and age is 22
Q70. Write a query to show the record of those students whose name begins
4
with the ‘m’ character.
Q71. Write a query to show all Subject_Id along with the number of students in
there.
Q72. Write a query to fetch the values of the Stu_Name column from the
Student table in the upper case.
Q73. Write a query to show the unique values of Stu_age from the Student
table.
Q74. Write a query to view all student details from the Student table order by
Stu_Name Descending.
Q75. Write a query to show the three minimum marks from the student table.
5
Query Answers
● DDL stands for Data Definition Language. They include CREATE, DROP,
ALTER and TRUNCATE statements.
● DDL statements are used to create, remove or modify database objects like
table. You do not need to commit the changes after running DDL commands.
● CREATE statement can be used to create any database objects like tables,
views, functions, procedures, triggers etc.
● DROP statement can be used to remove any database objects like tables,
views, functions, procedures, triggers etc.
● ALTER statement can be used to modify the structure of a database objects.
● TRUNCATE statement can be used to remove all the data from a table at
once.
DML:
DML stands for Data Manipulation Language. DML includes INSERT, UPDATE,
DELETE and MERGE statements. DML statements are used to add, remove or
modify data from database tables
●
● INSERT statement will add rows or records to a table.
6
● UPDATE statement will modify the data in the table.
● DELETE statement will remove one or multiple rows from a table.
DCL:
● DCL stands for Data Control Language. DCL includes GRANT and REVOKE
statements.
● GRANT statements are used to provide access privileges to a database
object to any database or schema.
● REVOKE statements are used to remove access privileges from a database
object from any database or schema.
TCL:
DQL:
● Delete can be used to remove either few or all the records from a table.
Whereas truncate will always remove all the records from the table. Truncate
cannot have WHERE condition.
7
● Delete is a DML statement hence we will need to commit the transaction in
order to save the changes to database. Whereas truncate is a DDL statement
hence no commit is required.
● For example:
● Delete only the records from employee table where the name is ‘Thoufiq’
● DELETE FROM employee WHERE name = ‘Thoufiq’;
● COMMIT;
● delete all records from the employee table.
● DELETE FROM employee;
● COMMIT;
● delete all the records from the employee table. No commit is required here.
● TRUNCATE TABLE employee;
example:
We display the gender as ‘Male’ when the gender column in the employee table has
value as ‘M’. And if the gender column has value as ‘F’ then we display the value as
‘Female’. If the gender column has values anything other than M or F then the query
would return ‘Other’
Q4.What is the difference between LEFT, RIGHT, FULL outer join and INNER
join?
8
INNER JOIN:
● INNER JOIN will fetch only those records which are present in both the
joined tables.
LEFT JOIN
● LEFT JOIN will fetch all records from the left table (table placed on the
left side during the join) even if those records are not present in right
table (table placed on the right side during the join).
9
RIGHT JOIN:
● RIGHT JOIN will fetch all records from the right table
FULL JOIN
● FULL JOIN will fetch all records from both left and right table.
10
SELF JOIN
NATURAL JOIN
● NATURAL JOIN is similar to INNER join but we do not need to use the ON
clause during the join.
CROSS JOIN
● CROSS JOIN will join all the records from left table with all the records from
right table.
11
Q5.What is the difference between DISTINCT and GROUP BY?
GROUP BY clause will group together the data based on the columns specified in
group by.
Q6• What are the rules to follow when using UNION operator?
•UNION operator can be used to combine two different SQL Queries. The output
would be the result combined from both these queries. Duplicate records would not
be returned.
•You can combine two queries using UNION operator if they follow the below rules:
•Data type of all the columns in both the queries must be same.
12
Q7.What are aggregate functions? Name and explain different types of
aggregate functions in SQL?
Aggregate function can be used to perform calculation on a set of values, which will
then return a single value. We can use aggregate function either with GROUP BY
clause or without it.
Find the sum of all the salaries from the entire employee
Query will return the sum of salaries for each department in the employee table:
query will return the average salary for each department in the employee
table:
MIN
query will return the minimum salary for each department in the employee
table:
MAX:
13
find the maximum salary from the entire employee table:
COUNT:
● find the total number of records (or employees) from the entire employee
table:
● SELECT COUNT(emp_id) as no_of_emp FROM employee;
● query will return the maximum salary for each department in the employee
table:
● SELECT dept_id, COUNT(emp_id) as no_of_emp_per_dept
● FROM employee
● GROUP BY dept_id;
Q9.Write the SQL query to get the third maximum salary of an employee from a
table named employees.
Yes, we can use aggregate function as a window function by using the OVER
clause. Aggregate function will reduce the number of rows or records since they
perform calculation of a set of row values to return a single value. Whereas window
function does not reduce the number of records.
14
Q11.How can you convert a text into date format? Consider the How can you
convert a text into date format? Consider the given text as “31-01-2021“.given
text as “31-01-2021“.
Q12.Extract a substring from the name column starting from the third position
up to 4 characters.
15
Q13.What are subqueries? Where can we use them?
A SELECT query statement which is placed inside another SELECT query is termed
as a subquery. Subquery can also be termed as inner query.
•Example:
Q14.Is it good to have the same subquery multiple times in your query? If no
then how can you solve this?
It’s not a good practice to use the same subquery multiple times in your query.
Repeating the same subquery multiple times in your query can impact the query
performance (since the same query would execute multiple times) and also becomes
difficult to maintain
WHERE clause is used to filter records from the table. We can also specify join
conditions between two tables in the WHERE clause.
Whereas HAVING clause is used to filter records returned from the GROUP BY
clause. So if a SQL query has WHERE, GROUP BY and HAVING clause then first
the data gets filtered based on WHERE condition, only after this grouping of data
takes place. Finally based on the conditions in HAVING clause the grouped data
again gets filtered.
16
Check the SQL Query.
● Make sure all the table joins are correct and all the filter conditions are
applied as intended.
● Also check for any Cartesian joins that may happen unintentionally.
● If using tables with huge list of columns then make sure to only fetch columns
which are required for the current query.
● If required check the columns used in join conditions.
Q18.What is the difference between primary key, unique key and foreign key
● Primary key, unique key and foreign key are constraints we can create on a
table.
● Primary Key : When you make a column in the table as primary key then
this column will always have unique or distinct values. Duplicate values and
NULL value will not be allowed in a primary key column.
● Foreign key is used to create a master child kind of relationship between two
tables. When we make a column in a table as foreign key, this column will
then have to be referenced from another column from some other table.
● Unique Key : When you make a column in the table as unique key then this
column will always have unique or distinct values. Duplicate values will not be
allowed.
Q19.What is a View?
● View is a database object which is created based on a SQL Query. It’s like
giving a name to the results returned from a SQL Query and storing it in the
database as a view.
17
Q20.When can a function NOT be called from SELECT query?
If the function includes DML operations like INSERT, UPDATE, DELETE etc then it
cannot be called from a SELECT query. Because SELECT statement cannot change
the state of the database.
Q21.What is a trigger?
● DDL triggers are invoked when a DDL operation (CREATE, ALTER, DROP)
occurs on the respective table (table on which the trigger was created).
18
•What is MERGE statement?
Merge is part of the DML commands in SQL which can be used either perform
INSERT or UPDATE based on the data in the respective table.
MERGE Products t
USING Products_Info s
ON (s.ProductID = t.ProductID)
WHEN MATCHED
t.Product_Name = s.Product_Name,
t.Cost = s.Cost
VALUES(s.ProductID,s.Product_Name,s.Cost)
THEN DELETE;
19
Q24.What is Normalization in a Database?
● Function should always return a value whereas for a procedure it’s not
mandatory to return a value.
● Function can be called from a SELECT query whereas procedure cannot be
called from a SELECT query.
● Function is generally used to perform some calculation and return a result.
Whereas procedure is generally used to implement some business logic.
20
Q27. Write a query to fetch the EmpFname from the EmployeeInfo table in
upper case and use the ALIAS name as EmpName.
SELECT GETDATE();
SELECT SYSTDATE();
Q30. Write a query to retrieve the first four characters of EmpLname from the
EmployeeInfo table.
Q31. Write a query to fetch only the place name(string before brackets) from
the Address column of EmployeeInfo table.
Q32. Write a query to create a new table which consists of data and structure
copied from the other table.
21
SELECT * FROM EmployeePosition WHERE Salary BETWEEN '50000' AND
'100000';
Q34. Write a query to find the names of employees that begin with ‘S’
To order the records in ascending and descnding order, you have to use the ORDER
BY statement in SQL.
To fetch details mathcing a certain value, you have to use the LIKE operator in SQL.
22
SELECT * FROM EmployeeInfo WHERE Address LIKE 'DELHI(DEL)%';
Q42. Write a query to fetch all employees who also hold the managerial
position.
To retrieve the even records from a table, you have to use the MOD() function as
follows:
23
SELECT Salary
FROM EmployeePosition E1
WHERE N-1 = (
SELECT COUNT( DISTINCT ( E2.Salary ) )
FROM EmployeePosition E2
WHERE E2.Salary > E1.Salary );
Q48. Write a query to retrieve duplicate records from a table.
To display the first record from the EmployeeInfo table, you can write a query as
follows:
To display the last record from the EmployeeInfo table, you can write a query as
follows:
24
SELECT * FROM EmployeeInfo WHERE EmpID = (SELECT MAX(EmpID) FROM
EmployeeInfo);
Q53. Write a query to add email validation to your database
SELECT *
FROM EmployeeInfo WHERE
EmpID <= (SELECT COUNT(EmpID)/2 from EmployeeInfo);
Q58. Write a query to select last record from the table Student.
SELECT * FROM Student WHERE Rowid = SELECT MAX(Rowid) from Student;
Q59. Write a query to access the first Nth records from the table student.
Where N=4.
SELECT * FROM Student WHERE Rownum < = 4;
Q60. Write a query to find all even rows from the table Student.
SELECT * FROM Student WHERE MOD (Rowid,2) = 0 ;
Q61. From the above given tables write a query to find all the students name in
which students got less than 90 marks.
SELECT * FROM Student WHERE Stu_Marks <90;
25
If we have to print subject names in which students got less than 90 marks.
Q62. Write a query to create a new table name Student_marks from the given
table Student.
CREATE TABLE Student_Marks SELECT * FROM Student;
Q63. Write a query to show the 3rd highest marks from the Student table.
select Stu_Marks from Student ORDER BY Stu_Marks DESC limit 2,1;
Q64. Write a query to show the record of the three highest marks from the
student table.
SELECT (Stu_Marks) FROM (SELECT DISTINCT Stu_Marks from Student ORDER
BY Stu_Marks DESC) WHERE ROWNUM<=3;
Q65. Write a query to fetch the stu_Name and Stu_Marks of those students
whose age is 20.
SELECT Stu_Name, Stu_Marks FROM Student WHERE Stu_Age = 20;
Q67. Write a query to show all the record of those students whose marks is
greater than 82 and age is 22.
SELECT * FROM Student WHERE Stu_Marks > 82 and Stu_Age = 22;
Q68. Write a query to show the record of those students whose name begins
with the ‘m’ character.
SELECT * FROM Student WHERE Stu_Name LIKE '%m';
26
Q69. Write a query to show all Subject_Id along with the number of students in
there.
SELECT Stu_Subject_ID COUNT(Stu_Subject_ID) as 'Number of Students' FROM
Student GROUP BY Stu_Subject_ID;
Q70. Write a query to fetch the values of the Stu_Name column from the
Student table in the upper case.
SELECT UPPER(Stu_Name) from Student;
Q71. Write a query to show the unique values of Stu_age from the Student
table.
SELECT DISTINCT(Stu_Age) from Student;
Q72. Write a query to view all student details from the Student table order by
Stu_Name Descending.
SELECT * FROM Student ORDER BY Stu_Name DESC;
Q73. Write a query to show the three minimum marks from the student table.
SELECT DISTINCT Stu_Marks FROM Student a WHERE 3 <= (SELECT COUNT(DI
STINCT Stu_Marks) FROM Student b WHERE a. Stu_Marks <= b.Stu_Marks ) ORD
ER BY a.Stu_Marks DESC;
Q75. Write a query to add the Stu_Address column to the existing Student
table:
ALTER TABLE Student ADD Stu_Addressvarchar (25) ;
INDEX
27