Top 101 SQL Interview Questions (2026)
SQL is one of the most tested skills in data analyst, backend engineer, and business intelligence interviews. Employers use SQL questions to evaluate your ability to write efficient queries, perform joins, use aggregate functions, and reason about database schema design. Whether you're preparing for a product company or an MNC, mastering SQL interview questions is non-negotiable.
How do you perform pattern matching in SQL using wildcards?
String search is a common requirement. Interviewers want to ensure you know the standard syntax and variations across different database systems for flexible text matching.
What's the difference between DELETE and TRUNCATE command in SQL?
The choice affects performance and transaction logs. Interviewers ask this to see if you understand the implications of each command on data and schema. It is a common pitfall for junior developers.
What is the difference between LIKE and equals operators in SQL?
Choosing the right operator affects query accuracy and performance. Interviewers want to ensure you don't use pattern matching when exact matches are needed.
What is the difference between UNION and UNION ALL?
Interviewers check if you understand the cost of removing duplicates. UNION_ALL is often faster. Knowing when to use which demonstrates practical optimization skills.
What is DBMS and what are its advantages?
Every backend developer must understand data management. This question verifies your foundational knowledge of what a DBMS is and why organizations prefer it over flat files.
What is the ER model in DBMS?
ER diagrams are the blueprint for database design. Interviewers check if you can translate business requirements into data structures. This is a prerequisite for effective schema creation.
What is the difference between DELETE and TRUNCATE commands in SQL?
Candidates need to know the performance and structural implications of removing data. Interviewers check if you understand logging, rollback capabilities, and identity reset behaviors. Choosing the wrong command can lead to unnecessary performance degradation or accidental data loss.
How do foreign keys enforce referential integrity?
Data integrity is paramount in database systems. Interviewers ask this to ensure you understand how to prevent invalid data relationships. Foreign keys are the primary mechanism for enforcing that child records actually reference existing parent records.
What is the difference between CHAR and VARCHAR2 in SQL?
Interviewers ask this to assess fundamental knowledge of database design and efficiency. They want to ensure you understand how different data types impact storage space and performance. Knowing the distinction helps in optimizing table schemas for large datasets where space matters.
What is the Difference Between RDBMS and DBMS?
Understanding the evolution and capabilities of database systems is crucial for selecting the right tool. Interviewers check if you know the structural and functional differences.
What is a transaction in DBMS?
Transactions ensure data correctness. Interviewers check if you understand atomicity and grouping. This is vital for financial and critical systems.
What is the difference between UNION and UNION ALL in SQL?
Choosing the right set operation affects performance and result accuracy. Interviewers check if you understand when to remove duplicates and when to keep all rows for speed.
What is the difference between DELETE and TRUNCATE command?
Understanding these commands is essential for database maintenance. Interviewers check if you know the performance implications and transactional differences.
What are the different types of keys in relational models?
Keys define relationships and constraints in databases. Interviewers assess your ability to model data correctly. Misunderstanding keys leads to poor schema design and integrity issues.
What is a cross join and how does it differ from an inner join?
Cross joins are often misunderstood. Interviewers assess if you know when to generate all combinations versus filtered matches.
What is the difference between DELETE and TRUNCATE commands?
Choosing the right command affects performance and safety. Interviewers check if you understand the implications of row-by-row deletion versus table reset. This shows attention to detail in database maintenance tasks.
What is SQL and what are its primary uses?
SQL is the standard for relational databases. Knowing its purpose is fundamental for any data-related role.
What is the difference between DDL, DML, and DCL?
Knowing command categories helps in writing correct scripts. Interviewers assess fundamental SQL literacy. This is a baseline requirement for DB roles.
What is PostgreSQL?
PostgreSQL is a standard database technology. The interviewer wants to ensure the candidate understands its features, advantages, and use cases compared to other databases. It confirms technical competency in database management.
What are the ACID properties of a database transaction?
ACID properties define the reliability of database transactions. Interviewers want to confirm you understand how databases maintain consistency during failures. This is fundamental for building trustworthy applications.
Write a SQL query to fetch student names containing 'nee'.
Database querying is a fundamental skill for data roles. This tests basic syntax knowledge, specifically the LIKE operator and wildcard usage for string matching.
Explain foreign keys and referential integrity
Foreign keys prevent orphaned data. Interviewers check if you understand how relationships are enforced automatically.
What is the difference between the LIKE and = operators in SQL?
Developers often confuse exact matches with partial matches. This question checks if you understand when to use precise equality versus flexible pattern matching. It is fundamental for writing accurate and efficient data retrieval logic.
Explain foreign keys and how they enforce referential integrity
Referential integrity is fundamental to relational databases. Interviewers want to ensure you understand how to prevent orphan records and maintain valid relationships.
What is the purpose of the UNIQUE constraint?
Interviewers need to confirm you understand how to prevent duplicate data entry. Data integrity is critical for business logic, and this constraint is a primary tool for maintaining it. They also check if you know how it differs from a primary key.
What is the difference between CHAR and VARCHAR2?
Interviewers ask this to verify that candidates understand how database storage works at a fundamental level. They want to ensure you know when to use fixed-length types for consistency versus variable-length types for space efficiency. Misunderstanding these can lead to significant storage bloat or performance issues in large-scale applications.
What is a cross join and when should you use it?
Cross joins are often misunderstood and misused. Interviewers ask this to ensure you know exactly when to generate a full combination of rows versus a filtered join. It is essential for generating test data or creating grids of possibilities.
What’s the Difference Between RDBMS and DBMS?
It clarifies the scope of database technologies. Interviewers ask this to see if you understand the evolution from hierarchical models to relational ones. It sets the stage for discussing SQL and ACID properties.
What is the difference between DDL, DML, and DCL in SQL?
Basic SQL classification is fundamental. Interviewers check if you can categorize commands correctly and understand their purposes.
Do you have knowledge of SQL? Can you write a query to fetch student names with 'nee' as a substring?
Data retrieval is a daily task for developers. This question tests your ability to write basic queries and use string manipulation functions. It confirms you can interact with databases to extract specific information efficiently.
What is a view in SQL and why is it useful?
Candidates need to demonstrate they can abstract complex logic into reusable components. Views simplify maintenance, improve readability, and allow administrators to restrict access to sensitive columns without altering base tables.
Write a SQL query to fetch names containing 'nee'.
Data retrieval is a daily task for developers. This checks if you know how to filter data using wildcards and substring patterns efficiently.
Write a SQL query to find duplicate records in a table
Data manipulation is crucial for analyzing business metrics. This question checks basic SQL syntax knowledge, specifically the use of GROUP BY and HAVING clauses to identify anomalies or duplicates in datasets.
What is the difference between LIKE and = operators in SQL?
Knowing when to use exact matches versus patterns is crucial for accurate data filtering. Interviewers assess your logical approach to querying data.
What is the purpose of the UNIQUE constraint in SQL?
Data integrity is critical in any application. Interviewers ask this to see if you know how to enforce business rules at the database level to prevent invalid data states caused by duplicates.
What is Python and why is it popular in data science?
Python is a dominant language in tech. Interviewers want to confirm you understand its strengths beyond just syntax.
What are correlated subqueries and how do they work?
Correlated subqueries are powerful but can be slow. Interviewers ask this to gauge your ability to write complex logic and understand performance implications. They also test if you know when to replace them with joins for better performance.
Explain correlated subqueries and give an example
Correlated subqueries are powerful but can be slow. Interviewers want to ensure candidates understand the execution model where the inner query runs for each outer row. It tests performance awareness.
Explain normalization and describe the first three normal forms.
Poor database design leads to data anomalies and slow performance. Interviewers ask this to see if you can build scalable systems. Understanding normalization helps prevent update, insert, and delete anomalies that corrupt data consistency over time.
Explain correlated subqueries and provide an example
Correlated subqueries are powerful but slow. Interviewers check if you understand the dependency on outer rows. They also want to see if you know when to avoid them.
Explain normalization and describe the different normal forms
Normalization is the backbone of efficient database design. Interviewers want to ensure you can structure data to minimize anomalies and redundancy while preserving relationships.
How do clustered and non-clustered indexes differ?
Indexes are critical for speed. Interviewers want to know if you understand how data is physically ordered. Choosing the wrong index type can cripple performance.
How do clustered and non-clustered indexes differ and when to use them?
Indexing is critical for performance. Interviewers assess your ability to choose the right index type to optimize read speeds without sacrificing write performance or storage.
Explain normalization and the different normal forms
Normalization is the foundation of efficient database design. Interviewers assess if you can prevent update, insert, and delete anomalies. It shows theoretical depth required for scalable systems.
Explain the difference between WHERE and HAVING clauses in SQL
Understanding the execution order of SQL clauses is crucial for writing efficient and correct queries. Candidates must distinguish between filtering raw rows and filtering aggregated groups.
Explain anti-joins and how to implement them in SQL
Anti-joins are essential for finding missing data or exclusions. Interviewers assess if you know the standard patterns (LEFT JOIN...IS NULL or NOT EXISTS).
Describe a PRIMARY KEY and how it differs from a UNIQUE key
It distinguishes between basic uniqueness and strict identity. Interviewers check if you know that a primary key implies NOT NULL and is unique per table. This is crucial for designing robust schemas.
What is ER model in the DBMS?
ER models are the blueprint for databases. Interviewers assess your ability to visualize and plan data structures before implementation.
What is Join and what are the different types of Joins in SQL?
Joins are the most common operation in data retrieval. Interviewers ask this to ensure you can combine data from multiple tables effectively. Mastery of joins is essential for complex reporting and analysis tasks.
What is ER Model in DBMS and what are its components?
ER diagrams are the blueprint for database design. Interviewers ask this to assess your ability to translate business requirements into data structures. It shows your conceptual modeling capabilities.
What are SQL joins and what are the main types?
Joining tables is the most common task in database work. Interviewers ask this to ensure you can retrieve related data correctly. Misunderstanding join types leads to missing data or duplicate rows, which causes significant business logic errors in applications.
Explain RANK, DENSE_RANK, and ROW_NUMBER differences
Ranking logic varies significantly between functions. Interviewers need to ensure candidates pick the right one for tie-breaking scenarios. Confusion here leads to incorrect analytics.
What is Join and explain its types?
Joins are essential for retrieving related data. Interviewers assess your ability to combine datasets efficiently. Mastery here is required for reporting and analytics.
What is a Transaction and what are its ACID properties?
Transactions ensure reliable processing of financial or critical data. Interviewers ask this to verify your understanding of atomicity and consistency. It is crucial for preventing partial updates that could corrupt data.
What is a CTE and when would you use it in SQL?
CTEs help manage complex queries by breaking them into logical steps. Interviewers look for candidates who prioritize maintainability and clarity over nested subqueries.
What is database normalization and why is it important?
Normalization is key to efficient database design. Interviewers check if you understand how to structure tables to minimize anomalies and maintain data consistency.
What are SQL joins and how do INNER, LEFT, RIGHT, FULL differ?
Joins are fundamental to relational databases. Interviewers test if you can combine data from multiple sources correctly. They also evaluate your ability to choose the right join type for specific business requirements.
Explain correlated subqueries and provide an example use case
Correlated subqueries are powerful but expensive. Interviewers want to know if you understand how they reference outer rows and when to use them versus joins.
Do you have knowledge of SQL? Can you write a query to fetch student names?
SQL is essential for data retrieval. Interviewers test basic querying skills and the ability to filter data using string operators. This confirms the candidate can interact with databases effectively.
Explain ACID properties in a database transaction.
ACID properties are fundamental to ensuring data correctness in financial and e-commerce transactions. Interviewers want to see if you can articulate how databases maintain consistency even during failures or concurrent access, which is vital for trust in the platform.
Explain the difference between RANK, DENSE_RANK, and ROW_NUMBER
Ranking is common in reporting. Interviewers check if you know how ties are handled differently by each function.
Describe set operations UNION, INTERSECT, and EXCEPT
Set operations are powerful for combining and comparing results. Interviewers ask this to see if candidates know how to merge, find commonalities, or find differences between datasets efficiently.
Explain anti-joins and how to implement them
Finding 'A but not B' is a common requirement. Interviewers check if you know the LEFT JOIN...IS NULL pattern or NOT EXISTS alternative.
What is a view in SQL and why would you use it?
Recruiters ask this to see if you can write maintainable code and protect sensitive data. Views allow you to hide complex logic behind a simple interface, which is crucial for team collaboration. Understanding views also shows you know how to enforce security by restricting column or row access without altering the underlying tables.
Describe set operations like UNION, INTERSECT, and EXCEPT
Set operations are powerful for combining datasets. Interviewers check if you know when to merge, intersect, or subtract results.
How do you perform pattern matching in SQL?
Real-world data often requires flexible searching. Interviewers check if you know standard operators like LIKE and advanced ones like REGEXP.
What are the ACID properties of a database and why do they matter?
ACID properties ensure data integrity during transactions, which is non-negotiable for financial applications like e-commerce. Interviewers want to confirm you understand atomicity, consistency, isolation, and durability. This shows you can design systems that prevent data loss or corruption during failures.
What is a composite primary key and when should you use it?
Real-world data often requires multiple attributes to uniquely identify a record. Interviewers want to know if you can design robust schemas that handle complex relationships correctly without creating redundancy.
What are EXISTS and NOT EXISTS and how do they differ from IN?
EXISTS is often more efficient and handles NULLs better than IN. Interviewers check if you know the semantic differences and performance characteristics.
Explain the difference between EXISTS and IN clauses.
Both clauses achieve similar goals but behave differently with NULLs and perform differently at scale. Interviewers ask this to ensure you choose the most robust and efficient method for your specific scenario.
What is a CTE and when would you use it?
CTEs improve code maintainability. Interviewers want to see if you move beyond nested subqueries to cleaner logic. They also check if you know recursive capabilities for hierarchical data.
Explain the difference between WHERE and HAVING clauses
Interviewers want to see if you understand the order of operations in SQL execution. Confusing these leads to syntax errors and incorrect results. It proves you grasp when to filter raw data versus aggregated groups.
How do LAG and LEAD functions work in SQL?
Analyzing trends over time is a core data task. Interviewers ask this to see if you can avoid self-joins for simple row comparisons. LAG and LEAD are efficient tools for calculating deltas or detecting patterns in sequential data.
Define database normalization and its main goals
Normalization ensures data consistency and reduces storage waste. Interviewers assess your ability to design efficient schemas. Poor normalization leads to update anomalies and data inconsistencies, which are costly in production.
What is a composite primary key and when is it used?
Candidates must demonstrate they can handle complex entity relationships. Interviewers look for the ability to identify scenarios where a single column cannot uniquely identify a row. This shows maturity in database schema design.
What is Join and what are its types in SQL?
Joins are fundamental for querying relational data. Interviewers assess your ability to retrieve complex data sets and understand inner vs outer joins.
What is a transaction and explain ACID properties?
Transactions ensure reliable data processing. Interviewers want to confirm you understand how to maintain data integrity during failures or concurrent access.
Explain LAG and LEAD functions
Comparing current values to historical ones is common. Interviewers ask this to verify knowledge of window functions that avoid self-joins. It shows efficiency in time-series analysis.
Explain ACID properties of a database transaction
ACID compliance guarantees data reliability even during failures. Interviewers want to verify you understand atomicity, consistency, isolation, and durability. This is critical for handling payments and inventory updates.
How do WHERE and HAVING clauses differ in SQL?
Interviewers need to confirm you understand the logical processing order of SQL statements. Many candidates confuse these two because they both filter data. Knowing when to use each prevents errors in reporting and ensures efficient query execution by filtering early where possible.
How would you calculate a running total of sales for each product?
Running totals are common in reporting. Interviewers evaluate your ability to use analytic functions to accumulate data over time or partitions efficiently.
Explain the difference between joins and subqueries?
Efficient data retrieval is crucial. Interviewers want to know if you understand performance implications and readability trade-offs between different query structures.
Explain the purpose of LAG and LEAD functions
Comparing current row to previous/next is vital for trends. Interviewers check if you know these functions to avoid self-joins.
How do you calculate a running total in SQL?
Running totals are common in financial and analytical reports. Interviewers ask this to verify you know modern SQL features beyond basic aggregation. Using window functions is often more efficient and cleaner than self-joins for this task.
How would you calculate a running total of sales?
Running totals are common business metrics. Interviewers check if you know window functions instead of self-joins or cursors. This shows modern SQL fluency.
What is a Common Table Expression (CTE) and when do you use it?
Complex queries are hard to debug and maintain. CTEs help break down logic into manageable steps. Interviewers ask this to see if you prioritize code readability and modularity over writing monolithic subqueries.
What is a cross join and how does it differ from inner join?
Cross joins create massive datasets. Interviewers check if you understand the explosion risk and intentional use cases.
Which SQL query finds the third largest salary in an employee table?
This question assesses a developer's ability to manipulate data efficiently using SQL. It tests understanding of ranking functions, ordering, and limiting result sets. Employers look for candidates who can solve problems involving relative positioning in datasets without relying on complex subqueries if simpler methods exist.
Explain the purpose of LAG and LEAD functions in SQL
Comparing rows sequentially is common in trend analysis. Interviewers want to see if you know how to access adjacent rows efficiently.
What are SQL joins and how do INNER, LEFT, RIGHT, and FULL differ?
Joins are the most common operation in relational databases. Interviewers assess your ability to retrieve related data accurately and understand the nuances of different join types to avoid data loss or duplication.
What is an anti-join and how do you implement it in SQL?
Finding differences between datasets is a common requirement. Interviewers ask this to see if you know standard patterns like LEFT JOIN ... IS NULL or NOT EXISTS. It demonstrates depth in understanding set logic beyond simple equality.
What is database normalization and its benefits?
Reinforcing normalization benefits ensures candidates understand the 'why' behind the practice. Interviewers look for depth beyond just definitions. This confirms architectural maturity.
What are the different Types of Keys in Relational Model?
Keys define relationships and constraints. Interviewers assess your ability to design schemas that enforce referential integrity and uniqueness.
What are the different Types of DBMS Architecture?
Architecture impacts scalability and security. Interviewers assess your ability to design systems that separate concerns effectively.
Describe PRIMARY KEY and how it differs from UNIQUE key
Candidates often blur the lines between these two. Interviewers want to ensure clarity on NOT NULL requirements and quantity limits per table. This distinction is crucial for proper schema design.
What is a view in SQL and why use it?
Recruiters ask this to see if you understand database abstraction layers. They want to know if you can design solutions that hide complexity from end-users. It also checks your awareness of security practices, as views are often used to restrict access to sensitive columns.
Explain anti-joins in SQL
Finding 'in A but not B' is a common requirement. Interviewers ask this to see if candidates know the standard implementation techniques like LEFT JOIN...IS NULL or NOT EXISTS.
What are set operations like UNION, INTERSECT, and EXCEPT?
Set operations allow powerful data manipulation. Interviewers ask this to see if you can merge, compare, or exclude datasets efficiently. These operations are fundamental for complex reporting requirements involving multiple sources.
What SQL query finds the third largest salary in an employee table?
Recruiters use this question to assess logical thinking and proficiency in database querying, specifically regarding ranking and filtering data. It checks if the candidate understands how to handle duplicates and order data correctly to find specific rank values. This is a common scenario in reporting and analytics where finding top N records is a frequent requirement.
What is the difference between RANK, DENSE_RANK, and ROW_NUMBER?
Ranking data is essential for reporting top performers or leaders. Interviewers ask this to ensure you understand how ties are handled differently by each function. Choosing the wrong rank function can skew analysis results significantly.
What is PostgreSQL and what are its key features?
PostgreSQL is widely used in enterprise applications. Knowledge of it indicates database proficiency. Interviewers assess if the candidate understands its advantages over other databases like MySQL.
Ready to practice sql questions?
Get AI-powered feedback on your answers with our mock interview simulator.
Start Free Practice