SQL Normalization
SQL Normalization Interview with follow-up questions
1. Can you explain what SQL Normalization is and why it is important?
Normalization is the process of organizing a relational database to minimize data redundancy and eliminate data anomalies. It works by decomposing large, poorly structured tables into smaller, well-structured ones connected by relationships.
Why it is important:
- Eliminates update anomalies: Without normalization, updating one fact (e.g., a customer's city) may require changes in multiple rows — miss one and the data becomes inconsistent.
- Eliminates insertion anomalies: You shouldn't need to insert a dummy value just to store a piece of data.
- Eliminates deletion anomalies: Deleting one row shouldn't inadvertently destroy other information.
- Reduces storage: No duplicated data across rows.
- Improves data integrity: Constraints and relationships are easier to enforce on well-normalized tables.
The normal forms (applied progressively):
- 1NF: Atomic values, no repeating groups
- 2NF: No partial dependencies (non-key columns depend on the full primary key)
- 3NF: No transitive dependencies (non-key columns depend only on the primary key, not on each other)
- BCNF: Every determinant is a candidate key (stricter than 3NF)
- 4NF / 5NF: Address multi-valued and join dependencies (rarely needed in practice)
In practice: Most production databases target 3NF. BCNF and higher are applied selectively. Denormalization is sometimes chosen deliberately for performance (data warehouses, read-heavy OLAP workloads).
Follow-up 1
Can you provide an example of a situation where normalization would be beneficial?
Sure! Let's consider a hypothetical database for an online bookstore. Without normalization, we might have a single table called 'Books' that contains all the information about each book, including the author, publisher, and genre. However, this would lead to data duplication and potential inconsistencies. By normalizing the database, we can create separate tables for authors, publishers, and genres, and establish relationships between them. This allows us to avoid repeating author or publisher information for each book entry and ensures that any changes made to an author's details, for example, will automatically be reflected in all related book entries.
Follow-up 2
What are the potential downsides of normalization?
While normalization offers many benefits, there are also potential downsides to consider. One downside is that normalization can result in more complex database structures, with multiple tables and relationships. This can make the database design and querying process more complicated and time-consuming. Additionally, normalization can sometimes lead to increased storage requirements, as data is distributed across multiple tables. This can impact performance, especially when dealing with large datasets. It's important to strike a balance between normalization and denormalization based on the specific needs of the application.
Follow-up 3
How does normalization affect database performance?
Normalization can have both positive and negative impacts on database performance. On the positive side, normalization helps to improve data integrity and reduce data anomalies, which can lead to more accurate and reliable results. It also allows for efficient storage and retrieval of data, especially when dealing with smaller datasets. However, normalization can also introduce additional complexity and overhead, especially when dealing with larger datasets. Joining multiple tables together to retrieve data can be resource-intensive and slow down query performance. It's important to carefully consider the trade-offs and optimize the database design and indexing strategies to achieve the desired performance.
Follow-up 4
How does normalization relate to data redundancy?
Normalization aims to eliminate data redundancy by breaking down a database into multiple tables and establishing relationships between them. Redundancy occurs when the same piece of information is stored in multiple places, which can lead to inconsistencies and data anomalies. By normalizing the database, redundant data is minimized, and each piece of information is stored in only one place. This not only improves data integrity but also reduces storage requirements and ensures that any changes made to the data are reflected consistently throughout the database.
2. What are the different forms of normalization?
Normalization progresses through increasingly strict normal forms (NF). Each form adds rules on top of the previous:
First Normal Form (1NF)
- All columns contain atomic (indivisible) values
- No repeating groups or arrays within a single column
- Each row is unique
Violation: A phone_numbers column storing "555-1234, 555-5678" — split into separate rows or a related table.
Second Normal Form (2NF)
- Meets 1NF
- No partial dependencies — every non-key column must depend on the entire composite primary key
Only relevant when the primary key spans multiple columns. If order_id + product_id is the PK, storing product_name (which depends only on product_id) is a partial dependency.
Third Normal Form (3NF)
- Meets 2NF
- No transitive dependencies — non-key columns must depend only on the primary key, not on other non-key columns
Violation: employee_id → department_id → department_name. department_name depends on department_id, not directly on employee_id. Fix: move to a departments table.
Boyce-Codd Normal Form (BCNF)
- Stricter than 3NF: every functional dependency X → Y must have X as a super key
- Handles edge cases 3NF misses when there are overlapping candidate keys
Fourth Normal Form (4NF)
- Eliminates multi-valued dependencies — a table should not contain two or more independent multi-valued facts about an entity
Fifth Normal Form (5NF) / PJNF
- Eliminates join dependencies that are not implied by candidate keys
- Rarely applied in practice
Interview note: Most teams normalize to 3NF. BCNF is discussed in academic contexts and sometimes applied to specific problem tables.
Follow-up 1
Can you explain what 1NF is?
First Normal Form (1NF) is the most basic form of normalization. It requires that each column in a table contains only atomic values, meaning that each value in a column should be indivisible. Additionally, 1NF requires that each column has a unique name and that the order of the rows and columns is not significant.
For example, let's say we have a table called 'Employees' with the following columns: 'Employee ID', 'First Name', 'Last Name', and 'Skills'. In order to satisfy 1NF, we would need to ensure that each value in the 'Skills' column is atomic. Instead of storing multiple skills in a single cell (e.g., 'Java, Python, SQL'), each skill should be stored in a separate row.
Follow-up 2
What is the difference between 2NF and 3NF?
Second Normal Form (2NF) and Third Normal Form (3NF) are both forms of normalization that build upon the previous forms.
2NF requires that a table is in 1NF and that all non-key attributes are fully dependent on the entire primary key. This means that if a table has a composite primary key (made up of multiple columns), each non-key attribute must depend on the entire composite key, not just a part of it.
3NF, on the other hand, requires that a table is in 2NF and that there are no transitive dependencies. A transitive dependency occurs when a non-key attribute depends on another non-key attribute, rather than directly on the primary key.
In simpler terms, 2NF eliminates partial dependencies, while 3NF eliminates transitive dependencies.
Follow-up 3
Can you provide an example of a table in 3NF?
Certainly! Here's an example of a table in Third Normal Form (3NF):
Table: Orders
| Order ID | Customer ID | Product ID | Quantity |
|----------|-------------|------------|----------|
| 1 | 101 | 201 | 5 |
| 2 | 102 | 202 | 3 |
| 3 | 101 | 203 | 2 |
In this example, the 'Orders' table has a primary key composed of the 'Order ID' column. Each non-key attribute (e.g., 'Customer ID', 'Product ID', 'Quantity') depends directly on the primary key, and there are no transitive dependencies between the non-key attributes.
Follow-up 4
What is BCNF and how does it differ from 3NF?
Boyce-Codd Normal Form (BCNF) is a higher level of normalization than Third Normal Form (3NF). BCNF is based on the concept of functional dependencies, which are relationships between attributes in a table.
BCNF requires that for every non-trivial functional dependency X -> Y, X must be a superkey. In other words, every determinant (X) must be a candidate key.
The main difference between BCNF and 3NF is that BCNF eliminates all non-trivial dependencies, including those that are not directly related to the primary key. This means that BCNF provides a higher level of data integrity and eliminates more potential data anomalies than 3NF.
It's important to note that achieving BCNF may result in more tables and more complex relationships between them compared to 3NF.
3. How does normalization help in maintaining database integrity?
Normalization maintains database integrity by systematically eliminating the three types of data anomalies:
1. Update anomalies If the same fact appears in multiple rows, updating it requires changing every row. Miss one and the data is inconsistent. Normalization stores each fact once.
Example: A customer's city stored on every order row — if the customer moves, every order row must be updated. With normalization, city lives in the customers table and is updated in one place.
2. Insertion anomalies An un-normalized table may force you to insert incomplete or dummy data to satisfy the schema.
Example: A table that stores both course and instructor data requires inserting a course before you can store instructor information, even if no students are enrolled yet.
3. Deletion anomalies Deleting one record unintentionally destroys other information stored in the same row.
Example: Deleting the last student enrolled in a course also deletes the course's information if they share a table.
How each normal form addresses specific integrity issues:
- 1NF prevents ambiguous, multi-value cells that are hard to query and constrain
- 2NF ensures non-key data isn't duplicated across rows sharing part of a composite key
- 3NF ensures facts about one entity aren't mixed into another entity's table
- BCNF closes edge cases where overlapping candidate keys allow redundancy
The result: Each fact is stored exactly once. Constraints can be applied cleanly. Applications update data in one place. The risk of inconsistency shrinks dramatically.
Follow-up 1
Can you provide an example where normalization helps in maintaining data integrity?
Sure! Let's consider a database table for storing customer orders. Without normalization, we might have a single table with columns like customer name, customer address, order date, product name, product price, and quantity. In this case, if a customer places multiple orders, their name and address would be repeated for each order, leading to data redundancy. This redundancy can introduce inconsistencies, such as different addresses for the same customer. By normalizing the table into separate tables for customers and orders, we can eliminate this redundancy and ensure that each customer's information is stored only once, thus maintaining data integrity.
Follow-up 2
How does normalization affect data consistency?
Normalization helps in achieving data consistency by enforcing rules that prevent data duplication and inconsistencies. By eliminating data redundancy and ensuring that each piece of data is stored in only one place, normalization reduces the chances of conflicting or contradictory information. For example, if a customer's address is stored in multiple places, there is a risk of the address being updated in one place but not in others, leading to inconsistent data. Normalization helps in avoiding such issues and promotes data consistency.
Follow-up 3
What role does normalization play in handling database anomalies?
Normalization plays a crucial role in handling database anomalies. Anomalies are inconsistencies or errors that can occur in a database, such as update anomalies, insertion anomalies, and deletion anomalies. By following normalization rules, such as eliminating repeating groups, ensuring functional dependencies, and maintaining proper relationships between tables, normalization helps in reducing or eliminating these anomalies. For example, by separating data into multiple tables and establishing relationships between them, update anomalies can be avoided as changes in one table will not affect unrelated data. Normalization helps in structuring the database in a way that minimizes the risk of anomalies and ensures data integrity.
4. Can you explain the concept of functional dependency in the context of normalization?
A functional dependency (FD) describes a relationship between columns in a table: column B is functionally dependent on column A if knowing A's value determines B's value exactly.
Notation: A → B means "A functionally determines B"
Example:
In an employees table:
employee_id → {first_name, last_name, department_id, salary}— knowing the employee ID determines all other attributesdepartment_id → department_name— knowing the department ID determines the department name
Types of functional dependencies relevant to normalization:
Full functional dependency: B depends on all of a composite key A, not just part of it.
(order_id, product_id) → quantity— quantity depends on the full composite key ✓
Partial dependency (violates 2NF): B depends on only part of a composite key.
(order_id, product_id) → product_name— product_name depends only onproduct_id, not the full key ✗
Transitive dependency (violates 3NF): A → B → C, where B is not a candidate key.
employee_id → department_id → department_namedepartment_namedepends ondepartment_id, which depends onemployee_id- This creates redundancy —
department_nameis repeated for every employee in that department
Armstrong's Axioms (how FDs are reasoned about formally):
- Reflexivity: If B ⊆ A, then A → B
- Augmentation: If A → B, then AC → BC
- Transitivity: If A → B and B → C, then A → C
In interviews: You are most likely to be asked to identify transitive and partial dependencies when normalizing a given table to 2NF or 3NF.
Follow-up 1
How does functional dependency relate to 1NF, 2NF, and 3NF?
Functional dependency is closely related to the normalization forms 1NF, 2NF, and 3NF.
First Normal Form (1NF): Functional dependency is a fundamental concept in achieving 1NF. 1NF requires that each attribute in a relation must be atomic (indivisible) and there should be no repeating groups. By identifying and eliminating functional dependencies, we can ensure that each attribute contains only a single value.
Second Normal Form (2NF): 2NF builds upon 1NF and requires that every non-key attribute in a relation is fully functionally dependent on the primary key. This means that there should be no partial dependencies, where an attribute depends on only a part of the primary key. By analyzing functional dependencies, we can identify and eliminate partial dependencies.
Third Normal Form (3NF): 3NF builds upon 2NF and requires that there are no transitive dependencies in a relation. Transitive dependencies occur when an attribute depends on another non-key attribute, which in turn depends on the primary key. By identifying and eliminating transitive dependencies, we can achieve 3NF.
Follow-up 2
Can you provide an example of functional dependency?
Sure! Let's consider a relation called 'Students' with attributes 'StudentID', 'FirstName', 'LastName', and 'Grade'. If we assume that each student is uniquely identified by their 'StudentID', then we can say that 'StudentID' functionally determines 'FirstName', 'LastName', and 'Grade'. This can be represented as 'StudentID -> FirstName, LastName, Grade'. In this example, the value of 'StudentID' uniquely determines the values of 'FirstName', 'LastName', and 'Grade' for each student.
Follow-up 3
What is transitive dependency and how does it relate to normalization?
Transitive dependency is a type of functional dependency that occurs when an attribute depends on another non-key attribute, which in turn depends on the primary key. In other words, it describes a chain of dependencies where the value of one attribute determines the value of another attribute, which in turn determines the value of a third attribute.
Transitive dependencies are undesirable in database design because they can lead to data redundancy and update anomalies. To achieve normalization, we aim to eliminate transitive dependencies by decomposing the relation into smaller relations.
For example, let's consider a relation called 'Courses' with attributes 'CourseID', 'CourseName', 'Department', and 'DepartmentHead'. If we assume that 'DepartmentHead' depends on 'Department', and 'Department' depends on 'CourseID', then we have a transitive dependency. This can be represented as 'CourseID -> Department -> DepartmentHead'. To eliminate this transitive dependency, we can decompose the relation into two smaller relations: 'Courses' with attributes 'CourseID', 'CourseName', and 'Department', and 'Departments' with attributes 'Department' and 'DepartmentHead'.
5. When would you choose not to normalize a database?
Normalization is valuable but has trade-offs. There are legitimate scenarios where denormalization (intentionally introducing redundancy) is the better choice:
1. Read performance is critical Normalized schemas require JOINs to retrieve related data. At scale (millions of rows across many tables), JOINs become expensive. Denormalizing by embedding frequently joined columns avoids these joins.
Example: An e-commerce reporting dashboard that always needs order + customer_name + product_name may store these together rather than joining three tables on every query.
2. Data warehousing and analytics (OLAP) OLAP workloads favor star schemas and snowflake schemas — intentionally denormalized structures. Tools like BigQuery, Redshift, and Snowflake are optimized for wide, flat tables, not normalized OLTP schemas.
3. High write throughput causes join overhead In some real-time systems, even read-time joins are unacceptable. Precomputing and storing aggregated or joined data reduces query complexity.
4. Caching or snapshot requirements Audit logs and event stores intentionally store a full snapshot of state at a point in time (e.g., the customer's address at the time of the order) — this is deliberate denormalization for historical accuracy.
5. NoSQL / document databases by design Document databases (MongoDB, DynamoDB) embed related data within a document. This is denormalized by design and appropriate when documents are read as a unit.
The trade-off to state in interviews: Denormalization improves read performance at the cost of write complexity and data consistency risk. Any update to duplicated data must happen in multiple places — typically managed by the application layer or materialized views.
Follow-up 1
Can you provide an example where denormalization would be beneficial?
Sure! Let's say you have an e-commerce website where you need to display product information along with the customer's name and address. In a normalized database, you would have separate tables for products, customers, and addresses, and you would need to perform multiple joins to retrieve all the required information. However, by denormalizing the data and storing the customer's name and address directly in the product table, you can simplify the query and improve performance when displaying product information.
Follow-up 2
What are the trade-offs between normalization and denormalization?
Normalization and denormalization have different trade-offs:
Data integrity: Normalization ensures data integrity by eliminating redundancy and maintaining consistency. Denormalization, on the other hand, can introduce redundancy and increase the risk of data inconsistency.
Query performance: Normalization can sometimes result in complex joins and queries, which can impact performance. Denormalization can improve query performance by reducing the number of joins and simplifying data access.
Storage space: Normalization can reduce storage space by eliminating redundant data. Denormalization, on the other hand, can increase storage space due to the duplication of data.
Update anomalies: Normalization helps to minimize update anomalies by breaking down data into smaller, atomic units. Denormalization can increase the risk of update anomalies, as changes to denormalized data may need to be propagated to multiple locations.
The choice between normalization and denormalization depends on the specific requirements of the application and the trade-offs that are acceptable.
Follow-up 3
How does denormalization affect database performance?
Denormalization can have a positive impact on database performance in certain scenarios:
Reduced joins: Denormalization can reduce the number of joins required to retrieve data, which can improve query performance.
Simplified data access: By denormalizing data, you can simplify the data access process and avoid complex joins, which can result in faster data retrieval.
Improved aggregation: Denormalization can simplify data aggregation operations, such as calculating sums or averages, by pre-calculating and storing aggregated values.
However, it's important to note that denormalization can also have negative effects on performance if not implemented carefully. It can increase storage space, introduce redundancy, and potentially lead to data inconsistency if updates are not properly managed.
Live mock interview
Mock interview: SQL Normalization
- Read your scene and goals
- Talk it out; goals tick off live
- Get a score and stronger lines
Your voice and your AI key never touch our servers; the key stays in this browser and is sent only to Google. Only your round scores are saved to track progress.