42 SQL Interview Questions and Answers (2026)

$104,864
What's that number you're asking?
That's the average salary earned by SQL developers according to recent studies.
Not bad, not bad at all.
With salaries like that and SQL being the 4th most popular programming language in existence...
It's never been a better time to upgrade your SQL skills.
What is SQL?
- SQL (Structured Query Language) is a domain-specific language used to manage and manipulate relational databases.
- Its primary purpose is to query and manage data in databases, including tasks like creating, updating, deleting, and retrieving data.
What is a database?
- A database is an organized collection of structured information or data stored electronically in a computer system, allowing for efficient retrieval, insertion, and management of data.
What is a primary key in SQL?
- A primary key is a column or a set of columns in a table that uniquely identifies each row in that table.
- Primary keys enforce entity integrity by ensuring that each row can be uniquely identified and that no duplicate or NULL values exist in the key columns.
What is a foreign key?
- A foreign key is a field in a table that establishes a link between two tables in a relational database.
- It enforces referential integrity by ensuring that values in the foreign key field match values in the primary key of another table.
- This maintains data consistency and prevents orphaned records.
Explain the ACID properties in the context of database transactions.
- ACID stands for Atomicity, Consistency, Isolation, and Durability, which are properties that ensure the reliability of database transactions.
- Atomicity: Transactions are treated as single, indivisible units. Either all changes in a transaction are committed, or none of them are.
- Consistency: Transactions take the database from one consistent state to another, preserving data integrity.
- Isolation: Transactions are isolated from each other, ensuring that one transaction's changes do not interfere with others.
- Durability: Once a transaction is committed, its changes are permanent and will survive system failures.
Don't let one question ruin your next technical interview...
Explain the difference between SQL and NoSQL databases.
- SQL databases are relational databases that use structured tables with predefined schemas. E.g. MySQL, PostgreSQL, and Oracle.
- NoSQL databases are non-relational and use flexible schemas. E.g. MongoDB, Cassandra, and Redis.
What is a JOIN in SQL, and what are its types?
A JOIN clause is used to combine rows from two or more tables based on a related column. Types of JOINs include:
- INNER JOIN: Returns records that have matching values in both tables. Use when you want to retrieve only the records that have corresponding matches in both tables. SELECT * FROM TableA INNER JOIN TableB ON TableA.id = TableB.tableA_id;
- LEFT (OUTER) JOIN: Returns all records from the left table and the matched records from the right table. Unmatched right table records result in NULL values.SELECT * FROM TableA LEFT JOIN TableB ON TableA.id = TableB.tableA_id;
- RIGHT (OUTER) JOIN: Returns all records from the right table and the matched records from the left table. Unmatched left table records result in NULL values. SELECT * FROM TableA RIGHT JOIN TableB ON TableA.id = TableB.tableA_id;
- FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table. Unmatched records from both tables result in NULL values on the side without a match. SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.id = TableB.tableA_id;
- CROSS JOIN: Returns the Cartesian product of rows from both tables. Each row from the first table is combined with all rows from the second table.SELECT * FROM TableA CROSS JOIN TableB;
- SELF JOIN: Joins a table to itself as if it were two separate tables, temporarily renaming at least one table in the SQL statement. SELECT a.EmployeeName, b.ManagerName FROM Employees a JOIN Employees b ON a.ManagerID = b.EmployeeID;
What is a view in SQL?
- A view is a virtual table based on the result-set of an SQL statement.
- It contains rows and columns just like a real table but does not store data physically; it pulls data from underlying tables.
- Example: CREATE VIEW SalesEmployees AS SELECT EmployeeID, FirstName, LastName, Salary FROM Employees WHERE Department = 'Sales';
What is an index in SQL, and why is it used?
- An index is a database object that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space.
- Indexes are used to quickly locate data without having to search every row in a table.
- Example: CREATE INDEX idx_customers_email ON Customers (Email);
- Creates a new index on the Email column.
Explain the difference between clustered and non-clustered indexes.
- Clustered Index: Sorts and stores the data rows in the table based on the index key; only one per table.
- Non-Clustered Index: Contains a pointer to the data rows; multiple can exist per table.
What is the difference between BETWEEN and IN operators in SQL?
- BETWEEN Operator: Used to filter records within a certain inclusive range. SELECT * FROM Employees WHERE Salary BETWEEN 50000 AND 80000;
- IN Operator: Used to filter records that match any value in a specified list. SELECT * FROM Employees WHERE Department IN ('Sales', 'Marketing', 'HR');
What is the difference between DELETE and TRUNCATE commands?
- DELETE: Removes specified rows from a table; can use WHERE clause; logs individual row deletions; can be rolled back.
- TRUNCATE: Removes all rows from a table; cannot use WHERE clause; logs deallocation of data pages; faster than DELETE; cannot be rolled back in some databases.
What are constraints in SQL, and name a few?
Constraints are rules applied to table columns to enforce data integrity. Examples include:
- NOT NULL
- UNIQUE
- PRIMARY KEY
- FOREIGN KEY
- CHECK
- DEFAULT
What are aggregate functions in SQL? Can you name a few?
- Aggregate functions perform calculations on multiple values and return a single value.
- Examples include:
- COUNT()
- SUM()
- AVG()
- MAX()
- MIN()
Explain SQL normalization and denormalization.
- Normalization: It is the process of organizing data in a relational database to minimize data redundancy and dependency. It is typically used to maintain data integrity, reduce storage space, and simplify data maintenance.
- Denormalization: It is the opposite of normalization, where data is intentionally duplicated or combined to improve query performance. Denormalization is used when read-heavy operations are more critical than data modification and
What is SQL injection, and how can you prevent it?
- SQL injection is a code injection technique that exploits security vulnerabilities in an application's software. Prevention methods include:
- Using parameterized queries
- Employing stored procedures
- Validating user input
- Using ORM frameworks
Explain ORDER BY in SQL.
- The ORDER BY clause is used to sort query results based on one or more columns. You can specify the sort order as ascending (ASC) or descending (DESC). It is commonly used to arrange data in a specific order for presentation.
- Example: -- Order by a Single Column (Descending Order) SELECT FirstName, LastName, City FROM Customers ORDER BY LastName DESC;
Explain the difference between the GROUP BY and HAVING clauses in SQL.
- GROUP BY: It is used to group rows that have the same values in specified columns. Typically used with aggregate functions like SUM or COUNT.
- HAVING: It filters grouped rows based on aggregate function results. It allows you to specify conditions on grouped data after using GROUP BY.
- Example: /* This query: - Groups by regions (GROUP BY) - Filters regions with total sales (HAVING) over 10,000 */ SELECT Region, SUM(Amount) AS TotalSales FROM Sales GROUP BY Region HAVING SUM(Amount) > 10000;
Explain the MERGE statement.
- The MERGE statement combines INSERT, UPDATE, and DELETE operations in a single SQL command based on a condition, often used for upserting (update or insert).
- Example:MERGE INTO TargetTable AS Target USING SourceTable AS Source ON Target.ID = Source.ID WHEN MATCHED THEN UPDATE SET Target.Name = Source.Name WHEN NOT MATCHED THEN INSERT (ID, Name) VALUES (Source.ID, Source.Name);
- This matches rows between TargetTable and SourceTable. If a match is found, it updates; otherwise, it inserts.
What is a stored procedure in SQL?
- A stored procedure is a prepared SQL code that can be saved and reused.
- It can accept parameters and allows for modular programming, improving performance and security.
- Example: -- This stored procedure calculates the total sales for a specific category -- provided as an input parameter and returns the result. DELIMITER $$ CREATE PROCEDURE GetTotalSalesByCategory(IN category_name VARCHAR(50), OUT total_sales DECIMAL(10, 2)) BEGIN -- Calculate the total sales for the given category and store it in the output parameter SELECT SUM(sales) INTO total_sales FROM sales_data WHERE category = category_name; END$$ DELIMITER ;
What is a trigger in SQL?
- A trigger is a database object that is automatically executed or fired when certain events occur, such as INSERT, UPDATE, or DELETE operations on a table.
- Example: -- This trigger automatically updates the "last_updated" column in a table -- whenever a row in the "products" table is updated. DELIMITER $$ CREATE TRIGGER UpdateLastUpdatedColumn AFTER UPDATE ON products FOR EACH ROW BEGIN -- Update the "last_updated" column to the current timestamp UPDATE products SET last_updated = NOW() WHERE id = NEW.id; END$$ DELIMITER ;
Explain database sharding and its benefits in managing large-scale databases.
- Database sharding is a technique for horizontally partitioning a database into smaller, more manageable pieces called shards. Each shard is hosted on a separate server.
- It improves scalability, distribution, and performance for large-scale databases by distributing data and query load across multiple servers.
What are Common Table Expressions (CTEs)?
- A CTE is a temporary named result set defined within the execution scope of a single SQL statement, improving readability and maintenance.
- Example:-- Calculates total sales by region (SalesCTE) and selects regions with sales > 10,000. WITH SalesCTE AS ( SELECT Region, SUM(Amount) AS TotalSales FROM Sales GROUP BY Region ) SELECT Region FROM SalesCTE WHERE TotalSales > 10000;
Explain SQL deadlock and provide strategies for preventing and resolving deadlocks in database systems.
- A deadlock occurs when two or more transactions block each other by holding resources the other needs, creating a cycle of dependency that prevents progress.
- Strategies to Prevent Deadlocks:
- Consistent Locking Order: Ensure all transactions acquire locks in the same sequence.
- Minimize Lock Time: Keep transactions short to reduce lock durations.
- Use Indexes: Reduce the scope of locks by optimizing queries with indexes.
- Lower Isolation Levels: Use READ COMMITTED or READ UNCOMMITTED to reduce locking where possible.
- Strategies to Resolve Deadlocks:
- Deadlock Detection: Database systems like SQL Server automatically detect and terminate one transaction (the victim).
- Retry Logic: Implement retry mechanisms in application logic to handle terminated transactions.
- Timeouts: Set transaction timeouts to avoid prolonged blocking.
What is the purpose of the SQL LIKE operator, and how can wildcards be used with it?
The LIKE operator is used to search for a specified pattern in a column. Wildcards can be used with LIKE:
- %: Matches any sequence of characters, zero or more characters. WHERE Name LIKE 'Jo%'; -- Matches 'John', 'Joe', 'Jordan', and 'Jo'
- _: Matches any single character. WHERE Name LIKE 'J_n'; -- Matches 'Jan', 'Jon', but not 'John' or 'Joon'
- []: Matches any character within the specified range. WHERE Name LIKE 'Jo[hns]%'; -- Matches 'John', 'Josh', 'Jon', but not 'Jody'
- [^]: Matches any character not in the specified range. WHERE Name LIKE 'Jo[^hn]%'; -- Matches 'Jonas', 'Jodie', but not 'John' or 'Johanna'
What is a pivot table in SQL?
- A pivot table is a data summarization tool used to transform rows into columns, providing a clear, aggregated view of data. It is often used to perform data analysis by grouping and aggregating data.
How do you implement error handling in SQL?
- TRY...CATCH (T-SQL): Handle errors in SQL Server. BEGIN TRY -- SQL statements END TRY BEGIN CATCH -- Handle errors SELECT ERROR_MESSAGE() AS ErrorMessage; END CATCH;
- EXCEPTION (PL/pgSQL): Handle errors in PostgreSQL.BEGIN -- SQL statements EXCEPTION WHEN OTHERS THEN RAISE NOTICE 'Error occurred'; END;
- DECLARE HANDLER (MySQL): Define error-handling behavior.DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN -- Handle errors ROLLBACK; END;
- Rollback on Errors: Use ROLLBACK to undo transactions if errors occur.
- Check Constraints: Validate data to avoid errors at the database level.
- Logging: Log errors to a table or file for debugging.
Explain the SQL concept of NULL.
- NULL represents missing or unknown data in SQL. It is not the same as an empty string or zero. In database design, NULL should be used sparingly and appropriately.
- It's important to handle NULL values in queries using functions like IS NULL or IS NOT NULL to ensure accurate results.
What is Write-Ahead Logging (WAL)?
- Write-Ahead Logging (WAL) is a database mechanism that ensures atomicity and durability by recording all changes in a log file before applying them to the main database.
- This enables recovery from crashes by replaying or rolling back the log to maintain a consistent state.
How do you optimize SQL queries?
- SQL query optimization is the process of improving the efficiency and speed of SQL queries. Techniques include:
- Creating appropriate indexes on columns used in WHERE clauses.
- Avoiding SELECT * and fetching only necessary columns.
- Rewriting inefficient queries.
- Using EXPLAIN or query execution plans to analyze and optimize query performance.
- Using joins appropriately
Explain database replication, its use cases, and its types.
- Database replication is the process of copying data from one database to another in real-time or near-real-time. Benefits of database replication include improved availability, fault tolerance, and read scalability.
- Use cases include creating high availability solutions, distributing data across geographically dispersed locations, and offloading read-heavy workloads.
- Types include:
- Snapshot Replication
- Transactional Replication
- Merge Replication
What is SQL concurrency control?
- Concurrency control in SQL ensures that multiple users can access and modify data simultaneously without conflicts.
- Techniques such as locking, isolation levels, and transactions are used to maintain data consistency by preventing issues like lost updates and data corruption.
Explain SQL partitioning.
- Partitioning is the practice of splitting a large table into smaller, more manageable partitions based on specific criteria (e.g., range, list, or hash).
- It can improve database performance by reducing the amount of data that needs to be scanned during queries, particularly for tables with millions of records.
- For example, partitioning by date can make it faster to retrieve data for a specific time period.
Explain SQL materialized views.
Explain SQL transaction isolation levels.
What do you understand by multi-version concurrency control?
What is SQL query caching and how does it improve database performance?
What are windowing functions, and how are they different from aggregate functions?
What are the best practices for securing a SQL database?
Name some strategies for database backup and recovery.
How do you handle transactions in distributed databases?
What is the difference between OLTP and OLAP systems?
About TechPrep
Never walk into a technical interview unprepared again. TechPrep empowers software engineers to stop guessing and start getting offers. We provide the exact questions asked by tech companies across Data Structures & Algorithms, System Design, Low-Level Design & Practical coding rounds. Don't leave your career up to chance. Join thousands of engineers who have successfully navigated the tech hiring maze and landed roles at top tech companies.