SQL / Query Optimization Interview Questions 2026

Published July 22, 2026 · Updated July 22, 2026

66 SQL / Query Optimization interview questions from Amazon, TCS, General, Airbnb, LinkedIn, Salesforce, Oracle, Cognizant, Razorpay, Deloitte, Grab and more. Real questions from Technical, System Design, and HR rounds.

66 questions
Companies: Amazon, TCS, General, Airbnb…

All SQL / Query Optimization Questions

Amazon Hard System Design round 5-9 years

Design a database schema for an e-commerce platform that handles flash sales. During a flash sale, 10,000 users try to buy the last 100 units simultaneously. Your schema must prevent overselling. Show the schema and the critical query.

↑ 84 upvotes · 52 engineers asked this · Staff
Amazon Hard Screening round 4-8 years

Explain the CAP theorem with a real database example for each combination. Which pair does a standard RDBMS choose?

↑ 84 upvotes · 42 engineers asked this · SDE2
Amazon Hard Scenario round 4-8 years

Your PostgreSQL database has a table with 200 million records. A nightly batch job runs UPDATE on 5 million rows. The next morning, queries that were taking 50ms now take 8 seconds. What happened and how do you fix it?

↑ 83 upvotes · 51 engineers asked this · SDE2
TCS Medium Technical round 2-5 years

Your PostgreSQL query plan shows 'Seq Scan' on a small 1000-row table despite having an index. Why might Postgres choose not to use the index?

↑ 83 upvotes · 41 engineers asked this · SDE2
Amazon Hard Scenario round 3-7 years

You have a query that takes 45 seconds on a table with 50 million rows. EXPLAIN shows a full table scan. The WHERE clause filters on user_id and created_at. There's already an index on user_id. What do you do?

↑ 82 upvotes · 50 engineers asked this · SDE2
Amazon Medium Technical round 2-5 years

Write a SQL query to detect customers who haven't made a purchase in the last 30 days but were active in the 30 days before that (churned customers).

↑ 82 upvotes · 40 engineers asked this · SDE2
Amazon Hard Screening round 3-7 years

Explain database connection pool sizing. A service has 4 CPU cores and connects to PostgreSQL. What's the optimal pool size?

↑ 81 upvotes · 39 engineers asked this · SDE2
General Hard System Design round 4-7 years

You have a users table with 100M rows. You need to add a full-text search on the bio column (free text). Show how to implement this in PostgreSQL.

↑ 80 upvotes · 38 engineers asked this · SDE2
General Hard Screening round 4-8 years

Explain PostgreSQL's MVCC (Multi-Version Concurrency Control). How does it enable concurrent reads and writes without blocking?

↑ 79 upvotes · 37 engineers asked this · SDE2
TCS Medium Screening round 2-5 years

Your database has a column with type VARCHAR(255) for email addresses. The column is indexed. Would changing it to VARCHAR(100) improve performance? What about TEXT?

↑ 78 upvotes · 36 engineers asked this · SDE2
General Hard System Design round 4-8 years

Design a schema for a multi-level referral system where tracking chains of 10+ levels is required, and you need to query 'who referred this user?' and 'who did this user refer?' efficiently.

↑ 77 upvotes · 35 engineers asked this · SDE2
TCS Hard Screening round 3-6 years

You run EXPLAIN on a query and see 'Using index condition' vs 'Using where'. What's the difference and which is better?

↑ 76 upvotes · 34 engineers asked this · SDE2
General Medium Screening round 3-7 years

Explain the difference between OLTP and OLAP databases. Why can't you use the same database for both? What are the architectural alternatives?

↑ 75 upvotes · 33 engineers asked this · SDE2
Amazon Medium Screening round 3-6 years

Explain how database indexes can SLOW DOWN queries. When would you remove an index?

↑ 75 upvotes · 3 engineers asked this · SDE2
General Hard System Design round 4-8 years

You're designing a database for an online exam system. 10,000 students take an exam simultaneously. Each student answers 50 questions. Design the schema and identify the hot spots.

↑ 54 upvotes · 32 engineers asked this · SDE2
General Medium Screening round 2-5 years

Explain ACID properties with a real banking transaction example. Which property does READ COMMITTED isolation level sacrifice?

↑ 53 upvotes · 31 engineers asked this · SDE2
Amazon Hard Technical round 3-6 years

Your query has LEFT JOIN to a large table but most rows in the result have NULL for the joined table columns. EXPLAIN shows full scan of the joined table. How do you optimize?

↑ 52 upvotes · 30 engineers asked this · SDE2
Amazon Hard Scenario round 3-7 years

You need to move 50 million rows from table A to table B (archive) and delete them from A. How do you do this without locking the production table?

