{"id":17448,"date":"2024-08-05T11:49:53","date_gmt":"2024-08-05T06:19:53","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=17448"},"modified":"2024-12-12T09:17:58","modified_gmt":"2024-12-12T14:17:58","slug":"top-sql-scenario-based-interview-questions-with-answers","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/top-sql-scenario-based-interview-questions-with-answers\/","title":{"rendered":"Top SQL Scenario Based Interview Questions with Answers"},"content":{"rendered":"\n<p>In the competitive world of data management and analysis, SQL remains one of the most vital skills for database professionals. During interviews, employers often test candidates with scenario-based questions to assess their problem-solving abilities and practical knowledge of <a href=\"https:\/\/www.h2kinfosys.com\/blog\/software-testing-sql-interview-questions\/\" data-type=\"post\" data-id=\"17358\">SQL<\/a>. In this blog, we&#8217;ll explore some of the top SQL scenario-based <a href=\"https:\/\/www.h2kinfosys.com\/blog\/sql-reporting-interview-questions\/\" data-type=\"post\" data-id=\"17285\">interview questions<\/a> along with detailed answers to help you prepare effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. Scenario: Extracting Data with Conditions<\/strong><\/h2>\n\n\n\n<p><strong>Question:<\/strong><br>You have a table named <code>Employees<\/code> with columns <code>EmployeeID<\/code>, <code>Name<\/code>, <code>Department<\/code>, and <code>Salary<\/code>. Write an SQL query to find the names of employees who work in the &#8216;HR&#8217; department and have a salary greater than $50,000.<\/p>\n\n\n\n<p><strong>Answer:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sqlCopy code<code>SELECT Name\nFROM Employees\nWHERE Department = 'HR' AND Salary &gt; 50000;\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><br>This query selects the <code>Name<\/code> column from the <code>Employees<\/code> table where the <code>Department<\/code> is &#8216;HR&#8217; and the <code>Salary<\/code> is greater than $50,000. The <code>AND<\/code> operator ensures that both conditions must be true for a record to be included in the result set.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. Scenario: Finding Duplicate Records<\/strong><\/h2>\n\n\n\n<p><strong>Question:<\/strong><br>You have a table called <code>Orders<\/code> with columns <code>OrderID<\/code>, <code>CustomerID<\/code>, <code>OrderDate<\/code>, and <code>OrderAmount<\/code>. Write a query to find duplicate <code>CustomerID<\/code> values.<\/p>\n\n\n\n<p><strong>Answer:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sqlCopy code<code>SELECT CustomerID, COUNT(*)\nFROM Orders\nGROUP BY CustomerID\nHAVING COUNT(*) &gt; 1;\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><br>The query groups the records by <code>CustomerID<\/code> and counts the number of occurrences for each <code>CustomerID<\/code>. The <code>HAVING<\/code> clause filters the results to include only those <code>CustomerID<\/code>s that appear more than once, indicating duplicates.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. Scenario: Aggregating Data<\/strong><\/h2>\n\n\n\n<p><strong>Question:<\/strong><br>Given a table <code>Sales<\/code> with columns <code>SaleID<\/code>, <code>ProductID<\/code>, <code>Quantity<\/code>, and <code>SaleDate<\/code>, write a query to find the total quantity sold for each product.<\/p>\n\n\n\n<p><strong>Answer:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sqlCopy code<code>SELECT ProductID, SUM(Quantity) AS TotalQuantity\nFROM Sales\nGROUP BY ProductID;\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><br>This query aggregates the <code>Quantity<\/code> column for each <code>ProductID<\/code> using the <code>SUM<\/code> function. The <code>GROUP BY<\/code> clause groups the rows by <code>ProductID<\/code>, allowing the calculation of the total quantity sold for each product.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. Scenario: Date Range Queries<\/strong><\/h2>\n\n\n\n<p><strong>Question:<\/strong><br>You have a table <code>Transactions<\/code> with columns <code>TransactionID<\/code>, <code>AccountID<\/code>, <code>TransactionDate<\/code>, and <code>Amount<\/code>. Write a query to find all transactions that occurred in the last 30 days.<\/p>\n\n\n\n<p><strong>Answer:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sqlCopy code<code>SELECT *\nFROM Transactions\nWHERE TransactionDate &gt;= DATEADD(DAY, -30, GETDATE());\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><br>The <code>DATEADD<\/code> function subtracts 30 days from the current date (<code>GETDATE()<\/code>). The <code>WHERE<\/code> clause filters the transactions to include only those that occurred within the last 30 days. This query is useful for retrieving recent transactions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. Scenario: Updating Records<\/strong><\/h2>\n\n\n\n<p><strong>Question:<\/strong><br>You have a table <code>Products<\/code> with columns <code>ProductID<\/code>, <code>ProductName<\/code>, <code>Price<\/code>, and <code>StockQuantity<\/code>. Write a query to increase the price of all products by 10% that have less than 100 units in stock.<\/p>\n\n\n\n<p><strong>Answer:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sqlCopy code<code>UPDATE Products\nSET Price = Price * 1.10\nWHERE StockQuantity &lt; 100;\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><br>The <code>UPDATE<\/code> statement modifies the <code>Price<\/code> of products. The <code>SET<\/code> clause specifies the new price as 10% higher than the current price. The <code>WHERE<\/code> clause restricts the update to products with a <code>StockQuantity<\/code> of less than 100 units.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. Scenario: Deleting Specific Records<\/strong><\/h2>\n\n\n\n<p><strong>Question:<\/strong><br>You have a table <code>Users<\/code> with columns <code>UserID<\/code>, <code>Username<\/code>, <code>Email<\/code>, and <code>Status<\/code>. Write a query to delete all users whose status is &#8216;inactive&#8217; and have not logged in for the past year.<\/p>\n\n\n\n<p><strong>Answer:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sqlCopy code<code>DELETE FROM Users\nWHERE Status = 'inactive' AND LastLoginDate &lt; DATEADD(YEAR, -1, GETDATE());\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><br>The <code>DELETE<\/code> statement removes records from the <code>Users<\/code> table. The <code>WHERE<\/code> clause ensures that only users with a status of &#8216;inactive&#8217; and a <code>LastLoginDate<\/code> older than one year are deleted. The <code>DATEADD<\/code> function calculates the date one year ago.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. Scenario: Joining Multiple Tables<\/strong><\/h2>\n\n\n\n<p><strong>Question:<\/strong><br>You have two tables, <code>Customers<\/code> and <code>Orders<\/code>. The <code>Customers<\/code> table has columns <code>CustomerID<\/code>, <code>Name<\/code>, and <code>City<\/code>, while the <code>Orders<\/code> table has columns <code>OrderID<\/code>, <code>CustomerID<\/code>, <code>OrderDate<\/code>, and <code>TotalAmount<\/code>. Write a query to find the total amount spent by each customer.<\/p>\n\n\n\n<p><strong>Answer:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sqlCopy code<code>SELECT c.Name, SUM(o.TotalAmount) AS TotalSpent\nFROM Customers c\nJOIN Orders o ON c.CustomerID = o.CustomerID\nGROUP BY c.Name;\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><br>The <code>JOIN<\/code> clause combines the <code>Customers<\/code> and <code>Orders<\/code> tables based on the matching <code>CustomerID<\/code>. The <code>SUM<\/code> function calculates the total amount spent by each customer. The results are grouped by the customer&#8217;s name (<code>c.Name<\/code>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>8. Scenario: Subqueries<\/strong><\/h2>\n\n\n\n<p><strong>Question:<\/strong><br>You have a table <code>Employees<\/code> with columns <code>EmployeeID<\/code>, <code>Name<\/code>, <code>Department<\/code>, and <code>Salary<\/code>. Write a query to find the names of employees who earn more than the average salary in their department.<\/p>\n\n\n\n<p><strong>Answer:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sqlCopy code<code>SELECT Name\nFROM Employees e\nWHERE Salary &gt; (SELECT AVG(Salary) FROM Employees WHERE Department = e.Department);\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><br>The subquery <code>(SELECT AVG(Salary) FROM Employees WHERE Department = e.Department)<\/code> calculates the average salary for each department. The main query retrieves the names of employees whose salary is greater than this average. The correlation between the main query and the subquery is established by the alias <code>e<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>9. Scenario: Handling NULL Values<\/strong><\/h2>\n\n\n\n<p><strong>Question:<\/strong><br>You have a table <code>Products<\/code> with columns <code>ProductID<\/code>, <code>ProductName<\/code>, <code>Category<\/code>, and <code>Discount<\/code>. Write a query to display the <code>ProductName<\/code> and <code>Discount<\/code>, replacing NULL discounts with &#8216;No Discount&#8217;.<\/p>\n\n\n\n<p><strong>Answer:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sqlCopy code<code>SELECT ProductName, ISNULL(CAST(Discount AS VARCHAR), 'No Discount') AS Discount\nFROM Products;\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><br>The <code>ISNULL<\/code> function replaces NULL values in the <code>Discount<\/code> column with the string &#8216;No Discount&#8217;. The <code>CAST<\/code> function converts the <code>Discount<\/code> column to a string format, allowing the use of the &#8216;No Discount&#8217; label.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>10. Scenario: Ranking and Window Functions<\/strong><\/h2>\n\n\n\n<p><strong>Question:<\/strong><br>You have a table <code>Sales<\/code> with columns <code>SaleID<\/code>, <code>ProductID<\/code>, <code>SaleAmount<\/code>, and <code>SaleDate<\/code>. Write a query to rank the sales amounts for each product and display the rank along with the sale details.<\/p>\n\n\n\n<p><strong>Answer:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sqlCopy code<code>SELECT SaleID, ProductID, SaleAmount, \n       RANK() OVER (PARTITION BY ProductID ORDER BY SaleAmount DESC) AS SaleRank\nFROM Sales;\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><br>The <code>RANK()<\/code> function assigns a rank to each sale amount within the partition of <code>ProductID<\/code>, ordered by <code>SaleAmount<\/code> in descending order. The <code>PARTITION BY<\/code> clause ensures that the ranking is reset for each product.<\/p>\n\n\n\n<p>Recommended Post: <a href=\"https:\/\/www.h2kinfosys.com\/courses\/qa-online-training-course-details\/\">QA Testing Training<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Scenario-based SQL interview questions are a common way to evaluate a candidate&#8217;s practical knowledge and problem-solving abilities. By understanding these scenarios and practicing the associated queries, you can enhance your SQL skills and be better prepared for your next interview. These questions cover a range of topics, including data extraction, aggregation, updates, deletions, and more, providing a comprehensive overview of the skills required for a successful career in <a href=\"https:\/\/www.ibm.com\/docs\/en\/zos-basic-skills?topic=zos-what-is-database-management-system\" data-type=\"link\" data-id=\"https:\/\/www.ibm.com\/docs\/en\/zos-basic-skills?topic=zos-what-is-database-management-system\" rel=\"sponsored nofollow noopener\" target=\"_blank\">database management<\/a> and analysis.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the competitive world of data management and analysis, SQL remains one of the most vital skills for database professionals. During interviews, employers often test candidates with scenario-based questions to assess their problem-solving abilities and practical knowledge of SQL. In this blog, we&#8217;ll explore some of the top SQL scenario-based interview questions along with detailed [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":17454,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-17448","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/17448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=17448"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/17448\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/17454"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=17448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=17448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=17448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}