…
…
SQL and Database Fundamentals · Lesson 2 / 41 · Introduction to databases
PreviewIn this lesson you will
COUNT(*)You can read the explanation without an account. The exercises and quiz at the end of the lesson are for members.
The director of student affairs wants you to know by next week which information the system keeps where. All you have are the names of the seven tables: department, instructor, student, course, term, course_offering, enrollment. In this lesson you will query each table and draw the map of the system.
When you enter a building for the first time, you first look at the floor plan: which room is where, which door opens to what. When you enter a database for the first time, the floor plan is the list of tables. But names alone are not enough. What does course_offering say by itself? Is it every time a course is offered, or the courses that are open right now? You will find the answer by looking at the table, not by guessing.
At the end of this lesson you will fill in the table below yourself. For now it is empty:
| Table | What does one row describe? | Row count | Primary key |
|---|---|---|---|
department | ? | ? | ? |
instructor | ? | ? | ? |
student | ? | ? | ? |
course | ? | ? | ? |
term | ? | ? | ? |
course_offering | ? | ? | ? |
enrollment |
In this lesson you will:
SELECT * and write in one sentence what one row describes.COUNT(*).In lesson 1 you wrote SELECT * FROM department; and saw all the departments. The first step of exploration is the same: get all columns of the table and read one row carefully. The question to ask is: "What does one row in this table correspond to in real life?" Let's start with the smallest table.
SELECT * FROM term;
| id | name | start_date | end_date |
|---|---|---|---|
| 1 | 2025-Fall | 2025-09-22 | 2026-01-16 |
| 2 | 2026-Spring | 2026-02-16 | 2026-06-12 |
| 3 | 2026-Fall | 2026-09-21 | 2027-01-15 |
(3 rows)
Three rows, three terms. Each row describes one academic term: its name, the day it starts, and the day it ends. In the term line of the map you can write "One row is one academic term". It looks this simple, but in some tables the answer is not clear at first glance. You only understand the difference between course and course_offering, or what the enrollment table records, when you look at the rows.
In the cell below, the instructor table is ready. Run it and look at what one row describes. Then change the table name to course, course_offering, and enrollment one by one; ask yourself the same question each time. On the site you see the first 50 rows of the result and the total row count. The query only reads and does not change the data; try as much as you like.
Try it now· not graded; doesn't affect progress
In the course table each row is a course in the catalog: its code, name, and credits. In the course_offering table the same course can appear more than once, because each row is one section of a course offered in a particular term: which term, which instructor, what capacity, which room. "Algorithms and Programming I" appears once in the catalog, but if it is offered in both 2025-Fall and 2026-Fall, it has two rows in the course_offering table. In the enrollment table each row is one student's enrollment in one section; the midterm, final exam, and letter grade are also kept in this row.
Now let's name the structure you have just seen. In a relational database, data lives in tables. Each table holds one kind of thing: student holds students, course holds courses. Each row of a table is a record, that is, one thing of that kind: one student, one course. Each column is a property: first name, last name, birth date. The box where a row and a column meet is called a cell.
In Codd's mathematical language, a table is called a relation; the name "relational database" comes from there. The word "relation" in the message relation "students" does not exist, which you saw in lesson 1 when you misspelled a table name, also means table.
Let's look at a few columns of the student table (the order may be different on the site; look at the id column):
| id | student_no | first_name | last_name | city | has_scholarship |
|---|---|---|---|---|---|
| 1 | 202303001 | Anika | Park | Mumbai | true |
| 2 | 202405001 | Anika | Rivera | Madrid | true |
| 3 | 202604001 | Felix | Abara | Nairobi | false |
Here there are three rows, so three students. city is a column: every student has been asked "which city?". In the cell where row 2 and the city column meet, it says "Madrid".
This structure has three rules. The first is that all values in a column are of the same type. student_no is a string, year is an integer, birth_date is a date, and has_scholarship can only be true or false (true/false). You cannot write "third year" into the year column; the database rejects it. You will see the details of types in lesson 25.
The second is that each cell holds a single value. If a student has two email addresses, writing both into the same cell separated by a comma goes against this model. When you ask "Who are the students with a second email?", the database cannot look inside the comma; it sees the cell as one single string. If you need more than one value, a separate structure is built; you will learn this in lesson 31 when you model data.
The third is that rows have no natural order. SELECT * FROM student; often returns rows in the order they were added, but this is not a promise. The database reads rows in whatever order is easiest for it; after an update or a maintenance operation this order can change. If you want a particular order, you have to say so explicitly; you will learn how in lesson 6.
Now fill in the "What does one row describe?" column of the map: match each table with what it holds.
What does each table hold? Think about what one row describes.
For each row, pick its match from the list, then check.
departmentinstructorstudentcoursetermcourse_offeringenrollmentLet's test the row-order idea with a situation.
Yesterday the result of SELECT * FROM student; started with Anika Park, and today it starts with a different student. What may have happened?
COUNT(*)The third column of the map is the row count. Opening the enrollment table with SELECT * and counting rows one by one takes long and is easy to get wrong. The database does this for you:
SELECT COUNT(*) FROM enrollment;
| count |
|---|
| 781 |
(1 row)
COUNT(*) means "count the rows"; the asterisk here means "all rows". The result is a table with one cell, and the column name is count. You can think of it as a counter at a door: it does not care who came in, it tells you how many people came in. For now you will only use this pattern; counting a particular column and the other aggregate functions come in lesson 11. Until then, COUNT(*) is at hand to check whether a query returned the right number of rows.
Make a prediction before you see the numbers.
Which of the seven tables has the most rows?
In the cell below, the count for enrollment is ready. Change the table name to count all seven tables and write the numbers into the map.
Try it now· not graded; doesn't affect progress
The numbers you should find are: department 8, instructor 20, student 120, course 33, term 3, course_offering 54, enrollment 781. These numbers also confirm what the tables describe. There are 33 courses but 54 sections offered: some courses were offered in more than one term. There are 120 students but 781 enrollments: more than six enrollments per student on average.
When the director says "Get the information of the student with number 202405001", you think of exactly one student. When they say "Get Anika's information", you have to ask which Anika. A table must have a column that tells every row apart from the others; otherwise, when you want to update or delete a record, you cannot say which one you mean.
This column is called the primary key. A primary key has two rules: its value is different in every row, and it cannot be left empty. In the case database, the primary key of all seven tables is the id column.
SELECT id, student_no, first_name, last_name FROM student;
Result (two of 120 rows):
| id | student_no | first_name | last_name |
|---|---|---|---|
| 1 | 202303001 | Anika | Park |
| 2 | 202405001 | Anika | Rivera |
(120 rows)
Students 1 and 2 are both Anika. The first name column repeats; in the case, the name "Kenji" appears in exactly 6 students. That is why the first name cannot be a primary key. id, on the other hand, is different in every row and empty in none.
What about student_no? It is also different for every student and never empty. Columns that could be a primary key but were not chosen are called candidate keys. A table can have more than one candidate; the designer picks one of them as the primary key. In the case, id was chosen and student_no stayed a candidate. The email may look unique too, but 9 students have no email: the second rule breaks.
If there were no id column, which column of the student table could be the primary key?
Match the new concepts to tell them apart.
Match each definition with its concept.
For each row, pick its match from the list, then check.
While browsing the tables you may have noticed that some cells are empty. Let's look at the course table:
SELECT * FROM course;
Result (two of 33 rows):
| id | code | name | credits | ects | department_id | prerequisite_id |
|---|---|---|---|---|---|---|
| 1 | CPR101 | Algorithms and Programming I | 4 | 6 | 1 | NULL |
| 2 | CPR102 | Algorithms and Programming II | 4 | 6 | 1 | 1 |
(33 rows)
The prerequisite_id column shows a course's prerequisite. The prerequisite of course 2 is course 1: "To take Programming II, you must first pass Programming I." Course 1 has no prerequisite, so its cell is empty. You will see in detail in lesson 14 how this column points to another row.
In a database, an empty cell is called NULL. NULL is not zero and not an empty string: it means "no value, unknown". In the enrollment table, the final exam score of 310 records is NULL; these students did not take the exam and score zero, the final of this term has simply not happened yet. Someone who takes NULL for zero would write the wrong report "310 students scored zero on the final". You will learn to work with NULL in lesson 7; for now it is enough to recognize it when you see it.
What you did in this lesson is a routine, and it works in every new database: in the datasets you will meet in workshops, and in a system you are given for the first time at a workplace. First you learn the names of the tables, then you look at the rows of each, count them, and find what tells each row apart.
Put the steps you follow when getting to know a new database in order.
Click the lines in order; to undo one you placed wrong, click it in the list below.
Shuffled lines
Your order
The completed map looks like this:
| Table | What does one row describe? | Row count | Primary key |
|---|---|---|---|
department | One department and its faculty | 8 | id |
instructor | One instructor and their title | 20 | id |
student | One student | 120 | id |
course | One course in the catalog | 33 | id |
term | One academic term | 3 | id |
course_offering |
Let's try the same routine in another database. A gym's member table looks like this:
SELECT * FROM member;
| member_no | first_name | last_name | phone | plan | age | active |
|---|---|---|---|---|---|---|
| 101 | Sam | Carter | 555 0111 | annual | 22 | true |
| 102 | Sam | Carter | NULL | monthly | 19 | true |
| 103 | Emma | Reed | 555 0122 | monthly | 16 | false |
| 104 | Jake | Miller | 555 0133 | annual | 17 | true |
| 105 | Nora | Diaz |
(5 rows)
One row describes one member. There are two "Sam Carter"s: two different people with the same first and last name but a different age and plan. Member 102's phone has not been entered, so the cell is NULL. If the gym staff say "Change Sam Carter's plan to annual", you cannot know which Sam Carter; if they say "member 102", you can.
Which column should be the primary key in the member table?
To fill in the instructor line of the map, look at the table, then count.
SELECT * FROM instructor;
Result (two of 20 rows):
| id | first_name | last_name | title | department_id | hire_date | |
|---|---|---|---|---|---|---|
| 1 | Diego | Costa | Associate Professor | 1 | diego.costa@northbridge.example | 2019-02-01 |
| 2 | Lucía | Kowalski | Lecturer | 1 | lucia.kowalski@northbridge.example | 2022-09-01 |
(20 rows)
One row describes one instructor: their name, title, email, and the day they started. id is different for every instructor and is the primary key.
Deriving uniqueness from today's data. Someone looks at the department table and thinks: "Today the names of the 8 departments are all different, so the department name should be the primary key." Today's data seems to allow it. But if another faculty opens a second department with the same name tomorrow, the system rejects the new row; and if a department's name changes, everything linked to it has to be updated. A primary key is not a column that does not repeat right now, but a column that by the nature of the data will never repeat and never be empty. In the case, every table gets its own id for this job.
SELECT COUNT(*) FROM table; gives a table's row count in one cell; knowing what a row describes also explains which table will be big (enrollment 781, student 120).id); a unique column that was not chosen is a candidate key (student_no). An empty cell is NULL: not zero, but "no value".Next lesson: in the next unit you start answering questions from a single table: first you will select the columns you want, name them, and do calculations with them.
Exercises and quiz are for members
This lesson has 6 coding exercises and a 10-question quiz. Sign up free to solve them, save your progress and move on to the next lesson.
| ? |
| ? |
| One section of a course offered in a term |
| 54 |
id |
enrollment | One student's enrollment in one section, with grades | 781 | id |
| student |
| 20 |
| true |
In the course_offering table the same course number can appear in more than one row.
SELECT * FROM course_offering;
Result (three of 54 rows):
| id | course_id | term_id | instructor_id | capacity | room |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 3 | 50 | C-419 |
| 2 | 1 | 3 | 3 | 40 | C-417 |
| 3 | 2 | 2 | 2 | 60 | A-108 |
(54 rows)
In rows 1 and 2 the course_id is the same (1): Algorithms and Programming I was offered in two different terms. That is why course_id cannot be a primary key; id tells each section apart. To check the count:
SELECT COUNT(*) FROM course_offering;
| count |
|---|
| 54 |