{"id":112007,"date":"2025-09-15T15:02:37","date_gmt":"2025-09-15T09:32:37","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/sql-query-interview-questions\/"},"modified":"2025-09-15T13:50:03","modified_gmt":"2025-09-15T08:20:03","slug":"sql-query-interview-questions","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/sql-query-interview-questions\/","title":{"rendered":"25 Tricky SQL Query Interview Questions with Answers"},"content":{"rendered":"\n<p>SQL queries are an important part of every coding interview. When a company interviews a fresher or an experienced developer, they want to see whether the person is able to understand and extract the data correctly or not.<\/p>\n\n\n\n<p>These questions range from basic to advanced level - like finding the second highest salary, removing duplicates, generating running totals, or writing complex joins.<\/p>\n\n\n\n<p>In this guide, we have provided easy, medium, and hard level SQL query questions, which you will practice and be ready for the interview. The concept, approach, and answer of each question are also provided, so that you don't just memorize - learn to solve by understanding.<\/p>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy Pro<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/practical-sql-training\" class=\"courses-cta-title-link\">SQL Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Master SQL and Database management with this SQL course: Practical training with guided projects, AI support, and expert instructors.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>7 Hrs<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>2 Projects<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/practical-sql-training\" class=\"courses-cta-button\">\n                Take SQL Course Now\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"easy-level-questions\">Easy Level Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-write-a-query-to-get-the-second-highest-salary-from-the-employees-table\">1. Write a query to get the second-highest salary from the employees table.<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: sorting and checking top-N logic.<\/li>\n\n\n\n<li>Approach: sort the salaries in descending order and take the 2nd value, or find the maximum salary that is smaller than the maximum.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n-- Method 1: Using LIMIT and OFFSET\nSELECT salary \nFROM employees \nORDER BY salary DESC \nLIMIT 1 OFFSET 1;\n\n-- Method 2: Using subquery\nSELECT MAX(salary) \nFROM employees \nWHERE salary &amp;lt; (SELECT MAX(salary) FROM employees);\n\n-- Method 3: Using ROW_NUMBER()\nSELECT salary \nFROM (\n    SELECT salary, ROW_NUMBER() OVER (ORDER BY salary DESC) as rn \n    FROM employees\n) t \nWHERE rn = 2;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"2-find-duplicate-user-records-in-a-table\">2. Find duplicate user records in a table<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: understand the use of GROUP BY and COUNT.<\/li>\n\n\n\n<li>Approach: count users with the same email and show where the count is >1.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n\n<p>Find all duplicate email addresses in the users table.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n-- Method 1: Using GROUP BY and HAVING\nSELECT email, COUNT(*) \nFROM users \nGROUP BY email \nHAVING COUNT(*) &gt; 1;\n\n-- Method 2: Show all duplicate rows\nSELECT u1.* FROM users u1\nJOIN (\n    SELECT email \n    FROM users \n    GROUP BY email \n    HAVING COUNT(*) &gt; 1\n) u2 ON u1.email = u2.email;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"3-delete-duplicate-records-from-the-users-table-keep-only-the-record-with-the-lowest-id\">3. Delete duplicate records from the users table. Keep only the record with the lowest ID.<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: check duplicate delete logic and self-join.<\/li>\n\n\n\n<li>Approach: delete the highest ID in the records with the same email.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n-- Method 1: Using self join\nDELETE u1 FROM users u1\nJOIN users u2 \nWHERE u1.id &gt; u2.id AND u1.email = u2.email;\n\n-- Method 2: Using window function\nDELETE FROM users \nWHERE id NOT IN (\n    SELECT min_id FROM (\n        SELECT MIN(id) as min_id \n        FROM users \n        GROUP BY email\n    ) t\n);\n<\/pre><\/div>\n\n\n<p><strong>Also Read:<\/strong> <a href=\"https:\/\/www.mygreatlearning.com\/blog\/sql-commands\/\">SQL Commands with Examples<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-find-all-employees-who-do-not-have-a-manager-in-the-employees-table\">4.  Find all employees who do not have a manager in the employees table.<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: handle NULL and self-join.<\/li>\n\n\n\n<li>Approach: show those whose manager_id is NULL or the manager does not exist.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSELECT * FROM employees \nWHERE manager_id IS NULL;\n\n-- Or if manager_id references employee_id in same table\nSELECT e1.* FROM employees e1\nLEFT JOIN employees e2 ON e1.manager_id = e2.employee_id\nWHERE e2.employee_id IS NULL AND e1.manager_id IS NOT NULL;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"5-calculate-the-running-total-of-sales-for-each-day\">5. Calculate the running total of sales for each day.<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: understand the window function.<\/li>\n\n\n\n<li>Approach: Calculate cumulative total from SUM() OVER(ORDER BY date).<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n-- Method 1: Using window function (preferred)\nSELECT \n    sale_date,\n    daily_sales,\n    SUM(daily_sales) OVER (ORDER BY sale_date) as running_total\nFROM sales\nORDER BY sale_date;\n\n-- Method 2: Using self join (older method)\nSELECT \n    s1.sale_date,\n    s1.daily_sales,\n    SUM(s2.daily_sales) as running_total\nFROM sales s1\nJOIN sales s2 ON s2.sale_date &amp;lt;= s1.sale_date\nGROUP BY s1.sale_date, s1.daily_sales\nORDER BY s1.sale_date;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"6-find-customers-who-never-placed-an-order\">6. Find customers who never placed an order<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: LEFT JOIN and NULL logic test.<\/li>\n\n\n\n<li>Approach: By linking customers to orders and not showing them which orders they have.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n-- Method 1: Using LEFT JOIN\nSELECT c.* FROM customers c\nLEFT JOIN orders o ON c.customer_id = o.customer_id\nWHERE o.customer_id IS NULL;\n\n-- Method 2: Using NOT EXISTS\nSELECT * FROM customers c\nWHERE NOT EXISTS (\n    SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id\n);\n\n-- Method 3: Using NOT IN (be careful with NULL values)\nSELECT * FROM customers \nWHERE customer_id NOT IN (\n    SELECT customer_id FROM orders WHERE customer_id IS NOT NULL\n);\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"7-write-a-query-to-find-the-3rd-highest-salary\">7. Write a query to find the 3rd highest salary.<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: understanding ranking window functions.<\/li>\n\n\n\n<li>Approach: Sort the salary desk and find the 3rd value.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n-- Method 1: Using DENSE_RANK()\nSELECT salary \nFROM (\n    SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) as rank_num\n    FROM employees\n) t \nWHERE rank_num = 3;\n\n-- Method 2: Using LIMIT and OFFSET\nSELECT DISTINCT salary \nFROM employees \nORDER BY salary DESC \nLIMIT 1 OFFSET 2;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"8-count-the-number-of-employees-in-each-department-show-departments-with-zero-employees-too\">8. Count the number of employees in each department. Show departments with zero employees, too.<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: LEFT JOIN and GROUP BY.<\/li>\n\n\n\n<li>Approach: Counting by joining employees with departments.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSELECT \n    d.department_name,\n    COUNT(e.employee_id) as employee_coundatt\nFROM departments d\nLEFT JOIN employees e ON d.department_id = e.department_id\nGROUP BY d.department_id, d.department_name\nORDER BY employee_count DESC;\n<\/pre><\/div>\n\n\n<p><strong>Also Read:<\/strong> <a href=\"https:\/\/www.mygreatlearning.com\/blog\/sql-joins\/\">SQL Joins - Inner, Left, Right &amp; Full Join<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"medium-level-questions\">Medium Level Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-find-employees-earning-more-than-their-manager\">9. Find employees earning more than their manager<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: self-join and comparison logic.<\/li>\n\n\n\n<li>Approach: Join the employee and his manager and compare the salary.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSELECT \n    e1.employee_name,\n    e1.salary as employee_salary,\n    e2.employee_name as manager_name,\n    e2.salary as manager_salary\nFROM employees e1\nJOIN employees e2 ON e1.manager_id = e2.employee_id\nWHERE e1.salary &gt; e2.salary;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"10-find-all-dates-that-appear-for-3-or-more-consecutive-days-in-the-login_logs-table\">10. Find all dates that appear for 3 or more consecutive days in the login_logs table.<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: date gaps and row_number difference trick.<\/li>\n\n\n\n<li>Approach: counting the dates in continuous groups.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nWITH consecutive_groups AS (\n    SELECT \n        login_date,\n        login_date - INTERVAL ROW_NUMBER() OVER (ORDER BY login_date) DAY as group_id\n    FROM (SELECT DISTINCT login_date FROM login_logs) t\n)\nSELECT \n    MIN(login_date) as start_date,\n    MAX(login_date) as end_date,\n    COUNT(*) as consecutive_days\nFROM consecutive_groups\nGROUP BY group_id\nHAVING COUNT(*) &gt;= 3;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"11-convert-rows-to-columns-show-total-sales-by-product-for-each-month\">11. Convert rows to columns. Show total sales by product for each month.<\/h3>\n\n\n\n<p><b>Question:<\/b> <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: conditional aggregation system.<\/li>\n\n\n\n<li>Approach: Creating columns by months using CASE WHEN or PIVOT.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n-- Method 1: Using CASE statements\nSELECT \n    product_name,\n    SUM(CASE WHEN MONTH(sale_date) = 1 THEN amount ELSE 0 END) as Jan,\n    SUM(CASE WHEN MONTH(sale_date) = 2 THEN amount ELSE 0 END) as Feb,\n    SUM(CASE WHEN MONTH(sale_date) = 3 THEN amount ELSE 0 END) as Mar\nFROM sales\nGROUP BY product_name;\n\n-- Method 2: Using PIVOT (SQL Server)\nSELECT *\nFROM (\n    SELECT product_name, MONTH(sale_date) as month, amount\n    FROM sales\n) t\nPIVOT (\n    SUM(amount) FOR month IN (&#x5B;1], &#x5B;2], &#x5B;3])\n) p;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"12-find-missing-numbers-in-a-sequence-from-1-to-100\">12. Find missing numbers in a sequence from 1 to 100.<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: detecting missing by generating a sequence.<\/li>\n\n\n\n<li>Approach: Generating 1\u2013100 numbers and joining them to the table to show nulls.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n-- Method 1: Using recursive CTE\nWITH RECURSIVE numbers AS (\n    SELECT 1 as num\n    UNION ALL\n    SELECT num + 1 FROM numbers WHERE num &amp;lt; 100\n)\nSELECT n.num as missing_number\nFROM numbers n\nLEFT JOIN your_table t ON n.num = t.id\nWHERE t.id IS NULL;\n\n-- Method 2: Using generate_series (PostgreSQL)\nSELECT generate_series(1, 100) as missing_number\nEXCEPT\nSELECT id FROM your_table\nORDER BY missing_number;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"13-calculate-each-employees-salary-as-a-percentage-of-the-total-company-salary\">13. Calculate each employee's salary as a percentage of the total company salary.<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: window aggregation.<\/li>\n\n\n\n<li>Approach: Converting every salary into a percentage of the total salary.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSELECT \n    employee_name,\n    salary,\n    ROUND(\n        (salary * 100.0 \/ SUM(salary) OVER()), 2\n    ) as percentage_of_total\nFROM employees;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"14-find-top-n-records-per-group\">14. Find top N records per group<\/h3>\n\n\n\n<p><strong>Question:<\/strong>\u00a0Find the top 2 highest-paid employees in each department.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: partitioned ranking.<\/li>\n\n\n\n<li>Approach: ROW_NUMBER partition by department and take top 2.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n-- Method 1: Using ROW_NUMBER()\nSELECT \n    department_name,\n    employee_name,\n    salary\nFROM (\n    SELECT \n        d.department_name,\n        e.employee_name,\n        e.salary,\n        ROW_NUMBER() OVER (PARTITION BY e.department_id ORDER BY e.salary DESC) as rn\n    FROM employees e\n    JOIN departments d ON e.department_id = d.department_id\n) t\nWHERE rn &amp;lt;= 2;\n\n-- Method 2: Using RANK() (includes ties)\nSELECT \n    department_name,\n    employee_name,\n    salary\nFROM (\n    SELECT \n        d.department_name,\n        e.employee_name,\n        e.salary,\n        RANK() OVER (PARTITION BY e.department_id ORDER BY e.salary DESC) as rank_num\n    FROM employees e\n    JOIN departments d ON e.department_id = d.department_id\n) t\nWHERE rank_num &amp;lt;= 2;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"15-find-all-pairs-of-employees-who-work-in-the-same-department-and-have-a-salary-difference-of-less-than-5000\">15. Find all pairs of employees who work in the same department and have a salary difference of less than 5000.<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: self-join and pair comparison.<\/li>\n\n\n\n<li>Approach: Join two employees of the same department and check the difference.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSELECT \n    e1.employee_name as employee1,\n    e2.employee_name as employee2,\n    e1.salary as salary1,\n    e2.salary as salary2,\n    ABS(e1.salary - e2.salary) as salary_difference\nFROM employees e1\nJOIN employees e2 ON e1.department_id = e2.department_id\nWHERE e1.employee_id &amp;lt; e2.employee_id  -- Avoid duplicates\nAND ABS(e1.salary - e2.salary) &amp;lt; 5000;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"16-find-employees-who-joined-in-the-last-quarter-of-any-year\">16. Find employees who joined in the last quarter of any year.<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: date functions society.<\/li>\n\n\n\n<li>Approach: Filter by extracting the month or quarter of the join_date.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSELECT \n    employee_name,\n    join_date,\n    YEAR(join_date) as join_year\nFROM employees\nWHERE MONTH(join_date) IN (10, 11, 12);\n\n-- Or using date functions\nSELECT \n    employee_name,\n    join_date\nFROM employees\nWHERE QUARTER(join_date) = 4;\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"hard-level-questions\">Hard Level Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"17-show-employee-hierarchy-with-levels-manager-sub-manager-employee\">17. Show employee hierarchy with levels (manager, sub-manager, employee).<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: recursive CTE check.<\/li>\n\n\n\n<li>Approach: Building manager \u2192 sub \u2192 employee chain and giving level.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n-- Recursive CTE to build hierarchy\nWITH RECURSIVE employee_hierarchy AS (\n    -- Base case: top-level managers\n    SELECT \n        employee_id,\n        employee_name,\n        manager_id,\n        0 as level,\n        employee_name as path\n    FROM employees\n    WHERE manager_id IS NULL\n    \n    UNION ALL\n    \n    -- Recursive case\n    SELECT \n        e.employee_id,\n        e.employee_name,\n        e.manager_id,\n        eh.level + 1,\n        CONCAT(eh.path, &#039; -&gt; &#039;, e.employee_name)\n    FROM employees e\n    JOIN employee_hierarchy eh ON e.manager_id = eh.employee_id\n)\nSELECT * FROM employee_hierarchy\nORDER BY level, employee_name;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"18-calculate-the-3-month-moving-average-of-sales\">18. Calculate the 3-month moving average of sales.<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: window frame logic.<\/li>\n\n\n\n<li>Approach: finding the average from ROWS BETWEEN 2 PRECEDING.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSELECT \n    sale_month,\n    monthly_sales,\n    AVG(monthly_sales) OVER (\n        ORDER BY sale_month \n        ROWS BETWEEN 2 PRECEDING AND CURRENT ROW\n    ) as moving_avg_3_months\nFROM monthly_sales\nORDER BY sale_month;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"19-find-continuous-ranges-of-available-seats-in-a-cinema\">19. Find continuous ranges of available seats in a cinema.<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: gap detection with row_number.<\/li>\n\n\n\n<li>Approach: seat_number - row_number grouping trick.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nWITH seat_groups AS (\n    SELECT \n        seat_number,\n        seat_number - ROW_NUMBER() OVER (ORDER BY seat_number) as group_id\n    FROM cinema_seats\n    WHERE is_available = 1\n)\nSELECT \n    MIN(seat_number) as range_start,\n    MAX(seat_number) as range_end,\n    COUNT(*) as consecutive_seats\nFROM seat_groups\nGROUP BY group_id\nORDER BY range_start;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"20-calculate-total-sales-but-only-count-orders-above-the-average-order-value-per-customer\">20. Calculate total sales, but only count orders above the average order value per customer.<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: correlated avg and conditional sum.<\/li>\n\n\n\n<li>Approach: Find the average of each customer and sum only his bad orders.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nWITH customer_avg AS (\n    SELECT \n        customer_id,\n        AVG(order_amount) as avg_order_amount\n    FROM orders\n    GROUP BY customer_id\n)\nSELECT \n    o.customer_id,\n    SUM(CASE \n        WHEN o.order_amount &gt; ca.avg_order_amount \n        THEN o.order_amount \n        ELSE 0 \n    END) as total_above_avg_sales\nFROM orders o\nJOIN customer_avg ca ON o.customer_id = ca.customer_id\nGROUP BY o.customer_id;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"21-calculate-median-salary-without-using-median-function\">21. Calculate median salary without using MEDIAN() function.<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: ranking logic.<\/li>\n\n\n\n<li>Approach: choose middle rows and take avg or use percentile_cont.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n-- Method 1: Using ROW_NUMBER\nWITH ranked_salaries AS (\n    SELECT \n        salary,\n        ROW_NUMBER() OVER (ORDER BY salary) as row_asc,\n        ROW_NUMBER() OVER (ORDER BY salary DESC) as row_desc\n    FROM employees\n)\nSELECT AVG(salary) as median_salary\nFROM ranked_salaries\nWHERE row_asc IN (row_desc, row_desc - 1, row_desc + 1);\n\n-- Method 2: Using PERCENTILE_CONT (if available)\nSELECT \n    PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary) as median_salary\nFROM employees;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"22-find-customers-who-bought-product-a-and-product-b-in-the-same-order\">22. Find customers who bought product A and product B in the same order.<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: EXISTS\/self join logic.<\/li>\n\n\n\n<li>Approach: checking entries of both products in the same order_id.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSELECT DISTINCT o.customer_id, o.order_id\nFROM orders o\nWHERE EXISTS (\n    SELECT 1 FROM order_items oi1 \n    WHERE oi1.order_id = o.order_id \n    AND oi1.product_name = &#039;Product A&#039;\n)\nAND EXISTS (\n    SELECT 1 FROM order_items oi2 \n    WHERE oi2.order_id = o.order_id \n    AND oi2.product_name = &#039;Product B&#039;\n);\n\n-- Alternative using self-join\nSELECT DISTINCT oi1.order_id, oi1.customer_id\nFROM order_items oi1\nJOIN order_items oi2 ON oi1.order_id = oi2.order_id\nWHERE oi1.product_name = &#039;Product A&#039;\nAND oi2.product_name = &#039;Product B&#039;;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"23-create-a-query-that-works-when-you-dont-know-column-names-in-advance\">23. Create a query that works when you don't know column names in advance.<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: dynamic schema handling.<\/li>\n\n\n\n<li>Approach: converting columns into rows using UNPIVOT\/UNION ALL.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n-- Using UNPIVOT to handle unknown columns\nSELECT \n    id,\n    column_name,\n    column_value\nFROM your_table\nUNPIVOT (\n    column_value FOR column_name IN (col1, col2, col3, col4)\n) u;\n\n-- Or using UNION ALL for flexibility\nSELECT id, &#039;col1&#039; as column_name, col1 as column_value FROM your_table WHERE col1 IS NOT NULL\nUNION ALL\nSELECT id, &#039;col2&#039; as column_name, col2 as column_value FROM your_table WHERE col2 IS NOT NULL\nUNION ALL\nSELECT id, &#039;col3&#039; as column_name, col3 as column_value FROM your_table WHERE col3 IS NOT NULL;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"24-find-the-first-and-last-order-date-for-each-customer-and-calculate-the-days-between-them\">24. Find the first and last order date for each customer, and calculate the days between them.<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: window first\/last value.<\/li>\n\n\n\n<li>Approach: partition by customer to find min-max dates and difference.<\/li>\n<\/ul>\n\n\n\n<p><b>Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSELECT \n    customer_id,\n    order_date,\n    FIRST_VALUE(order_date) OVER (\n        PARTITION BY customer_id \n        ORDER BY order_date \n        ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING\n    ) as first_order_date,\n    LAST_VALUE(order_date) OVER (\n        PARTITION BY customer_id \n        ORDER BY order_date \n        ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING\n    ) as last_order_date,\n    DATEDIFF(\n        LAST_VALUE(order_date) OVER (\n            PARTITION BY customer_id \n            ORDER BY order_date \n            ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING\n        ),\n        FIRST_VALUE(order_date) OVER (\n            PARTITION BY customer_id \n            ORDER BY order_date \n            ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING\n        )\n    ) as days_between_first_last\nFROM orders;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"25-optimize-this-slow-query-that-finds-customers-with-more-than-10-orders\">25. Optimize this slow query that finds customers with more than 10 orders.<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concept: performance and aggregation.<\/li>\n\n\n\n<li>Approach: Using GROUP BY or a window function instead of a subquery.<\/li>\n<\/ul>\n\n\n\n<p><b>Original slow query:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSELECT c.customer_id, c.customer_name\nFROM customers c\nWHERE (\n    SELECT COUNT(*) \n    FROM orders o \n    WHERE o.customer_id = c.customer_id\n) &gt; 10;\n<\/pre><\/div>\n\n\n<p><b>Optimized Answer:<\/b><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n-- Method 1: Using JOIN instead of subquery\nSELECT c.customer_id, c.customer_name\nFROM customers c\nJOIN (\n    SELECT customer_id\n    FROM orders\n    GROUP BY customer_id\n    HAVING COUNT(*) &gt; 10\n) o ON c.customer_id = o.customer_id;\n\n-- Method 2: Using window function\nSELECT DISTINCT customer_id, customer_name\nFROM (\n    SELECT \n        c.customer_id,\n        c.customer_name,\n        COUNT(*) OVER (PARTITION BY c.customer_id) as order_count\n    FROM customers c\n    JOIN orders o ON c.customer_id = o.customer_id\n) t\nWHERE order_count &gt; 10;\n\n-- Method 3: Simple JOIN with GROUP BY\nSELECT c.customer_id, c.customer_name\nFROM customers c\nJOIN orders o ON c.customer_id = o.customer_id\nGROUP BY c.customer_id, c.customer_name\nHAVING COUNT(o.order_id) &gt; 10;\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"key-tips-for-interview-success\">Key Tips for Interview Success<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"important-concepts-to-remember\">Important Concepts to Remember:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Window Functions:<\/b> ROW_NUMBER(), RANK(), DENSE_RANK(), LAG(), LEAD()<\/li>\n\n\n\n<li><b>CTEs (Common Table Expressions):<\/b> Great for complex queries<\/li>\n\n\n\n<li><b>Recursive CTEs:<\/b> For hierarchical data<\/li>\n\n\n\n<li><b>JOIN Types:<\/b> INNER, LEFT, RIGHT, FULL OUTER, CROSS<\/li>\n\n\n\n<li><b>Subqueries:<\/b> Correlated vs Non-correlated<\/li>\n\n\n\n<li><b>Performance:<\/b> Use indexes, avoid SELECT *, use LIMIT<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"common-mistakes-to-avoid\">Common Mistakes to Avoid:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Not handling <b>NULL<\/b> values properly<\/li>\n\n\n\n<li>Using <b>NOT IN<\/b> with <b>NULL<\/b> values<\/li>\n\n\n\n<li>Forgetting to use <b>DISTINCT<\/b> when needed<\/li>\n\n\n\n<li>Not considering performance implications<\/li>\n\n\n\n<li>Wrong <b>JOIN<\/b> types<\/li>\n\n\n\n<li>Incorrect <b>GROUP BY<\/b> usage<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"best-practices\">Best Practices:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Always test your queries with sample data<\/li>\n\n\n\n<li>Consider edge cases (empty tables, <b>NULL<\/b> values)<\/li>\n\n\n\n<li>Use proper formatting and indentation<\/li>\n\n\n\n<li>Add comments for complex logic<\/li>\n\n\n\n<li>Think about performance and scalability<\/li>\n\n\n\n<li>Know your database-specific functions<\/li>\n<\/ul>\n\n\n\n<p>This guide covers the most common tricky SQL query questions asked in interviews. Practice these patterns, and you will be ready for most <a href=\"https:\/\/www.mygreatlearning.com\/blog\/sql-quiz\/\">SQL challenges<\/a>.<\/p>\n\n\n\n<p><strong>Also Check:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.mygreatlearning.com\/blog\/sql-editor-tool\/\">Online SQL Compiler and Editor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.mygreatlearning.com\/blog\/hands-on-sql-projects\/\">SQL Projects: Boost Your Skills and Portfolio<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.mygreatlearning.com\/blog\/sql-quiz\/\">SQL MCQs and Quiz<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.mygreatlearning.com\/blog\/sql-exercises\/\">SQL Practice Questions and Online Exercises for Beginners<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.mygreatlearning.com\/blog\/what-does-an-sql-developer-do\/\">How to Become SQL Developer<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.mygreatlearning.com\/blog\/sql-interview-questions\/\">SQL Interview Questions and Answers<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Prepare for your SQL Query interview with 25 tricky questions, ranging from basic to advanced levels.<\/p>\n","protected":false},"author":41,"featured_media":112018,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[36844],"content_type":[],"class_list":["post-112007","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-sql"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>25 Tricky SQL Query Interview Questions with Answers<\/title>\n<meta name=\"description\" content=\"Prepare for your SQL Query interview with 25 tricky questions, ranging from basic to advanced levels.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/sql-query-interview-questions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"25 Tricky SQL Query Interview Questions with Answers\" \/>\n<meta property=\"og:description\" content=\"Prepare for your SQL Query interview with 25 tricky questions, ranging from basic to advanced levels.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/sql-query-interview-questions\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-09-15T09:32:37+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/sql-query-interview-questions.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1408\" \/>\n\t<meta property=\"og:image:height\" content=\"768\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-query-interview-questions\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-query-interview-questions\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"25 Tricky SQL Query Interview Questions with Answers\",\"datePublished\":\"2025-09-15T09:32:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-query-interview-questions\\\/\"},\"wordCount\":1057,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-query-interview-questions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/09\\\/sql-query-interview-questions.webp\",\"keywords\":[\"sql\"],\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-query-interview-questions\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-query-interview-questions\\\/\",\"name\":\"25 Tricky SQL Query Interview Questions with Answers\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-query-interview-questions\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-query-interview-questions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/09\\\/sql-query-interview-questions.webp\",\"datePublished\":\"2025-09-15T09:32:37+00:00\",\"description\":\"Prepare for your SQL Query interview with 25 tricky questions, ranging from basic to advanced levels.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-query-interview-questions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-query-interview-questions\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-query-interview-questions\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/09\\\/sql-query-interview-questions.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/09\\\/sql-query-interview-questions.webp\",\"width\":1408,\"height\":768,\"caption\":\"SQL Query Interview Questions\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-query-interview-questions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"25 Tricky SQL Query Interview Questions with Answers\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"25 Tricky SQL Query Interview Questions with Answers","description":"Prepare for your SQL Query interview with 25 tricky questions, ranging from basic to advanced levels.","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:\/\/www.mygreatlearning.com\/blog\/sql-query-interview-questions\/","og_locale":"en_US","og_type":"article","og_title":"25 Tricky SQL Query Interview Questions with Answers","og_description":"Prepare for your SQL Query interview with 25 tricky questions, ranging from basic to advanced levels.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/sql-query-interview-questions\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2025-09-15T09:32:37+00:00","og_image":[{"width":1408,"height":768,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/sql-query-interview-questions.webp","type":"image\/webp"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/sql-query-interview-questions\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/sql-query-interview-questions\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"25 Tricky SQL Query Interview Questions with Answers","datePublished":"2025-09-15T09:32:37+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/sql-query-interview-questions\/"},"wordCount":1057,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/sql-query-interview-questions\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/sql-query-interview-questions.webp","keywords":["sql"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/sql-query-interview-questions\/","url":"https:\/\/www.mygreatlearning.com\/blog\/sql-query-interview-questions\/","name":"25 Tricky SQL Query Interview Questions with Answers","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/sql-query-interview-questions\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/sql-query-interview-questions\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/sql-query-interview-questions.webp","datePublished":"2025-09-15T09:32:37+00:00","description":"Prepare for your SQL Query interview with 25 tricky questions, ranging from basic to advanced levels.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/sql-query-interview-questions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/sql-query-interview-questions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/sql-query-interview-questions\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/sql-query-interview-questions.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/sql-query-interview-questions.webp","width":1408,"height":768,"caption":"SQL Query Interview Questions"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/sql-query-interview-questions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"25 Tricky SQL Query Interview Questions with Answers"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/sql-query-interview-questions.webp",1408,768,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/sql-query-interview-questions-150x150.webp",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/sql-query-interview-questions-300x164.webp",300,164,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/sql-query-interview-questions-768x419.webp",768,419,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/sql-query-interview-questions-1024x559.webp",1024,559,true],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/sql-query-interview-questions.webp",1408,768,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/sql-query-interview-questions.webp",1408,768,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/sql-query-interview-questions-640x768.webp",640,768,true],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/sql-query-interview-questions-96x96.webp",96,96,true],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/sql-query-interview-questions-150x82.webp",150,82,true]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"Prepare for your SQL Query interview with 25 tricky questions, ranging from basic to advanced levels.","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/112007","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=112007"}],"version-history":[{"count":12,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/112007\/revisions"}],"predecessor-version":[{"id":112895,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/112007\/revisions\/112895"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/112018"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=112007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=112007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=112007"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=112007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}