Hey friends! We’ve put together 50 SQL query interview questions and answers for you to practice. Writing and running these SQL queries will help you build strong SQL knowledge. You can run them instantly, track your progress, and fix any mistakes along the way to improve.
Start by running the pre-made SQL scripts to create test data. These scripts will set up sample tables like Worker, Bonus, and Title with data already filled in. Just run the SQL scripts to set everything up and begin practicing. By the end, you’ll feel more confident and ready to tackle SQL interviews at top companies like Amazon, Flipkart, Facebook, and more.
50 SQL Query Interview Questions You Need to Know
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.

SQL Query Interview Questions for Freshers
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.
1. SQL Query to Display FIRST_NAME with Alias WORKER_NAME
An alias is a user-friendly label for a column in the SQL table. Use the ‘As’ clause to specify the alias in the query.
Output
2. SQL Query to Display FIRST_NAME in Upper Case from the Worker Table
SQL provides string functions to change the case. In this case, you can call the upper() function.
Output
3. SQL Query to Display Unique DEPARTMENT Values from the Worker Table
Use the DISTINCT keyword to list the unique values of a SQL table column removing the duplicates.
Output
4. SQL Query to Display the First 3 Chars of FIRST_NAME from the Worker Table
As mentioned earlier, SQL supports inline string operations like SUBSTRING(). Use this function on the column name to extract a part of it.
Output
5. SQL Query to Find the Position of Alphabet ‘a’ in the FIRST_NAME Column
SQL lets you work with text directly using functions like POSITION() or INSTR(). You can use these to find where a specific letter shows up in a word.
Output
6. SQL Query to Fetch FIRST_NAME from the Worker Table With No White Spaces on the Right
SQL has functions to clean up text, like removing extra spaces. To get rid of spaces on the right side of a name, you can use the RTRIM() function.
Output
7. SQL Query to List DEPARTMENT from the Worker Table With No White Spaces on the Left
To get rid of spaces on the left side of a value, you can use the LTRIM() function. Now, try creating the query yourself:
Output
8. SQL Query to Display Unique DEPARTMENT Values and Their Lengths from the Worker Table
To get unique department names and see how many characters each has, you can use DISTINCT for uniqueness and LENGTH() to count the characters.
Output
9. SQL Query to Replace ‘a’ with ‘A’ in FIRST_NAME from the Worker Table
If you want to swap all lowercase ‘a’s with uppercase ‘A’s in the first names, you can use the REPLACE() function. Give it a try yourself.
Output
10. SQL Query to Combine FIRST_NAME and LAST_NAME into COMPLETE_NAME
To join the first and last names with a space in between, you can use the || operator, which works like a glue for text in SQL. You can also use MySQL CONCAT() function.
Output
11. SQL Query to Print Worker Details Ordered by FIRST_NAME Ascending and DEPARTMENT Descending
To get all the worker details sorted by their first names in alphabetical order, you can use the ORDER BY clause with ASC (which means ascending). Try to write it down now.
Output
12. SQL Query to Print Worker Details Ordered by FIRST_NAME Ascending and DEPARTMENT Descending
To sort by two columns—first by FIRST_NAME in alphabetical order and then by DEPARTMENT in reverse order—you can use the ORDER BY clause and specify ASC for ascending and DESC for descending.
Output
13. SQL Query to Print Worker Details with First Names “Vipul” and “Satish”
To get details for workers with specific first names, you can use the IN operator to check for multiple values.
Output
14. SQL Query to Print Worker Details Excluding First Names (“Vipul” and “Satish”)
To get details of workers except for those with specific first names, you can use the NOT IN operator to exclude certain values.
Output
15. SQL Query to Print Worker Details with DEPARTMENT Name as “Admin”
If you want to search for workers in a department that contains ‘Admin’ (in any part of the name), you can use LIKE with % to match any characters before or after ‘Admin’.
Output
16. SQL Query to Print Worker Details Whose FIRST_NAME Contains ‘a’
Again, to find workers whose first name contains the letter ‘a’, you can use the LIKE operator with % to match any part of the name.
Output
17. SQL Query to List Worker Info Whose FIRST_NAME Ends with ‘a’
The logic here is similar to the last query, but now we’re looking for names that end with ‘a’. The % symbol is placed before ‘a’ to match anything that comes before it.
Output
18. SQL Query to Fetch Workers Whose FIRST_NAME Ends with ‘h’ and Has 6 Letters
To find workers whose first name ends with ‘h’ and has exactly six characters, we can use LIKE to match the pattern and LENGTH to check the number of characters.
Output
19. SQL Query to Show Worker Info Whose SALARY is Between 100000 & 500000
To find workers whose salary is between 100,000 and 500,000, you can use the BETWEEN operator, which is great for checking ranges.
Output
20. SQL Query to Display Workers Who Joined in Feb 2021
To find workers who joined in February 2021, you can use the WHERE clause with a DATE comparison using strftime() to match that specific month and year.
Output
SQL Query Interview Questions for Experienced
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.
21. SQL Query to Print Employee Count in ‘Admin’ Department
To find the count of employees working in the ‘Admin’ department, you can use the COUNT function to count rows that match the department. Check – MySQL Aggregate Functions
Output
22. SQL Query to Fetch Worker Names with Salaries >= 50000 and <= 100000
The required query is:
Output
23. SQL Query to List Worker Count Per Department in Descending Order
The required query is:
Output
24. SQL Query to Print Worker Details Who Are Also Managers
The required query is:
Output
25. SQL Query to Fetch Duplicate Records with Matching Data in Specific Fields of a Table
The required query is:
Output
26. SQL Query to Show Only Odd Rows from a Table
The required query is:
Output
27. SQL Query to Show Only Even Rows from a Table
The required query is:
Output
28. SQL Query to Clone a New Table from Another Table
The general query to clone a table with data is:
Output
29. SQL Query to Display Intersecting Records of Two Tables
The required query is:
Output
30. SQL Query to Show Records from One Table That Are Not Present in Another Table
The required query is:
Output
31. SQL Query to Show the Current Date and Time
The following query returns the current date:
Output
32. SQL Query to Show the Top n (say 10) Records of a Table
Specify the SQL query in the below code box:
Output
Advanced SQL Query Interview Questions 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.
33. SQL Query to Determine the Nth (say n=5) Highest Salary
To find the nth highest salary (like the 5th highest), you can use a combination of ORDER BY and LIMIT, or use a subquery with ROW_NUMBER to rank the salaries.
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;
34. SQL Query to Determine 5th Highest Salary Without Using TOP or Limit
To find the 5th highest salary without using TOP or LIMIT, we can use a subquery with DISTINCT and COUNT to rank the salaries.
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 );
35. SQL Query to Fetch the List of Employees with the Same Salary
To find employees with the same salary, use a self-join to compare the SALARY from two instances of the Worker table. Then, make sure the WORKER_IDs are different to avoid comparing the same person to themselves. Finally, use DISTINCT to eliminate duplicates and get unique results.
Output
36. SQL Query to List the Employee with the Second-Highest Salary
To find the second-highest salary, use a subquery to get the highest salary, and then find the maximum salary that is less than that. This way, you’ll get the second-highest value.
Output
37. SQL Query to Display One Row Twice in the Results from a Table
To show one row twice, use the UNION ALL operator to select the same data from the table twice. Unlike UNION, UNION ALL doesn’t remove duplicates, so the row will appear two times in the result.
Output
38. SQL Query to Fetch Intersecting Records of Two Tables
To get intersecting records of two tables, use the INTERSECT operator. This will return only the records that appear in both tables, based on the columns you specify.
Output
39. SQL Query to Fetch the First 50% of Records from a Table
To fetch the first 50% of records from a table, you can use a LIMIT clause in combination with a subquery that counts the total number of rows. By dividing this count by 2, you can retrieve the first half of the records.
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.
40. SQL Query to Fetch Departments with Less than Five People in Them
To find departments with fewer than five people, use a GROUP BY clause to group by the department, then use HAVING to filter departments where the count of employees is less than 5.
Output
41. SQL Query to Show All Departments with the Number of People in There
To show all departments with the number of people in each, use a GROUP BY clause to group by the department, and then use the COUNT function to count the number of employees in each department.
Output
42. SQL Query to Show the Last Record from a Table
To fetch the last record using the MAX() function, use a subquery to get the max WORKER_ID and then filter the main query to return the row matching that WORKER_ID.
Output
43. SQL Query to Fetch the First Row of a Table
To fetch the first row from a table using MIN(), you can use a subquery to get the min value of a column (such as WORKER_ID) and then filter the main query to return the row matching that value.
Output
44. SQL Query to Fetch the Last Five Records from a Table
To fetch the last five records, you can order the table by a column (like WORKER_ID or a timestamp) in descending order and limit the result to five rows.
Output
45. SQL Query to Show Employees with the Highest Salary in Each Department
To get the employees with the highest salary in each department, you can use a subquery to find the maximum salary per department and then join it with the main table to fetch the employee names and salaries.
Output
46. SQL Query to Fetch the Top Three Max Salaries from a Table
To fetch the top three max salaries, you can use a subquery that counts how many distinct salaries are greater than or equal to each salary, and then filter it to get the top three.
Output
47. SQL Query to Fetch the Three Min Salaries from a Table
To fetch the three min salaries, you can do the same as we did for the max salaries. But, this time order the salaries in ascending order and limit the result to three rows.
Output
48. SQL Query to Fetch the Nth Max Salaries from a Table
To fetch the nth max salary using a variable, you can define the value of n
using a WITH
clause and then use it in the query to limit the results.
Output
49. SQL Query to Fetch Departments and Their Total Salaries
To fetch departments along with the total salaries, you can group the data by department and sum the salaries for each group.
Output
50. SQL Query to Fetch Workers with the Highest Salary
To fetch departments along with the total salaries, you can group the data by department and sum the salaries for each group.
Output
Conclusion: Top SQL Query Interview Questions for Practice
This wraps up our post on common SQL query interview questions and answers. We encourage you to practice these questions to help you excel in your job interviews.
Thanks for reading! We hope you found this guide useful. If you did, feel free to share it with your friends and colleagues on Facebook/Twitter . Don’t forget to follow us on social media for more helpful resources.
SQL Performance Interview Guide
Check 25 SQL performance-related questions and answers.
Keep Learning SQL,
TechBeamers.
Hello Anna – The answer given by us seems correct. for MySQL, can you try this query:
SELECT * FROM Worker
WHERE DATE_FORMAT(JOINING_DATE, ‘%Y’) = ‘2021’
AND DATE_FORMAT(JOINING_DATE, ‘%m’) = ’02’;
I have checked and it is working fine with MySQL.
Hi Meenakshi Agarwal,
Above “50 SQL Query Interview Questions for Practice” which sql database you used like(mysql, SQLite, oracle) which one because when click on “show solution” , it give different answer .
Example: Q-20. Write an SQL query to print details of the Workers who joined in Feb 2021.
It show incorrect my Ans using MYSql :
SELECT * FROM Worker
WHERE YEAR(JOINING_DATE) = 2021
AND MONTH(JOINING_DATE) = 2;
why ?