Top 10 SQL Queries for interview

The term SQL stands for Structured Query Language,it’s a programming language that’s used to update and communicate with the database. SQL skills are indispensable and are in demand as with the boost in technology, data is piling up. And, In today’s world this data has become a necessity for every organization. In this post there are a lot of SQL Queries with answers which are likely to be asked during an interview.

Consider the below table for reference while trying to solve the questions asked.

Employee Details

EmployeeDetails

Ques-1 Write an SQL Query to fetch the EmpId and FullName of all the employees working under Manager with ID- 321.

Ans- We can use EmployeeDetails to fetch the data from the MangerId.

            SELECT  EmpId, FullName

FROM EmployeeDetails

WHERE ManagerId = 321;


Ques-2 Write an  SQL Query to fetch common details from the two tables.

Ans- INTERSECT operator is used in SQL to fetch the common details.

SELECT * FROM EmployeeSalary

INTERSECT

SELECT * FROM ManagerSalary;


In the case of MySQL, we use the sub-query as MySQL does not have the INTERSECT operator.

SELECT *

FROM EmployeeSalary

WHERE EmpId IN

(SELECT EmpId from ManagerSalary);


Ques-3 Write an SQL query to find current date and time.

Ans-

 Finding date and time in MySQL

SELECT NOW();

 

Finding date and time in SQL Server

             SELECT getdate();

 

Finding date and time in Oracle

            SELECT SYSDATE FROM DUAL;


Ques-4 Write an SQL Query to update the employee name by removing leading and trailing spaces.

Ans- The functions LTRIM and RTRIM can be used with the update command to remove the space characters.

UPDATE EmployeeDetails

SET FullName = LTRIM(RTRIM(FullName));


Ques-5 Write the SQL Query to fetch the first record from table EmployeeDetails.

Ans- The query to fetch the first record is

SELECT * FROM EmployeeDetails

WHERE Rownum = 1; 

 

Ques-6 Write a query to find all the employees whose salary is between 10,000 to 12,000.

Ans- The query to find the salary of the employees ranging between 10,000 to 12,000 is-

SELECT * FROM EmployeeSalary

WHERE Salary BETWEEN ‘10000’ AND ‘12000’;


Ques-7 Write an SQL query to fetch all the employees working on Project other than P1.

Ans- We can use NOT operator to find the rows which are not satisfying the condition mentioned above

SELECT EmpId

FROM EmployeeSalary

WHERE NOT Project=’P1′;


Or, we can also use NOT Equal to operator

SELECT EmpId

FROM EmployeeSalary

WHERE Project <> ‘P1’;

 

Ques-8 Write an SQL query to fetch records that are present in one table but not in another table.

Ans- This can be done with the help of MINUS operator, which is used to subtract the result obtained via the first query from the result obtained by the second query.

 

SELECT * FROM EmployeeSalary

MINUS

SELECT * FROM ManagerSalary;


For MySQL we can use left join as MySQL does not have the MINUS operator.

 

SELECT EmployeeSalary.*

FROM EmployeeSalary

LEFT JOIN

ManagerSalary USING (EmpId)

WHERE ManagerSalary.EmpId IS NULL;

 

Ques-9 Write an SQL query to fetch the position of a given character(s) in a field.

Ans- INSTR function is used to return the location of a substring inside a string.

SELECT INSTR(FullName, ‘Snow’)

FROM EmployeeDetails;

 

Ques-10 Write an SQL query to upper case the name of the employee and lower case the city values.

Ans- With the help of SQL Upper and Lower function the desired result can be accomplished.

SELECT UPPER(FullName), LOWER(City)

So, this is the end of the article. We have covered some of the important questions that are asked during the SQL Queries interview and the ways in which you can excel yourself before any interview. Thanks for reading.:-)

References:-

SQL Queries for Interview
SQL Tutorials

Leave a Reply

Your email address will not be published. Required fields are marked *