# SQL

A declarative language for defining, querying, changing, and controlling data in relational database systems.

- Type: term
- Domain: Engineering & architecture
- Level: Foundational
- Last reviewed: 2026-09-23
- Original: [Soroush’s Product Wiki](<https://wiki.soroush.uk/topic/sql/>)

## What it means

SQL, or Structured Query Language, is the standard language family used with relational databases. It expresses desired results and changes through statements for data definition, queries, updates, transactions, and access control, while the database chooses an execution strategy. Products implement standards with differing features and dialects.

## Why it matters

SQL lets applications and analysts work with related data without specifying low-level traversal procedures. Query meaning, parameterization, transactions, and indexing directly affect correctness, security, latency, and infrastructure cost.

## When to use it

Use SQL to work with relational data and systems that expose compatible query interfaces. Prefer application APIs for enforcing a product boundary, and use specialized query languages when another data model or workload is the true source of record.

## How it works

1. Define tables, columns, data types, keys, and constraints with data-definition statements.
2. Form queries by selecting desired columns and combining filtering, joins, grouping, ordering, and aggregation.
3. Use parameterized statements for untrusted values and grant only the privileges each caller needs.
4. Group dependent changes into transactions with suitable isolation and explicit error handling.
5. Inspect execution plans and measurements before changing queries, indexes, or structures.
6. Account for the target database dialect and test migrations and queries against its actual behavior.

## A practical example

A support tool uses a parameterized SQL query to join accounts, subscriptions, and recent invoices for one customer. An execution plan reveals a costly scan, so the team adds a targeted index after measuring the write and storage cost.

## Where it breaks

SQL dialects differ, and valid statements can still be slow, unsafe, or semantically wrong. Declarative syntax does not remove the need to understand data modeling, nulls, concurrency, access control, query plans, and transaction boundaries.

## Common mistakes

- Building SQL by concatenating untrusted input instead of using parameters.
- Using SELECT \* when callers need a stable, limited set of columns.
- Optimizing from query text alone without inspecting plans and production-like data.
- Assuming statements behave identically across database products and versions.

## Visualization

[View the visualization on the website](<https://wiki.soroush.uk/topic/sql/#visualization-sql>)

## Sources

1. [The SQL Language](<https://www.postgresql.org/docs/current/tutorial-sql.html>) — PostgreSQL Global Development Group · Official technical documentation
2. [SQL Syntax](<https://www.postgresql.org/docs/current/sql-syntax.html>) — PostgreSQL Global Development Group · Official technical documentation

## Related topics

- [Relational Database](<https://wiki.soroush.uk/topic/relational-database/>)
- [Database Schema](<https://wiki.soroush.uk/topic/database-schema/>)
- [Database Index](<https://wiki.soroush.uk/topic/database-index/>)