↑ 51 upvotes · 29 engineers asked this · SDE2
TCS Medium Screening round 1-4 years

A developer creates an index on every column 'to make queries faster.' Why is this wrong and what's the actual impact?

↑ 50 upvotes · 28 engineers asked this · SDE2
TCS Medium Screening round 2-5 years

Explain window functions with an example. How is RANK() different from DENSE_RANK() and ROW_NUMBER()?

↑ 49 upvotes · 27 engineers asked this · SDE2
Amazon Hard Scenario round 3-7 years

Your application runs this query every second: SELECT count(*) FROM orders WHERE status='processing'. The count is displayed on a dashboard. The orders table has 100M rows. How do you make this fast?

↑ 48 upvotes · 26 engineers asked this · SDE2
TCS Easy Screening round 1-3 years

Explain the difference between UNION and UNION ALL. In what scenario does UNION ALL perform significantly better?

↑ 47 upvotes · 25 engineers asked this · SDE1
General Hard System Design round 4-8 years

You have a slowly changing dimension in a data warehouse. Employee department changes over time and you need to query 'what department was this employee in on date X?' Design the table.

↑ 46 upvotes · 24 engineers asked this · SDE2
TCS Medium Technical round 1-4 years

Write a query to find the second highest salary from an employees table. What if there are multiple employees with the highest salary?

↑ 45 upvotes · 23 engineers asked this · SDE2
Oracle Hard Technical round 3-7 yearsSQL

A query that joins 4 tables and runs fine on a 1M-row test database takes 40 seconds in production on 500M rows. Walk through your approach to diagnosing and fixing it.

↑ 37 upvotes · 24 engineers asked this · Senior SDE
Airbnb Medium Technical round 2-5 yearsSQL

Write a query to find hosts whose average review rating dropped by more than 0.5 stars comparing their most recent 10 reviews to their prior 10 reviews.

↑ 34 upvotes · 25 engineers asked this · Data Engineer
Cognizant Hard Technical round 2-6 yearsSQL

Write a query to find, for each department, the employee with the second-highest salary, using window functions instead of a correlated subquery.

↑ 33 upvotes · 25 engineers asked this · Developer
General Hard Scenario round 3-7 years

A data analyst complains that a report that runs daily takes 8 hours but used to take 30 minutes, starting 2 weeks ago. Nothing changed in the report query. What do you investigate?

↑ 31 upvotes · 19 engineers asked this · SDE2
Salesforce Medium Technical round 2-5 yearsSQL

Write a query to find customers who placed an order in each of the last 3 consecutive months, using an Orders table with customer_id and order_date.

↑ 30 upvotes · 19 engineers asked this · Senior SDE
Cognizant Easy Technical round 0-3 yearsSQL

Explain the difference between INNER JOIN, LEFT JOIN, and FULL OUTER JOIN with a simple example of Orders and Customers tables.

↑ 30 upvotes · 22 engineers asked this · Developer
LinkedIn Medium Technical round 2-5 yearsSQL

Write a query to find the top 3 skills most commonly shared between pairs of connected users, given a UserSkills table and a Connections table.

↑ 28 upvotes · 17 engineers asked this · Data Engineer
Cognizant Medium Technical round 2-5 yearsSQL

A SELECT query filtering on a non-indexed column takes 8 seconds on a 10 million row table. Would adding an index always help? What's the trade-off of adding too many indexes?

↑ 26 upvotes · 15 engineers asked this · Developer
General Hard Scenario round 3-8 years

A junior developer accidentally ran DROP TABLE orders in production. The table has 50M rows and no recent backup. The last backup is from 3 days ago. What do you do?

↑ 24 upvotes · 12 engineers asked this · SDE2
Deloitte Medium Technical round 2-5 yearsSQL

Write a query to find customers who have placed orders in every month of the current year, given an Orders table with customer_id and order_date.

↑ 23 upvotes · 14 engineers asked this · Senior SDE
Grab Medium Technical round 2-5 yearsSQL

Write a query to find, for each city, the percentage of rides that were cancelled within 2 minutes of being booked, given a Rides table with city, booked_at, and cancelled_at.

↑ 22 upvotes · 13 engineers asked this · Data Engineer
Cognizant Medium Technical round 1-4 yearsSQL

What problem does database normalization (1NF, 2NF, 3NF) actually solve? Give an example of an update anomaly in an unnormalized table.

↑ 21 upvotes · 12 engineers asked this · Developer
Razorpay Medium Technical round 3-6 yearsPostgreSQL

In PostgreSQL, what is the difference between SERIALIZABLE and READ COMMITTED isolation levels for a transaction that reads then updates an account balance?

↑ 20 upvotes · 9 engineers asked this · SDE2
Amazon Hard Scenario round 3-6 years

How do you optimize a slow COUNT(*) query on a table with 100 million rows?

