Basic SQL for Data Engineers
By the end of this lesson, you will be able to:
Explain what a relational database is
Describe how data is structured into tables, rows, and columns
Understand the purpose of primary keys (PK) and foreign keys (FK)
Interpret and handle
NULLvalues in a relational context
What Is a Relational Database?
A relational database stores data in a structured way using tables. Each table represents a specific type of entity—like users, orders, or products.
Rows (also called records) represent individual data entries
Columns define the attributes of that data (like name, email, price)
Tables can be connected through keys, allowing you to link related data across the database. That’s where the “relational” part comes in.
Tables, Rows, and Columns
Here’s an example of a simple consumers table:
| consumer_id (PK) | name | |
|---|---|---|
| 1 | Alice | alice@email.com |
| 2 | Bob | bob@email.com |
id,name, andemailare columnsEach line is a row
The combination of column names and types is the schema
Primary Keys (PK)
A primary key is used to uniquely identify each row in a table. It must be:
Unique: no two rows can have the same value
Not null: every row must have a value
Example:
CREATE TABLE users (
user_id SERIAL PRIMARY KEY,
name TEXT,
email TEXT
);
Here, user_id is the primary key—each user gets a unique ID.
Foreign Keys (FK)
A foreign key is a column in one table that refers to the primary key of another table. It:
Creates a relationship between the two tables
Enforces referential integrity (you can’t reference a row that doesn’t exist)
Example:
CREATE TABLE orders (
order_id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(user_id),
total DECIMAL
);
In this case, user_id in the orders table is a foreign key pointing to user_id in the users table.
Understanding NULL
A NULL value means “no value” or “unknown.” It’s different from zero or an empty string.
NULLdoes not equal anything—not even anotherNULLUse
IS NULLorIS NOT NULLin queries to filter them
SELECT * FROM users WHERE email IS NULL;
This query returns users who haven’t provided an email address.
Finish Course Early?
You have not completed all required lessons and assessments.