Top 50 SQL Query Interview Questions and Answers – Practice Now

SQL (Structured Query Language) is the main skill to work with tools like MySQL, Oracle, and PostgreSQL. It's super helpful for pulling data, running analyses, or changing database setups. Want to nail your SQL interview? Check out our post on 50 SQL query interview questions and answers.
Meenakshi Agarwal
By
Meenakshi Agarwal
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large...
27 Min Read

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_IDFIRST_NAMELAST_NAMESALARYJOINING_DATEDEPARTMENT
001MonikaArora1000002021-02-20 09:00:00HR
002NiharikaVerma800002021-06-11 09:00:00Admin
003VishalSinghal3000002021-02-20 09:00:00HR
004AmitabhSingh5000002021-02-20 09:00:00Admin
005VivekBhati5000002021-06-11 09:00:00Admin
006VipulDiwan2000002021-06-11 09:00:00Account
007SatishKumar750002021-01-20 09:00:00Account
008GeetikaChauhan900002021-04-11 09:00:00Admin
Worker Table

Sample Table – Bonus

WORKER_REF_IDBONUS_DATEBONUS_AMOUNT
12023-02-20 00:00:005000
22023-06-11 00:00:003000
32023-02-20 00:00:004000
12023-02-20 00:00:004500
22023-06-11 00:00:003500
Bonus Table

Sample Table – Title

WORKER_REF_IDWORKER_TITLEAFFECTED_FROM
1Manager2023-02-20 00:00:00
2Executive2023-06-11 00:00:00
8Executive2023-06-11 00:00:00
5Manager2023-06-11 00:00:00
4Asst. Manager2023-06-11 00:00:00
7Executive2023-06-11 00:00:00
6Lead2023-06-11 00:00:00
3Lead2023-06-11 00:00:00
Title Table

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

Initialize Test Data
Run the following script to create test tables and insert data.
Invalid SQL query.
Tables and data initialized successfully!
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.

Query #1
Invalid SQL query.
SQL query is valid!
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.

Query #2
Invalid SQL query.
SQL query is valid!
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.

Query #3
Invalid SQL query.
SQL query is valid!
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.

Query #4
Invalid SQL query.
SQL query is valid!
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.

Query #5
Invalid SQL query.
SQL query is valid!
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.

Query #6
Invalid SQL query.
SQL query is valid!
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:

Query #7
Invalid SQL query.
SQL query is valid!
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.

Query #8
Invalid SQL query.
SQL query is valid!
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.

Query #9
Invalid SQL query.
SQL query is valid!
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.

Query #10
Invalid SQL query.
SQL query is valid!
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.

Query #11
Invalid SQL query.
SQL query is valid!
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.

Query #12
Invalid SQL query.
SQL query is valid!
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.

Query #13
Invalid SQL query.
SQL query is valid!
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.

Query #14
Invalid SQL query.
SQL query is valid!
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’.

Query #15
Invalid SQL query.
SQL query is valid!
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.

Query #16
Invalid SQL query.
SQL query is valid!
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.

Query #17
Invalid SQL query.
SQL query is valid!
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.

Query #18
Invalid SQL query.
SQL query is valid!
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.

Query #19
Invalid SQL query.
SQL query is valid!
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.

Query #20
Invalid SQL query.
SQL query is valid!
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

Query #21
Invalid SQL query.
SQL query is valid!
Output

22. SQL Query to Fetch Worker Names with Salaries >= 50000 and <= 100000

The required query is:

Query #22
Invalid SQL query.
SQL query is valid!
Output

23. SQL Query to List Worker Count Per Department in Descending Order

The required query is:

Query #23
Invalid SQL query.
SQL query is valid!
Output

24. SQL Query to Print Worker Details Who Are Also Managers

The required query is:

Query #24
Invalid SQL query.
SQL query is valid!
Output

25. SQL Query to Fetch Duplicate Records with Matching Data in Specific Fields of a Table

The required query is:

Query #25
Invalid SQL query.
SQL query is valid!
Output

26. SQL Query to Show Only Odd Rows from a Table

The required query is:

Query #26
Invalid SQL query.
SQL query is valid!
Output

27. SQL Query to Show Only Even Rows from a Table

The required query is:

Query #27
Invalid SQL query.
SQL query is valid!
Output

28. SQL Query to Clone a New Table from Another Table

The general query to clone a table with data is:

Query #28
Invalid SQL query.
SQL query is valid!
Output

29. SQL Query to Display Intersecting Records of Two Tables

The required query is:

Query #29
Invalid SQL query.
SQL query is valid!
Output

30. SQL Query to Show Records from One Table That Are Not Present in Another Table

The required query is:

Query #30
Invalid SQL query.
SQL query is valid!
Output

31. SQL Query to Show the Current Date and Time

The following query returns the current date:

Query #31
Invalid SQL query.
SQL query is valid!
Output

32. SQL Query to Show the Top n (say 10) Records of a Table

Specify the SQL query in the below code box:

Query #32
Invalid SQL query.
SQL query is valid!
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.

Query #33
Invalid SQL query.
SQL query is valid!
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.

Query #34
Invalid SQL query.
SQL query is valid!
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.

Query #35
Invalid SQL query.
SQL query is valid!
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.

Query #36
Invalid SQL query.
SQL query is valid!
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.

Query #37
Invalid SQL query.
SQL query is valid!
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.

Query #38
Invalid SQL query.
SQL query is valid!
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.

Query #39
Invalid SQL query.
SQL query is valid!
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.

Query #40
Invalid SQL query.
SQL query is valid!
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.

Query #41
Invalid SQL query.
SQL query is valid!
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.

Query #42
Invalid SQL query.
SQL query is valid!
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.

Query #43
Invalid SQL query.
SQL query is valid!
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.

Query #44
Invalid SQL query.
SQL query is valid!
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.

Query #45
Invalid SQL query.
SQL query is valid!
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.

Query #46
Invalid SQL query.
SQL query is valid!
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.

Query #47
Invalid SQL query.
SQL query is valid!
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.

Query #48
Invalid SQL query.
SQL query is valid!
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.

Query #49
Invalid SQL query.
SQL query is valid!
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.

Query #50
Invalid SQL query.
SQL query is valid!
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.

25 QuestionsRead Now

Keep Learning SQL,
TechBeamers.

Share This Article
2 Comments
  • 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 ?

Leave a Reply

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