DBMS and SQL Questions For Interview
DBMS and SQL Questions For Interview
Answer: A DBMS offers advantages like data integrity, data security, data
consistency, data sharing, and data independence. It enables efficient data storage
and retrieval, supports concurrent user access, provides backup and recovery
mechanisms, and allows for data scalability and extensibility.
Answer: Some common database models include the hierarchical model, network
model, relational model, and object-oriented model. Each model organizes data in
a specific way, offering different strengths and limitations in terms of data
manipulation and querying.
Answer: The relational model organizes data into tables, consisting of rows
(tuples) and columns (attributes). It establishes relationships between tables using
keys. This model enables efficient data querying, manipulation, and supports the
use of Structured Query Language (SQL) for managing and accessing the data.
Answer: A primary key is a unique identifier for each record in a table. It ensures
data integrity by preventing duplicate or null values. It serves as a reference point
for establishing relationships with other tables, allowing efficient data retrieval and
manipulation.
Answer: A foreign key is a column or set of columns in a table that refers to the
primary key of another table. It establishes a relationship between two tables,
enforcing referential integrity. Foreign keys maintain data consistency and enable
efficient data retrieval across related tables.
Answer: ACID stands for Atomicity, Consistency, Isolation, and Durability. These
properties ensure reliable and consistent database operations. Atomicity guarantees
that a transaction is treated as a single unit of work. Consistency ensures that a
transaction brings the database from one valid state to another. Isolation ensures
concurrent transactions don't interfere with each other. Durability ensures that once
a transaction is committed, its changes are permanent.
Answer: A database schema is a blueprint or structure that defines the logical and
physical layout of a database. It describes the tables, relationships, constraints, and
other database objects. The schema provides a framework for organizing and
representing data in a consistent and understandable manner.
Answer: A stored procedure is a precompiled set of SQL statements that are stored
in a database and can be executed repeatedly. It allows for modular programming,
code reusability, and simplifies complex database operations. Stored procedures
enhance performance by reducing network traffic and improving security by
limiting direct SQL access.
Question 21: What is the difference between a clustered and non-clustered index?
Answer: A clustered index determines the physical order of data in a table, while a
non-clustered index creates a separate structure that contains index key values and
pointers to the actual data. In a table, only one clustered index can exist, but
multiple non-clustered indexes can be created.
Question 22: What is a deadlock in a database?
Answer: A deadlock occurs when two or more transactions are waiting for each
other to release resources, resulting in a circular dependency and a state of
inactivity. Deadlocks can lead to system performance degradation and require
intervention, such as implementing deadlock detection and resolution algorithms.
Question 24: What is the purpose of the COMMIT and ROLLBACK statements in
a transaction?
Answer: The COMMIT statement is used to permanently save the changes made
within a transaction to the database. It signifies the successful completion of the
transaction. The ROLLBACK statement, on the other hand, is used to undo the
changes made within a transaction and restore the database to its state before the
transaction started.
Question 27: What is the difference between a clustered and non-clustered index?
Answer: A clustered index determines the physical order of data in a table, while a
non-clustered index creates a separate structure that contains index key values and
pointers to the actual data. In a table, only one clustered index can exist, but
multiple non-clustered indexes can be created.
Answer: A deadlock occurs when two or more transactions are waiting for each
other to release resources, resulting in a circular dependency and a state of
inactivity. Deadlocks can lead to system performance degradation and require
intervention, such as implementing deadlock detection and resolution algorithms.
Answer: A data dictionary is a centralized repository that stores metadata about the
database, including information about tables, columns, data types, constraints, and
relationships. It provides a comprehensive view of the database structure and
serves as a reference for developers, administrators, and users to understand and
query the database effectively.
Question 30: What is the difference between DELETE and TRUNCATE
commands?
Answer: The DELETE command is used to remove specific rows from a table
based on a condition. It allows for a selective deletion and triggers associated
triggers or constraints. On the other hand, the TRUNCATE command is used to
remove all rows from a table, effectively resetting it. TRUNCATE is faster than
DELETE but cannot be rolled back and does not trigger constraints or triggers.
Question 31: What is the difference between UNION and UNION ALL operators
in SQL?
Answer: The UNION operator combines the result sets of two or more SELECT
statements and removes duplicate rows. It performs a distinct operation.
Conversely, the UNION ALL operator also combines result sets but includes all
rows, including duplicates. UNION ALL is faster than UNION but does not
eliminate duplicate rows.
Question 32: What is the difference between a primary key and a unique key?
Answer: A primary key is a column or set of columns that uniquely identifies each
row in a table. It ensures data integrity and enforces entity integrity. Only one
primary key can exist per table. A unique key, on the other hand, allows for
uniqueness but does not necessarily serve as the primary identifier for the table.
Multiple unique keys can exist in a table.
Answer: A self-join occurs when a table is joined with itself. It is used to retrieve
related information from a single table. To perform a self-join, aliases are used to
differentiate between the table instances being joined. Self-joins are commonly
used in hierarchical or recursive data structures.
Question 34: What is the difference between a candidate key and a composite key?
Answer: A candidate key is a column or set of columns that can uniquely identify a
row in a table. It is a potential candidate for a primary key. A composite key, on
the other hand, is a key that consists of multiple columns, combined to uniquely
identify a row. It offers a more granular level of uniqueness compared to a single
column candidate key.
Question 35: What is the difference between a view and a table in a database?
Answer: A table is a physical storage structure that holds data in rows and
columns. It is a persistent object in a database. A view, on the other hand, is a
virtual table created by defining a query on one or more tables. Views do not store
data physically but provide a way to present selected data.
Question 37: What is the difference between a clustered and non-clustered index?
Answer: A clustered index determines the physical order of data in a table, while a
non-clustered index creates a separate structure that contains index key values and
pointers to the actual data. In a table, only one clustered index can exist, but
multiple non-clustered indexes can be created.
Answer: Database backups are essential for data protection. Regular backups can
be scheduled using database-specific backup tools or scripts. They can be full,
incremental, or differential backups. Recovery involves restoring the database from
a backup and applying transaction logs to bring it to a consistent state. Point-in-
time recovery allows restoring the database to a specific transaction or time.
Backup strategies should consider recovery objectives, retention policies, and
storage options.