↑ 19 upvotes · 17 engineers asked this · SDE2
Cognizant Medium Technical round 2-5 yearsSQL

How would you find out why a specific SQL query is slow before making any changes to it?

↑ 19 upvotes · 8 engineers asked this · Developer
TCS Medium Screening round 2-5 years

Explain the difference between RANK(), DENSE_RANK(), and ROW_NUMBER() window functions.

↑ 18 upvotes · 16 engineers asked this · SDE2
Oracle Medium Technical round 3-6 yearsPostgreSQL

What is the difference between a PostgreSQL VIEW and a MATERIALIZED VIEW? When would a materialized view actually hurt you?

↑ 18 upvotes · 8 engineers asked this · Senior SDE
TCS Easy Screening round 1-3 years

What is the difference between WHERE and HAVING clauses? Can you use aggregate functions in WHERE?

↑ 17 upvotes · 15 engineers asked this · SDE1
TCS Hard Scenario round 4-8 years

Your OLTP database is being used for reporting queries that scan millions of rows, causing locks that block transactional queries. You cannot add more hardware immediately. What do you do today?

↑ 16 upvotes · 14 engineers asked this · SDE2
Amazon Hard Technical round 3-6 years

You notice a SQL query uses an index, but adding ORDER BY makes it 10x slower. Explain why and how to fix it.

↑ 16 upvotes · 14 engineers asked this · SDE2
General Medium Screening round 2-5 years

Explain what happens step by step when you run: BEGIN; UPDATE accounts SET balance = balance - 100 WHERE id = 1; -- crash here. And why this matters for your application design.

↑ 15 upvotes · 13 engineers asked this · SDE2
Amazon Hard Screening round 3-6 years

What is the difference between optimistic and pessimistic locking in databases?

↑ 15 upvotes · 13 engineers asked this · SDE2
TCS Medium Technical round 2-5 years

A developer writes SELECT * FROM users WHERE email LIKE '%@gmail.com'. It's slow despite an index on email. Why? How would you make this fast while keeping the same search capability?

↑ 14 upvotes · 12 engineers asked this · SDE2
General Hard Screening round 3-6 years

Explain what happens during a VACUUM operation in PostgreSQL. Why is it necessary?

↑ 14 upvotes · 12 engineers asked this · SDE2
TCS Hard Scenario round 3-6 years

You're asked to delete 10 million old log records from a 50M row table. A simple DELETE WHERE created_at < '2024-01-01' locks the table for hours. How do you do this safely in production?

↑ 13 upvotes · 11 engineers asked this · SDE2
Amazon Medium Screening round 3-6 years

What is a materialized view and when would you use one over a regular view?

↑ 13 upvotes · 11 engineers asked this · SDE2
Amazon Hard System Design round 4-8 years

Your database read replicas are always 30 seconds behind the primary. During this lag, users see stale data after updating their profile. How do you handle read-after-write consistency without routing all reads to primary?

↑ 12 upvotes · 10 engineers asked this · SDE2
TCS Medium Technical round 2-5 years

Write a SQL query to detect duplicate rows in a table and delete all but one (keeping the one with the lowest ID).

↑ 12 upvotes · 10 engineers asked this · SDE2
TCS Hard Technical round 3-6 years

You need to find duplicate records in a table of 10 million rows based on email, phone, and name (any two matching means duplicate). Write the SQL and explain the performance considerations.

↑ 11 upvotes · 9 engineers asked this · SDE2
Amazon Hard Screening round 5-9 years

Explain database partitioning vs sharding. When would you use each?

↑ 11 upvotes · 9 engineers asked this · Staff
General Hard Technical round 4-8 years

A stored procedure runs fine in isolation but causes deadlocks when called concurrently by 50 threads. The procedure does: UPDATE accounts, then UPDATE transactions. Another procedure does the same in reverse order. What's happening and how do you fix it?

↑ 10 upvotes · 8 engineers asked this · SDE2
Amazon Hard Screening round 3-6 years

What is a covering index? Show how adding one column to an index can change a query from a disk read to a memory read.

↑ 10 upvotes · 8 engineers asked this · SDE2
TCS Hard Technical round 3-6 years

You have a MySQL query with GROUP BY that was fast on 1 million rows and is now slow on 50 million. EXPLAIN shows 'Using filesort' and 'Using temporary'. What does this mean and how do you fix it?

↑ 9 upvotes · 7 engineers asked this · SDE2
TCS Medium Screening round 2-5 years

What are database transactions and ACID properties? Give a real example where violating each property causes data corruption.

↑ 9 upvotes · 7 engineers asked this · SDE2
General Hard System Design round 5-9 years

