Lesson 1
Tables, rows and columns
For readers who have never written SQL. Nothing to install: every example runs in this page.
- What a database holds
- Tables, rows and columns
- Column types
- Your first query
- When a name is wrong
- 4 exercises
Every query runs on real PostgreSQL, in your browser. It downloads once, about 6 MB, then starts in a few seconds.
For a reference page on the same statements, read SQL Tutorial for Beginners with Real Examples.
The database
A database keeps its data in tables
Every lesson uses the same database: a small shop. Each box in the diagram is a table. Customers place orders, each order lists the products it holds, and employees in departments take the orders. The lines between the boxes link one table to another, which lesson 8 explains.
Hover a table or a column in the diagram to read its comment: what it holds, and what it links to. The slides that query a table show its diagram too.
The idea
A table is a grid of rows and columns
Every column has a name, such as name, and every row is one thing: here, one department. Where a row meets a column there is exactly one value.
A value can also be missing. SQL calls a missing value NULL, and lesson 4 is about it.
The idea
Every column holds one kind of value
This is how the shop's products table was created:
create table products (
product_id integer primary key,
name text not null unique,
category text not null,
price numeric(10, 2) not null check (price > 0),
stock integer not null check (stock >= 0)
);
Each line names a column and its type: integer for whole numbers, text for words, and numeric(10, 2) for amounts with two decimals. PostgreSQL refuses a value of the wrong type, so a price is always a number. The rest of each line comes back in lesson 8.
Try it
Ask for a whole table
select * from departments;
The answer is itself a table: 2 columns and 7 rows, one per department.
The idea
A query asks for rows, and the answer is a table
select starts a query. The star means every column, and from names the table to read. PostgreSQL sends back a new table, which the page shows under the editor. A query only reads: the table stays as it was.
Try it
Look at the products
select * from products;
26 rows, and every row has the same five columns.
Watch out
A table name must be spelled exactly
The table is called products, with an s. Without it, PostgreSQL finds no such table:
select * from product;
ERROR: relation "product" does not exist
PostgreSQL calls a table a relation, which is the word in its message. The Tables button lists every table and its columns, so you never have to remember a name.
Exercise 1 of 4
Show every row and every column of the departments table.
Expected result
It shows once PostgreSQL is ready.
Each check sends the exercise, your SQL and any PostgreSQL error to dbschema.com, without your IP address, so that the hints can improve.
Show answer
select * from departments
Exercise 2 of 4
Show every row and every column of the employees table.
Expected result
It shows once PostgreSQL is ready.
Each check sends the exercise, your SQL and any PostgreSQL error to dbschema.com, without your IP address, so that the hints can improve.
Show answer
select * from employees
Exercise 3 of 4
How many rows does the customers table have? Run a query here to find out, then pick the answer.
Each answer sends the exercise and whether the answer was right to dbschema.com, without your IP address.
Exercise 4 of 4
Which column of the orders table holds the employee who took the order? The Tables button lists the columns of every table.
Each answer sends the exercise and whether the answer was right to dbschema.com, without your IP address.