CodeStride
Getting Started

Databases and SQL in SQL

Topic 1 of 45 · 7 minFree lesson

Almost every app you use keeps its data in a database: your messages, your playlists, the products in a shop. A database is an organized store of data that a program can search and change quickly. SQL (Structured Query Language) is the language you use to ask a database questions. You'll hear it said as "S-Q-L" or "sequel".

In this course you'll use PostgreSQL, one of the most popular databases in the world. It runs right here in your browser.

Tables, rows and columns

A database keeps its data in tables. A table looks like a spreadsheet. Each column holds one kind of fact, like a name or a price. Each row is one thing, like one product.

This topic uses the database of a small plant shop. Its products table has a row for each product the shop sells.

Your first query

A query is a question you ask the database. SELECT * means "give me every column", and FROM products names the table.

Example 1
SELECT * FROM products;
id | name | category | price | stock----+-----------------+----------+-------+------- 1 | Fern | indoor | 12.50 | 30 2 | Cactus | indoor | 8.00 | 45 3 | Monstera | indoor | 35.00 | 12 4 | Snake Plant | indoor | 22.00 | 0 5 | Lavender | outdoor | 9.50 | 60 6 | Rosemary | herbs | 6.00 | 40 7 | Basil | herbs | 4.50 | 80 8 | Olive Tree | outdoor | 89.00 | 5 9 | Watering Can | tools | 15.00 | 25 10 | Clay Pot | tools | 7.50 | 100 11 | Bonsai | indoor | 49.00 | 3 12 | Mint | herbs | 4.00 | 70 13 | Potting Soil | | 11.00 | 50 14 | Sunflower Seeds | outdoor | 3.50 | 200 15 | Plant Food | | 9.00 | 0(15 rows)

The database answers with a table of its own: the columns, then one line per row. The last line counts the rows.

Asking another table

The shop has other tables too. Change the table name to see a different one. Here are the shop's customers.

Example 2
SELECT * FROM customers;
id | name | email | city | joined----+--------------+-------------------+--------+------------ 1 | Ana Silva | ana@example.com | Lisbon | 2024-02-11 2 | Ben Okafor | ben@example.com | Lagos | 2024-03-05 3 | Chloe Martin | chloe@example.com | Paris | 2024-05-20 4 | Dev Patel | dev@example.com | London | 2024-06-01 5 | Emma Jones | emma@example.com | London | 2024-08-14 6 | Felix Wagner | felix@example.com | Berlin | 2024-09-30 7 | Grace Kim | grace@example.com | | 2024-11-02 8 | Hugo Rossi | hugo@example.com | Rome | 2025-01-17 9 | Isla Brown | isla@example.com | Lisbon | 2025-02-08 10 | Jon Berg | jon@example.com | | 2025-03-22(10 rows)

Some customers have no city. An empty space like that is a missing value, which you'll meet properly in a later topic.

How a query is written

SQL keywords like SELECT and FROM don't care about capitals, and a query can spread over several lines. Most people write keywords in capitals, so they stand out from table and column names. A semicolon ; marks the end of a query, so you can write several in a row.

Example 3
select *from orders;
id | customer_id | ordered | status----+-------------+------------+----------- 1 | 1 | 2025-01-05 | delivered 2 | 2 | 2025-01-12 | delivered 3 | 1 | 2025-02-03 | delivered 4 | 3 | 2025-02-14 | delivered 5 | 4 | 2025-02-20 | cancelled 6 | 5 | 2025-03-01 | delivered 7 | 6 | 2025-03-09 | delivered 8 | 3 | 2025-03-15 | shipped 9 | 8 | 2025-03-18 | shipped 10 | 1 | 2025-03-25 | pending 11 | 9 | 2025-03-27 | pending 12 | 5 | 2025-03-28 | pending(12 rows)

Notice the customer_id column. Its numbers point at rows in the customers table, so order 1 belongs to customer 1, Ana Silva. You'll combine tables like this later in the course.

Common mistakeMisspelling a table's name. PostgreSQL can't guess what you meant, so it stops with an error that names the table it couldn't find.
Example 4
SELECT * FROM product;
ERROR: relation "product" does not exist

The table is called products, with an s. Check the name in the list of tables under the editor.

TipUnder every SQL editor in this course, a list shows the tables you can use and their columns. Open it whenever you're not sure of a name.

Try it yourself

Run the code in the editor and answer 8 practice questions on databases and sql. It's free, you only need an account.