⌘K

Databases

SQL

SQL (Structured Query Language) is the standard language for defining, querying and modifying data held in relational databases.

Updated 12 Aug 2026

Declaring what you want

SQL is declarative: you describe the result you want and the database's query planner decides how to produce it. That separation is why the same query can run unchanged as a table grows from a thousand rows to a hundred million — only the plan changes.

The families of statements

GroupStatementsPurpose
DQLSELECTRead data
DMLINSERT, UPDATE, DELETEChange rows
DDLCREATE, ALTER, DROPDefine structure
DCLGRANT, REVOKEControl access
TCLBEGIN, COMMIT, ROLLBACKGroup work into transactions

How a SELECT is evaluated

  1. 1FROM and JOIN assemble the working set of rows
  2. 2WHERE filters individual rows
  3. 3GROUP BY collapses rows into groups
  4. 4HAVING filters those groups
  5. 5SELECT computes the output columns
  6. 6ORDER BY sorts, then LIMIT trims

Because SELECT runs after WHERE, a column alias defined in the SELECT list cannot be used in the WHERE clause — repeat the expression or wrap the query.

Readable SQL

Long queries become unreviewable when everything sits on one line. Put each clause on its own line, indent joins and conditions consistently, name every join condition explicitly, and avoid SELECT * in application code so a new column cannot silently change behaviour.

SQL Formatter

Paste a query to reindent and normalise keywords instantly.

Try SQL Formatter

Characteristics

  • Declarative — describes the result, not the algorithm
  • Set-based: operations apply to rows in bulk
  • Standardised by ANSI, with dialect differences per engine
  • Transactional, offering atomic multi-statement changes

Common uses

  • Application data access in relational databases
  • Reporting, dashboards and business analytics
  • Data migrations and schema management
  • ETL pipelines and warehouse transformations

Advantages

  • One language works across most relational engines
  • The planner optimises execution as data grows
  • Transactions guarantee consistency across multiple changes
  • Decades of tooling, documentation and expertise

Limitations

  • Dialect differences complicate portability
  • Poorly written queries can be catastrophically slow
  • Awkward for hierarchical or graph-shaped data
  • String-concatenated queries invite SQL injection

Examples

A grouped, filtered aggregate query
SELECT c.country,
       COUNT(*) AS orders,
       SUM(o.total) AS revenue
FROM orders o
JOIN customers c ON c.id = o.customer_id
WHERE o.created_at >= '2026-01-01'
GROUP BY c.country
HAVING COUNT(*) > 25
ORDER BY revenue DESC
LIMIT 10;

Frequently asked questions

What is SQL used for?

Storing, querying and modifying structured data in relational databases, plus reporting and analytics on top of it.

Is SQL a programming language?

It is a domain-specific declarative language. Procedural extensions such as PL/pgSQL add loops and variables.

What is the difference between WHERE and HAVING?

WHERE filters rows before grouping; HAVING filters the groups produced by GROUP BY.

What is SQL injection?

An attack where user input is concatenated into a query and executed as code. Use parameterised statements to prevent it.

SQL or NoSQL?

SQL suits related, consistent data with ad-hoc queries; NoSQL suits flexible schemas and very high write throughput.

Related terms

All terms

Related tools

Related guides

All guides