Database Tables, Rows and Columns
What you will learn
- What a table, a row (record) and a column (field) are
- How to create a table and put rows in it
- Why every cell must hold one value
- Why the order of rows is not something you can rely on
This lesson runs on the sample database you chose in lesson 4.1 (use the Example data picker at the top to change it). It creates a new table in your browser's copy of the data, so run the examples from top to bottom; Reset data starts again.
1. A table is a grid
In a relational database (lesson 1.4) data lives in tables. A table looks like a spreadsheet sheet. Here are the first three rows of a real table of the sample database:
SELECT customer_id, first_name, last_name, city, joined_on FROM customers ORDER BY customer_id LIMIT 3;
customer_id first_name last_name city joined_on
----------- ---------- --------- ----- ----------
1 Vik Ahmed Leeds 2025-10-01
2 Wanda Ahmed Leeds 2024-01-17
3 Dana Costa Cairo 2025-10-17
(3 rows)
SELECT customer_id, full_name, city, joined_on FROM customers ORDER BY customer_id LIMIT 3;
customer_id full_name city joined_on
----------- ----------- ------- ----------
1 Chen Park Lima 2025-05-07
2 Rafi Uddin Manila 2020-01-08
3 Grace Kamau Toronto 2025-03-04
(3 rows)
SELECT student_id, first_name, last_name, enrolled_year FROM students ORDER BY student_id LIMIT 3;
student_id first_name last_name enrolled_year
---------- ---------- --------- -------------
1 Vera Khan 2026
2 Elena Haque 2024
3 Chloe Garcia 2025
(3 rows)
- A column (also called a field or attribute) is one kind of fact, such as a first name or a city. It has a name and a type (lesson 2.2). All values in a column are the same kind of thing.
- A row (also called a record or tuple) is everything we know about one thing: here, one customer or one student.
- A cell is the value where a row and a column meet.
The rule of thumb: one table is about one kind of thing (customers, products, orders), and one row is about one such thing.
Quick check: how many rows and how many columns does the result above have?
Three rows and five columns (customer_id, first_name, last_name, city, joined_on). The whole customers table has 420 rows and 9 columns; press Tables at the top to see them.
Quick check: how many rows and how many columns does the result above have?
Three rows and four columns (customer_id, full_name, city, joined_on). The whole customers table has 300 rows and 6 columns; press Tables at the top to see them.
Quick check: how many rows and how many columns does the result above have?
Three rows and four columns (student_id, first_name, last_name, enrolled_year). The whole students table has 360 rows and 7 columns; press Tables at the top to see them.
2. Creating a table
CREATE TABLE names the table and lists its columns with their types. We make a small new table of our own, put three rows in it, and read it back:
Pickup points are places where customers collect parcels.
CREATE TABLE pickup_points (point_id INTEGER, name TEXT, city TEXT, opened INTEGER);
INSERT INTO pickup_points VALUES (1, 'Central Locker', 'Dhaka', 2024), (2, 'Harbour Desk', 'Lagos', 2025), (3, 'Station Kiosk', 'Dhaka', 2023);
SELECT * FROM pickup_points;
point_id name city opened
-------- -------------- ----- ------
1 Central Locker Dhaka 2024
2 Harbour Desk Lagos 2025
3 Station Kiosk Dhaka 2023
(3 rows)
Cash machines (ATMs) of the bank:
CREATE TABLE atms (atm_id INTEGER, location TEXT, city TEXT, installed INTEGER);
INSERT INTO atms VALUES (1, 'Central branch', 'Lisbon', 2022), (2, 'Airport hall', 'Lagos', 2023), (3, 'Old Town square', 'Lisbon', 2021);
SELECT * FROM atms;
atm_id location city installed
------ --------------- ------ ---------
1 Central branch Lisbon 2022
2 Airport hall Lagos 2023
3 Old Town square Lisbon 2021
(3 rows)
After-school clubs:
CREATE TABLE clubs (club_id INTEGER, name TEXT, room TEXT, founded INTEGER);
INSERT INTO clubs VALUES (1, 'Chess', 'R01', 2019), (2, 'Robotics', 'R02', 2021), (3, 'Choir', 'R01', 2018);
SELECT * FROM clubs;
club_id name room founded
------- -------- ---- -------
1 Chess R01 2019
2 Robotics R02 2021
3 Choir R01 2018
(3 rows)
SELECT * means "all columns". The first line of a result is the list of column names; each following line is one row.
You can also ask the database to describe its own table:
PRAGMA table_info(pickup_points);
cid name type notnull dflt_value pk
--- -------- ------- ------- ---------- --
0 point_id INTEGER 0 NULL 0
1 name TEXT 0 NULL 0
2 city TEXT 0 NULL 0
3 opened INTEGER 0 NULL 0
(4 rows)
PRAGMA table_info(atms);
cid name type notnull dflt_value pk
--- --------- ------- ------- ---------- --
0 atm_id INTEGER 0 NULL 0
1 location TEXT 0 NULL 0
2 city TEXT 0 NULL 0
3 installed INTEGER 0 NULL 0
(4 rows)
PRAGMA table_info(clubs);
cid name type notnull dflt_value pk
--- ------- ------- ------- ---------- --
0 club_id INTEGER 0 NULL 0
1 name TEXT 0 NULL 0
2 room TEXT 0 NULL 0
3 founded INTEGER 0 NULL 0
(4 rows)
Each row describes one column: its position (cid), its name, its type, a not-null flag, its default and a primary-key flag. (PRAGMA is an SQLite-specific command; other systems have their own way to describe a table.) The last three are explained in lessons 2.3 to 2.7.
Counting and picking columns work as you would expect:
SELECT COUNT(*) AS rows_in_table FROM pickup_points;
SELECT name, city FROM pickup_points;
rows_in_table
-------------
3
(1 row)
name city
-------------- -----
Central Locker Dhaka
Harbour Desk Lagos
Station Kiosk Dhaka
(3 rows)
SELECT COUNT(*) AS rows_in_table FROM atms;
SELECT location, city FROM atms;
rows_in_table
-------------
3
(1 row)
location city
--------------- ------
Central branch Lisbon
Airport hall Lagos
Old Town square Lisbon
(3 rows)
SELECT COUNT(*) AS rows_in_table FROM clubs;
SELECT name, room FROM clubs;
rows_in_table
-------------
3
(1 row)
name room
-------- ----
Chess R01
Robotics R02
Choir R01
(3 rows)
3. One value per cell
Every cell holds one value. It is tempting to squeeze two facts into one cell, for example a place that serves two cities, or a club that meets in two rooms:
INSERT INTO pickup_points VALUES (4, 'Corner Shop', 'Dhaka, Leeds', 2024);
SELECT name FROM pickup_points WHERE city = 'Dhaka' ORDER BY point_id;
name
--------------
Central Locker
Station Kiosk
(2 rows)
The Corner Shop serves Dhaka, but it is missing from the answer, because its cell is the single text Dhaka, Leeds, which is not equal to Dhaka.
INSERT INTO atms VALUES (4, 'Mobile van', 'Lisbon, Cairo', 2024);
SELECT location FROM atms WHERE city = 'Lisbon' ORDER BY atm_id;
location
---------------
Central branch
Old Town square
(2 rows)
The mobile van visits Lisbon, but it is missing from the answer, because its cell is the single text Lisbon, Cairo, which is not equal to Lisbon.
INSERT INTO clubs VALUES (4, 'Drama', 'R01, R03', 2020);
SELECT name FROM clubs WHERE room = 'R01' ORDER BY club_id;
name
-----
Chess
Choir
(2 rows)
The Drama club meets in room R01, but it is missing from the answer, because its cell is the single text R01, R03, which is not equal to R01.
The database cannot see two values inside one cell. The proper fix is a separate table for the second kind of fact (chapter 3), not a longer text.
4. Row order means nothing
A table is a set of rows. Unless you ask for an order with ORDER BY (lesson 4.3), the database may return rows in any order, and the order can change after updates. Never write a program that assumes "the first row is the oldest one". If order matters, store a column that says so (a date, a number) and sort by it.
Worked example: design a table
Ask: what is one row about? Then list the facts you want for each row, and give each a name and a type:
One row is one discount coupon.
| Column | Type | Example |
|---|---|---|
| coupon_id | INTEGER | 1 |
| code | TEXT | "SPRING10" |
| percent_off | INTEGER | 10 |
| valid_until | TEXT | "2026-12-31" |
CREATE TABLE coupons (coupon_id INTEGER, code TEXT, percent_off INTEGER, valid_until TEXT);
PRAGMA table_info(coupons);
cid name type notnull dflt_value pk
--- ----------- ------- ------- ---------- --
0 coupon_id INTEGER 0 NULL 0
1 code TEXT 0 NULL 0
2 percent_off INTEGER 0 NULL 0
3 valid_until TEXT 0 NULL 0
(4 rows)
One row is one safe-deposit box.
| Column | Type | Example |
|---|---|---|
| box_id | INTEGER | 1 |
| branch_id | INTEGER | 3 |
| size | TEXT | "small" |
| rent_cents | INTEGER | 12000 |
CREATE TABLE safe_boxes (box_id INTEGER, branch_id INTEGER, size TEXT, rent_cents INTEGER);
PRAGMA table_info(safe_boxes);
cid name type notnull dflt_value pk
--- ---------- ------- ------- ---------- --
0 box_id INTEGER 0 NULL 0
1 branch_id INTEGER 0 NULL 0
2 size TEXT 0 NULL 0
3 rent_cents INTEGER 0 NULL 0
(4 rows)
One row is one copy of a book on the library shelf.
| Column | Type | Example |
|---|---|---|
| copy_id | INTEGER | 1 |
| title | TEXT | "River Stories" |
| author | TEXT | "N. Akter" |
| year | INTEGER | 2019 |
CREATE TABLE library_books (copy_id INTEGER, title TEXT, author TEXT, year INTEGER);
PRAGMA table_info(library_books);
cid name type notnull dflt_value pk
--- ------- ------- ------- ---------- --
0 copy_id INTEGER 0 NULL 0
1 title TEXT 0 NULL 0
2 author TEXT 0 NULL 0
3 year INTEGER 0 NULL 0
(4 rows)
One cell, one value: if something has two owners or two authors, do not write both in one cell. Lesson 3.4 shows how many-to-many links are stored. The names, codes and amounts are made up.
Common mistakes
- One table for everything. Customers, orders and products in one wide table repeat data; use one table per kind of thing.
- Several values in one cell (
Dhaka, Leeds). Searches silently miss them. - Relying on row order without
ORDER BY. - Vague column names such as
dataorinfo. A column name should say what the value is.
Key takeaways
- A table is a grid: columns describe the facts, rows are the things.
- One table is about one kind of thing; one row is about one such thing.
- Every cell holds a single value.
- Row order is not guaranteed unless you use
ORDER BY. CREATE TABLEdefines the columns;INSERTadds rows;SELECTreads them.
Practice
1. What is the difference between a row and a column?
Show answer
A column is one kind of fact (for example city) with a name and type; a row holds all the facts about one thing (for example one customer).
2. A table orders has columns id, date, customer, total. What does one row represent?
Show answer
One order.
3. Why did a row with two values in one cell (such as Dhaka, Leeds) not appear in the query WHERE city = 'Dhaka'?
Show answer
Its cell contains the single text Dhaka, Leeds, which is not equal to Dhaka. Two values in one cell break searching.
4. Can you rely on the database returning rows in the order you inserted them?
Show answer
No. Without ORDER BY the order is not guaranteed.
5. Write the CREATE TABLE statement for a table with an id, a name and a price in whole cents.
Show answer
CREATE TABLE gift_items (item_id INTEGER, name TEXT, price_cents INTEGER);
Show answer
CREATE TABLE fee_types (fee_id INTEGER, name TEXT, amount_cents INTEGER);
Show answer
CREATE TABLE canteen_items (item_id INTEGER, name TEXT, price_cents INTEGER);
6. Name the other words for "row" and "column".
Show answer
Row: record or tuple. Column: field or attribute.