Design a schema to support a multi-tenant SaaS application where each tenant has its own isolated data. Compare row-level isolation, schema-per-tenant, and database-per-tenant. When would you choose each?

↑ 8 upvotes · 6 engineers asked this · Staff
TCS Easy Screening round 1-3 years

Explain the difference between INNER JOIN, LEFT JOIN, and FULL OUTER JOIN. When does the choice affect the result count?

↑ 8 upvotes · 6 engineers asked this · SDE1
TCS Hard Technical round 3-6 years

Your application has this query pattern: SELECT * FROM orders WHERE status='pending' running every 5 seconds. Table has 50M rows, 99.9% of orders are 'completed'. An index on status exists. EXPLAIN shows it's still doing a table scan. Why?

↑ 7 upvotes · 5 engineers asked this · SDE2
TCS Medium Technical round 2-5 years

Write a SQL query to find the second highest salary from an Employees table without using LIMIT/TOP or subqueries.

↑ 7 upvotes · 5 engineers asked this · SDE2
Amazon Hard Scenario round 4-8 years

A developer adds a NOT NULL constraint to a column in a production table with 100 million rows. The ALTER TABLE runs for 6 hours and locks the table. How should this have been done?

↑ 6 upvotes · 4 engineers asked this · SDE2
Amazon Medium Technical round 2-5 years

What is the N+1 query problem? How do you detect it in production without reading every query?

↑ 6 upvotes · 4 engineers asked this · SDE2
TCS Hard Technical round 4-7 years

You run EXPLAIN ANALYZE on a query joining 4 tables. It shows 'Hash Join' with 'rows=1 actual rows=50000'. What does this tell you and what's your fix?

↑ 5 upvotes · 3 engineers asked this · SDE2
Amazon Hard Technical round 3-7 years

You have a table with 500 million rows. A query runs in 0.1s in development (10K rows) but 45 seconds in production. Same index exists on both. Why?

↑ 5 upvotes · 3 engineers asked this · SDE2

Practice these questions with AI feedback

Get instant grading on your answers, identify your weak areas, and generate a personalised 14-day study plan — all free.

Build my study plan →

Same topic at specific companies

Amazon SQL / Query Optimization TCS SQL / Query Optimization General SQL / Query Optimization Airbnb SQL / Query Optimization LinkedIn SQL / Query Optimization Salesforce SQL / Query Optimization Oracle SQL / Query Optimization Cognizant SQL / Query Optimization Razorpay SQL / Query Optimization Deloitte SQL / Query Optimization

Related topics

DSA / Arrays System Design DSA / Trees DSA / Graphs Behavioral Testing Domain DevOps API Testing DSA / Hashmaps GCP / Real-time Data Java / Pub-Sub React / Performance Node.js / Memory Python / Async AWS / Lambda Kafka / Consumer Microservices / Saga Pattern TypeScript / Type System MongoDB / Indexing Security / SQL Injection Performance / Latency Tail Redis / Cache Stampede Elasticsearch / Cluster Health GraphQL / Security Data Engineering / Exactly Once Snowflake / Architecture PySpark / Core Concepts Airflow / Scheduling Web / WebAssembly Debugging / Production JavaScript / V8 Engine

Frequently asked questions

Design a database schema for an e-commerce platform that handles flash sales. During a flash sale, 10,000 users try to buy the last 100 units simultaneously. Your schema must prevent overselling. Show the schema and the critical query.
Design a database schema for an e-commerce platform that handles flash sales. During a flash sale, 10,000 users try to buy the last 100 units simultaneously. Your schema must prevent overselling. Show the schema and the critical query.
Explain the CAP theorem with a real database example for each combination. Which pair does a standard RDBMS choose?
Explain the CAP theorem with a real database example for each combination. Which pair does a standard RDBMS choose?
Your PostgreSQL database has a table with 200 million records. A nightly batch job runs UPDATE on 5 million rows. The next morning, queries that were taking 50ms now take 8 seconds. What happened and how do you fix it?
Your PostgreSQL database has a table with 200 million records. A nightly batch job runs UPDATE on 5 million rows. The next morning, queries that were taking 50ms now take 8 seconds. What happened and how do you fix it?
Your PostgreSQL query plan shows 'Seq Scan' on a small 1000-row table despite having an index. Why might Postgres choose not to use the index?
Your PostgreSQL query plan shows 'Seq Scan' on a small 1000-row table despite having an index. Why might Postgres choose not to use the index?
You have a query that takes 45 seconds on a table with 50 million rows. EXPLAIN shows a full table scan. The WHERE clause filters on user_id and created_at. There's already an index on user_id. What do you do?
You have a query that takes 45 seconds on a table with 50 million rows. EXPLAIN shows a full table scan. The WHERE clause filters on user_id and created_at. There's already an index on user_id. What do you do?