Database Interview Question
Database Interview Question
1. What is a database?
2. What is DBMS?
Answer: DBMS stands for Database Management System, which is software that
interacts with the database, applications, and users to capture and analyze data.
Answer: DBMS stores data as files, whereas RDBMS stores data in a tabular form
and supports relationships between tables.
4. What is SQL?
Answer: SQL stands for Structured Query Language, which is used to communicate
with and manage databases.
Answer: A table is a collection of related data entries organized in rows and columns.
Answer: A primary key is a unique identifier for a record in a table, ensuring each
record is unique.
Answer: A foreign key is a column that creates a relationship between two tables by
referencing the primary key of another table.
Answer: A candidate key is a column or a set of columns that can uniquely identify a
record in a table and could be chosen as the primary key.
Answer: A unique key ensures that all values in a column are different across all
records.
12. What is the difference between primary key and unique key?
Answer: A primary key uniquely identifies each record in a table and cannot be
NULL, while a unique key also ensures uniqueness but can have a NULL value.
Answer: Constraints are rules applied to columns to enforce data integrity. Examples
include PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, and CHECK.
Answer: A default constraint sets a default value for a column when no value is
specified.
Answer: A NOT NULL constraint ensures that a column cannot have a NULL value.
Answer: A clustered index sorts the records in the table based on the index key,
physically rearranging the data.
Answer: A non-clustered index creates a separate structure from the table to store
the index, which references the original table rows.
Answer: A view is a virtual table based on the result set of an SQL query, which does
not store data itself but displays data from one or more tables.
21. What is the difference between a view and a table?
Answer: A table stores data physically, while a view is a virtual table created by a
query.
Answer: Triggers are special types of stored procedures that automatically execute or
fire when certain events occur in a table.
Answer: A cursor is a database object that allows row-by-row processing of the result
set.
Answer: A subquery is a query nested inside another query, which is used to return
data that will be used in the main query.
Answer: A JOIN is used to combine rows from two or more tables based on a related
column between them.
Answer: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, CROSS JOIN, SELF
JOIN.
Answer: An INNER JOIN returns records that have matching values in both tables.
Answer: A LEFT JOIN returns all records from the left table and the matched records
from the right table. Records with no match in the right table will contain NULL.
Answer: A RIGHT JOIN returns all records from the right table and the matched
records from the left table. Records with no match in the left table will contain NULL.
31. What is a FULL JOIN?
Answer: A FULL JOIN returns all records when there is a match in either the left or
right table. Records that don't match will have NULL in the columns.
Answer: A CROSS JOIN returns the Cartesian product of the two tables, meaning it
returns all possible combinations of rows.
Answer: A UNION combines the results of two or more SELECT queries into a single
result set.
Answer: UNION removes duplicate records from the result set, while UNION ALL
includes all duplicates.
Answer: DELETE removes specific rows based on a condition and can be rolled
back, while TRUNCATE removes all rows from a table quickly without logging
individual row deletions and cannot be rolled back.
Answer: TRUNCATE removes all rows from a table but keeps the table structure,
while DROP removes the entire table, including its structure.
Answer: 1NF ensures that each column contains atomic (indivisible) values and each
record is unique.
41. Explain the Second Normal Form (2NF).
Answer: 2NF ensures that all non-key attributes are fully dependent on the primary
key.
Answer: 3NF ensures that all columns are dependent on the primary key and no
transitive dependencies exist.
Answer: Atomicity ensures that all operations within a transaction are completed
successfully or none at all.
Answer: Consistency ensures that a transaction brings the database from one valid
state to another.
Answer: Isolation ensures that transactions are executed in isolation from each other,
preventing interference.
Answer: Durability ensures that once a transaction is committed, it remains so, even
in the event of a system failure.
Answer: The different types of SQL commands are DDL (Data Definition Language),
DML (Data Manipulation Language), DCL (Data Control Language), and TCL
(Transaction Control Language).
Answer: DDL stands for Data Definition Language, which includes commands like
CREATE, ALTER, and DROP to define and modify the structure of database objects.
Answer: DML stands for Data Manipulation Language, which includes commands like
SELECT, INSERT, UPDATE, and DELETE to manipulate data in a database.
Answer: DCL stands for Data Control Language, which includes commands like
GRANT and REVOKE to control access to data in the database.
Answer: TCL stands for Transaction Control Language, which includes commands
like COMMIT, ROLLBACK, and SAVEPOINT to manage transactions in a database.
Answer: The WHERE clause filters rows before aggregation, while the HAVING
clause filters groups after aggregation.
Answer: The GROUP BY clause groups rows that have the same values in specified
columns and allows aggregate functions to be applied to each group.
Answer: The ORDER BY clause is used to sort the result set in ascending or
descending order based on one or more columns.
60. What are aggregate functions in SQL?
Answer: The COUNT() function returns the number of rows in a result set.
Answer: The SUM() function returns the total sum of a numeric column.
Answer: The AVG() function returns the average value of a numeric column.
Answer: A correlated subquery is a subquery that references columns from the outer
query and is executed once for each row processed by the outer query.
Answer: A scalar subquery returns a single value and can be used in a SELECT
statement as if it were a literal value.
Answer: A derived table is a subquery that appears in the FROM clause and is
treated as a temporary table within the query.
71. What is a CASE statement in SQL?
72. What is the difference between INNER JOIN and OUTER JOIN?
Answer: INNER JOIN returns only matching rows from both tables, while OUTER
JOIN returns all rows from one table and matching rows from the other, filling with
NULLs where there is no match.
Answer: A materialized view is a database object that stores the result of a query
physically and can be refreshed periodically, unlike a regular view which is virtual.
Answer: A schema is a logical container for database objects like tables, views, and
procedures, helping organize and group related objects.
Answer: A data type defines the kind of data that can be stored in a column, such as
INTEGER, VARCHAR, DATE, etc.
80. What is a surrogate key?
Answer: A foreign key constraint ensures that the values in a foreign key column
match the values in the primary key column of the referenced table.
82. What is the difference between DROP and DELETE commands in SQL?
Answer: DROP removes the entire table, including its structure, while DELETE
removes data from the table but retains the table structure.
Answer: SQL injection is a code injection technique where an attacker can execute
malicious SQL code by inserting it into a query, potentially compromising the
database.
Answer: A deadlock occurs when two or more transactions hold locks on resources
that the other transactions need, causing a cycle of dependencies and blocking each
other indefinitely.
Answer: A savepoint allows you to set a point within a transaction to which you can
later roll back, without affecting the entire transaction.
86. What is the difference between a local and a global temporary table?
Answer: A local temporary table is only visible to the session that created it and is
deleted when the session ends, while a global temporary table is visible to all
sessions and is deleted when the last session referencing it ends.
Answer: A transaction log records all transactions and changes made to the
database, providing a way to recover data in case of a failure.
Answer: The ROLLBACK statement undoes all changes made by the current
transaction, returning the database to its previous state.
89. What is a composite index?
Answer: A bitmap index uses a bitmap for each distinct value in a column, allowing
efficient querying for columns with a low cardinality.
Answer: A hash index uses a hash function to map search keys to corresponding
bucket addresses, allowing for fast data retrieval.
Answer: Partitioning divides a table into smaller, more manageable pieces, called
partitions, which can improve query performance and manageability.
Answer: A B-tree index is a balanced tree structure that maintains sorted data,
allowing for efficient insertion, deletion, and search operations.
Answer: A full-text index allows for efficient searching of text data within a table,
supporting advanced text queries like those that search for specific words or phrases.
Answer: A unique constraint ensures that all values in a column or a set of columns
are unique across all rows in a table.