What is SQL?
SQL (Structured Query Language) is a standard language used to:
- Query data from relational databases
- Modify data (INSERT, UPDATE, DELETE)
- Define database structure (tables, indexes, constraints)
- Control access (GRANT, REVOKE)
It operates on data stored in tables (relations) and is based on relational algebra.
Examples of SQL-based databases:
- PostgreSQL
- MySQL
- Oracle
- SQL Server
What does it mean that SQL is declarative?
Short definition (interview-ready)
SQL is declarative because you describe what result you want, not how to get it.
You specify:
- What data you need
- What conditions it must satisfy
You do NOT specify:
- Which indexes to use
- Whether to use a hash join or nested loop
- In what order rows are read
That responsibility belongs to the query optimizer inside the database.
Declarative vs Imperative (key contrast)
Imperative (how)
for (User u : users) {
if (u.age > 30) {
result.add(u);
}
}
You explicitly control:
- Iteration
- Condition checks
- Order of execution
Declarative (what)
SELECT *
FROM users
WHERE age > 30;
You say:
- “Give me users older than 30”
The database decides:
- Full scan vs index scan
- Join algorithms
- Execution order
Why SQL being declarative matters (important!)
Because of this:
- The same query can run efficiently on:
- Small tables
- Huge tables
- Different hardware
- The DB can re-optimize queries as data grows
- You get performance improvements without code changes
This is why hand-written “clever” SQL often loses to the optimizer.
What SQL does not guarantee (trap question)
Declarative SQL does NOT guarantee:
- Row order (unless
ORDER BY) - Execution plan stability
- Deterministic evaluation order in
WHERE
Example:
SELECT *
FROM users;
➡️ Result order is undefined.
One-sentence senior-level answer
SQL is a declarative language for working with relational data, where you specify the desired result set, and the database engine decides the most efficient execution strategy using its optimizer.
Common interview mistakes ❌
- Saying “SQL is procedural”
- Talking about loops or execution order
- Thinking
WHEREis evaluated beforeFROMlogically - Assuming row order without
ORDER BY