Databases, tables and your first SQL query
Learn what a database, a table and SQL are, set up the free DB Browser for SQLite with the course's sample shop database, and run your first SELECT query.
π― By the end of this lesson you will be able to:
- Explain what a database, table, row and column are
- Install DB Browser for SQLite and load the sample shop database
- Run a query and read the result
Outline
What you'll learn and why it matters
Almost every app you use, from online shops to banking apps to your college's results portal, keeps its information in a database: an organised store of data that a computer can search quickly. SQL (Structured Query Language, often said βsequelβ or βS-Q-Lβ) is the language used to ask databases questions, such as βwhich customers in India ordered this month?β. Analysts, developers, testers and product managers use it every day, and it is one of the most requested skills in job adverts.
In this lesson you set up a free database tool, load a small online shop's data, and ask it your first question.
Tables, rows and columns
Most databases store data in tables, which look a lot like a spreadsheet. Each table holds one kind of thing: customers, products or orders.
| Word | Meaning | In the customers table |
|---|---|---|
| Table | A collection of records of one kind | customers |
| Column | One kind of information, with a name | name, city, country |
| Row | One record: one customer | Asha Patel from Mumbai |
| Primary key | A column whose value is unique for every row | customer_id |
A database usually holds several related tables. Our sample shop has three: customers, products and orders. A program that manages databases is called a database management system. We use SQLite, which is free, needs no server, and stores a whole database in one file. The SQL you learn works almost the same in MySQL, PostgreSQL, SQL Server and Oracle.
Set up DB Browser for SQLite
DB Browser for SQLite is a free, open-source program for creating and querying SQLite databases. It works on Windows, macOS and Linux. This course was checked against version 3.13.1, the latest release when it was reviewed.
- Go to sqlitebrowser.org/dl and download the version for your computer. On Windows choose the standard installer (or the
.zip (no installer)version if you cannot install programs on your computer). On macOS choose the Universal.dmg. - Install it and open DB Browser for SQLite.
- Download the sample database script below and save it somewhere easy to find, such as a folder called
sql-course.
β¬ Download shop.sqlCreates the customers, products and orders tables for a small made-up online shop. All names and emails are fictional.
Now load the sample data:
- In DB Browser, click New Database (or press Ctrl + N). Save it as
shop.dbin yoursql-coursefolder. - A window may open asking you to define a table. Click Cancel; the script will create the tables for you.
- Open
shop.sqlin a text editor (such as Notepad or TextEdit), select everything (Ctrl + A, or Cmd + A on a Mac) and copy it. - In DB Browser, click the Execute SQL tab, paste the script into the editor, and press F5 (or Ctrl + Return) to run it.
- Click Write Changes to save the data to
shop.db. - Click the Browse Data tab and choose the
customerstable. You should see 10 customers.
Prefer the command line? SQLite also has a command-line program called sqlite3 (included on macOS and most Linux systems; for Windows it is available from sqlite.org). From your sql-course folder:
sqlite3 shop.db < shop.sql
sqlite3 shop.dbThen type queries at the sqlite> prompt, and .quit to leave.
Your first query
A request for data is called a query. In the Execute SQL tab, delete what is there, type this and press F5:
SELECT * FROM customers;Result: 10 rows
| customer_id | name | city | country | joined_on | |
|---|---|---|---|---|---|
| 1 | Asha Patel | Mumbai | India | asha@example.com | 2025-01-15 |
| 2 | Ravi Kumar | Bengaluru | India | ravi@example.com | 2025-02-03 |
| 3 | Meera Nair | Kochi | India | NULL | 2025-03-21 |
| 4 | John Smith | London | UK | john@example.com | 2025-03-30 |
| 5 | Fatima Khan | Dubai | UAE | fatima@example.com | 2025-04-12 |
| 6 | Li Wei | Singapore | Singapore | liwei@example.com | 2025-05-08 |
| 7 | Sara Lopez | Toronto | Canada | NULL | 2025-06-19 |
| 8 | Arjun Singh | Delhi | India | arjun@example.com | 2025-07-02 |
| 9 | Emma Brown | Sydney | Australia | emma@example.com | 2025-08-25 |
| 10 | Kofi Mensah | NULL | Ghana | kofi@example.com | 2025-09-14 |
| Part | Meaning |
|---|---|
SELECT | βGive me...β. Every query that reads data starts with SELECT. |
* | β...every column...β |
FROM customers | β...from the customers table.β |
; | Marks the end of the statement. It is needed when you run several statements together, so always add it. |
The result appears in the grid under the editor. Some cells show NULL. That means βno valueβ: Meera and Sara did not give an email address, and Kofi's city is unknown. You will learn to work with NULL in lesson 5.
Practice
Guided exercise
Look at the products
Show every column of every row in the products table. How many products does the shop sell, and which is the most expensive?
- In the Execute SQL tab, type
SELECT * FROMfollowed by the table name. - End with a semicolon and press F5.
- Count the rows and read the
pricecolumn.
Show the solution
SELECT * FROM products;Result: 8 rows
| product_id | name | category | price |
|---|---|---|---|
| 1 | Notebook A5 | Stationery | 120 |
| 2 | Gel Pens (pack of 5) | Stationery | 90 |
| 3 | Python Basics Book | Books | 450 |
| 4 | SQL Pocket Guide | Books | 350 |
| 5 | Desk Lamp | Electronics | 1200 |
| 6 | USB-C Cable | Electronics | 299 |
| 7 | Water Bottle | Home | 399 |
| 8 | Coffee Mug | Home | 249 |
How to get there: There are 8 products. The Desk Lamp, at 1200 (prices are in Indian rupees), is the most expensive. In lesson 4 you will make SQL find that for you instead of reading the whole table.
Your turn
Look at the orders
Show every row of the orders table. Which column tells you who placed each order?
Show the solution
SELECT * FROM orders;How to get there: There are 15 orders. customer_id holds the id of the customer who placed the order, and product_id the product they bought. Numbers instead of names may look odd; lesson 8 explains why databases do this, and lesson 9 shows how to bring the names back.
Common mistakes
Misspelling the table name
You see: no such table: customer
Fix: Check the exact name in the Database Structure tab. Our table is customers, plural.
Forgetting to click Write Changes after loading the data
You see: Next time you open shop.db, the tables are missing
Fix: Load shop.sql again and click Write Changes (DB Browser keeps changes pending until you do).
Running the query in the wrong place
You see: Nothing happens when you type in Browse Data
Fix: Queries go in the Execute SQL tab; press F5 to run them.
Typing smart quotes copied from a document
You see: unrecognized token: "β"
Fix: Type quotes yourself in the editor; SQL needs plain straight quotes.
Recap
- A database stores data in tables made of columns (kinds of information) and rows (records).
- A primary key column, such as
customer_id, gives every row a unique id. - SQL is the language for asking databases questions.
SELECT * FROM table;shows every column and row. - NULL means no value.
SELECTonly reads data, so it is always safe to run.
Key terms
- Database
- An organised store of data that a computer can search and update quickly.
- Table
- A set of rows of one kind of thing, with named columns.
- Query
- A request for data, written in SQL.
- NULL
- A marker meaning a value is missing or unknown.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. In a table of customers, what is one row?
2. What does `SELECT * FROM products;` do?
3. What is special about a primary key column?
4. A cell shows NULL. What does that mean?
Read more
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.