SQL basics: databases, tables and your first query

A database keeps its data in tables that look a lot like Excel sheets, only with stricter rules. SQL is the language you use to ask those tables questions. This lesson covers the words you need for everything that follows, and you write your first query in the first minute.

In short: a database is made of tables. Each table has fixed columns, each with a data type, and any number of rows. SQL is the language for asking questions: SELECT * FROM employees; returns every column and every row of the employees table.

What you should know first: nothing. If you have ever kept a list in Excel, you already know the idea.

Try it now

The exercises run on a real, small database from an online office supplies shop. The table is shown above the editor. Run shows the result, Check compares it with the task, and the hints walk you through it if you get stuck.

Task It is your first day in accounting and you want to get to know the team. Show the whole employees table: every column and every row.

employees 10 rows, 7 columns
employee_idfirst_namelast_namedepartmentsalaryhire_datemanager_id
1KarenMitchellManagement936002018-01-02NULL
2TomBradleySales624002019-04-011
3JuliaReyesSales468002021-06-152
4MarcusAllenSales492002020-09-012
5SarahLongWarehouse396002022-02-011
6FelixTurnerWarehouse354002024-03-185
7NinaCooperAccounting528002019-10-011
8DavidRossCustomer service384002023-05-022
9LauraHughesCustomer serviceNULL2025-09-012
10PaulWardAccounting432002022-11-147

What is a database?

A database is an organized collection of data, managed by a program called a database management system. Well-known ones are MySQL, PostgreSQL, Microsoft SQL Server and SQLite. Inside, the data sits in tables, and the tables are linked by ID numbers: an order stores the customer's number instead of copying their name and address all over again.

Our practice database has five tables: customers, products, employees, orders and order_items. Most business software you use at work has a database like this underneath, whether it handles inventory, accounting, customer records or payroll.

Tables, rows and columns: an Excel sheet with fixed columns

A table looks like a tidy Excel list. These are the first three rows of the employees table:

employee_idfirst_namelast_namedepartmentsalaryhire_datemanager_id
1KarenMitchellManagement936002018-01-02NULL
2TomBradleySales624002019-04-011
3JuliaReyesSales468002021-06-152

Each row is one record, here one person. Each column is one property with a fixed name, such as last_name or salary. NULL means "no value": Karen Mitchell has no manager above her. You will meet NULL again in the lesson NULL values.

In ExcelIn a database
WorkbookDatabase
Worksheet holding a listTable
Headers in row 1Column names, defined up front
Any cell can hold anythingEvery column has a data type
Rows are numbered 1, 2, 3 …Rows have no fixed order and are found by a key
Data and formulas share the sheetData lives in tables, analysis is a separate query

The key difference: in a spreadsheet, someone can type "ask HR" into the salary column. A database refuses. That strictness is one reason databases stay reliable with a lot of rows and many people working on them at once.

Data types: what a column accepts

When a table is created, every column gets a data type. Our practice database mostly uses three:

  • INTEGER: whole numbers, such as employee_id or stock.
  • REAL: numbers with decimals, such as price.
  • TEXT: text, such as name or city.

Dates like hire_date are stored as text in the form YYYY-MM-DD, so 2021-06-15 is the 15th of June 2021. That format reads the same in the US and the UK, with none of the 03/04 guesswork, and it sorts correctly even as text. MySQL displays its dates the same way.

Primary keys: a unique ID for every row

Two employees can share a name. To keep every row distinguishable, each table has a primary key: a column whose value is never repeated and never missing. In employees it is employee_id, in customers it is customer_id. At work you know this as an employee number or account number.

Other tables point to a row through that number. The orders table stores only the customer_id, not the whole customer. Putting such tables back together is what JOIN does, in level 3 of the course.

What is SQL, and why learn it?

SQL stands for Structured Query Language. You describe the result you want, and the database works out how to fetch it. SQL is an international standard (ISO/IEC 9075), which is why the basics look the same in every major database system.

At work, SQL lets you pull data straight from the source instead of waiting for someone to send an export: all open orders, revenue per customer, everyone in one department. Much of what you do in Excel with filters, sorting and SUMIF becomes a single query you can rerun whenever you need fresh numbers.

What a query looks like

SELECT first_name, department
FROM employees;

Read it as a sentence: "Select first_name and department from the employees table." The answer is another table, with every row of employees but only two columns. The first four rows:

first_namedepartment
KarenManagement
TomSales
JuliaSales
MarcusSales

A SELECT query only reads. It changes nothing in the table, so you cannot break anything by trying.

One more exercise: a single column

This time you do not want every column, just one. Replace the star with its name.

Task Reception is printing new door signs and needs the last name of every employee. From the employees table, get only the last_name column.

employees 10 rows, 7 columns
employee_idfirst_namelast_namedepartmentsalaryhire_datemanager_id
1KarenMitchellManagement936002018-01-02NULL
2TomBradleySales624002019-04-011
3JuliaReyesSales468002021-06-152
4MarcusAllenSales492002020-09-012
5SarahLongWarehouse396002022-02-011
6FelixTurnerWarehouse354002024-03-185
7NinaCooperAccounting528002019-10-011
8DavidRossCustomer service384002023-05-022
9LauraHughesCustomer serviceNULL2025-09-012
10PaulWardAccounting432002022-11-147

Common mistakes

Misspelling the table name

SELECT * FROM employee finds nothing, because the table is called employees. The exercise says "There is no table "employee"", lists the tables that do exist and suggests the right one.

Leaving out FROM

SELECT * employees gets the reply "FROM is missing before "employees"". Without the word FROM, the database has no idea that employees is meant to be a table. The order is always SELECT, the columns, then FROM and the table.

Asking the wrong table for a column

SELECT last_name FROM customers fails, and the exercise tells you "The column "last_name" exists in employees, but not in customers." The column exists, just in the other table. Make sure the column and the table belong together. The columns of each table are listed above the editor.

The basics in MySQL, PostgreSQL, SQL Server and SQLite

Tables, rows, columns, primary keys and SELECT work the same way in all four. The differences show up in data types and in how a table numbers new rows automatically:

DatabaseAuto-numbered primary keyDates
MySQLAUTO_INCREMENTits own DATE type
PostgreSQLGENERATED ALWAYS AS IDENTITYits own date type
SQL ServerIDENTITY(1, 1)its own date type
SQLite (these exercises)INTEGER PRIMARY KEY counts up by itselfno date type; here text as YYYY-MM-DD

SQLite is also more relaxed about types than the others: the type belongs to each value, not to the column. For the queries in this course that makes no difference.

FAQ

Is SQL a programming language?

SQL is a query language. You do not write loops or step-by-step instructions; you describe the result you want. That is why people without any coding background pick it up quickly.

Do I need to install anything?

No. You write every query right here on the page, and it runs on a real database on our server. Each query gets a fresh copy of the data, so experiment as much as you like.

Is SQL hard to learn?

The start is gentle: SELECT, FROM and WHERE read almost like plain English. It gets more demanding once you combine tables and summarize groups, which is why the course adds one idea per lesson and lets you practice each before moving on.

Will SQL replace Excel for me?

No, they work together. SQL fetches and condenses the data from the database; Excel is where you keep calculating, build charts and share the result. Knowing both means many reports no longer need a manual export.

Learn this next: SELECT

You know what tables are and you have already pulled columns with SELECT. Next you pick several columns in a set order and learn when SELECT * is fine and when it is not.

Practice SELECT

Write to us

Choose language

Select your preferred language