{"id":596,"date":"2019-12-04T19:11:48","date_gmt":"2019-12-04T13:41:48","guid":{"rendered":"http:\/\/http:\/\/artoftesting.com\/\/?p=596"},"modified":"2025-05-12T14:24:58","modified_gmt":"2025-05-12T08:54:58","slug":"sql-queries-for-interview","status":"publish","type":"post","link":"https:\/\/artoftesting.com\/sql-queries-for-interview","title":{"rendered":"Top 50 SQL Query Interview Questions and Answers for Practice"},"content":{"rendered":"\n<p>Hello friends! in this post, we will see some of the most common SQL queries asked in interviews. Whether you are a DBA, developer, tester, or data analyst, these <strong>SQL query interview questions and answers<\/strong> are going to help you. <br><br>In fact, I have been asked most of these questions during interviews in the different phases of my career.<\/p>\n\n\n\n<p>If you want to skip the basic questions and start with some tricky SQL queries then you can directly move to our <a href=\"http:\/\/artoftesting.com\/sql-queries-for-interview#SQL_Query_Interview_Questions_for_Experienced\">SQL queries interview questions for the experienced<\/a> section.&nbsp;<\/p>\n\n\n\n<p>Consider the below two tables for reference while trying to solve the <strong>SQL queries for practice<\/strong>.<\/p>\n\n\n\n<p><strong>Table &#8211; EmployeeDetails<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table class=\"has-background\" style=\"background-color:#e9fbe5\"><tbody><tr><th class=\"has-text-align-left\" data-align=\"left\">EmpId<\/th><th class=\"has-text-align-left\" data-align=\"left\">FullName<\/th><th class=\"has-text-align-left\" data-align=\"left\">ManagerId<\/th><th class=\"has-text-align-left\" data-align=\"left\">DateOfJoining<\/th><td><strong>City<\/strong><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">121<\/td><td class=\"has-text-align-left\" data-align=\"left\">John Snow<\/td><td class=\"has-text-align-left\" data-align=\"left\">321<\/td><td class=\"has-text-align-left\" data-align=\"left\">01\/31\/2019<\/td><td>Toronto<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">321<\/td><td class=\"has-text-align-left\" data-align=\"left\">Walter White<\/td><td class=\"has-text-align-left\" data-align=\"left\">986<\/td><td class=\"has-text-align-left\" data-align=\"left\">01\/30\/2020<\/td><td>California<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">421<\/td><td class=\"has-text-align-left\" data-align=\"left\">Kuldeep Rana<\/td><td class=\"has-text-align-left\" data-align=\"left\">876<\/td><td class=\"has-text-align-left\" data-align=\"left\">27\/11\/2021<\/td><td>New Delhi<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><br><strong>Table &#8211; EmployeeSalary<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table class=\"has-background\" style=\"background-color:#e9fbe5\"><tbody><tr><th class=\"has-text-align-left\" data-align=\"left\">EmpId<\/th><th class=\"has-text-align-left\" data-align=\"left\">Project<\/th><th class=\"has-text-align-left\" data-align=\"left\">Salary<\/th><td><strong>Variable<\/strong><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">121<\/td><td class=\"has-text-align-left\" data-align=\"left\">P1<\/td><td class=\"has-text-align-left\" data-align=\"left\">8000<\/td><td>500<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">321<\/td><td class=\"has-text-align-left\" data-align=\"left\">P2<\/td><td class=\"has-text-align-left\" data-align=\"left\">10000<\/td><td>1000<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">421<\/td><td class=\"has-text-align-left\" data-align=\"left\">P1<\/td><td class=\"has-text-align-left\" data-align=\"left\">12000<\/td><td>0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><br>For your convenience, I have compiled the top 10 questions for you. You can try solving these questions and click on the links to go to their respective answers.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"#sql_minus_operator\">SQL Query to fetch records that are present in one table but not in another table.<\/a><\/li>\n\n\n\n<li><a href=\"#is_null\">SQL query to fetch all the employees who are not working on any project.<\/a><\/li>\n\n\n\n<li><a href=\"#date\">SQL query to fetch all the Employees from EmployeeDetails who joined in the Year 2020.<\/a><\/li>\n\n\n\n<li><a href=\"#exists\">Fetch all employees from EmployeeDetails who have a salary record in EmployeeSalary.<\/a><\/li>\n\n\n\n<li><a href=\"#groupby\">Write an SQL query to fetch a project-wise count of employees.<\/a><\/li>\n\n\n\n<li><a href=\"#leftjoin\">Fetch employee names and salaries even if the salary value is not present for the employee.<\/a><\/li>\n\n\n\n<li><a href=\"#employee_also_manager\">Write an SQL query to fetch all the Employees who are also managers.<\/a><\/li>\n\n\n\n<li><a href=\"#remove_duplicate\">Write an SQL query to fetch duplicate records from EmployeeDetails.<\/a><\/li>\n\n\n\n<li><a href=\"#odd_rows\">Write an SQL query to fetch only odd rows from the table.<\/a><\/li>\n\n\n\n<li><a href=\"#3rd_highest_salary\">Write a query to find the 3rd highest salary from a table without top or limit keyword.<\/a><br><br><\/li>\n<\/ol>\n\n\n\n<p>Or, you can also jump to our below two sections on SQL query interview questions for freshers and experienced professionals.<br><\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_73 counter-flat ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\"><p class=\"ez-toc-title\" style=\"cursor:inherit\">Content<\/p>\n<\/div><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/artoftesting.com\/sql-queries-for-interview\/#SQL_Query_Interview_Questions_for_Freshers\" title=\"SQL Query Interview Questions for Freshers\">SQL Query Interview Questions for Freshers<\/a><\/li><li class='ez-toc-page-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/artoftesting.com\/sql-queries-for-interview\/#SQL_Query_Interview_Questions_for_Experienced\" title=\"SQL Query Interview Questions for Experienced\">SQL Query Interview Questions for Experienced<\/a><\/li><li class='ez-toc-page-1'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/artoftesting.com\/sql-queries-for-interview\/#Scenario-based_SQL_Query_Interview_Questions\" title=\"Scenario-based SQL Query Interview Questions\">Scenario-based SQL Query Interview Questions<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"SQL_Query_Interview_Questions_for_Freshers\"><\/span>SQL Query Interview Questions for Freshers<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"728\" height=\"90\" src=\"http:\/\/artoftesting.com\/wp-content\/uploads\/2020\/07\/sql-query-interview-questions-for-freshers.jpg\" alt=\"SQL Query Interview Questions for freshers\" class=\"wp-image-3619\" srcset=\"https:\/\/artoftesting.com\/wp-content\/uploads\/2020\/07\/sql-query-interview-questions-for-freshers.jpg 728w, https:\/\/artoftesting.com\/wp-content\/uploads\/2020\/07\/sql-query-interview-questions-for-freshers-300x37.jpg 300w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/><\/figure>\n\n\n\n<p>Here is a list of top SQL query interview questions and answers for fresher candidates that will help them in their interviews. In these queries, we will focus on the basic SQL commands only.<br><br><\/p>\n\n\n\n<p><strong>1. Write an SQL query to fetch the EmpId and FullName of all the employees working under the Manager with id &#8211; \u2018986\u2019.<\/strong><br>We can use the EmployeeDetails table to fetch the employee details with a where clause for the manager-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT  EmpId, FullName\nFROM EmployeeDetails\nWHERE ManagerId = 986;\n<\/pre><\/div>\n\n\n<p><br><br><strong>2. Write an SQL query to fetch the different projects available from the EmployeeSalary table.<\/strong><br>While referring to the EmployeeSalary table, we can see that this table contains project values corresponding to each employee, or we can say that we will have duplicate project values while selecting Project values from this table. <br><br>So, we will use the distinct clause to get the unique values of the Project.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT DISTINCT(Project)\nFROM EmployeeSalary;\n<\/pre><\/div>\n\n\n<p><br><br><strong>3. Write an SQL query to fetch the count of employees working in project &#8216;P1&#8217;.<\/strong><br>Here, we would be using aggregate function count() with the SQL <strong>where<\/strong> clause-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT COUNT(*) \nFROM EmployeeSalary \nWHERE Project = 'P1';\n<\/pre><\/div>\n\n\n<p><br><br><strong>4. Write an SQL query to find the maximum, minimum, and average salary of the employees.<br><\/strong>We can use the aggregate function of SQL to fetch the max, min, and average values-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT Max(Salary), \nMin(Salary), \nAVG(Salary) \nFROM EmployeeSalary;\n<\/pre><\/div>\n\n\n<p><br><br><strong>5. Write an SQL query to find the employee id whose salary lies in the range of 9000 and 15000.<\/strong><br>Here, we can use the &#8216;Between&#8217; operator with a where clause.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT EmpId, Salary\nFROM EmployeeSalary\nWHERE Salary BETWEEN 9000 AND 15000;\n<\/pre><\/div>\n\n\n<p><br><br><strong>6. Write an SQL query to fetch those employees who live in Toronto and work under the manager with ManagerId &#8211; 321.<\/strong><br>Since we have to satisfy both the conditions &#8211; employees living in \u2018Toronto\u2019 and working in Project \u2018P2\u2019. So, we will use the AND operator here-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT EmpId, City, ManagerId\nFROM EmployeeDetails\nWHERE City='Toronto' AND ManagerId='321';\n<\/pre><\/div>\n\n\n<p><br><br><strong>7. <strong>Write an SQL query to<\/strong><\/strong> f<strong>etch all the employees who either live in California or work under a manager with ManagerId &#8211; 321.<\/strong><br>This interview question requires us to satisfy either of the conditions &#8211; employees living in \u2018California\u2019 and working under Manager with ManagerId &#8211; 321. So, we will use the OR operator here-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT EmpId, City, ManagerId\nFROM EmployeeDetails\nWHERE City='California' OR ManagerId='321';\n<\/pre><\/div>\n\n\n<p><br><br><strong>8. Write an SQL query to fetch all those employees who work on Projects other than P1.<\/strong><br>Here, we can use the NOT operator to fetch the rows which are not satisfying the given condition.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT EmpId\nFROM EmployeeSalary\nWHERE NOT Project='P1';\n<\/pre><\/div>\n\n\n<p><br>Or using the &#8216;not equal to&#8217; operator-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT EmpId\nFROM EmployeeSalary\nWHERE Project &lt;&gt; 'P1';\n<\/pre><\/div>\n\n\n<p>For the difference between NOT and &lt;&gt; SQL operators, check this link &#8211; <a href=\"https:\/\/answers.splunk.com\/answers\/93873\/difference-between-the-not-and-operators.html\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Difference between the NOT and != operators<\/a>.<\/p>\n\n\n\n<p><br><br><strong>9. Write an SQL query to display the total salary of each employee adding the Salary with Variable value.<\/strong><br>Here, we can simply use the &#8216;+&#8217; operator in SQL. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT EmpId,\nSalary+Variable as TotalSalary \nFROM EmployeeSalary;\n<\/pre><\/div>\n\n\n<p><br><br><strong>10. Write an SQL query to fetch the employees whose name begins with any two characters, followed by a text \u201chn\u201d and ends with any sequence of characters.<\/strong><br>For this question, we can create an SQL query using like operator with \u2018_\u2019 and \u2018%\u2019 wild card characters, where \u2018_\u2019 matches a single character and \u2018%\u2019 matches \u20180 or multiple characters.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT FullName\nFROM EmployeeDetails\nWHERE FullName LIKE \u2018__hn%\u2019;\n<\/pre><\/div>\n\n\n<p><br><br><strong>11. Write an SQL query to fetch all the EmpIds which are present in either of the tables &#8211; \u2018EmployeeDetails\u2019 and \u2018EmployeeSalary&#8217;.<\/strong><br>In order to get unique employee ids from both tables, we can use the Union clause which can combine the results of the two SQL queries and return unique rows.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT EmpId FROM EmployeeDetails\nUNION \nSELECT EmpId FROM EmployeeSalary;\n<\/pre><\/div>\n\n\n<p><br><br><strong>12. <strong>Write an SQL query to fetch common records between two tables.<\/strong><\/strong><br>SQL Server &#8211; Using INTERSECT operator-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT * FROM EmployeeSalary\nINTERSECT\nSELECT * FROM ManagerSalary;\n<\/pre><\/div>\n\n\n<p><br>MySQL &#8211; Since MySQL doesn&#8217;t have INTERSECT operator so we can use the subquery-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT *\nFROM EmployeeSalary\nWHERE EmpId IN \n(SELECT EmpId from ManagerSalary);\n<\/pre><\/div>\n\n\n<p><br><\/p>\n\n\n\n<span id=\"sql_minus_operator\"><\/span>\n\n\n\n<p><strong>13. <strong>Write an SQL query to fetch records that are present in one table but not in another table.<\/strong><\/strong><br>SQL Server &#8211; Using MINUS- operator-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT * FROM EmployeeSalary\nMINUS\nSELECT * FROM ManagerSalary;\n<\/pre><\/div>\n\n\n<p><br>MySQL &#8211; Since MySQL doesn&#8217;t have a MINUS operator so we can use LEFT join-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT EmployeeSalary.*\nFROM EmployeeSalary\nLEFT JOIN\nManagerSalary USING (EmpId)\nWHERE ManagerSalary.EmpId IS NULL;\n<\/pre><\/div>\n\n\n<p><br><br><strong>14. Write an SQL query to fetch the EmpIds that are present in both the tables &#8211;&nbsp; \u2018EmployeeDetails\u2019 and \u2018EmployeeSalary.<\/strong><br>Using subquery-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT EmpId FROM \nEmployeeDetails \nwhere EmpId IN \n(SELECT EmpId FROM EmployeeSalary);\n<\/pre><\/div>\n\n\n<p><br><br><strong>15. Write an SQL query to fetch the EmpIds that are present in EmployeeDetails but not in EmployeeSalary.<\/strong><br>Using subquery-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT EmpId FROM \nEmployeeDetails \nwhere EmpId Not IN \n(SELECT EmpId FROM EmployeeSalary);\n<\/pre><\/div>\n\n\n<p><br><br><strong>16. Write an SQL query to fetch the employee&#8217;s full names and replace the space with \u2018-\u2019.<\/strong><br>Using the &#8216;Replace&#8217; function-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT REPLACE(FullName, ' ', '-') \nFROM EmployeeDetails;\n<\/pre><\/div>\n\n\n<p><br><br><strong>17. Write an SQL query to fetch the position of a given character(s) in a field.<\/strong><br>Using the &#8216;Instr&#8217; function-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT INSTR(FullName, 'Snow')\nFROM EmployeeDetails;\n<\/pre><\/div>\n\n\n<p><br><br><strong>18. Write an SQL query to display both the EmpId and ManagerId together.<\/strong><br>Here we can use the CONCAT command.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT CONCAT(EmpId, ManagerId) as NewId\nFROM EmployeeDetails;\n<\/pre><\/div>\n\n\n<p><br><br><strong>19. Write a query to fetch only the first name(string before space) from the FullName column of the EmployeeDetails table.<\/strong><br>In this question, we are required to first fetch the location of the space character in the FullName field and then extract the first name out of the FullName field. <br><br>For finding the location we will use the LOCATE method in MySQL and CHARINDEX in SQL SERVER and for fetching the string before space, we will use the SUBSTRING OR MID method.<br><br>MySQL &#8211; using MID<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT MID(FullName, 1, LOCATE(' ',FullName)) \nFROM EmployeeDetails;\n<\/pre><\/div>\n\n\n<p><br>SQL Server &#8211; using SUBSTRING<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT SUBSTRING(FullName, 1, CHARINDEX(' ',FullName)) \nFROM EmployeeDetails;\n<\/pre><\/div>\n\n\n<p><br><br><strong>20. Write an SQL query to uppercase the name of the employee and lowercase the city values.<\/strong><br>We can use SQL Upper and Lower functions to achieve the intended results.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT UPPER(FullName), LOWER(City) \nFROM EmployeeDetails;\n<\/pre><\/div>\n\n\n<p><br><br><strong>21. Write an SQL query to find the count of the total occurrences of a particular character &#8211; \u2018n\u2019 in the FullName field.<\/strong><br>Here, we can use the &#8216;Length&#8217; function. We can subtract the total length of the FullName field from the length of the FullName after replacing the character &#8211; \u2018n\u2019.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT FullName, \nLENGTH(FullName) - LENGTH(REPLACE(FullName, 'n', ''))\nFROM EmployeeDetails;\n<\/pre><\/div>\n\n\n<p><br><br><strong>22. Write an SQL query to update the employee names by removing leading and trailing spaces.<\/strong><br>Using the &#8216;Update&#8217; command with the &#8216;LTRIM&#8217; and &#8216;RTRIM&#8217; functions.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nUPDATE EmployeeDetails \nSET FullName = LTRIM(RTRIM(FullName));\n<\/pre><\/div>\n\n\n<p> <br><\/p>\n\n\n\n<p><strong>23. Write an SQL query to update the Salary field by value 10% in case of &#8216;Employee&#8217; and 5% in case of &#8216;Manager&#8217;.<\/strong><br>We can use the <code>CASE<\/code> statement within an <code>UPDATE<\/code> query to update multiple rows based on different conditions.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; gutter: false; title: ; notranslate\" title=\"\">\nUPDATE Employees\nSET Salary = CASE\n               WHEN Role = 'Manager' THEN Salary * 1.05\n               WHEN Role = 'Developer' THEN Salary * 1.10\n          ELSE Salary\n END;\n<\/pre><\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<span id=\"is_null\"><\/span>\n\n\n\n<p><strong>24. Fetch all the employees who are not working on any project.<\/strong><br>This is one of the very basic interview questions in which the interviewer wants to see if the person knows about the commonly used &#8211; Is NULL operator.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT EmpId \nFROM EmployeeSalary \nWHERE Project IS NULL;\n<\/pre><\/div>\n\n\n<p><br><br><strong>25. Write an SQL query to fetch employee names having a salary greater than or equal to 5000 and less than or equal to 10000.<\/strong><br>Here, we will use BETWEEN in the &#8216;where&#8217; clause to return the EmpId of the employees with salary satisfying the required criteria and then use it as a subquery to find the fullName of the employee from the EmployeeDetails table.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT FullName \nFROM EmployeeDetails \nWHERE EmpId IN \n(SELECT EmpId FROM EmployeeSalary \nWHERE Salary BETWEEN 5000 AND 10000);\n<\/pre><\/div>\n\n\n<p><br><br><strong>26. Write an SQL query to find the current date-time.<\/strong><br>MySQL-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; gutter: false; title: ; notranslate\" title=\"\">\nSELECT NOW();\n<\/pre><\/div>\n\n\n<p><br>SQL Server-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; gutter: false; title: ; notranslate\" title=\"\">\nSELECT getdate();\n<\/pre><\/div>\n\n\n<p><br>Oracle-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; gutter: false; title: ; notranslate\" title=\"\">\nSELECT SYSDATE FROM DUAL;\n<\/pre><\/div>\n\n\n<p><br><\/p>\n\n\n\n<span id=\"date\"><\/span>\n\n\n\n<p><strong>27. Write an SQL query to fetch all the Employee<\/strong> details from the <strong>EmployeeDetails table who joined in the Year 2020.<\/strong><br>Using BETWEEN for the date range \u201901-01-2020\u2032 AND \u201931-12-2020\u2032-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT * FROM EmployeeDetails\nWHERE DateOfJoining BETWEEN '2020\/01\/01'\nAND '2020\/12\/31';\n<\/pre><\/div>\n\n\n<p><br>Also, we can extract the year part from the joining date (using YEAR in MySQL)-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT * FROM EmployeeDetails \nWHERE YEAR(DateOfJoining) = '2020';\n<\/pre><\/div>\n\n\n<p><br><\/p>\n\n\n\n<span id=\"exists\"><\/span>\n\n\n\n<p><strong>28. Write an SQL query to fetch all employee records from the EmployeeDetails table who have a salary record in the EmployeeSalary table.<\/strong><br>Using \u2018Exists\u2019-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT * FROM EmployeeDetails E\nWHERE EXISTS\n(SELECT * FROM EmployeeSalary S \nWHERE  E.EmpId = S.EmpId);\n<\/pre><\/div>\n\n\n<p><br><\/p>\n\n\n\n<span id=\"groupby\"><\/span>\n\n\n\n<p><strong>29. Write an SQL query to fetch the project-wise count of employees sorted by project\u2019s count in descending order.<\/strong><br>The query has two requirements \u2013 first to fetch the project-wise count and then to sort the result by that count. <br><br>For project-wise count, we will be using the GROUP BY clause and for sorting, we will use the ORDER BY clause on the alias of the project count.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT Project, count(EmpId) EmpProjectCount\nFROM EmployeeSalary\nGROUP BY Project\nORDER BY EmpProjectCount DESC;\n<\/pre><\/div>\n\n\n<p><br><\/p>\n\n\n\n<span id=\"leftjoin\"><\/span>\n\n\n\n<p><strong>30. Write a query to fetch employee names and salary records. Display the employee details even if the salary record is not present for the employee.<\/strong><br>This is again one of the very common interview questions in which the interviewer just wants to check the basic knowledge of SQL JOINS. <br><br>Here, we can use the left join with the EmployeeDetail table on the left side of the EmployeeSalary table.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT E.FullName, S.Salary \nFROM EmployeeDetails E \nLEFT JOIN \nEmployeeSalary S\nON E.EmpId = S.EmpId;\n<\/pre><\/div>\n\n\n<p><br><br><strong>31. Write an SQL query to join 3 tables.<\/strong><br>Considering 3 tables TableA, TableB, and TableC, we can use 2 joins clauses like below-<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"278\" height=\"228\" src=\"http:\/\/artoftesting.com\/wp-content\/uploads\/2021\/02\/sql-join-3-tables.png\" alt=\"sql query interview questions\" class=\"wp-image-4219\"\/><\/figure><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT column1, column2\nFROM TableA\nJOIN TableB ON TableA.Column3 = TableB.Column3\nJOIN TableC ON TableA.Column4 = TableC.Column4;\n<\/pre><\/div>\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>For more questions on SQL Joins, you can also check our top <a href=\"http:\/\/artoftesting.com\/sql-joins-interview-questions\"><strong>SQL Joins Interview Questions<\/strong><\/a>.<\/p>\n<\/blockquote>\n\n\n\n<div style=\"height:38px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"SQL_Query_Interview_Questions_for_Experienced\"><\/span>SQL Query Interview Questions for Experienced<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"728\" height=\"90\" src=\"http:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/sql-query-interview-questions-for-experienced.jpg\" alt=\"SQL Query Interview Questions for experienced\" class=\"wp-image-3617\" srcset=\"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/sql-query-interview-questions-for-experienced.jpg 728w, https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/sql-query-interview-questions-for-experienced-300x37.jpg 300w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/><\/figure>\n\n\n\n<p>Here is a list of some of the most frequently asked SQL query interview questions for experienced professionals. These questions cover SQL queries on advanced SQL JOIN concepts, fetching duplicate rows, odd and even rows, nth highest salary, etc.<\/p>\n\n\n\n<span id=\"employee_also_manager\"><\/span>\n\n\n\n<p><strong>32. Write an SQL query to fetch all the Employees who are also managers from the EmployeeDetails table.<\/strong><br>Here, we have to use Self-Join as the requirement wants us to analyze the EmployeeDetails table as two tables. We will use different aliases &#8216;E&#8217; and &#8216;M&#8217; for the same EmployeeDetails table.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT DISTINCT E.FullName\nFROM EmployeeDetails E\nINNER JOIN EmployeeDetails M\nON E.EmpID = M.ManagerID;\n<\/pre><\/div>\n\n\n<p><br><\/p>\n\n\n\n<span id=\"remove_duplicate\"><\/span>\n\n\n\n<p><strong>33. Write an SQL query to fetch duplicate records from EmployeeDetails (without considering the primary key &#8211; EmpId).<\/strong><br>In order to find duplicate records from the table, we can use GROUP BY on all the fields and then use the HAVING clause to return only those fields whose count is greater than 1 i.e. the rows having duplicate records.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT FullName, ManagerId, DateOfJoining, City, COUNT(*)\nFROM EmployeeDetails\nGROUP BY FullName, ManagerId, DateOfJoining, City\nHAVING COUNT(*) &gt; 1;\n<\/pre><\/div>\n\n\n<p><br><br><strong>34. Write an SQL query to remove duplicates from a table without using a temporary table.<\/strong><br>Here, we can use delete with alias and inner join. We will check for the equality of all the matching records and then remove the row with a higher EmpId.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nDELETE E1 FROM EmployeeDetails E1\nINNER JOIN EmployeeDetails E2 \nWHERE E1.EmpId &gt; E2.EmpId \nAND E1.FullName = E2.FullName \nAND E1.ManagerId = E2.ManagerId\nAND E1.DateOfJoining = E2.DateOfJoining\nAND E1.City = E2.City;\n<\/pre><\/div>\n\n\n<p><br><\/p>\n\n\n\n<span id=\"odd_rows\"><\/span>\n\n\n\n<p><strong>35. Write an SQL query to fetch only odd rows from the table.<\/strong><br>In case we have an auto-increment field e.g. EmpId then we can simply use the below query-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT * FROM EmployeeDetails \nWHERE MOD (EmpId, 2) &lt;&gt; 0;\n<\/pre><\/div>\n\n\n<p><br>In case we don\u2019t have such a field then we can use the below queries.<br><br>Using Row_number in SQL server and checking that the remainder when divided by 2 is 1-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT E.EmpId, E.Project, E.Salary\nFROM (\n    SELECT *, Row_Number() OVER(ORDER BY EmpId) AS RowNumber\n    FROM EmployeeSalary\n) E\nWHERE E.RowNumber % 2 = 1;\n<\/pre><\/div>\n\n\n<p><br>Using a user-defined variable in MySQL-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT *\nFROM (\n      SELECT *, @rowNumber := @rowNumber+ 1 rn\n      FROM EmployeeSalary\n      JOIN (SELECT @rowNumber:= 0) r\n     ) t \nWHERE rn % 2 = 1;\n<\/pre><\/div>\n\n\n<p><br><br><strong>36. Write an SQL query to fetch only even rows from the table.<\/strong><br>In case we have an auto-increment field e.g. EmpId then we can simply use the below query-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT * FROM EmployeeDetails \nWHERE MOD (EmpId, 2) = 0;\n<\/pre><\/div>\n\n\n<p><br>In case we don\u2019t have such a field then we can use the below queries.<br><br>Using Row_number in SQL server and checking that the remainder, when divided by 2, is 1-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT E.EmpId, E.Project, E.Salary\nFROM (\n    SELECT *, Row_Number() OVER(ORDER BY EmpId) AS RowNumber\n    FROM EmployeeSalary\n) E\nWHERE E.RowNumber % 2 = 0;\n<\/pre><\/div>\n\n\n<p><br>Using a user-defined variable in MySQL-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT *\nFROM (\n      SELECT *, @rowNumber := @rowNumber+ 1 rn\n      FROM EmployeeSalary\n      JOIN (SELECT @rowNumber:= 0) r\n     ) t \nWHERE rn % 2 = 0;\n<\/pre><\/div>\n\n\n<p><br><br><strong>37. Write an SQL query to create a new table with data and structure copied from another table<\/strong>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nCREATE TABLE NewTable \nSELECT * FROM EmployeeSalary;\n<\/pre><\/div>\n\n\n<p><br><br><strong>38. Write an SQL query to create an empty table with the same structure as some other table.<\/strong><br>Here, we can use the same query as above with the False \u2018WHERE\u2019 condition-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nCREATE TABLE NewTable \nSELECT * FROM EmployeeSalary where 1=0;\n<\/pre><\/div>\n\n\n<p><br><br><strong>39. Write an SQL query to fetch top n records.<\/strong><br>In MySQL using LIMIT-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT *\nFROM EmployeeSalary\nORDER BY Salary DESC LIMIT N;\n<\/pre><\/div>\n\n\n<p><br>In SQL server using TOP command-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT TOP N *\nFROM EmployeeSalary\nORDER BY Salary DESC;\n<\/pre><\/div>\n\n\n<p><br><br><strong>40. Write an SQL query to find the nth highest salary from a table.<\/strong><br>Using Top keyword (SQL Server)-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT TOP 1 Salary\nFROM (\n      SELECT DISTINCT TOP N Salary\n      FROM Employee\n      ORDER BY Salary DESC\n      )\nORDER BY Salary ASC;\n<\/pre><\/div>\n\n\n<p><br>Using limit clause(MySQL)-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT Salary\nFROM Employee\nORDER BY Salary DESC LIMIT N-1,1;\n<\/pre><\/div>\n\n\n<p><br><\/p>\n\n\n\n<span id=\"3rd_highest_salary\"><\/span>\n\n\n\n<p><strong>41. Write SQL query to find the 3rd highest salary from a table without using the TOP\/limit keyword.<br><\/strong>This is one of the most commonly asked interview questions. For this, we will use a correlated subquery. <br><br>In order to find the 3rd highest salary, we will find the salary value until the inner query returns a count of 2 rows having a salary greater than other distinct salaries.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT Salary\nFROM EmployeeSalary Emp1\nWHERE 2 = (\n                SELECT COUNT( DISTINCT ( Emp2.Salary ) )\n                FROM EmployeeSalary Emp2\n                WHERE Emp2.Salary &gt; Emp1.Salary\n            )\n<\/pre><\/div>\n\n\n<p><br>For the nth highest salary-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT Salary\nFROM EmployeeSalary Emp1\nWHERE N-1 = (\n                SELECT COUNT( DISTINCT ( Emp2.Salary ) )\n                FROM EmployeeSalary Emp2\n                WHERE Emp2.Salary &gt; Emp1.Salary\n            )\n<\/pre><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Scenario-based_SQL_Query_Interview_Questions\"><\/span>Scenario-based SQL Query Interview Questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Let&#8217;s see some interview questions based on different scenarios. The questions are of varying difficulty levels, and the goal is to prepare you for different real-time scenario-based questions.<\/p>\n\n\n\n<p><strong>42. Consider a <code>SalesData<\/code> with columns <code>SaleID<\/code>, <code>ProductID<\/code>, <code>RegionID<\/code>, <code>SaleAmount<\/code>. Write a query to find the total sales amount for each product in each region.<br><\/strong>The following query sums up <code>SaleAmount<\/code> for each combination of <code>ProductID<\/code> and <code>RegionID<\/code>, giving an insight into the total sales per product per region.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT ProductID, RegionID, SUM(SaleAmount) AS TotalSales \nFROM SalesData \nGROUP BY ProductID, RegionID;\n<\/pre><\/div>\n\n\n<p><br><br><strong>43. Write a query to find employees who earn more than their managers.<br><\/strong>Here, we will write a query that joins the <code>EmployeeDetails<\/code> table with itself to compare the salaries of employees with their respective managers.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT E.Name AS EmployeeName, \nM.Name AS ManagerName, \nE.Salary AS EmployeeSalary, \nM.Salary AS ManagerSalary \nFROM EmployeeDetails E JOIN EmployeeDetails M \nON E.ManagerID = M.EmployeeID \nWHERE E.Salary &gt; M.Salary;\n<\/pre><\/div>\n\n\n<p><br><br><strong>44. Consider a <code>BookCheckout<\/code> table with columns &#8211; <code>CheckoutID<\/code>, <code>MemberID<\/code>, <code>BookID<\/code>, <code>CheckoutDate<\/code>, <code>ReturnDate<\/code>. Write an SQL query to find the number of books checked out by each member.<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT MemberID, COUNT(*) AS NumberOfBooksCheckedOut \nFROM BookCheckout \nGROUP BY MemberID;\n<\/pre><\/div>\n\n\n<p><br><br><strong>45. Consider a <code>StudentGrades<\/code> table with columns &#8211; <code>StudentID<\/code>, <code>CourseID<\/code>, <code>Grade<\/code>. Write a query to find students who have scored an &#8216;A&#8217; in more than three courses.<br><\/strong>Here we will write an SQL query that filters students who have received an &#8216;A&#8217; grade and groups them by <code>StudentID<\/code>, counting the number of &#8216;A&#8217; grades per student.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT StudentID FROM StudentGrades \nWHERE Grade = 'A' \nGROUP BY StudentID \nHAVING COUNT(*) &gt; 3;\n<\/pre><\/div>\n\n\n<p><br><br><strong>46. Consider a table <code>OrderDetails<\/code> with columns &#8211; <code>OrderID<\/code>, <code>CustomerID<\/code>, <code>ProductID<\/code>, <code>OrderDate<\/code>, <code>Quantity<\/code>, <code>Price<\/code>. Write a query to find the average order value for each customer.<br><\/strong>The below query calculates the average order value (quantity multiplied by price) for each customer.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT CustomerID, AVG(Quantity * Price) AS AvgOrderValue \nFROM OrderDetails \nGROUP BY CustomerID;\n<\/pre><\/div>\n\n\n<p><br><br><strong>47. Consider a table <code>PatientVisits<\/code> with Columns <code>VisitID<\/code>, <code>PatientID<\/code>, <code>DoctorID<\/code>, <code>VisitDate<\/code>, <code>Diagnosis<\/code>.  Write a query to find the latest visit date for each patient.<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT PatientID, MAX(VisitDate) AS LatestVisitDate \nFROM PatientVisits \nGROUP BY PatientID;\n<\/pre><\/div>\n\n\n<p><br><br><strong>48. For a table <code>FlightBookings<\/code> with columns &#8211; <code>BookingID<\/code>, <code>FlightID<\/code>, <code>PassengerID<\/code>, <code>BookingDate<\/code>, <code>TravelDate<\/code>, <code>Class<\/code>, write a query to count the number of bookings for each flight class.<br><\/strong>Here, we will write an SQL query that groups the bookings by <code>Class<\/code> and counts the number of bookings in each class.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT Class, COUNT(*) AS NumberOfBookings \nFROM FlightBookings \nGROUP BY Class;\n<\/pre><\/div>\n\n\n<p><br><br><strong>49. Consider a table <code>FoodOrders<\/code> with columns &#8211; <code>OrderID<\/code>, <code>TableID<\/code>, <code>MenuItemID<\/code>, <code>OrderTime<\/code>, <code>Quantity<\/code>. Write a query to find the most ordered menu item.<br><\/strong>For the desired output, we will group the orders by <code>MenuItemID<\/code> and then sort the results by the count in descending order, fetching the top result.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT MenuItemID \nFROM FoodOrders \nGROUP BY MenuItemID \nORDER BY COUNT(*) DESC \nLIMIT 1;\n<\/pre><\/div>\n\n\n<p><br><br><strong>50. Consider a table <code>Transactions<\/code> with columns &#8211; <code>TransactionID<\/code>, <code>CustomerID<\/code>, <code>ProductID<\/code>, <code>TransactionDate<\/code>, <code>Amount<\/code>. Write a query to find the total transaction amount for each month.<br><\/strong>The below query sums the <code>Amount<\/code> for each month, giving a monthly total transaction amount.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT MONTH(TransactionDate) AS Month, \nSUM(Amount) AS TotalAmount \nFROM Transactions \nGROUP BY MONTH(TransactionDate);\n<\/pre><\/div>\n\n\n<p><br><br><strong>51. Consider a table <code>EmployeeAttendance<\/code> with columns &#8211; <code>AttendanceID<\/code>, <code>EmployeeID<\/code>, <code>Date<\/code>, <code>Status<\/code>. Write a query to find employees with more than 5 absences in a month.<br><\/strong>This query filters the records for absent status, groups them by <code>EmployeeID<\/code> and month, and counts absences, filtering for more than 5 absences.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT EmployeeID, \nMONTH(Date) AS Month, \nCOUNT(*) AS Absences \nFROM EmployeeAttendance \nWHERE Status = 'Absent' \nGROUP BY EmployeeID, MONTH(Date) \nHAVING COUNT(*) &gt; 5;\n<\/pre><\/div>\n\n\n<p><br>This concludes our post on frequently asked <strong>SQL query interview questions and answers<\/strong>. I hope you practice these questions and ace your database interviews. <br><br>If you feel, we have missed any of the common <strong>interview questions on SQL<\/strong> then do let us know in the comments and we will add those questions to our list.<br><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Do check our article on &#8211;&nbsp;<a href=\"http:\/\/artoftesting.com\/dbms-interview-questions\"><strong>RDBMS Interview Questions<\/strong><\/a>, focussing on the theoretical interview questions based on the DBMS and SQL concepts.<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Hello friends! in this post, we will see some of the most common SQL queries asked in interviews. Whether you are a DBA, developer, tester, or data analyst, these SQL query interview questions and answers are going to help you. In fact, I have been asked most of these questions during interviews in the different &#8230; <a title=\"Top 50 SQL Query Interview Questions and Answers for Practice\" class=\"read-more\" href=\"https:\/\/artoftesting.com\/sql-queries-for-interview\" aria-label=\"Read more about Top 50 SQL Query Interview Questions and Answers for Practice\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":3253,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,21],"tags":[],"class_list":["post-596","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-interview","category-sql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Top 50 SQL Query Interview Questions for Practice [2025]<\/title>\n<meta name=\"description\" content=\"Practice real-world queries on joins, subqueries, aggregations, and more to ace your next database job interview!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/artoftesting.com\/sql-queries-for-interview\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Top 50 SQL Query Interview Questions for Practice [2025]\" \/>\n<meta property=\"og:description\" content=\"Practice real-world queries on joins, subqueries, aggregations, and more to ace your next database job interview!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/artoftesting.com\/sql-queries-for-interview\" \/>\n<meta property=\"og:site_name\" content=\"ArtOfTesting\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/facebook.com\/artoftesting\" \/>\n<meta property=\"article:published_time\" content=\"2019-12-04T13:41:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-12T08:54:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/SQL-query-interview-questions.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"700\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Kuldeep Rana\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@theartoftesting\" \/>\n<meta name=\"twitter:site\" content=\"@theartoftesting\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kuldeep Rana\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/artoftesting.com\/sql-queries-for-interview#article\",\"isPartOf\":{\"@id\":\"https:\/\/artoftesting.com\/sql-queries-for-interview\"},\"author\":{\"name\":\"Kuldeep Rana\",\"@id\":\"https:\/\/artoftesting.com\/#\/schema\/person\/7846d06225b52c778d160becf65996a5\"},\"headline\":\"Top 50 SQL Query Interview Questions and Answers for Practice\",\"datePublished\":\"2019-12-04T13:41:48+00:00\",\"dateModified\":\"2025-05-12T08:54:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/artoftesting.com\/sql-queries-for-interview\"},\"wordCount\":2566,\"commentCount\":70,\"publisher\":{\"@id\":\"https:\/\/artoftesting.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/artoftesting.com\/sql-queries-for-interview#primaryimage\"},\"thumbnailUrl\":\"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/SQL-query-interview-questions.jpg\",\"articleSection\":[\"Interview\",\"SQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/artoftesting.com\/sql-queries-for-interview#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/artoftesting.com\/sql-queries-for-interview\",\"url\":\"https:\/\/artoftesting.com\/sql-queries-for-interview\",\"name\":\"Top 50 SQL Query Interview Questions for Practice [2025]\",\"isPartOf\":{\"@id\":\"https:\/\/artoftesting.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/artoftesting.com\/sql-queries-for-interview#primaryimage\"},\"image\":{\"@id\":\"https:\/\/artoftesting.com\/sql-queries-for-interview#primaryimage\"},\"thumbnailUrl\":\"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/SQL-query-interview-questions.jpg\",\"datePublished\":\"2019-12-04T13:41:48+00:00\",\"dateModified\":\"2025-05-12T08:54:58+00:00\",\"description\":\"Practice real-world queries on joins, subqueries, aggregations, and more to ace your next database job interview!\",\"breadcrumb\":{\"@id\":\"https:\/\/artoftesting.com\/sql-queries-for-interview#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/artoftesting.com\/sql-queries-for-interview\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/artoftesting.com\/sql-queries-for-interview#primaryimage\",\"url\":\"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/SQL-query-interview-questions.jpg\",\"contentUrl\":\"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/SQL-query-interview-questions.jpg\",\"width\":700,\"height\":400,\"caption\":\"SQL query interview questions\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/artoftesting.com\/sql-queries-for-interview#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/artoftesting.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Interview\",\"item\":\"https:\/\/artoftesting.com\/category\/interview\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Top 50 SQL Query Interview Questions and Answers for Practice\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/artoftesting.com\/#website\",\"url\":\"https:\/\/artoftesting.com\/\",\"name\":\"ArtOfTesting\",\"description\":\"A Beginners Guide to Testing\",\"publisher\":{\"@id\":\"https:\/\/artoftesting.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/artoftesting.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/artoftesting.com\/#organization\",\"name\":\"ArtOfTesting\",\"url\":\"https:\/\/artoftesting.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/artoftesting.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/Artoftesting_logo.png\",\"contentUrl\":\"http:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/Artoftesting_logo.png\",\"width\":400,\"height\":60,\"caption\":\"ArtOfTesting\"},\"image\":{\"@id\":\"https:\/\/artoftesting.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/facebook.com\/artoftesting\",\"https:\/\/x.com\/theartoftesting\",\"https:\/\/www.linkedin.com\/groups\/4797819\/\",\"https:\/\/in.pinterest.com\/artoftesting\/\",\"https:\/\/www.youtube.com\/channel\/UCQ9PUVenvvyrUdDQ9yKn31Q\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/artoftesting.com\/#\/schema\/person\/7846d06225b52c778d160becf65996a5\",\"name\":\"Kuldeep Rana\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/artoftesting.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cb5979a4b81ca7739c75080e473fad391a8665364e72abaddec9002dd4553326?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cb5979a4b81ca7739c75080e473fad391a8665364e72abaddec9002dd4553326?s=96&d=mm&r=g\",\"caption\":\"Kuldeep Rana\"},\"description\":\"Kuldeep is the founder and lead author of ArtOfTesting. He is skilled in test automation, performance testing, big data, and CI-CD. He brings his decade of experience to his current role where he is dedicated to educating the QA professionals.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Top 50 SQL Query Interview Questions for Practice [2025]","description":"Practice real-world queries on joins, subqueries, aggregations, and more to ace your next database job interview!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/artoftesting.com\/sql-queries-for-interview","og_locale":"en_US","og_type":"article","og_title":"Top 50 SQL Query Interview Questions for Practice [2025]","og_description":"Practice real-world queries on joins, subqueries, aggregations, and more to ace your next database job interview!","og_url":"https:\/\/artoftesting.com\/sql-queries-for-interview","og_site_name":"ArtOfTesting","article_publisher":"https:\/\/facebook.com\/artoftesting","article_published_time":"2019-12-04T13:41:48+00:00","article_modified_time":"2025-05-12T08:54:58+00:00","og_image":[{"width":700,"height":400,"url":"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/SQL-query-interview-questions.jpg","type":"image\/jpeg"}],"author":"Kuldeep Rana","twitter_card":"summary_large_image","twitter_creator":"@theartoftesting","twitter_site":"@theartoftesting","twitter_misc":{"Written by":"Kuldeep Rana","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/artoftesting.com\/sql-queries-for-interview#article","isPartOf":{"@id":"https:\/\/artoftesting.com\/sql-queries-for-interview"},"author":{"name":"Kuldeep Rana","@id":"https:\/\/artoftesting.com\/#\/schema\/person\/7846d06225b52c778d160becf65996a5"},"headline":"Top 50 SQL Query Interview Questions and Answers for Practice","datePublished":"2019-12-04T13:41:48+00:00","dateModified":"2025-05-12T08:54:58+00:00","mainEntityOfPage":{"@id":"https:\/\/artoftesting.com\/sql-queries-for-interview"},"wordCount":2566,"commentCount":70,"publisher":{"@id":"https:\/\/artoftesting.com\/#organization"},"image":{"@id":"https:\/\/artoftesting.com\/sql-queries-for-interview#primaryimage"},"thumbnailUrl":"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/SQL-query-interview-questions.jpg","articleSection":["Interview","SQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/artoftesting.com\/sql-queries-for-interview#respond"]}]},{"@type":"WebPage","@id":"https:\/\/artoftesting.com\/sql-queries-for-interview","url":"https:\/\/artoftesting.com\/sql-queries-for-interview","name":"Top 50 SQL Query Interview Questions for Practice [2025]","isPartOf":{"@id":"https:\/\/artoftesting.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/artoftesting.com\/sql-queries-for-interview#primaryimage"},"image":{"@id":"https:\/\/artoftesting.com\/sql-queries-for-interview#primaryimage"},"thumbnailUrl":"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/SQL-query-interview-questions.jpg","datePublished":"2019-12-04T13:41:48+00:00","dateModified":"2025-05-12T08:54:58+00:00","description":"Practice real-world queries on joins, subqueries, aggregations, and more to ace your next database job interview!","breadcrumb":{"@id":"https:\/\/artoftesting.com\/sql-queries-for-interview#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/artoftesting.com\/sql-queries-for-interview"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/artoftesting.com\/sql-queries-for-interview#primaryimage","url":"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/SQL-query-interview-questions.jpg","contentUrl":"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/SQL-query-interview-questions.jpg","width":700,"height":400,"caption":"SQL query interview questions"},{"@type":"BreadcrumbList","@id":"https:\/\/artoftesting.com\/sql-queries-for-interview#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/artoftesting.com\/"},{"@type":"ListItem","position":2,"name":"Interview","item":"https:\/\/artoftesting.com\/category\/interview"},{"@type":"ListItem","position":3,"name":"Top 50 SQL Query Interview Questions and Answers for Practice"}]},{"@type":"WebSite","@id":"https:\/\/artoftesting.com\/#website","url":"https:\/\/artoftesting.com\/","name":"ArtOfTesting","description":"A Beginners Guide to Testing","publisher":{"@id":"https:\/\/artoftesting.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/artoftesting.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/artoftesting.com\/#organization","name":"ArtOfTesting","url":"https:\/\/artoftesting.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/artoftesting.com\/#\/schema\/logo\/image\/","url":"http:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/Artoftesting_logo.png","contentUrl":"http:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/Artoftesting_logo.png","width":400,"height":60,"caption":"ArtOfTesting"},"image":{"@id":"https:\/\/artoftesting.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/facebook.com\/artoftesting","https:\/\/x.com\/theartoftesting","https:\/\/www.linkedin.com\/groups\/4797819\/","https:\/\/in.pinterest.com\/artoftesting\/","https:\/\/www.youtube.com\/channel\/UCQ9PUVenvvyrUdDQ9yKn31Q"]},{"@type":"Person","@id":"https:\/\/artoftesting.com\/#\/schema\/person\/7846d06225b52c778d160becf65996a5","name":"Kuldeep Rana","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/artoftesting.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cb5979a4b81ca7739c75080e473fad391a8665364e72abaddec9002dd4553326?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cb5979a4b81ca7739c75080e473fad391a8665364e72abaddec9002dd4553326?s=96&d=mm&r=g","caption":"Kuldeep Rana"},"description":"Kuldeep is the founder and lead author of ArtOfTesting. He is skilled in test automation, performance testing, big data, and CI-CD. He brings his decade of experience to his current role where he is dedicated to educating the QA professionals."}]}},"_links":{"self":[{"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/posts\/596","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/comments?post=596"}],"version-history":[{"count":34,"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/posts\/596\/revisions"}],"predecessor-version":[{"id":8478,"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/posts\/596\/revisions\/8478"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/media\/3253"}],"wp:attachment":[{"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/media?parent=596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/categories?post=596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/tags?post=596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}