DBMS Q/A
What is a database?
A database is an organized collection of data stored so it can be accessed, managed and
updated efficiently.
Example: “A Users table storing id, name, email is a simple database structure.”
What is a DBMS and an RDBMS?
A DBMS is software that manages databases; an RDBMS stores data in tables with relations
and uses SQL.
Example: “MySQL is an RDBMS — it stores tables and enforces relationships via keys.”
What are tables, rows, and columns?
Tables are collections of rows (records); columns are the attributes of those records.
Example: “In Orders, each row is one order and columns are order_id, amount, date.”
What are DDL, DML, DCL and TCL?
DDL defines schema (CREATE), DML manipulates data (SELECT, INSERT), DCL manages
permissions (GRANT) and TCL controls transactions (COMMIT).
Example: “CREATE TABLE is DDL; INSERT is DML; COMMIT is TCL.”
What is SQL?
SQL is the declarative language used to query and modify relational databases.
Example: “SELECT name FROM users WHERE id=1;”
What is an INNER JOIN?
Returns rows that have matching keys in both tables.
Example: “SELECT [Link], [Link] FROM users u INNER JOIN orders o ON
[Link]=o.user_id;”
What is a LEFT (OUTER) JOIN?
Returns all rows from the left table and matches from the right, filling NULLs when no match
exists.
Example: “List all users and their orders — even users with no orders — using LEFT JOIN.”
What is a RIGHT JOIN?
Opposite of LEFT JOIN: returns all rows from the right table and matching left rows (less
commonly used).
Example: “Get all orders and attach user data, using RIGHT JOIN or swap tables and LEFT
JOIN.”
What is a FULL OUTER JOIN?
Returns all rows from both tables, with NULLs where there’s no match.
DBMS Q/A
Example: “Combine customer list and supplier list and see unmatched rows with FULL JOIN.”
What is a CROSS JOIN?
Produces a Cartesian product — every combination of rows between two tables.
Example: “10 products × 5 colors = 50 rows via CROSS JOIN.”
What is a subquery?
A query nested inside another query, used to compute values or filter results.
Example: “WHERE salary > (SELECT AVG(salary) FROM employees)”
What are aggregate functions?
Functions like COUNT, SUM, AVG, MIN, MAX that summarize multiple rows into one value.
Example: “SELECT COUNT(*) FROM orders gives total orders.”
How do GROUP BY and HAVING work?
GROUP BY groups rows for aggregation; HAVING filters those groups based on aggregate
conditions.
Example: “GROUP BY department HAVING COUNT(*)>5 shows departments with >5
employees.”
Difference between WHERE and HAVING?
WHERE filters rows before grouping; HAVING filters groups after aggregation.
Example: “Use WHERE age>30 but HAVING AVG(salary)>50000.”
Difference between UNION and UNION ALL?
UNION removes duplicates; UNION ALL keeps duplicates and is faster.
Example: “Combine two result sets with UNION ALL if duplicates are acceptable.”
What is a transaction?
A group of DB operations treated as one unit — either all succeed (COMMIT) or all fail
(ROLLBACK).
Example: “Transferring money: withdraw from A and deposit to B inside one transaction.”
What are ACID properties?
Atomicity, Consistency, Isolation, Durability — guarantees for reliable transactions.
Example: “After COMMIT, changes are durable even after a crash.”
What is normalization?
Organizing tables to reduce redundancy and avoid update anomalies by splitting data into
related tables.
Example: “Store customer address in Customers, not repeated in each Orders row.”
DBMS Q/A
Explain 1NF, 2NF, 3NF briefly.
1NF: atomic values; 2NF: no partial dependency on a composite key; 3NF: no transitive
dependencies.
Example: “Move advisor info to its own table to fix a transitive dependency (3NF).”
What is denormalization?
Intentionally adding redundancy to speed up reads, at the cost of extra storage and update
complexity.
Example: “Copy customer name into Orders to avoid joining for frequent reports.”
What is a primary key?
A column (or set) that uniquely identifies each row and cannot be NULL.
Example: “id as PRIMARY KEY in users.”
What is a foreign key?
A column referring to a primary key in another table to enforce referential integrity.
Example: “orders.user_id referencing [Link].”
Primary key vs Unique constraint?
Primary key = unique + not-null + one per table; UNIQUE enforces uniqueness but can allow
NULLs and multiple UNIQUEs.
Example: “Email can be UNIQUE; id is PRIMARY KEY.”
What are NOT NULL, CHECK, DEFAULT constraints?
NOT NULL prevents NULLs; CHECK enforces a condition; DEFAULT supplies a value when
none provided.
Example: “age INT CHECK (age>=0) DEFAULT 0 NOT NULL.”
What is a composite key?
A primary key made of two or more columns together uniquely identifying a row.
Example: “(student_id, course_id) in enrollments.”
Surrogate key vs natural key?
Natural key uses real-world data (email); surrogate is an artificial ID (user_id) for
simplicity/performance.
Example: “Use user_id auto-increment as surrogate key even if email is unique.”
What is an index and why use it?
A structure (often B-tree) to speed up lookups; speeds SELECT but slows writes and uses
space.
Example: “Index email for fast user lookup by email.”
DBMS Q/A
What is query optimization?
DB engine chooses the fastest execution plan (indexes, join order) to run a query efficiently.
Example: “EXPLAIN shows whether a query uses an index or a full table scan.”
What is a view?
A virtual table defined by a stored SELECT — useful for simplifying queries or restricting
access.
Example: “CREATE VIEW ActiveUsers AS SELECT id,name FROM users WHERE
active=1;”
Why use views?
To reuse complex queries, simplify access, and provide a restricted or consistent interface.
Example: “Expose only name and email to junior analysts via a view.”
What is a stored procedure?
Predefined SQL code stored in the DB that can take params and be executed as a unit.
Example: “GetUserOrders(userId) procedure returns orders for a user.”
Why use stored procedures?
For performance (precompiled), reuse, and encapsulating logic on the server side.
Example: “Centralize business logic like invoicing in a procedure.”
What is a trigger?
A DB routine that automatically runs on certain events (INSERT/UPDATE/DELETE) on a table.
Example: “Log changes to users on AFTER UPDATE via a trigger.”
What is referential integrity?
Ensuring relationships between tables remain valid, typically via foreign keys.
Example: “Cannot insert order with user_id that doesn’t exist in users.”
What is ON DELETE CASCADE?
A foreign key option that deletes child rows automatically when the parent is deleted.
Example: “Deleting a user removes their orders if ON DELETE CASCADE set.”
What are the DBMS abstraction levels?
Physical (how data is stored), logical (schema), and view (user’s perspective).
Example: “You design tables at the logical level; storage engine handles the physical level.”
What operations does a DBMS handle?
Schema definition, data update, data retrieval, and administration (users, backups).
Example: “DBA handles backups and user permissions; devs write queries.”
DBMS Q/A
Difference between DELETE, TRUNCATE, and DROP?
DELETE removes rows (can be rolled back); TRUNCATE quickly removes all rows (often
non-rollback); DROP removes the table schema.
Example: “Use DELETE WHERE to remove specific rows; TRUNCATE to empty a table fast.”
What is a data dictionary/catalog?
Metadata store describing tables, columns, types and privileges — the DB’s internal “schema of
schemas.”
Example: “Information schema tables list column names and types.”
Clustered vs non-clustered index?
Clustered index defines physical row order (one per table); non-clustered is a separate lookup
structure.
Example: “Primary key often implemented as clustered index on id.”
What is a cursor?
A mechanism to iterate result rows one by one; useful for row-based processing but slower
than set ops.
Example: “Use cursor in stored proc to process each record sequentially.”
What is NoSQL?
Non-relational databases offering flexible schemas — document, key-value, wide-column, or
graph models.
Example: “MongoDB stores JSON-like documents; Redis is a key-value store.”
SQL vs NoSQL — main differences?
SQL = structured schema, ACID, vertical scaling; NoSQL = flexible schema, horizontal scaling,
eventual consistency in many cases.
Example: “Use SQL for transactions (banking) and NoSQL for large, evolving user content.”
Examples of SQL and NoSQL databases?
SQL: MySQL, PostgreSQL, Oracle; NoSQL: MongoDB, Cassandra, Redis, Neo4j.
Example: “Postgres for transactional app, Cassandra for high-write analytics.”
When to use NoSQL vs SQL?
Use SQL for structured data and transactions; NoSQL for high scale, flexible schemas or
specialized data models.
Example: “Use MongoDB for user activity logs; Postgres for orders/payments.”
What is data redundancy?
Duplicate data across the DB that can cause inconsistencies; normalization reduces it.
Example: “Storing address in both customers and orders is redundant.”
DBMS Q/A
What is a deadlock?
Two or more transactions waiting on locks held by each other — DB detects it and rolls back
one to recover.
Example: “T1 locks A then B, T2 locks B then A → deadlock.”
What is eventual consistency?
A model where updates propagate to all nodes eventually — reads may be stale for a short
time.
Example: “After writing to one server, another replica may show the update seconds later.”
What is the CAP theorem?
In distributed systems you can guarantee only two of Consistency, Availability, and Partition
tolerance simultaneously.
Example: “Some distributed stores choose Availability + Partition tolerance over immediate
Consistency.”
ACID vs BASE?
ACID = strict transactional guarantees; BASE = Basically Available, Soft state, Eventually
consistent — used by many NoSQL systems.
Example: “Banking needs ACID; a social feed may accept BASE for performance.”
OLTP vs OLAP?
OLTP handles transactional workloads (many small, fast operations); OLAP handles analytical
queries over large datasets.
Example: “Order entry is OLTP; monthly sales roll-up is OLAP.”
Why denormalize in a data warehouse?
To speed up complex read queries by reducing costly joins; warehouses prioritize read
performance.
Example: “Store customer name with sales facts for fast reporting.”
Schema vs instance?
Schema is the DB design (tables/columns); instance is the actual data at a point in time.
Example: “Schema defines users table; instance is the current rows inside it.”
What is a join condition?
The ON clause that specifies how rows from two tables match (e.g., ON [Link]=b.a_id).
Example: “ON orders.user_id = [Link] links orders to users.”
What is a composite index?
An index on multiple columns to speed queries that filter/sort by that column combination.
Example: “Index on (customer_id, order_date) speeds queries filtering by both.”
DBMS Q/A
What is SQL injection and prevention?
Attack where untrusted input is executed as SQL; prevent by using parameterized queries or
prepared statements.
Example: “Use ? or $1 parameters instead of concatenating user input into SQL strings.”
Main differences between OLTP and OLAP databases?
OLTP: normalized, fast writes, many small transactions; OLAP: denormalized, read-heavy,
complex queries for analytics.
Example: “Use star schema in OLAP for reporting speed.”
What is an execution plan?
A DB-provided plan showing how a query will run (index use, join order) useful for performance
tuning.
Example: “Run EXPLAIN to see if your query is doing a table scan.”
What is an ORM?
Object-Relational Mapping maps database tables to programming language objects to simplify
DB access.
Example: “Hibernate maps User class to users table so you can use [Link]().”
Quick summary / key takeaways?
Design with normalization for integrity, use indexes and EXPLAIN for performance, wrap related
ops in transactions for safety, and pick SQL vs NoSQL based on consistency and scale needs.
Example: “For an e-commerce app: Postgres (SQL) for orders; Redis for session caching.”