🌍 freeCodeCamp · subject
freeCodeCamp Relational Databases Syllabus
The Relational Databases syllabus for freeCodeCamp in full: 5 chapters and 16 topics, every one listed below, plus 50 Relational Databases flashcards with answers.
Relational Databases syllabus: all 5 chapters
Expand any chapter to see its topics and sub-topics.
-
Bash and the Command Line
2 topics- Navigating the Filesystem
- Bash Scripting
-
Git and Version Control
2 topics- Repositories and Commits
- Branching and Merging
-
PostgreSQL Fundamentals
3 topics- Creating Tables and Databases
- Data Types and Constraints
- Inserting and Updating Data
-
Querying Data with SQL
4 topics- SELECT and WHERE
- Joins
- Aggregations and Grouping
- Relationships and Normalization
-
Certification Projects
5 topics- Celestial Bodies Database
- World Cup Database
- Salon Appointment Scheduler
- Periodic Table Database
- Number Guessing Game
freeCodeCamp Relational Databases flashcards — 50 questions with answers
In the bash terminal, what does the `pwd` command do?
`pwd` stands for "print working directory" — it outputs the absolute path of the directory you are currently in.
What is the difference between an absolute path and a relative path, and what do `.` and `..` mean?
An absolute path starts from the root directory `/` (e.g., `/home/user/project`); a relative path starts from the current working directory. `.` refers to the current directory and `..` refers to the parent directory (e.g., `cd ..` moves up one level).
What do the `ls` flags `-l` and `-a` show?
`ls -l` shows the long listing format (permissions, owner, size, modification date); `ls -a` shows all files including hidden ones whose names begin with a dot. Combined as `ls -la`.
Which commands create, copy, move/rename, and delete files or directories in bash?
`touch file` creates an empty file; `mkdir dir` creates a directory (`-p` for nested paths); `cp src dest` copies (`-r` for directories); `mv src dest` moves or renames; `rm file` deletes (`rm -r dir` for directories).
In `chmod 755 script.sh`, what permissions does each octal digit grant?
Each digit is a sum of read $= 4$, write $= 2$, execute $= 1$. Owner gets $7 = 4 + 2 + 1$ (rwx); group and others each get $5 = 4 + 1$ (r-x). This makes a script executable by everyone but writable only by the owner.
What is the shebang line in a bash script and what does it do?
It is the first line `#!/bin/bash`. It tells the operating system which interpreter to use to execute the script, so the file can be run directly (e.g., `./script.sh`) once it is executable.
How do you assign and use a variable in bash, and how do you read user input into one?
Assign with no spaces around `=`: `NAME=value`. Access it with a dollar sign: `$NAME` (or `${NAME}`). Read keyboard input with `read NAME`, which stores the user's input in the variable.
Which operators does bash use inside `[[ ... ]]` to compare integers in an if statement?
`-eq` (equal), `-ne` (not equal), `-lt` (less than), `-le` (less or equal), `-gt` (greater than), `-ge` (greater or equal). Example: `if [[ $NUM -gt 10 ]]`. Strings use `=` and `!=` instead.
Give the basic syntax of `while` and `until` loops in bash.
`while [[ CONDITION ]]; do COMMANDS; done` repeats while the condition is true; `until [[ CONDITION ]]; do COMMANDS; done` repeats until the condition becomes true (i.e., while it is false). `for X in LIST; do ...; done` iterates over a list.
In a bash script, what do `$1`, `$#`, `$@`, and `$?` mean?
`$1` is the first positional (command-line) argument, `$#` is the number of arguments, `$@` expands to all arguments, and `$?` is the exit status of the last command ($0$ means success, non-zero means failure).
What is command substitution in bash and why is it useful in database scripts?
`$(command)` runs the command and substitutes its output in place. Example: `RESULT=$($PSQL "SELECT count(*) FROM users")` captures a psql query result into a variable for use in the script.
What does `git init` do, and what is a git repository?
`git init` creates a new repository by adding a hidden `.git` directory to the project folder. A repository is the full project history: all tracked files, commits, and branches stored by git.
Describe the roles of the working directory, the staging area, and `git add` / `git commit`.
Changes are made in the working directory; `git add file` moves them to the staging area (index); `git commit -m "message"` permanently records the staged snapshot in the repository history.
What is the difference between `git status` and `git log`?
`git status` shows the current state of the working directory and staging area (modified, staged, untracked files); `git log` shows the commit history (hashes, authors, dates, messages), with `--oneline` for a condensed view.
What do `git clone`, `git push`, and `git pull` do?
`git clone URL` copies a remote repository to your machine; `git push` uploads your local commits to the remote; `git pull` fetches remote commits and merges them into your current branch.
How do you create a new branch and switch to it in one command, and why use branches?
`git checkout -b branch_name` (or `git switch -c branch_name`) creates and switches to a new branch. Branches let you develop features or fixes in isolation without changing the main branch until the work is merged.
What does `git merge` do, and what is a fast-forward merge?
`git merge branch` combines the named branch's commits into the current branch. If the current branch has no new commits since the branch diverged, git simply moves the pointer forward (fast-forward); otherwise it creates a merge commit combining both histories.
What is a merge conflict and how do you resolve it?
A conflict occurs when the same lines were changed differently on both branches. Git marks the file with `<<<<<<<`, `=======`, `>>>>>>>` sections; you manually edit the file to the desired content, remove the markers, then `git add` the file and complete the merge with a commit.
In PostgreSQL, how do you create a new database and connect to it in psql?
`CREATE DATABASE database_name;` creates it; the psql meta-command `\c database_name` connects to it. Note that SQL statements end with a semicolon, while `\` commands do not.
Give the general syntax for creating a table with columns in PostgreSQL.
`CREATE TABLE table_name(column_name DATATYPE CONSTRAINTS, ...);` — for example: `CREATE TABLE students(student_id SERIAL PRIMARY KEY, name VARCHAR(30) NOT NULL);`. A table can also be created empty with `CREATE TABLE name();` and columns added later.
Show the other 30 Relational Databases flashcards
How do you add, drop, and rename a column with ALTER TABLE, and how do you delete a table or database?
`ALTER TABLE t ADD COLUMN col TYPE;`, `ALTER TABLE t DROP COLUMN col;`, `ALTER TABLE t RENAME COLUMN old TO new;`. Delete a table with `DROP TABLE t;` and a database with `DROP DATABASE db;` (cannot be connected to it).
What do the psql meta-commands `\l`, `\c`, `\d`, and `\dt` display?
`\l` lists all databases; `\c name` connects to a database; `\d table_name` describes a table's columns, types, and constraints; `\dt` lists the tables in the current database.
What does the SERIAL type do in PostgreSQL?
SERIAL is an auto-incrementing integer: each new row automatically receives the next value in a sequence ($1, 2, 3, \dots$). It is commonly used for surrogate primary key columns such as `user_id SERIAL PRIMARY KEY`.
Compare VARCHAR(n) with TEXT, and explain NUMERIC(precision, scale).
`VARCHAR(n)` stores variable-length text up to a maximum of $n$ characters; `TEXT` stores unlimited-length text. `NUMERIC(p, s)` stores exact decimals with $p$ total digits and $s$ digits after the decimal point — e.g., `NUMERIC(4,1)` can store values like $999.9$.
What does a PRIMARY KEY constraint guarantee, and how many can a table have?
A primary key uniquely identifies each row: the column(s) must be unique and NOT NULL. A table can have only one primary key, though it may be composite (span multiple columns), e.g. `PRIMARY KEY(col1, col2)`.
What is a FOREIGN KEY and what does referential integrity mean?
A foreign key is a column that references the primary key (or unique column) of another table, e.g. `ALTER TABLE orders ADD FOREIGN KEY(customer_id) REFERENCES customers(customer_id);`. Referential integrity means the database rejects values that do not exist in the referenced table.
What do the NOT NULL, UNIQUE, and CHECK constraints enforce?
NOT NULL requires the column to always have a value; UNIQUE forbids duplicate values in the column; CHECK enforces a boolean condition on inserted values, e.g. `CHECK (price > 0)`.
Give the syntax for inserting one or more rows into a table.
`INSERT INTO table_name(col1, col2) VALUES (val1, val2);` — multiple rows can be inserted at once by separating value tuples with commas: `VALUES (a, b), (c, d);`. Text values are wrapped in single quotes.
How do you change existing rows with UPDATE, and what happens if you omit the WHERE clause?
`UPDATE table_name SET column = new_value WHERE condition;` changes only matching rows. Without WHERE, every row in the table is updated — a common and dangerous mistake.
How do you delete specific rows from a table in SQL?
`DELETE FROM table_name WHERE condition;` removes only the rows matching the condition. `DELETE FROM table_name;` with no WHERE clause removes all rows while keeping the table structure.
What is the basic syntax of a SELECT query, and how do ORDER BY, LIMIT, and DISTINCT modify it?
`SELECT columns FROM table;` (`*` selects all columns). `ORDER BY col ASC|DESC` sorts the results, `LIMIT n` returns at most $n$ rows, and `SELECT DISTINCT col` removes duplicate values from the output.
List the main comparison and logical operators used in a WHERE clause.
Comparisons: `=`, `!=` (or `<>`), `<`, `>`, `<=`, `>=`; range/set tests: `BETWEEN a AND b`, `IN (list)`, `IS NULL` / `IS NOT NULL`; conditions are combined with `AND`, `OR`, and negated with `NOT`.
How do the wildcards in LIKE work, and what does ILIKE do in PostgreSQL?
In `WHERE name LIKE pattern`, `%` matches any sequence of characters (including none) and `_` matches exactly one character — e.g., `'W%'` matches names starting with W. `ILIKE` performs the same match case-insensitively.
What rows does an INNER JOIN return? Give the syntax.
`SELECT * FROM a INNER JOIN b ON a.key = b.key;` returns only rows where the join condition matches in both tables — rows without a match on either side are excluded.
What is the difference between a LEFT JOIN and a RIGHT JOIN?
LEFT JOIN returns all rows from the left (first) table plus matching rows from the right table, filling non-matches with NULL; RIGHT JOIN does the mirror image, keeping all rows of the right table.
What does a FULL (OUTER) JOIN return?
All rows from both tables: matched rows are combined, and rows with no match in the other table appear with NULL for the missing side. It is the union of the LEFT and RIGHT join results.
When can you use USING instead of ON in a join, and how does it differ?
`JOIN b USING(col)` works when the join column has the same name in both tables; it is shorthand for `ON a.col = b.col` and outputs the column once instead of twice. Multiple tables can be chained: `FULL JOIN c USING(col)`.
Name the five core SQL aggregate functions and state what AVG computes.
COUNT, SUM, AVG, MIN, MAX. AVG returns the arithmetic mean of a column: $$\bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_{i}$$ NULL values are ignored by aggregates (except `COUNT(*)`, which counts rows).
What does GROUP BY do, and what rule applies to the columns in the SELECT list when it is used?
GROUP BY collapses rows sharing the same value(s) into groups so aggregates are computed per group, e.g. `SELECT major, COUNT(*) FROM students GROUP BY major;`. Every selected column must either appear in the GROUP BY clause or be inside an aggregate function.
What is the difference between WHERE and HAVING?
WHERE filters individual rows before grouping and cannot contain aggregate functions; HAVING filters the groups after GROUP BY and may use aggregates, e.g. `GROUP BY major HAVING COUNT(*) > 5;`.
How is a one-to-many relationship implemented between two tables?
The "many" side table gets a foreign key column referencing the primary key of the "one" side. Example: many `orders` rows carry a `customer_id` that references `customers(customer_id)` — one customer, many orders.
How is a many-to-many relationship implemented in a relational database?
With a third junction (join) table containing foreign keys to both tables — e.g., `students_courses(student_id, course_id)` referencing `students` and `courses` — often with a composite primary key `PRIMARY KEY(student_id, course_id)` to prevent duplicate pairs.
What does First Normal Form (1NF) require?
Every column holds atomic (indivisible) values, each row is unique (there is a primary key), and there are no repeating groups — e.g., no comma-separated lists or `phone1, phone2, phone3` columns.
State the requirements of Second Normal Form (2NF) and Third Normal Form (3NF).
2NF: the table is in 1NF and every non-key column depends on the whole primary key (no partial dependencies on part of a composite key). 3NF: the table is in 2NF and non-key columns depend only on the key, not on other non-key columns (no transitive dependencies).
In the freeCodeCamp Celestial Bodies project, what tables and key structure are required?
A `universe` database with at least the tables `galaxy`, `star`, `planet`, and `moon` (plus one more). Each table needs a `name` column and a primary key named `table_name_id` that auto-increments (SERIAL). Foreign keys model the hierarchy: each star references a galaxy, each planet a star, each moon a planet.
How do you back up and restore a PostgreSQL database as a .sql file (as done in the Celestial Bodies project)?
Dump: `pg_dump -cC --inserts -U freecodecamp universe > universe.sql` writes SQL that recreates the database. Restore: `psql -U postgres < universe.sql` runs that file to rebuild it.
Describe the schema and workflow of the freeCodeCamp World Cup project.
A `worldcup` database with a `teams` table (`team_id SERIAL PRIMARY KEY`, unique `name`) and a `games` table whose `winner_id` and `opponent_id` are foreign keys to `teams(team_id)`. A bash script reads `games.csv` line by line (with `while read` and IFS set to a comma), inserts teams if missing, then inserts each game; a second script answers queries such as win percentages, e.g. $\frac{\text{wins}}{\text{games}} \times 100$ rounded with ROUND.
In the Salon Appointment Scheduler project, how are the tables related and how does the bash script talk to the database?
Tables: `customers` (unique `phone`), `services`, and `appointments` with foreign keys `customer_id` and `service_id`. The script defines `PSQL="psql --username=freecodecamp --dbname=salon -t --no-align -c"` and runs queries via command substitution, e.g. `CUSTOMER_ID=$($PSQL "SELECT customer_id FROM customers WHERE phone='$PHONE'")`, inserting a new customer when no row is returned.
Outline the schema and script behavior of the Periodic Table project.
The `periodic_table` database is normalized into `elements` (atomic_number, symbol, name), `properties` (melting/boiling points, `atomic_mass DECIMAL`, and a `type_id` foreign key), and a `types` table (metal, nonmetal, metalloid). The bash script takes one argument — an atomic number, symbol, or element name — decides which with a numeric test, joins the tables, and prints a sentence describing the element, or "Could not find that element in the database." if absent.
In the Number Guessing Game project, how is the random number generated and what is stored in the database?
Bash's `$RANDOM` variable with the modulo operator: `SECRET=$(( RANDOM % 1000 + 1 ))` yields an integer in $[1, 1000]$. The script asks for a username, looks it up in the database, reports games played and best game (fewest guesses), loops with `read` until the guess matches (printing higher/lower hints), then saves the game's guess count for that user.
freeCodeCamp Relational Databases FAQ
What is in the freeCodeCamp Relational Databases syllabus?
freeCodeCamp Relational Databases is split into 5 chapters: Bash and the Command Line, Git and Version Control, PostgreSQL Fundamentals, Querying Data with SQL and Certification Projects. Together they contain 16 topics.
How many chapters are in freeCodeCamp Relational Databases?
5 chapters, containing 16 topics. The largest chapter is Certification Projects, with 5 topics.
Are there flashcards for freeCodeCamp Relational Databases?
Yes — a 50-card Relational Databases deck, printed in full in the flashcards section of this page and available in the Examius app with spaced repetition. The free plan covers one exam of your choice, including its flashcards; Examius Pro, a one-time purchase, unlocks every other exam and deck.