SQL Query Interview Questions and Answers
SQL is a popular programming language used to manage, manipulate and query data in relational databases. Though SQL is not only the fastest way to communicate with a relational database but also deals with an effective way to manage higher dimensional structured data. SQL queries enable the user to work on multiple records with a single command. SQL query can be a request or question format to access the data saved in the database. The database saves the information in table format comprised of rows and columns. In this article, we can discuss about interview questions and answers on SQL queries.
Top 19 SQL Query Interview Questions and Answers
Below are the top questions and answers to SQL Query. These questions are helpful while giving mock tests or interviews.
Q1. Compose an SQL query to get the full name and employee id of all the employees under HR with id – 789.
Answer:
The user can execute the where clause for the HR row under the table with employee details.
SELECT fullname, employeeid
From employeetable
WHERE HR ID = 789
Q2. Compose an SQL query to get the various available projects from the team table.
Answer:
When referring to the team table, there can be duplicate values as every employee contains some project code. So to avoid this, use the below query,
SELECT DISTINCT (projectcode)
FROM team
Here distinct is used to fetch only the unique values.
Q3. Compose an SQL query to get the employee count under team S3.
Answer:
The query can be written with SQL where command and aggregate function count()
SELECT COUNT()
FROM employeetable
WHERE TEAM = S3
Q4. Compose an SQL query to get the minimum, maximum, and average pay of the employees.
Answer:
The user can choose an aggregate SQL function to get minimum, maximum, and average values as follows,
SELECT Maximum (pay),
Minimum (pay),
Average (pay)
From Employeetable
Q5. Compose an SQL query to fetch the employee details with the salary range from 10000 to 20000.
Answer:
The user can choose between function along with where option.
SELECT employeeid, pay
FROM employeetable
WHERE pay BETWEEN 10000 AND 20000;
Q6. Give an SQL query to get the employee id, with the location of Hungary added under the HR with ID as 987.
Answer:
As the user has to meet both conditions, he can use AND function
SELECT employeeid, City, HR id
FROM employeetable
WHERE City = Hungary AND HR id = 987
Q7. Compose an SQL query to get the employees who live in Paris or work under HR with an id of 234.
Answer:
Here the user should satisfy either one condition, and he can fetch the person under Paris or with an HR id of 234. So can use the OR operator.
SELECT employeeid, City, HR id
FROM employeetable
WHERE City = Paris OR HR id =234
Q8. Compose an SQL query to get all the employee details who were assigned tasks other than S3.
Answer:
Here the user cannot apply NOT function to get the rows that don’t meet the given condition.
SELECT employeeid
FROM employeetable
WHERE NOT Project=S3;
If the user prefers to use not equal to function,
SELECT employeeid
FROM employeetable
WHERE task <> S3;
Q9. Compose an SQL query to view each employee’s total pay, including variable pay and actual pay.
Answer:
The user can choose the addition operator
SELECT employeeid,
Pay + variable as totalpay
FROM employeetable
Q10. Compose an SQL query to get the name of the employees that begins with any two letters, followed by letters la, and finishes with any sequence of alphabets.
Answer:
To answer this question, the SQL query can be composed using operators like % and _. Here _ matches the single alphabet, and % goes with multiple or zero characters.
SELECT employeename
FROM employeetable
WHERE employeename LIKE '__la%';
Q11. Compose an SQL query to get all the employee id that is present in any one of the tables ‘employeepay’ or ‘employeetable’
Answer:
To fetch the unique employee id from two tables, the union function can be implemented. The union function merges the output of two SQL queries and executes it as unique rows.
SELECT employeeid FROM employeetable
UNION
SELECT employeeid FROM employeepay;
Q12. Compose an SQL query to get similar records between two different tables.
Answer:
Using intersect function, common records can be fetched from two different tables.
SELECT * FROM employeetable
INTERSECT output
SELECT * FROM employeepay;
In MySQL, there is no intersect function, so the user can write a subquery as follows,
SELECT *
FROM employeetable
WHERE employeeid IN
(SELECT employeeid from employeepay);
Q13. Compose an SQL query to get the records present only on one database table but not repeated in another.
Answer:
The user can implement a subtraction function that is a minus operator
SELECT * FROM employeetable
MINUS output
SELECT * FROM employeepay;
MySQL doesn’t support the minus function, so the left join clause is used here,
SELECT employeetable.*
FROM employeetable
LEFT JOIN
employeepay USING (employeeid)
WHERE employeepay. employeeid IS NULL;
Q14. Compose an SQL query to get the employee id which is common in both database tables.
Answer:
By using subquery, the common details can be fetched.
SELECT employeeid FROM
employeetable
where employeeid IN
(SELECT employeeid FROM employeepay);
Q15. Compose an SQL query to get the employee id which is present in employeetable but not in employeepay.
Answer:
By using subquery,
SELECT employeeid
FROM employeetable
where employeeid Not IN
(SELECT employeeid FROM employeepay)
Q16. Compose an SQL query to get the full name of an employee and replace the space with . character.
Answer:
By using replace option in SQL, the query can be compiled as follows,
SELECT REPLACE(employeefullname, ' ', '.')
FROM employeetable;
Q17. Compose an SQL query to get the position of the character(h) in any field.
Answer:
By using the INSTR function, the question can be answered
SELECT INSTR(employeefullname, h)
FROM employeetable;
Q18. Compose an SQL query to view both HR id and employee id together.
Answer:
The user can choose CONCATE function,
SELECT CONCATE (employeeid, HR id) as output id
FROM employeetable;
Q19. Compose an SQL query to view the total count of particular character -s in the full name of the employee field.
Answer:
Here the user can choose the Length function. From the full name of the employee field, the total count should be subtracted after replacing it with s.
SELECT FullNameofemployee,
LENGTH(name) - LENGTH(REPLACE(name, 's', ''))
FROM employeetable;
Conclusion
Hence these are standard questions and answer on SQL queries that were frequently asked in interviews. Hope this article helps beginners to understand the concepts of SQL queries which is an important function for higher dimensional data management.
Recommended Articles
This is a guide to SQL Query Interview Questions. Here we have discussed the top question and answers to prepare for your next interview. You may also look at the following articles to learn more –
- Data Engineer Interview Questions
- Sqoop Interview Questions
- Data Science Interview Questions
- Data Analytics Interview Questions
Are you preparing for the entrance exam ?
Join our Data Science test series to get more practice in your preparation
View More