…
…
SQL and Database Fundamentals · Lesson 32 / 41 · Data modeling
PreviewIn this lesson you will
You can read the explanation without an account. The exercises and quiz at the end of the lesson are for members.
The department secretary kept the Spring 2026 grades in one spreadsheet, with the student, department, faculty, course and letter grade in every row. The file was imported into the database as it is, and it now sits in a single table called raw with 238 rows. In this lesson you will see the problems of that table in numbers and split it into the right tables.
For someone who works in a spreadsheet, one table feels natural: every piece of information is in one row, everything you look for is in front of you, and you never write a JOIN. The secretary thought the same way. In this lesson you will see why that table becomes a problem over time. Then, with the keys you learned in the design unit, you will split it into the structure of the case database: the student, department, course and enrollment tables.
In this lesson you will:
COUNT(*) and COUNT(DISTINCT ...), and name the three problems that repetition causes.CREATE TABLE ... AS SELECT DISTINCT, then join the parts back to show that no information was lost.Let's rebuild the secretary's table from the case database with the CREATE TABLE ... AS SELECT from lesson 28. The query below uses four JOINs to put all the enrollments of Spring 2026 (term_id = 2) into one flat table. In this lesson and in the exercises, raw is always built with this command.
Example 1 (case): The raw table
CREATE TABLE raw AS SELECT s.student_no, s.first_name AS name, d.name AS department, d.faculty, c.code, c.name AS course, c.credits, e.letter_grade FROM enrollment e JOIN student s ON s.id = e.student_id JOIN department d ON d.id = s.department_id JOIN course_offering o ON o.id = e.offering_id JOIN course c ON c.id = o.course_id WHERE o.term_id = 2; SELECT * FROM raw ORDER BY student_no, code LIMIT 4;
| student_no | name | department | faculty | code | course | credits | letter_grade |
|---|---|---|---|---|---|---|---|
| 202302001 | Camila | Software Engineering | Faculty of Engineering | GEN102 | Quantitative Reasoning I | 3 | A |
| 202302001 | Camila | Software Engineering | Faculty of Engineering | GEN104 | Information Technology Skills | 2 | A |
| 202302001 | Camila | Software Engineering | Faculty of Engineering | SWE302 | Software Engineering | 3 | A |
| 202302002 | Rosa | Software Engineering |
Something stands out even in the first four rows. Camila took three courses, so her name, department and faculty are written three times. The name and credits of Quantitative Reasoning I also appear again in the row of every student who took it. Seeing everything in one row comes at the price of writing the same information many times.
Saying "there is a lot of repetition" is a feeling. Measuring it takes a query. COUNT(*) counts all rows, and COUNT(DISTINCT column) counts the different values in that column (lesson 11). The gap between the two shows how many times the same value was written again.
Example 2 (case): The size of the repetition
SELECT COUNT(*) AS row_count, COUNT(DISTINCT student_no) AS students, COUNT(DISTINCT department) AS departments, COUNT(DISTINCT faculty) AS faculties, COUNT(DISTINCT code) AS courses FROM raw;
Result (after raw is built):
| row_count | students | departments | faculties | courses |
|---|---|---|---|---|
| 238 | 88 | 7 | 5 | 12 |
In 238 rows there are only 88 students, 7 departments, 5 faculties and 12 courses. So 5 different faculty names have been written 238 times. To see how many times each faculty is written, grouping is enough: Faculty of Engineering 73, Faculty of Business and Management 72, Faculty of Humanities and Social Sciences 46, Faculty of Communication 27, School of Applied Technology 20. Run this count yourself in the cell below.
Try it now· not graded; doesn't affect progress
Now change the last query:
1. Write department instead of faculty in both places. Which two departments repeat the most? (Business Administration and Software Engineering, 49 times each.)
2. This time count by the course column and use ORDER BY repeats (ascending). How many times is the least repeated course written? (Algorithms and Programming II, 3 times.)
Repetition does not only take up space. The real harm shows up when you try to change the data. When a piece of information is stuck to a row it does not belong to, changing, adding or deleting that row damages other information. These damages are called anomalies, and there are three kinds.
raw at once. If you miss one, the same department appears under two different faculties and the data contradicts itself. The table can no longer tell you which one is right.raw is a grade a student received in a course, so there is no row to write a department without students. In fact, this department does not appear in raw at all.Which anomaly is each situation in the single-table design?
For each row, pick its match from the list, then check.
The way to fix these problems is normalization: splitting tables by clear rules and moving each piece of information to where it belongs.
The rules of normalization are described with steps called normal forms. In practice the first three are enough, and all three rest on the same question: "What does the information in this column depend on?" A column depends on something if, when you know that thing, you know the value of the column for certain. If you know the student number, you know the student's name; the name depends on the student number.
First normal form (1NF): Each cell holds a single value and there are no repeating groups of columns. Writing two phone numbers of a student in one cell like 555-0101, 555-0102, or creating columns like course1, course2, course3, breaks 1NF. When a third phone number or a fourth course arrives, the table is not enough, and the question "who takes this course?" means searching three columns at once. The fix is to move the repeating information into separate rows or into a separate table. raw is in 1NF: every cell holds a single value and every course is in its own row.
Second normal form (2NF): The table is in 1NF and no column depends on only a part of the key. This rule matters with composite keys (lesson 29). In raw, what identifies a row on its own is the pair (student_no, code): the same student takes the same course once in a term. The letter grade depends on both parts of this pair, because it is the grade of that student in that course. But the student's name depends only on student_no: whichever course it is, Camila is Camila. The course name depends only on code. Columns that depend on only a part of the key are called partial dependencies and they break 2NF.
The key of raw is (student_no, code). Which part of the key does the credits column depend on?
The fix for 2NF is to move the partially dependent columns into their own tables: the student's columns go to a student table, the course's columns go to a course table, and only the two keys and the letter grade stay behind.
Third normal form (3NF): The table is in 2NF and no non-key column depends on another non-key column. Imagine we split off the student information: (student_no, name, department, faculty). The faculty depends not on the student but on the department: the student number determines the department, and the department determines the faculty. This link from the student to the faculty through a middle step is called a transitive dependency and it breaks 3NF. The fix is to move the department and the faculty into a separate department table. In short, in 3NF every non-key column depends on the key, the whole key, and nothing but the key.
"The faculty depends on the department" is a claim, and you can check it in the data. If a department really determines a single faculty, the number of different faculties written for each department should be 1:
SELECT department, COUNT(DISTINCT faculty) AS faculty_count FROM raw GROUP BY department ORDER BY department;
| department | faculty_count |
|---|---|
| Business Administration | 1 |
| Computer Programming | 1 |
| Industrial Engineering | 1 |
| Management Information Systems | 1 |
| New Media and Communication | 1 |
| Psychology | 1 |
| Software Engineering | 1 |
All seven rows are 1. You also need to check the opposite direction: does the faculty determine the department? If you group by faculty and count COUNT(DISTINCT department) with the same query, Faculty of Engineering gives 2 and Faculty of Business and Management gives 2. So a dependency has a direction: whoever knows the department knows the faculty, but whoever knows the faculty does not know the department. That is why the faculty goes into the department table, not the other way around.
A warning: the data shows only today's rows. A 1 for every department means "there is no contradiction in this table"; the rule itself ("each department belongs to a single faculty") comes from how the university works. The query supports or rejects your claim; it does not make the rule by itself.
What problem does it cause to write the faculty name of the department in every row of the student table?
We know the rules; now let's really split the table. The tool is the CREATE TABLE ... AS SELECT you saw in lesson 28. You remove the duplicates of the repeating information with DISTINCT and move it into its own table. The question to ask for each table is the same: "Which columns in this table depend on the same thing?"
Example 3 (case): Splitting the raw table into four
CREATE TABLE department_split AS SELECT DISTINCT department, faculty FROM raw; CREATE TABLE course_split AS SELECT DISTINCT code, course, credits FROM raw; CREATE TABLE student_split AS SELECT DISTINCT student_no, name, department FROM raw; CREATE TABLE grade_split AS SELECT student_no, code, letter_grade FROM raw; SELECT (SELECT COUNT(*) FROM department_split) AS departments, (SELECT COUNT(*) FROM course_split) AS courses, (SELECT COUNT(*) FROM student_split) AS students, (SELECT COUNT(*) FROM grade_split) AS enrollments;
| departments | courses | students | enrollments |
|---|---|---|---|
| 7 | 12 | 88 | 238 |
The numbers match Example 2: 7 departments, 12 courses, 88 students. Each entity is written once in its own table. The student table still has department but faculty is gone: the information that links a student to a department belongs to the student, while the faculty belongs to the department. The grade table has no DISTINCT because each row is already a separate record; here we do not want to remove anything.
DISTINCT does not solve everything. Imagine you added the code column to the student table by mistake: SELECT DISTINCT student_no, name, department, code FROM raw. The result is 238 rows, still 88 students. DISTINCT removes only rows where all columns are the same; Camila's three rows differ in the code column, so all three stay. The course code depends on the enrollment, not on the student; that column does not belong in the student table. If a single column that does not fit the question "what does this table describe?" gets into a table, the repetition comes back.
The order of the splitting work also matters. Put the steps below in the order you do them, from start to finish.
Put the steps of normalizing a raw table in the order you do them.
Click the lines in order; to undo one you placed wrong, click it in the list below.
Shuffled lines
Your order
"We split the table into four; was information lost?" This is the most common worry about normalization. The answer comes from a query, not a guess: join the four tables on their keys and look at the row count. As you learned in lesson 18, if a join loses rows, something does not match, and if it multiplies rows, one side has unexpected repetition.
Example 4 (case): Joining the four tables back
SELECT COUNT(*) AS row_count FROM grade_split g JOIN student_split s ON s.student_no = g.student_no JOIN department_split d ON d.department = s.department JOIN course_split c ON c.code = g.code;
| row_count |
|---|
| 238 |
238 is the row count of raw. Each enrollment found its student, the student found their department, and the enrollment found its course exactly once; there is no loss and no multiplication. Every piece of information in raw can be rebuilt from the four tables. Splitting does not lose information; it only removes the repetition.
This check also catches a wrong split. Remember the faulty student table from a moment ago.
Suppose student_split had been built by mistake with the code column (238 rows, each student appearing as many times as the number of courses they took). How many rows would you get by joining it to grade_split on student_no?
SELECT COUNT(*) FROM grade_split g JOIN student_split s ON s.student_no = g.student_no;
At the end of these steps, the structure we reach is the case database you use throughout the course. raw → student, department, course, and the link table enrollment that connects students and courses. The case has one more table: course_offering. A real database keeps many terms, and which course is offered in which term with which instructor depends on neither the course nor the student; it depends on the offering itself. Our raw covers one term, so we did not need this separation.
In the case department table, the faculty name is still written as text in each department's row: Faculty of Engineering appears in three rows (Software, Industrial and Data Science). This is not an anomaly. The faculty is a property of the department and is written once for each department. If the faculty had its own information (a dean, a phone number, a building), it would be right to move it into a separate faculty table too; then the faculty name would stay in a single row. Whether to do this is a design decision. Also, Data Science and Analytics, which never appears in raw, is in the case department table: the insert anomaly is solved, and a department without students can be recorded.
Normalization has a cost: information is spread across tables and queries need JOINs. Sometimes a repetition is left on purpose for performance; this is called denormalization. The student.gpa column in the case is like this. The average can always be calculated from the grades in the enrollment table, so it is a repetition derived from other information; still, it is stored. The values of the first three students are 3.65, 3.88 and NULL (a new student with no grades yet). The cost is this: if you forget to update the average when a new grade is entered, the column shows a wrong value and nobody notices. Denormalization should be a conscious decision, not the result of neglect.
The report page of the student office recalculates every student's grade average from 781 enrollments each time it opens, and it opens slowly. What do you do?
Pick an option and see what happens. There are no points; you can try the other options too.
The same thinking works in every database. The analytics team of the Earbud music app exported the plays as one flat dump: who played which song, by which artist, the artist's genre and the date.
Example 5 (outside the case): Earbud play dump
CREATE TABLE dump AS SELECT l.nickname, s.title AS song, a.name AS artist, a.genre, p.played_on FROM play p JOIN listener l ON l.id = p.listener_id JOIN song s ON s.id = p.song_id JOIN artist a ON a.id = s.artist_id; SELECT COUNT(*) AS row_count, COUNT(DISTINCT artist) AS artists, COUNT(DISTINCT genre) AS genres FROM dump;
| row_count | artists | genres |
|---|---|---|
| 217 | 7 | 5 |
In 217 plays there are only 7 artists and 5 genres. The name of Broken Compass and the genre "rock" are written 70 times. If an artist's genre changes (say a band changes its style), you have to fix 70 rows at once: the update anomaly you know.
What does the genre column of the dump (pop, rock, rap...) depend on?
If you split the dump into 3NF you arrive at Earbud's own structure: artist (name, genre, city), song (title, duration, artist), listener, and play, which links the two. The app's database is already built this way; the dump is a JOIN-flattened view of these tables. Producing a flat dump for reports is fine; storing information there is not.
SELECT * FROM department_split ORDER BY department;
Result (after department_split is built):
| department | faculty |
|---|---|
| Business Administration | Faculty of Business and Management |
| Computer Programming | School of Applied Technology |
| Industrial Engineering | Faculty of Engineering |
| Management Information Systems | Faculty of Business and Management |
| New Media and Communication | Faculty of Communication |
| Psychology | Faculty of Humanities and Social Sciences |
| Software Engineering | Faculty of Engineering |
Faculty of Engineering went from 73 rows down to 2. These two rows are not repetition: each one states the faculty of a different department.
student_wrong student_no, name, department, code raw; (*) row_count, ( student_no) students student_wrong;
Thinking normalization means "splitting every column into its own table". The goal is not to increase the number of tables but to keep each piece of information in one place. A student's name and birth date depend on the same student, so they stay in the same table; splitting them into separate tables removes no anomaly and only adds extra JOINs. You find where a column belongs with the question "what does this information depend on?": if it depends on the student, it goes to the student table; if on the course, to the course table; if on both, to the link table.
COUNT(*) and COUNT(DISTINCT ...), and normalization keeps each piece of information in one place.GROUP BY and COUNT(DISTINCT ...).CREATE TABLE ... AS SELECT DISTINCT and checked by the row count after joining back; denormalization (for example the gpa column) should be a conscious decision, and its cost is keeping consistency by hand.Next lesson: a design is never finished in one go; you will learn to add a column to a table that already has data, rename a column and add a constraint with ALTER TABLE.
Exercises and quiz are for members
This lesson has 7 coding exercises and a 10-question quiz. Sign up free to solve them, save your progress and move on to the next lesson.
| GEN102 |
| Quantitative Reasoning I |
| 3 |
| B |
| row_count | students |
|---|---|
| 238 | 88 |
In an entity table, the row count should equal the number of different keys. The gap between 238 and 88 tells you that the table has a column that does not belong to that entity (here, code).