Home › Interview Questions › Sql Interview Questions

Sql Interview Questions

139 real Sql questions asked in live technical interviews — each with a model answer. Updated weekly.

🎤 Auto-captured by Assisting AI during live interviews

These Sql interview questions were captured from real interviews by candidates using Assisting AI. Each links to a full model answer. For real-time help during your own interview, get Assisting AI from ₹500/day.

How can you use ROW_NUMBER() to deduplicate rows by email and keep the row with the smallest id, instead of using SELECT DISTINCT?🟡 Medium · Conceptual · Asked 2×Explain how to use LEFT JOIN with COALESCE to handle NULL manager names, write a query to find employees with salary greater than the average salary, write a query to sum values with HAVING > 100000, and explain the difference between RANK() and DENSE_RANK().🟡 Medium · Conceptual · Asked 2×Write a SQL query that uses a CTE to find clients that operate in multiple states, calculates the difference between the maximum and minimum revenue per client, then joins the revenue and client tables, groups by client, orders by revenue difference descending, and limits the result to the top record.🔴 Hard · Coding · Asked 2×Using UNION ALL, find clients that appear in multiple states and compute the difference in total revenue between those states. Then use LEFT JOIN, GROUP BY, and ORDER BY DESC to display the results.🔴 Hard · Coding · Asked 2×Write a SQL query that left joins CLIENT to STATE, then groups by state name and sums revenue.🟡 Medium · Coding · Asked 2×What is the difference between using DISTINCT and using GROUP BY with HAVING COUNT > 1 in SQL queries?🟡 Medium · Conceptual · Asked 2×Can you introduce yourself, state your years of experience, and rate your proficiency in Python and SQL?🟢 Easy · Behavioral · Asked 2×What is a primary key?🟢 Easy · Conceptual · Asked 2×What does this SQL query do?🟢 Easy · Conceptual · Asked 2×Why should you avoid using SELECT * in SQL queries?🟢 Easy · Conceptual · Asked 2×Why shouldn't we add indexes on every column of a small table with ten columns, even if it seems to speed up execution?🟡 Medium · Conceptual · Asked 1×You notice that joins involving a particular table are slow, while other tables are snappy. What steps would you take to diagnose and identify the cause of the slow join?🟡 Medium · Debugging · Asked 1×What strategies would you use to handle incremental updates and inserts in a data pipeline to ensure data consistency and avoid duplicates?🟡 Medium · Conceptual · Asked 1×You have a table with one billion rows. How would you update the data in this table on a monthly basis?🟡 Medium · Conceptual · Asked 1×What alternative approach could you use instead of hashing, such as comparing columns directly, and how would you write the code?🟡 Medium · Conceptual · Asked 1×If you have columns like `p.hash` and `c.hash`, how would you store those hash values, and can you compare the underlying columns directly instead of using a hash?🟡 Medium · Conceptual · Asked 1×Why can't you directly compare hash values without storing them, and how would you store the hash value in a database?🟡 Medium · Conceptual · Asked 1×How does hashing work, and how would you store the hash value in a database column?🟡 Medium · Conceptual · Asked 1×What is the difference between using a column name as a key and calculating that value?🟢 Easy · Conceptual · Asked 1×How would you solve the Employee Hierarchy problem using a recursive CTE or repeated self-joins to build each employee’s reporting chain or count subordinates?🟡 Medium · Conceptual · Asked 1×Given an employee table with columns employee_name and manager_id, how would you write a query to display each employee’s name along with their manager’s name?🟢 Easy · Coding · Asked 1×How do you retrieve each employee's manager name using a self-join?🟢 Easy · Conceptual · Asked 1×If two records have the same order date, how would you determine which one is the latest order?🟡 Medium · Conceptual · Asked 1×Given a table of orders, can you write a SQL query to find the latest order for each customer?🟡 Medium · Coding · Asked 1×How can you retrieve the latest order per customer and filter customers with total spend greater than 500 using SQL?🟡 Medium · Coding · Asked 1×Why doesn't Customer ID 9 appear in the final result when aggregating orders and left joining to customers?🟡 Medium · Conceptual · Asked 1×What is the difference between COUNT(*) and COUNT(column) in SQL?🟢 Easy · Conceptual · Asked 1×Explain why using a LEFT JOIN on orders.customer_id = customers.id is the correct approach to combine orders and customers tables, and how it affects the result set.🟢 Easy · Conceptual · Asked 1×Can you locate the valid values in your query? They were silver and gold.🟡 Medium · Debugging · Asked 1×How can you reference the department name from the outer query inside an inner query?🟡 Medium · Conceptual · Asked 1×If a subquery does not return the department name, how can the outer query access that department information?🟡 Medium · Conceptual · Asked 1×If a subquery does not include the department column that the outer query references, what will happen when the outer query tries to use it?🟡 Medium · Conceptual · Asked 1×Assign row numbers based on salary for each department using a window function.🟡 Medium · Coding · Asked 1×Which query gives the row number based on salary for each department? You need to assign a row number.🟡 Medium · Coding · Asked 1×What are some query optimization techniques you can use in SQL databases?🟡 Medium · Conceptual · Asked 1×How do you join two tables? Please give an example.🟢 Easy · Conceptual · Asked 1×What is wrong with this query, and how would you fix it?🟢 Easy · Debugging · Asked 1×Write a SQL query that selects all columns from the 'transactions' table.🟢 Easy · Coding · Asked 1×Write a SQL query that returns the most recent successful transaction for each customer using a ROW_NUMBER() window function.🟡 Medium · Coding · Asked 1×Write a SQL query to find the latest successful transaction per Stella.🟡 Medium · Coding · Asked 1×Is your approach basically SQL?🟢 Easy · Conceptual · Asked 1×If a query is taking too long to execute, what checks do you perform and how do you troubleshoot it?🟡 Medium · Debugging · Asked 1×When executing your SQL query, if you notice a performance issue, how will you address it?🟡 Medium · Debugging · Asked 1×Given a dataset of employees and managers, write a query to fetch all employees who earn more than their managers.🟡 Medium · Conceptual · Asked 1×Which ranking functions do you use for ranking and bucketing data, and how do you decide which one to use?🟢 Easy · Conceptual · Asked 1×Have you worked on any other window functions?🟡 Medium · Conceptual · Asked 1×How would you partition by department to get the highest paid employee in each department?🟡 Medium · Conceptual · Asked 1×Is there also an aggregate function you can use? For example, if L is 1900 and Dave is 90,000, how would you handle salary ordering?🟡 Medium · Conceptual · Asked 1×So what about Dave? His salary was 90,000 rupees. How would that affect the result of the highest-paid-per-department query?🟡 Medium · Conceptual · Asked 1×What is the logic to select the top two highest paid employees in each department?🟡 Medium · Coding · Asked 1×Which employees will be selected as the top two highest paid employees in each department?🟡 Medium · Coding · Asked 1×Write a SQL query to fetch the top two highest paid employees in each department.🟡 Medium · Coding · Asked 1×Complete the SQL query by adding the missing FROM and JOIN clauses to join on empid.🟢 Easy · Coding · Asked 1×How would you optimize a complex SQL query that uses CASE statements?🟡 Medium · Conceptual · Asked 1×What is the difference between partitioning and partition key in a data warehouse?🟡 Medium · Conceptual · Asked 1×How would you generate a report showing the top five scoring students, including three tables, with the condition that students have the same score in a subject?🟡 Medium · Conceptual · Asked 1×What is the difference between ROW_NUMBER, DENSE_RANK, and RANK window functions?🟡 Medium · Conceptual · Asked 1×What is the difference between COUNT(*) and COUNT(email) in SQL?🟢 Easy · Conceptual · Asked 1×Given a table with columns ID, student, name, and email, how would you handle duplicate names?🟡 Medium · Conceptual · Asked 1×Can you write a SQL query or a Python script to find all duplicate rows in a tickets table?🟡 Medium · Coding · Asked 1×Why should the GROUP BY clause include the distance column instead of destination in this query?🟡 Medium · Conceptual · Asked 1×How can you use a window function with DENSE_RANK to find the second highest salary in each department?🟡 Medium · Conceptual · Asked 1×How can I implement the given SQL query using PySpark instead of SQL?🟡 Medium · Coding · Asked 1×How do you use a window function with DENSE_RANK to rank salaries within each department and filter for rank 2?🟡 Medium · Conceptual · Asked 1×How can you use a window function to retrieve the second highest salary per department?🟡 Medium · Conceptual · Asked 1×Why would you use a window function instead of a traditional GROUP BY in SQL?🟡 Medium · Conceptual · Asked 1×What is the difference between a left join and a right join?🟢 Easy · Conceptual · Asked 1×How would you optimize a SQL query in Snowflake that involves multiple joins?🟡 Medium · Conceptual · Asked 1×Write a SQL query to find duplicate employee IDs in the employee table, keep one record, and delete the remaining duplicates.🟡 Medium · Coding · Asked 1×Write a SQL query to find duplicate records in the employee table.🟢 Easy · Coding · Asked 1×Can you write a query to find duplicate records in the impertable table?🟢 Easy · Coding · Asked 1×Give me a brief on how a SQL join works.🟢 Easy · Conceptual · Asked 1×Given the following table of customer events, write a SQL query to find the most recent status for each customer.🟡 Medium · Coding · Asked 1×Can you tell me the difference between RANK() and DENSE_RANK()?🟢 Easy · Conceptual · Asked 1×What other window functions are you aware of?🟢 Easy · Conceptual · Asked 1×How would you retrieve the top two orders by amount for each store for a given month?🟡 Medium · Coding · Asked 1×How would you design a customer table with CustomerID as the primary key and columns for customer name, phone, email, address, and last updated date?🟡 Medium · Conceptual · Asked 1×In ETL pipeline design, what are the most critical aspects you focus on to track data lineage, and can you write an SQL query to implement a Slowly Changing Dimension (SCD) Type 2?🟡 Medium · Coding · Asked 1×Given a denormalized dataset, how would you extract the desired columns or transform it into a normalized form using Python, Spark, or SQL?🟡 Medium · Coding · Asked 1×You've written a query that returns the correct result but runs too slowly within the time window you have. Walk me through what you do, in order, from the moment you decide it's too slow.🟡 Medium · Debugging · Asked 1×The same query on the same table ran in 30 seconds last week but takes 3 minutes today, with no changes to the query. Where do you look?🟡 Medium · Debugging · Asked 1×You have a query with four nested subqueries that is hard to read. How would you restructure it?🟡 Medium · Conceptual · Asked 1×You need each customer's three most recent orders. How would you write that query?🟡 Medium · Coding · Asked 1×Two people run the exact same query on the same table, but one of them takes far longer than the other. How could that happen?🟡 Medium · Conceptual · Asked 1×How many tables were you responsible for, roughly, and how big was the biggest one?🟢 Easy · Behavioral · Asked 1×How would you write a SQL query to group each user's logins into consecutive‑day runs (gaps and islands) and then find the longest run per user?🟡 Medium · Conceptual · Asked 1×How do you perform a full outer join on `txn_id` and flag rows where either side is missing?🟡 Medium · Conceptual · Asked 1×Explain the SQL query that reconciles transactions from tables txn_a and txn_b.🟡 Medium · Conceptual · Asked 1×Explain the SQL query that breaks down transaction differences between tables txn_a and txn_b.🟡 Medium · Conceptual · Asked 1×Explain the recursive CTE query that calculates total salary per employee hierarchy, line by line.🟡 Medium · Conceptual · Asked 1×Explain the workflow of the recursive CTE query that calculates total salary per employee hierarchy.🟡 Medium · Conceptual · Asked 1×Explain Q3, the recursive CTE that walks the org hierarchy and sums salaries.🟡 Medium · Conceptual · Asked 1×What are the SQL queries to answer the following three questions: (1) Retrieve employee and manager names, showing 'TBO' if no manager; (2) List employees earning more than the average salary; (3) Get manager level and total salary for their hierarchy.🟡 Medium · Coding · Asked 1×How can I modify the query for Q3 to return only direct reports, not the entire hierarchy?🟡 Medium · Coding · Asked 1×What are the SQL queries to answer the following three questions: (1) Retrieve manager name, or 'TBD' if missing; (2) List employees earning more than the average salary; (3) Get manager name and total salary of their hierarchy, including the manager, but only if the total exceeds 100k.🟡 Medium · Coding · Asked 1×What are the SQL queries to answer the following three questions: (1) Retrieve employee and manager names, showing 'TBO' if no manager; (2) List employees earning more than the average salary; (3) Get manager salary at their level and total salary of their hierarchy.🟡 Medium · Coding · Asked 1×Explain the SQL queries for the following two questions: (1) Retrieve distinct student_name, class, and section; (2) Find student names that appear more than once in the table.🟡 Medium · Coding · Asked 1×How can you find customers who have data for all 12 months in a year using SQL?🟡 Medium · Conceptual · Asked 1×When implementing slowly changing dimensions, how do you decide whether to count changes that occur within seven days versus those that occur after seven days? Also, can you walk through the SQL syntax you would use for handling SCD type 2?🟡 Medium · Conceptual · Asked 1×How would you add a column that calculates the rolling average of revenue per transaction timestamp, given that you only have a transaction timestamp column and no order date?🟡 Medium · Coding · Asked 1×How would you compute a 7‑day rolling average of daily revenue when there can be multiple transactions per day, using transaction timestamps?🟡 Medium · Coding · Asked 1×How would you modify your query to compute a 30‑day rolling average of daily revenue when you only have a transaction timestamp column and no order date?🟡 Medium · Coding · Asked 1×Write an SQL query to compute a 7‑day rolling average of daily revenue using a window function.🟡 Medium · Coding · Asked 1×What is the purpose of the load_ts column in this query, and how does it differ from transaction_ts?🟢 Easy · Conceptual · Asked 1×How does this query calculate the rolling 7‑day average revenue using a window function?🟡 Medium · Conceptual · Asked 1×How would you join these two tables, and what would the resulting dataset contain?🟡 Medium · Conceptual · Asked 1×How would you design a time series database for product sales and store data, and how would you retrieve daily, monthly, and weekly summaries?🟡 Medium · Conceptual · Asked 1×What are the ways you can use to optimize a slow running query?🟡 Medium · Conceptual · Asked 1×Write a SQL query to retrieve the top 3 customers by total purchase amount in the last 6 months.🟡 Medium · Conceptual · Asked 1×Using the query SELECT salary FROM (SELECT salary, ROW_NUMBER() OVER (ORDER BY salary DESC) AS rn FROM employees) t WHERE rn = 3; how would you modify it to correctly handle duplicate salaries when retrieving the third highest salary?🟡 Medium · Conceptual · Asked 1×Given the same query, how would you adjust it to account for duplicate salaries when selecting the third highest salary?🟡 Medium · Conceptual · Asked 1×Given the list of salaries [500, 300, 300, 400, 400, 200], what would the ROW_NUMBER() values be if you order by salary descending?🟢 Easy · Conceptual · Asked 1×How would you retrieve the top 3 salaries from the employees table?🟢 Easy · Conceptual · Asked 1×How would you find the third highest salary in the employees table, not department-wise?🟡 Medium · Conceptual · Asked 1×How would you restrict the MCP to only allow predefined safe SQL operations?🟡 Medium · Conceptual · Asked 1×Would you allow your MCP to execute arbitrary SQL against a connected SQL database?🟡 Medium · Conceptual · Asked 1×What is the error in the following SQL query that finds employees working on projects greater than or equal to the average number of projects per employee?🟢 Easy · Debugging · Asked 1×What is a better approach to find employees working on projects greater than or equal to the average number of projects per employee?🟡 Medium · Conceptual · Asked 1×What is the best SQL query you would write to solve this problem?🟡 Medium · Conceptual · Asked 1×How would you use a self‑join on the customers table to find duplicate verified records by matching normalized emails?🟡 Medium · Conceptual · Asked 1×How would you write a SQL query to find the first and last purchase dates for each customer-product pair, ensuring that each pair has at least two completed orders?🟡 Medium · Coding · Asked 1×Write a SQL query that returns the first and last purchase dates for each customer-product pair, only including pairs with at least two completed orders.🟡 Medium · Coding · Asked 1×Describe how you would use a CTE and window functions to rank purchases by date and then filter for the first and last occurrences for each customer-product pair.🟡 Medium · Coding · Asked 1×Explain why you should use 'IS NOT NULL' instead of 'ISNULL' in SQL queries.🟢 Easy · Conceptual · Asked 1×Why do we use a calendar function?🟢 Easy · Conceptual · Asked 1×How do you calculate a running total in SQL?🟢 Easy · Conceptual · Asked 1×In SQL code, how would you write a query to find the second highest salary?🟡 Medium · Coding · Asked 1×Which type of join will you use?🟢 Easy · Conceptual · Asked 1×If you need to run a query, can you open it in any other compiler to execute it?🟢 Easy · Conceptual · Asked 1×Can you go ahead and write a query on the left side?🟡 Medium · Coding · Asked 1×Can you tell me what the result will be from the data?🟢 Easy · Conceptual · Asked 1×Explain the following SQL query: SELECT customer_name, COUNT(*) FROM customerTable WHERE CITY_Name LIKE '%Pu%'?🟢 Easy · Conceptual · Asked 1×What will be the result of the query?🟢 Easy · Conceptual · Asked 1×Given the table of user interactions with actions 'sent' and 'accepted', how would you find pairs of users who have not performed both actions on the same day?🟡 Medium · Coding · Asked 1×Initially, we will aggregate the data. If we look at the data, we see that four requests were sent on January 4, 2020. How would you aggregate this data to analyze request patterns?🟡 Medium · Conceptual · Asked 1×Why did you use ORDER BY in your experience query?🟡 Medium · Debugging · Asked 1×Why did you use PARTITION BY by industry?🟡 Medium · Behavioral · Asked 1×Write a SQL query to select the top 10 users by order count.🔴 Hard · Coding · Asked 1×What is the correct method to achieve the highest sales rank, dense rank, or row number?🟡 Medium · Conceptual · Asked 1×

🎤 Get Sql questions answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500

Browse Other Topics