Top 10 SQL Interview Questions

rahull.singh098 0 Tallied Votes 92 Views Share

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.
[Table for Reference]
SQL_DAta_table.JPG

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) 
FROM EmployeeDetails;

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

rproffitt 2,580 "Nothing to see here." Moderator

Is this dated? I've lost count of the number of times I've found SELECT * to cause performance issues.
What you also discover is a lot of SQL classes and textbooks continue to teach this. Shouldn't complain as one year had 5 figures income from a few clients over this small problem.

PS: To clarify. 5 figures from just that one client. It was a very good year. There were other issues other than the select statement but that was quite the performance drain which for some strange reason continues to be taught.

john_111 88 Junior Poster in Training

There is no table called ManagerSalary yet the post writer uses it in queries. This is careless teaching in the extreme! No teacher or lecturer would produce a tutorial minus required data. The original source has the same error and there it says "I assumed you'd guess the structure of this table." Well no because if it's structure is identical, it is just a sub set of the data and its existance serves no purpose, while leaving the user to guess things about just leads to confusion.

Also, the result of running these queries is not shown either!

Re use of select * - I was taught to state what data I wished to be returned when I first took classes on SQL - back in 2001.

pritaeas 2,194 ¯\_(ツ)_/¯ Moderator Featured Poster

Query 2 is dubious, as INTERSECT returns something different than is suggested here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.