{"canonical_url":"https://engineer.company/glossary/sql/","content_markdown":"# SQL\n\n## What it is\n\nSQL is a declarative language for defining, changing and reading data kept in\nrelational tables: you write down the result you want, and the database decides\nhow to produce it. The letters are usually expanded as Structured Query Language\nand pronounced either letter by letter or as \"sequel\", while the international\nstandard that defines it, ISO/IEC 9075, calls it simply Database Language SQL.\n\nDeclarative is the word doing the work. The statement below names a table, a\ncondition, a grouping and an order. It says nothing about indexes, loops or\nwhich rows to read first:\n\n```sql\nSELECT country, count(*) AS signups\nFROM accounts\nWHERE created_at \u003e= now() - interval '30 days'\nGROUP BY country\nORDER BY signups DESC;\n```\n\nIn PostgreSQL that text passes through four stages. A parser checks the syntax\nand builds a tree, a rewrite step expands any views the statement mentions, a\nplanner compares the possible ways to run it, such as a sequential scan against\nan index, and an executor finally fetches the rows. The same sentence can take a\nmillisecond or a minute depending on what the planner finds, which is why\nreading a plan belongs to writing SQL.\n\nThe language has parts that people name separately. Data definition creates and\nalters tables, schemas and indexes. Data manipulation inserts, updates, deletes\nand merges rows. Queries read them with SELECT. Grants and transactions decide\nwho may do what, and which changes land together. All of it travels as text over\na single connection, so SQL is the common ground between an application, a\nreporting tool and a person at a terminal.\n\nThe standard has been revised many times, most recently as SQL:2023, and every\nengine implements its own subset of it. PostgreSQL's conformance appendix states\nthat it meets at least 170 of the 177 mandatory Core features, and that at the\ntime of writing no database system claimed full Core conformance. Each engine\nalso adds a dialect of its own: Microsoft SQL Server speaks Transact-SQL,\nBigQuery speaks GoogleSQL, and PostgreSQL adds PL/pgSQL for procedural code that\nruns inside the database.\n\n## Why it matters\n\nThe decision SQL puts in front of a team is where the work on data happens:\ninside the database, in the language it was built to execute, or in application\ncode that fetches rows and processes them itself.\n\nA set-based statement runs next to the data. Filtering a million rows down to\nforty inside the database sends forty rows over the network; filtering in the\napplication sends a million and throws most of them away. Joins, aggregates and\ndeduplication follow the same arithmetic. Moving that kind of work into SQL is\noften the cheapest speed-up available to a slow system, because it runs on the\ndatabase already in place.\n\nOwnership is the counterweight. Logic written in SQL lives in migrations and\nfunctions, which need the same review, tests and version control as any other\ncode, and fewer engineers read a long PL/pgSQL function as fluently as they read\nGo or Python. A workable split sends whatever has to see many rows to the\ndatabase and keeps rules that change with the business in the application, where\nthey are easier to test in isolation.\n\nPortability is the second trade-off. The core of the language, SELECT with\njoins, GROUP BY and ORDER BY, moves between engines with few edits. The edges\nmove badly: date arithmetic, upserts, pagination, JSON functions and procedural\ncode all differ, and a migration from one database to another spends most of its\neffort there. Choosing an engine is also choosing a dialect, and the more logic\nlives in that dialect, the more a later move costs.\n\nHiring is the third. SQL is one of the few skills that analysts, backend\ndevelopers, data engineers and database administrators share, so a data layer\nwritten in plain SQL can be read and reviewed by any of them. An ORM hides the\nSQL until a page turns slow, and at that point someone still has to read what it\ngenerated.\n\n## What it looks like in practice\n\nOn one platform, PostgreSQL functions assemble each API response as a complete\nJSON document, and the Go handler forwards it as `json.RawMessage` without\ndecoding or re-encoding it. The\n[JSON passthrough architecture](/portfolio/designed-a-json-passthrough-from-postgres-to-api/)\nmakes shaping the response a SQL concern, done where the data already sits. A\nsmall QueryJSON helper turned that pattern into the easiest one to follow, and\nthe DTOs, mappers and response structs that a layered API usually collects went\naway. Handler code became much shorter, and a change to what a function returns\nreaches the client without anyone editing a handler.\n\nThe same language looks different in a data pipeline. Across\n[six ETL/ELT pipelines](/portfolio/designed-and-ran-6-etl-elt-pipelines/)\nspanning BigQuery, MSSQL and PostgreSQL, raw files were loaded into staging\nschemas with bcp, psql and SSIS, and the cleaning, normalising and enriching\nwere written as PL/pgSQL and T-SQL procedures. Three engines, each with its own\ndialect, inside one job: the portability question above, seen from the inside.\nThe page records the outcome as at least three hours of manual work reduced to\nunder 20 minutes end to end, data refreshed daily instead of weekly, and the\nPython APIs that read it running 30% faster.\n\n## Where it goes wrong\n\n**Statements glued together from strings.** A customer named O'Brien breaks a\nsearch page, or a crafted input reads someone else's data. The cause is user\ninput pasted into the SQL text, where the database cannot tell data from\ncommand. Send values as parameters instead, separately from the statement, as\nevery mainstream driver allows. In PostgreSQL's own client library a\nparameterised call also accepts only one command per string, which adds a second\nline of defence against injection.\n\n**Order that was only luck.** A list that came back sorted for months arrives\nshuffled after a data load or a new plan. Without ORDER BY, PostgreSQL returns\nrows in whatever order is fastest to produce, and a LIMIT without ORDER BY may\neven return a different subset each time. Any result whose order matters says so\nin the query.\n\n**NULL treated as a value.** A filter written `WHERE discount = NULL` returns\nnothing, and `NOT IN` against a subquery that contains a NULL returns nothing\neither. Any comparison with NULL yields unknown rather than true or false, and a\nWHERE clause keeps only the rows where its condition is true. Write `IS NULL`,\nand prefer `NOT EXISTS` to `NOT IN` when the inner column can hold a NULL.\n\n**Dialect that leaks into tests.** A test suite passes against SQLite and the\nsame code fails against PostgreSQL in production, or a move to SQL Server trips\non every `LIMIT`, which that engine spells `TOP` or `OFFSET ... FETCH`. Each\nengine accepts things the others reject. Test against the engine you deploy, and\nkeep engine-specific syntax in as few places as you can.\n\n## Further reading\n\n- [Part II. The SQL Language](https://www.postgresql.org/docs/current/sql.html)\n  — PostgreSQL's full tutorial and reference for the language, from syntax to\n  performance tips.\n- [Appendix D. SQL Conformance](https://www.postgresql.org/docs/current/features.html)\n  — the parts of ISO/IEC 9075 and which features PostgreSQL implements.\n- [Transact-SQL Reference](https://learn.microsoft.com/en-us/sql/t-sql/language-reference)\n  — the SQL Server dialect, documented by Microsoft.\n- [Introduction to SQL in BigQuery](https://cloud.google.com/bigquery/docs/introduction-sql)\n  — GoogleSQL and the legacy dialect it replaced.\n- [Query Language Understood by SQLite](https://www.sqlite.org/lang.html)\n  — the syntax of an embedded engine, statement by statement.\n\n\u003chttps://engineer.company/glossary/sql/\u003e\n","date_modified":"2026-11-09T21:50:25Z","description":"What SQL is, why one ISO standard still means several dialects, which work belongs in it rather than in application code, and the mistakes it punishes.","formats":{"gemtext":"https://engineer.company/glossary/sql/index.gmi","gopher":"https://engineer.company/glossary/sql/gophermap","html":"https://engineer.company/glossary/sql/","json":"https://engineer.company/glossary/sql/index.json","markdown":"https://engineer.company/glossary/sql/index.md","pdf":"https://engineer.company/glossary/sql/index.pdf"},"kind":"page","language":"en","title":"What is SQL?","url":"https://engineer.company/glossary/sql/","version":1}