What is a relational database and what is SQL?
A relational database stores data in related tables with a predefined schema, and SQL is a declarative query language for it. A query describes the result needed, and the optimizer decides how to find it, for example through an index. The core of SQL is the same across engines (SELECT, JOIN, GROUP BY), but dialects differ in details: row limiting, date functions, the empty string in Oracle.
SQL · Basics
A table holds three million transactions and you need the last five failed ones. How to get them without a loop?
A QA engineer checks whether failed payments reach the log. The database holds three million transactions, and only the last five with status FAILED matter. You can export everything and filter in code, or ask the database for exactly that in one query. This lesson explains what a relational database consists of, why SQL describes the result and not the steps, and where the "sameness" of SQL across engines ends.
How it works and why. A relational database stores data in tables (formally, relations). A row is one record: a user, an account, a transaction. A column is a property with a fixed type. The schema (the set of tables, types and constraints) is defined in advance, and the database itself rejects what contradicts it: text in a numeric field, a row pointing to a record that does not exist. Tables are linked by keys: accounts.user_id points to users.id, so every fact lives in one place and cannot drift from its copies.
Two tables linked by a key
| id | |
|---|---|
| 1 | anna@test.com |
| 2 | ivan@test.com |
| id | user_id | balance |
|---|---|---|
| 10 | 1 | 100.00 |
| 11 | 1 | 5.50 |
| 12 | 2 | 0.00 |
users.SQL is declarative. A query describes the result you need: from which table, under which condition, in which order. How to find it is decided by the DBMS: the optimizer builds a plan, for example uses an index instead of a full table scan. So the same query can run in milliseconds or minutes depending on indexes and statistics, while the result does not change.
Example: from a loop to a query. The imperative approach pulls all three million rows into the application:
// imperative: pull everything and filter by hand
const all = await db.query('SELECT * FROM transactions');
const failed = all
.filter((r) => r.status === 'FAILED')
.sort((a, b) => b.created_at - a.created_at)
.slice(0, 5);
The bottleneck here is the network and memory: filtering, sorting and trimming happen after all the data has been transferred. The declarative form hands the same work to the database, which has indexes and sits next to the data:
-- declarative: describe the result
SELECT id, amount, created_at
FROM transactions
WHERE status = 'FAILED'
ORDER BY created_at DESC
LIMIT 5;
The client receives five rows instead of three million. For QA it is also a way to check test data without pulling everything into an automated test: the query can be run in a database client (psql, DBeaver, DataGrip) in a minute.
Two ways to get five rows
A loop in code
Export everything, filter, sort, trim. Lots of data on the network, lots of code.
One SQL query
Describe the result. The database picks the plan and access path, the client receives only what is needed.
Limits and dialects. SQL is a standard (ISO/IEC 9075), but each DBMS adds its own dialect. The core is the same: SELECT, JOIN, GROUP BY. Details differ, and interviews are likely to ask about them:
-- PostgreSQL, MySQL
SELECT id FROM transactions ORDER BY id LIMIT 5;
-- SQL Server
SELECT TOP 5 id FROM transactions ORDER BY id;
-- standard form (PostgreSQL, Oracle 12c+; SQL Server only with ORDER BY … OFFSET 0 ROWS)
SELECT id FROM transactions
ORDER BY id
FETCH FIRST 5 ROWS ONLY;
Other differences: date functions, types (JSONB exists in PostgreSQL), identifier quoting, the behaviour of the empty string (Oracle treats it as NULL). Examples in this course are given for PostgreSQL. The relational model is not the only one: document stores (MongoDB) and key-value stores fit better when the schema changes often or there are almost no relations, but for integrity guarantees and transactions relational databases remain the base for financial data. PostgreSQL can also store flexible documents in JSONB.
Common mistakes.
- Thinking SQL runs top to bottom in the order written: the real order is different (there is a separate lesson).
- Moving the syntax of one dialect to another without checking:
LIMITdoes not work in SQL Server,TOPdoes not work in PostgreSQL. - Exporting a table into code to filter it instead of querying.
- Treating the database as "just storage": it checks types, uniqueness and relations itself.
- Running a query on a shared environment without knowing whether it only reads.
Check yourself
Why is a query with WHERE and ORDER BY faster in the database than the same loop in a test?
Answer
In the database, filtering and sorting happen next to the data, often with an index, and the client receives only the result. In a loop all rows first travel over the network into the test and only then get discarded.
Is it true that SQL is the same in every DBMS?
Answer
Only the core: SELECT, JOIN, GROUP BY, INSERT. There are differences in limiting rows (LIMIT, TOP, FETCH FIRST), date functions, types and the handling of the empty string, so an example written for one DBMS has to be checked on another.