This curated set of 20+ database interview questions reflects the most commonly asked topics across various company interviews I've faced. It includes conceptual questions and practical SQL query challenges designed to test real-world problem-solving skills. Each question is crafted to reinforce core database principles, query optimization techniques, and hands-on SQL proficiency.
These are only technical questions, it is not guaranteed that you will pass the interview if you know all the questions.
Note: Those questions are marked β are most asked.
- Difference between DBMS and RDBMS? βπ
- What is the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN? β
- What is a primary key and how does it differ from a unique key? ββ
- What is a clustered index vs a non-clustered index?
- What are transactions in SQL and what are ACID properties?
- What is the difference between DELETE, TRUNCATE, and DROP? ββπ
- What are window functions in SQL and when would you use them?
- How does a Common Table Expression (CTE) work and how is it different from a subquery? β
- What are the advantages and disadvantages of using stored procedures? β
- What is the difference between stored procedure and functions? βπ
- What is order of execution in sql?
- Find Nth highest salary from employee table (Include all possible varient)?
- Write the query to delete duplicate data or row from table (Include all possible varient)? β
Answer:
- Database Management System is a software that is used to define, create and maintain a database and provide controll to the data.
- RDBMS stands for Relational Database Management System. It is a type of database management system that stores data in a structured format using tables (rows and columns), and enforces relationships between data using keys.
| Feature | DBMS | RDBMS |
|---|---|---|
| Data Structure | Hierarchical or navigational | Tabular (tables with rows/columns) |
| Relationships | No relationships | Supports relationships via foreign keys |
| Normalization | Not enforced | Enforced to reduce redundancy |
| Useage | Deal with small quantity data | Deal with large amount of data |
| Examples | File System, XML | SQL Server, MySQL, PostgreSQL |
| Join Type | Description |
|---|---|
| INNER JOIN | Returns rows with matching values in both tables |
| LEFT JOIN | Returns all rows from the left table and matched rows from the right |
| RIGHT JOIN | Returns all rows from the right table and matched rows from the left |
| FULL JOIN | Returns all rows when there is a match in either table |
| Feature | Primary Key | Unique Key |
|---|---|---|
| Uniqueness | Ensures unique values | Ensures unique values |
| Nulls Allowed | Not allowed | Allowed (only one NULL) |
| Count per Table | Only one | Multiple allowed |
| Purpose | Entity identification | Enforce uniqueness |
| Type | Clustered Index | Non-Clustered Index |
|---|---|---|
| Data Storage | Sorts and stores data rows physically | Stores pointers to actual data rows |
| Count per Table | Only one | Multiple allowed |
| Speed | Faster for range queries | Faster for point queries |
| Example | Primary key by default | Secondary indexes |
- Transaction: A unit of work that is performed against a database.
- ACID:
- Atomicity: All or nothing
- Consistency: Maintains data integrity
- Isolation: Transactions are independent
- Durability: Changes persist after commit
| Command | Deletes Data | Removes Structure | Rollback Possible | Performance |
|---|---|---|---|---|
| DELETE | β | β | β | Slower |
| TRUNCATE | β (all rows) | β | β | Faster |
| DROP | β (all data) | β (table/schema) | β | Fastest |
- Perform calculations across a set of rows related to the current row.
- Examples:
ROW_NUMBER(),RANK(),LEAD(),LAG(),SUM() OVER() - Use cases:
- Ranking
- Running totals
- Time-based analysis
| Feature | CTE | Subquery |
|---|---|---|
| Readability | More readable and reusable | Less readable in complex queries |
| Recursion | Supports recursion | Does not support recursion |
| Scope | Temporary result set | Nested within a query |
Advantages:
- Reusability
- Improved performance
- Security (parameterized queries)
- Centralized business logic
Disadvantages:
- Harder to debug
- Versioning challenges
- Overuse can reduce flexibility
| Feature | Stored Procedure | Function |
|---|---|---|
| Return Type | Can return zero or more values | Must return a single value |
| Usage | Used for performing actions | Used for computations |
| Call in SELECT | β | β |
| Transactions | Can manage transactions | Cannot manage transactions |
Answer: FROM > WHERE > GROUP BY > HAVING > SELECT > ORDER BY > LIMIT