DataLens

What a relational database is: tables, rows, columns

Meet the database you'll use all track long, and learn the words everyone uses to talk about tables.

Level: Foundations · 8 min read

Practise this module

The questions for this lesson run in your browser and check your answer straight away.

The idea

If you've used a spreadsheet, you already understand most of a database. A database is an organised collection of data that a program can store, search and update safely, even when many people use it at once. A relational database stores that data in tables, and lets tables point at each other. PostgreSQL, the database this track teaches, is one of the most popular relational databases in the world, and it's free.

So why not just use spreadsheets? Three reasons you'll feel quickly at work:

Analogy

A spreadsheet is a notebook you write in by hand. A database is a well-run library: every book has a fixed place, a catalogue card, and a librarian (the database engine) who finds exactly what you ask for and stops anyone from shelving books in the wrong place.

Tables, rows and columns

A table holds one kind of thing. Here are the first rows of the customers table in our practice shop, Toko Kita, an online store selling coffee, kitchen gear and desk accessories:

customer_idnameemailcitysignup_datesegment
1Ayu Lestari[email protected]Jakarta2023-01-14Consumer
2Budi Santoso[email protected]Bandung2023-02-03Business
3Citra Dewi[email protected]Surabaya2023-03-22Consumer
4Dimas PratamaNULLJakarta2023-04-09Consumer

Keys: how tables point at each other

Look at the first column, customer_id. Every customer has a different number, and it never changes. That's a primary key: a column whose value uniquely identifies each row. Names can repeat (two customers called Ayu is perfectly possible), but a primary key never does.

Now look at the orders table:

order_idcustomer_idorder_datestatuschannel
1112024-01-03completedapp
232024-01-09completedweb
352024-01-17completedapp
432024-01-18completedapp

The order doesn't repeat the customer's name, email and city. It only stores customer_id. Order 2 and order 4 both belong to customer 3, Citra Dewi. A column that holds another table's primary key is called a foreign key, and it's the "relational" in relational database.

Key idea

Store each fact once, in the table where it belongs, and use keys to connect tables. If Citra changes her email, you update one row in customers, and every order still points to the right person.

Here's the whole Toko Kita database. You'll use it all through this track:

TableOne row is…Key columns
customersa customercustomer_id (primary key)
productsa product we sellproduct_id (primary key)
ordersone checkout by a customerorder_id (primary key), customer_id (foreign key)
order_itemsone product line inside an orderorder_id + product_id together, both foreign keys
employeesa staff memberemployee_id (primary key), manager_id (points to another employee)

Why is there an order_items table at all? Because one order can contain several products, and one product appears in many orders. Rather than cramming "Mug, Coffee, Pen" into a single cell, each product in an order gets its own row in order_items. That design is what lets you later ask questions like "how many mugs did we sell in March?" in one line.

Your first query

SQL reads almost like English. To see every column of every row in a table, you write:

SELECT * FROM products;

Press Try it above to run it. You'll see the 15 products the shop sells.

 product_id | name                 | category    | price  | cost   | stock
------------+----------------------+-------------+--------+--------+-------
          1 | French Press 600ml   | Kitchen     | 185000 | 110000 |    24
          2 | Pour-over Kettle     | Kitchen     | 320000 | 210000 |    12
          3 | Arabica Gayo 250g    | Coffee      |  95000 |  52000 |    80
...
(15 rows)

SQL keywords like SELECT and FROM aren't case-sensitive, so select * from products works too. The convention is to write keywords in capitals and table or column names in lowercase, so you can see the structure of a query at a glance.

Tip

Every SQL question on this site has a Tables tab under the editor. It shows the tables you're working with, their columns, and their first rows. Check it before you write a query, the same way you'd glance at a spreadsheet before writing a formula.

Why this matters for clean code

The habit you just learned, store each fact once and connect with keys, is called normalisation, and it's the foundation of good data design. Messy databases repeat data everywhere, and repeated data eventually disagrees with itself. Clean ones are boring and predictable, and that's exactly what you want when a report goes to your manager.

Recap

Practise this module

The questions for this lesson run in your browser and check your answer straight away.

Next: SELECT and FROM →