Himanshu SharmaSoftware engineer
Let’s talk

SQL · Lesson 1 of 49

What SQL is and how databases work

Learn what SQL is, how relational databases store data in tables, and how a first query asks a question, with output from a small company database.

10 minGetting startedUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonExplain what a relational database and SQL are, name the main kinds of SQL statement, and read your first query and its result.

What you will be able to do

After this lesson you can say what a database and SQL are, describe how data sits in tables, and read a simple query together with its result. There is nothing to install yet. Every query in the course runs against one small, made-up company database, and the real output is printed under each example.

What a database is

A database is an organised collection of data that a program, called a database management system (DBMS), looks after. MySQL, PostgreSQL, SQLite and SQL Server are all database management systems. Each one stores data on disk, lets many people read it at once, protects it from being corrupted by a crash, and answers questions about it quickly.

You could keep the same information in a spreadsheet. The difference shows up when the data grows or when several people and programs use it together. A database enforces rules (an email address must be unique, a salary cannot be empty), it handles two people editing at the same time, and it can find one row among millions without reading all of them.

Tables, rows and columns

Most databases you will meet are relational. They keep data in tables. A table is a grid with a fixed set of columns, and each row is one record. Our sample database has a table called departments:

sql
SELECT * FROM departments;
result
+----+-------------+-----------+
| id |    name     |   city    |
+----+-------------+-----------+
|  1 | Engineering | Pune      |
|  2 | Sales       | Mumbai    |
|  3 | Marketing   | Delhi     |
|  4 | Finance     | Mumbai    |
|  5 | Support     | Pune      |
|  6 | Research    | Bengaluru |
+----+-------------+-----------+

Each column has a name and a type of data. Here id is a whole number, while name and city hold text. Each row describes one department. Row three says: department number 3 is called Marketing and is based in Delhi.

The word "relational" comes from how tables connect. Another table, employees, has a column department_id. A value there refers to the id of a department. Instead of writing the word "Engineering" on every employee's row, the employee row stores the number 1, and you look up the name when you need it. This keeps one fact in one place. If a department is renamed, you change a single row.

What SQL is

SQL stands for Structured Query Language. It is the language almost every relational database understands. It was designed to read like a short English request, and it is declarative: you describe the result you want, not the steps to produce it. You never write a loop that visits each row. You write something like "give me the name and salary of every developer", and the database engine chooses the quickest way to find them.

SQL is standardised, so a statement such as SELECT looks the same everywhere. Each product also has its own extras and quirks. This course teaches the shared core, and every time something differs between MySQL, PostgreSQL, SQLite and SQL Server in a way that could trip you up, a short note says so. If you want a deeper tour of one product, the MySQL course goes into it.

Your first query

Here is a complete statement:

sql
SELECT first_name, job_title
FROM employees
WHERE department_id = 3;
result
+------------+--------------------+
| first_name |     job_title      |
+------------+--------------------+
| Dev        | Marketing Manager  |
| Anika      | Content Strategist |
| Yash       | Designer           |
+------------+--------------------+

Read it like a sentence. Select the first name and job title, from the employees table, where the department number is 3. The database returns a result set, which is itself shaped like a table, with the columns you asked for and only the rows that matched. The original employees table is untouched.

Three habits are worth forming straight away:

  • End each statement with a semicolon. Many tools run one statement at a time and need it to know where a statement stops.
  • Keywords such as SELECT and FROM are not case sensitive, but writing them in capitals makes queries easier to scan. Table and column names are written in lower case in this course.
  • Spread long queries over several lines, one clause per line. The database ignores the extra white space.

A query does not even need a table. You can use SQL as a calculator:

sql
SELECT 6 * 7 AS answer;
result
+--------+
| answer |
+--------+
|     42 |
+--------+

The families of statements

You will meet four groups of statements. People often give them letters, but the names matter less than the idea.

Family Purpose Examples
Querying Read data without changing it SELECT
Data changes Add, edit and remove rows INSERT, UPDATE, DELETE
Structure Create and modify tables and other objects CREATE TABLE, ALTER TABLE, DROP TABLE
Transaction control Group changes so they succeed or fail together COMMIT, ROLLBACK

The first half of the course is almost all SELECT, which is safe to experiment with because it cannot damage anything. Later lessons show how to change data carefully.

How a query travels

When you run a statement, a client program (a command line, a desktop tool, or your application code) sends the text to the database server. The server checks that the statement makes sense, plans how to answer it, runs the plan against the stored tables, and sends the result set back. SQLite is a small exception because it is a library inside your program rather than a separate server, but the idea is the same.

You can count rows, for example:

sql
SELECT COUNT(*) AS number_of_employees FROM employees;
result
+---------------------+
| number_of_employees |
+---------------------+
|                  20 |
+---------------------+

You do not need to know how the engine found the answer. That separation is what makes SQL last: the same question keeps working while the database underneath becomes faster or bigger.

Differences between databases

  • The core of SELECT, FROM and WHERE is the same on MySQL, PostgreSQL, SQLite and SQL Server.
  • Limiting the number of rows is written differently, and a later lesson covers it.
  • Some tools do not need the semicolon when you run a single statement, but you should always write it.

Common mistakes

  • Thinking SQL is a programming language with loops. Beginners often try to process one row at a time in their head. Think in whole tables: what should the result look like?
  • Forgetting the semicolon between statements, so two statements run together and produce a syntax error.
  • Treating a table as ordered. A table is a set of rows. Unless you ask for an order, the database may return rows in any order.
  • Using double quotes for text. Text values in SQL use single quotes, such as 'Pune'.

Exercise

Write a query that shows the name and city of every department. Then work out, without running it, how many rows the result will have by looking at the departments output above.

Show one possible answer
sql
SELECT name, city FROM departments;
result
+-------------+-----------+
|    name     |   city    |
+-------------+-----------+
| Engineering | Pune      |
| Sales       | Mumbai    |
| Marketing   | Delhi     |
| Finance     | Mumbai    |
| Support     | Pune      |
| Research    | Bengaluru |
+-------------+-----------+

There are six rows, one for each department.

Key points

  • A relational database stores data in tables made of rows and columns, and tables can be linked by shared values.
  • SQL (Structured Query Language) is the language you use to ask a database for data and to change it.
  • You describe what result you want, and the database engine decides how to get it.
  • Statements fall into a few families: reading data, changing data, defining structure and controlling transactions.
  • Standard SQL works on MySQL, PostgreSQL, SQLite and SQL Server, with small differences that this course points out.

Check yourself

1. In a relational database, what is a row?
2. What does it mean that SQL is declarative?
3. Which kind of statement is SELECT?
4. Why do tables usually share a column such as department_id?