# Relational Model & Tables

> Learn how a relational database keeps data in tables, rows and columns, and read a CREATE TABLE statement.

Subject: SQL | Lesson 1 of 44 | 4 min read
Interactive lesson: https://codewithanimation.in/sql/relational-model-and-tables
Next: [SQL Data Types](https://codewithanimation.in/sql/sql-data-types.md)

## A table is rows and columns

A relational database keeps its data in tables. A table is a named collection of rows. Picture a spreadsheet with one strict rule: every row has the same columns.

A row is one thing you store, such as one customer. A column is one kind of fact, such as `city`. A cell is the place where one row and one column meet.

Each column has a data type, such as `integer` or `text`. The type decides which values the column accepts.

## Why the same shape matters

Because every row has the same shape, the database can answer questions about thousands of rows in one way: find them, sort them, count them and combine them.

Without that shape you would search through loose text by hand. With it, you describe what you want in SQL and the database does the searching.

## Read a CREATE TABLE statement

This statement creates a `customers` table with four columns. Each column has a name and a type:

```sql
CREATE TABLE customers (
  id integer,
  name text,
  city text,
  age integer
);
```

The list inside the brackets is the schema: the column names and their types. It describes the shape of the data, not the data itself. Later, `INSERT` adds rows that fit this shape.

SQL does not promise any order for the rows. If you want them in a certain order, you must ask for it in the query. You will learn how in a later lesson.

## Common mistake: Assuming rows keep their order

Many beginners expect the first row they inserted to come back first. SQL does not promise that. The database may return rows in any order unless the query sorts them.

Another trap: identical rows are allowed. A table does not give each row a unique label by itself. Keys fix that, and you meet them in two lessons.

## Tip: Name things for people

Use short, clear column names such as `city`, and a plural noun for the table such as `customers`. A table that reads well is easier to query later.

## Try it: Light up the parts of a table

Tap Table, Row, Column, Cell and Schema to see each part of the `customers` table. Press Add a row to watch an `INSERT` add one.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/relational-model-and-tables

## Check yourself

### Question 1

In the `customers` table, what is one row?

- A. One customer, with all the facts we store about them
- B. One kind of fact, such as the city of every customer
- C. The names and types of the columns

Correct answer: A

A row is one thing you store. A column is one kind of fact, stored for every row.

- Why not B: That describes a column, not a row.
- Why not C: That is the schema, the shape of the table.

### Question 2

Which statement creates a table called `users` with a text column called `nickname`?

- A. CREATE TABLE users (nickname text);
- B. NEW TABLE users nickname text;
- C. CREATE TABLE users: nickname;

Correct answer: A

CREATE TABLE takes the table name, then the columns in brackets. Each column is a name followed by its type.

- Why not B: SQL uses CREATE TABLE, not NEW TABLE.
- Why not C: A column needs a type, and the list goes inside brackets.

### Question 3

You run a query that does not sort. In what order does PostgreSQL promise to return the rows?

- A. No order is promised
- B. The order they were inserted
- C. Sorted by the first column

Correct answer: A

SQL guarantees an order only when the query asks for one.

- Why not B: It often looks that way, but it is not guaranteed.
- Why not C: Only a query that asks for sorting gets sorted rows.

## Remember this

- A table is a named collection of rows, and every row has the same columns.
- A row is one thing you store, a column is one kind of fact, and a cell is where they meet.
- Each column has a data type, and the list of columns and types is called the schema.
- SQL returns rows in no promised order unless the query asks for sorting.
