Hello friends, we’ve brought you 50 frequently asked SQL query interview questions and answers for practice. Solving practice questions is the fastest way to learn any subject. That’s why we’ve selected these 50 SQL queries ⤵ to give you enough exercises for practice. You can now run these SQL exercises inline and get a feel of live execution.
If it is your first time here, you should start by running the readymade SQL scripts to create the test data. These scripts include a sample Worker table, a Bonus, and a Title table with pre-filled data. Just run the SQL scripts to set everything you need to practice with the SQL queries. By the end of this assignment, you will feel more confident to face SQL interviews at top IT MNCs like Amazon, Flipkart, Facebook, etc.
SQL Query Questions and Answers for Practice
We recommend you go through the questions and build queries by yourself. Try to find answers on your own. However, you need to set up the sample tables and test data. We have provided simple SQL scripts to seed the test data. Use those first to create the test database and tables.
By the way, our site has more SQL queries available for interview preparation. So if you are interested, then follow the link given below.
Prepare Sample Data To Practice SQL Skills
Sample Table – Worker
WORKER_ID | FIRST_NAME | LAST_NAME | SALARY | JOINING_DATE | DEPARTMENT |
---|---|---|---|---|---|
001 | Monika | Arora | 100000 | 2021-02-20 09:00:00 | HR |
002 | Niharika | Verma | 80000 | 2021-06-11 09:00:00 | Admin |
003 | Vishal | Singhal | 300000 | 2021-02-20 09:00:00 | HR |
004 | Amitabh | Singh | 500000 | 2021-02-20 09:00:00 | Admin |
005 | Vivek | Bhati | 500000 | 2021-06-11 09:00:00 | Admin |
006 | Vipul | Diwan | 200000 | 2021-06-11 09:00:00 | Account |
007 | Satish | Kumar | 75000 | 2021-01-20 09:00:00 | Account |
008 | Geetika | Chauhan | 90000 | 2021-04-11 09:00:00 | Admin |
Sample Table – Bonus
WORKER_REF_ID | BONUS_DATE | BONUS_AMOUNT |
---|---|---|
1 | 2023-02-20 00:00:00 | 5000 |
2 | 2023-06-11 00:00:00 | 3000 |
3 | 2023-02-20 00:00:00 | 4000 |
1 | 2023-02-20 00:00:00 | 4500 |
2 | 2023-06-11 00:00:00 | 3500 |
Sample Table – Title
WORKER_REF_ID | WORKER_TITLE | AFFECTED_FROM |
---|---|---|
1 | Manager | 2023-02-20 00:00:00 |
2 | Executive | 2023-06-11 00:00:00 |
8 | Executive | 2023-06-11 00:00:00 |
5 | Manager | 2023-06-11 00:00:00 |
4 | Asst. Manager | 2023-06-11 00:00:00 |
7 | Executive | 2023-06-11 00:00:00 |
6 | Lead | 2023-06-11 00:00:00 |
3 | Lead | 2023-06-11 00:00:00 |
To prepare the sample data, run the following queries in your database query executor or SQL command line. We’ve tested them with the latest version of MySQL Server and MySQL Workbench query browser. You can download these tools and install them to execute the SQL queries. However, these queries will run fine in any online MySQL compiler, you may use them.
SQL Script to Seed Sample Data.
Output
Running the above SQL on any MySQL instance will show a result similar to the one below.
Start with 20 Basic SQL Questions for Practice
Below are some of the most commonly asked SQL query questions and answers for practice. Get a timer to track your progress and start practicing.
Q-1. Write an SQL query to fetch “FIRST_NAME” from the Worker table using the alias name <WORKER_NAME>.
Ans.
The required query is:
Output
Q-2. Write an SQL query to fetch “FIRST_NAME” from the Worker table in upper case.
Ans.
The required query is:
Output
Q-3. Write an SQL query to fetch unique values of DEPARTMENT from the Worker table.
Ans.
The required query is:
Output
Q-4. Write an SQL query to print the first three characters of FIRST_NAME from the Worker table.
Ans.
The required query is:
Output
Q-5. Write an SQL query to find the position of the alphabet (‘a’) in the first name column ‘Amitabh’ from the Worker table.
Ans.
The required query is:
Output
Q-6. Write an SQL query to print the FIRST_NAME from the Worker table after removing white spaces from the right side.
Ans.
The required query is:
Output
Q-7. Write an SQL query to print the DEPARTMENT from the Worker table after removing white spaces from the left side.
Ans.
The required query is:
Output
Q-8. Write an SQL query that fetches the unique values of DEPARTMENT from the Worker table and prints its length.
Ans.
The required query is:
Output
Q-9. Write an SQL query to print the FIRST_NAME from the Worker table after replacing ‘a’ with ‘A’.
Ans.
The required query is:
Output
Q-10. Write an SQL query to print the FIRST_NAME and LAST_NAME from the Worker table into a single column COMPLETE_NAME. A space char should separate them.
Ans.
The required query is:
Output
Q-11. Write an SQL query to print all Worker details from the Worker table order by FIRST_NAME Ascending.
Ans.
The required query is:
Output
Q-12. Write an SQL query to print all Worker details from the Worker table order by FIRST_NAME Ascending and DEPARTMENT Descending.
Ans.
The required query is:
Output
Q-13. Write an SQL query to print details for Workers with the first names “Vipul” and “Satish” from the Worker table.
Ans.
The required query is:
Output
Q-14. Write an SQL query to print details of workers excluding first names, “Vipul” and “Satish” from the Worker table.
Ans.
The required query is:
Output
Q-15. Write an SQL query to print details of Workers with DEPARTMENT name as “Admin”.
Ans.
The required query is:
Output
Q-16. Write an SQL query to print details of the Workers whose FIRST_NAME contains ‘a’.
Ans.
The required query is:
Output
Q-17. Write an SQL query to print details of the Workers whose FIRST_NAME ends with ‘a’.
Ans.
The required query is:
Output
Q-18. Write an SQL query to print details of the Workers whose FIRST_NAME ends with ‘h’ and contains six alphabets.
Ans.
The required query is:
Output
Q-19. Write an SQL query to print details of the Workers whose SALARY lies between 100000 and 500000.
Ans.
The required query is:
Output
Q-20. Write an SQL query to print details of the Workers who joined in Feb 2021.
Ans.
The required query is:
Output
12 Medium SQL Query Interview Questions / Answers for Practice
At this point, you have acquired a good understanding of the basics of SQL, let’s move on to some more intermediate-level SQL query interview questions. These questions will require us to use more advanced SQL syntax and concepts, such as GROUP BY, HAVING, and ORDER BY.
Q-21. Write an SQL query to fetch the count of employees working in the department ‘Admin’.
Ans.
The required query is:
Output
Q-22. Write an SQL query to fetch worker names with salaries >= 50000 and <= 100000.
Ans.
The required query is:
Output
Q-23. Write an SQL query to fetch the number of workers for each department in descending order.
Ans.
The required query is:
Output
Q-24. Write an SQL query to print details of the Workers who are also Managers.
Ans.
The required query is:
Output
Q-25. Write an SQL query to fetch duplicate records having matching data in some fields of a table.
Ans.
The required query is:
Output
Q-26. Write an SQL query to show only odd rows from a table.
Ans.
The required query is:
Output
Q-27. Write an SQL query to show only even rows from a table.
Ans.
The required query is:
Output
Q-28. Write an SQL query to clone a new table from another table.
Ans.
The general query to clone a table with data is:
Output
Q-29. Write an SQL query to fetch intersecting records of two tables.
Ans.
The required query is:
Output
Q-30. Write an SQL query to show records from one table that another table does not have.
Ans.
The required query is:
Output
Q-31. Write an SQL query to show the current date and time.
Ans.
The following MySQL query returns the current date:
Output
Q-32. Write an SQL query to show the top n (say 10) records of a table.
Ans.
Specify the SQL query in the below code box:
Output
18 Complex SQL Queries for Practice
Now, that you have built a solid foundation in intermediate SQL, It’s time to practice with some advanced SQL query questions with answers. These interview questions involve queries with more complex SQL syntax and concepts, such as nested queries, joins, unions, and intersects.
Q-33. Write an SQL query to determine the nth (say n=5) highest salary from a table.
Ans.
MySQL query to find the nth highest salary:
Output
SQL Server query to find the nth highest salary:
SELECT TOP 1 Salary FROM ( SELECT DISTINCT TOP n Salary FROM Worker ORDER BY Salary DESC ) ORDER BY Salary ASC;
Q-34. Write an SQL query to determine the 5th highest salary without using the TOP or limit method.
Ans.
The following query is using the correlated subquery to return the 5th highest salary:
Output
Use the following generic method to find the nth highest salary without using TOP or limit.
SELECT Salary FROM Worker W1 WHERE n-1 = ( SELECT COUNT( DISTINCT ( W2.Salary ) ) FROM Worker W2 WHERE W2.Salary >= W1.Salary );
Q-35. Write an SQL query to fetch the list of employees with the same salary.
Ans.
The required query is:
Output
Q-36. Write an SQL query to show the second-highest salary from a table.
Ans.
The required query is:
Output
Q-37. Write an SQL query to show one row twice in the results from a table.
Ans.
The required query is:
Output
Q-38. Write an SQL query to fetch intersecting records of two tables.
Ans.
The required query is:
Output
Q-39. Write an SQL query to fetch the first 50% of records from a table.
Ans.
The required query is:
Output
Practicing SQL query interview questions is a great way to improve your understanding of the language and become more proficient in SQL. In addition to improving your technical skills, practicing SQL query questions can help you advance your career. Many employers seek candidates with strong SQL skills, so demonstrating your proficiency can get you a competitive edge.
Q-40. Write an SQL query to fetch the departments that have less than five people in them.
Ans.
The required query is:
Output
Q-41. Write an SQL query to show all departments along with the number of people in there.
Ans.
The following query returns the expected result:
Output
Q-42. Write an SQL query to show the last record from a table.
Ans.
The following query will return the last record from the Worker table:
Output
Q-43. Write an SQL query to fetch the first row of a table.
Ans.
The required query is:
Output
Q-44. Write an SQL query to fetch the last five records from a table.
Ans.
The required query is:
Output
Q-45. Write an SQL query to print the names of employees having the highest salary in each department.
Ans.
The required query is:
Output
Q-46. Write an SQL query to fetch three max salaries from a table.
Ans.
The required query is:
Output
Q-47. Write an SQL query to fetch three min salaries from a table.
Ans.
The required query is:
Output
Q-48. Write an SQL query to fetch nth max salaries from a table.
Ans.
The required query is:
Output
Q-49. Write an SQL query to fetch departments along with the total salaries paid for each of them.
Ans.
The required query is:
Output
Q-50. Write an SQL query to fetch the names of workers who earn the highest salary.
Ans.
The required query is:
Output
Summary: 50 SQL Query Interview Questions for Practice
We hope you enjoyed solving the SQL exercises and learned something new. Stay tuned for our next post, where we’ll bring you even more challenging SQL query interview questions to sharpen your proficiency.
Thanks for reading! We hope you found this tutorial helpful. If yes, please share it on Facebook/Twitter with your friends and colleagues You can also follow us on our social media platforms for more resources. if you seek more information on this topic, check out the “You Might Also Like” section below.
SQL Performance Interview Guide
Check 25 SQL performance-related questions and answers.
Keep Learning SQL,
TechBeamers.