SQL
Structured Query Language for managing relational databases. Tables, rows, columns, and powerful joins to query related data.
What is SQL?
In short
SQL (Structured Query Language) is the standard language for storing, reading, and changing data in relational databases, where data lives in tables made of rows and columns. You write declarative statements like SELECT, INSERT, UPDATE, and DELETE that describe the result you want, and the database engine figures out how to fetch it.
What SQL Actually Is
SQL is a language, not a database. The same language works across PostgreSQL, MySQL, Microsoft SQL Server, Oracle, and SQLite, with small dialect differences between them. It was created at IBM in the 1970s based on Edgar Codd's relational model and became an ANSI standard in 1986.
Data in a SQL database is organized into tables. A table for users might have columns like id, email, and created_at, and each row is one user. Relationships between tables are expressed with keys: a primary key uniquely identifies a row, and a foreign key in one table points at the primary key of another. An orders table holding a user_id foreign key is how you connect an order back to the customer who placed it.
SQL is declarative. You say what you want, not how to get it. When you write SELECT name FROM users WHERE country = 'India', you never specify whether to scan the whole table or use an index. The query planner inside the database decides that for you based on statistics and available indexes.
How a Query Runs Under the Hood
When you send a query, the engine parses it, checks that the tables and columns exist, and hands it to the query planner. The planner generates several possible execution plans, estimates the cost of each using stored statistics about table sizes and value distributions, and picks the cheapest one. You can see this plan yourself by prefixing a query with EXPLAIN.
The biggest performance lever is the index. Without one, finding rows where email = 'a@b.com' means reading every row in the table, a full table scan. With a B-tree index on the email column, the engine walks a sorted tree structure and finds the match in roughly log(n) steps. On a table with 10 million rows that is the difference between reading 10 million rows and reading around 24.
Joins are how SQL combines rows from multiple tables. A query that pulls each order along with its customer name joins the orders and users tables on user_id. Internally the engine may use a nested loop, a hash join, or a merge join depending on table sizes and indexes. Most SQL databases also wrap groups of statements in transactions that give ACID guarantees, so a money transfer that debits one account and credits another either fully commits or fully rolls back.
When To Use It and the Trade-offs
Reach for SQL when your data has a clear structure, relationships matter, and correctness is non-negotiable. Banking ledgers, e-commerce orders, inventory, and user accounts are the classic fit because transactions and constraints stop the data from drifting into invalid states. A UNIQUE constraint on email guarantees you never get two accounts on the same address, enforced by the database itself rather than hoped for in application code.
The trade-off is schema rigidity and horizontal scaling. You define columns and types up front, and changing them on a huge live table can be slow. Relational databases also scale up (bigger machine) more easily than they scale out (many machines), which is why high-write workloads sometimes move to NoSQL stores. That said, modern Postgres comfortably handles tens of thousands of transactions per second on a single node, so most applications never hit that wall.
A common mistake is fearing joins. Joins are what relational databases are built and optimized for. Avoiding them by duplicating data across tables usually creates more bugs than it prevents, because now the same fact lives in two places and can disagree.
Where it is used in production
PostgreSQL
Open-source SQL database used by Instagram, Reddit, and Twitch to store core relational data with strong transactional guarantees.
MySQL
Powers the relational layer at Facebook, YouTube, and Booking.com, often sharded across many servers for scale.
Amazon Aurora
AWS managed database speaking the MySQL and PostgreSQL SQL dialects, with storage that scales automatically under the hood.
SQLite
An embedded SQL engine that ships inside every Android phone, iPhone, and web browser to store local app data in a single file.
Frequently asked questions
- Is SQL a programming language?
- It is a query language, not a general-purpose programming language. It is declarative and built for working with data in relational databases. You can't write a full application in plain SQL, though procedural extensions like PL/pgSQL add loops and variables.
- What is the difference between SQL and MySQL?
- SQL is the language. MySQL is a specific database product that you talk to using SQL. PostgreSQL, SQL Server, and Oracle are other products that all speak SQL with minor dialect differences.
- When should I use NoSQL instead of SQL?
- Choose NoSQL when your data is unstructured or schema-less, when you need to scale writes across many servers, or when you don't need multi-row transactions. Choose SQL when relationships and correctness matter, such as financial or order data.
- Why are my SQL queries slow?
- The most common cause is a missing index, forcing a full table scan. Run EXPLAIN on the query to see the plan, and add an index on the columns used in WHERE and JOIN conditions. Avoid SELECT star on wide tables and watch for queries inside loops.
- What does ACID mean in SQL databases?
- ACID is four transaction guarantees: Atomicity (all changes succeed or none do), Consistency (the database moves only between valid states), Isolation (concurrent transactions don't corrupt each other), and Durability (committed changes survive a crash).
Learn SQL hands-on
This page explains the idea. The full lesson lets you step through the ring as servers join and leave, read the implementation, and check yourself with a quiz. It is one of 760+ lessons in the System Design Masterclass, from your first API call to distributed consensus. Eleven Foundation lessons are free, no signup. Lifetime access is ₹499 in India or $7.99 worldwide, one payment, no subscription.
Related lessons
Lessons that touch on SQL as part of a larger topic.
NewSQL Databases
Distributed SQL databases that promise the scalability of NoSQL with the ACID guarantees of traditional SQL. CockroachDB, Google Spanner, and the NewSQL movement
intermediate · database types storage
SQL vs NoSQL
Database model comparison, relational vs non-relational data stores
foundation · core fundamentals
Apache Ignite
Distributed cache meets SQL engine, when you need to query your cache, not just look up keys
foundation · caching strategies
Parameterized Queries
The complete defense against SQL injection, separating SQL structure from user data
intermediate · security architecture
Prepared Statements
Pre-compiled SQL templates that combine security with performance, parse once, execute many times
intermediate · security architecture
See also
Related glossary terms you might want to look up next.
NoSQL
Databases that don't use traditional table-based relational models. Includes document stores, key-value, graph, and column-family databases.
Database
An organized collection of data that can be easily accessed, managed, and updated. The backbone of almost every application.
ACID
Four guarantees for database transactions: Atomicity (all or nothing), Consistency (valid states only), Isolation (no interference), Durability (changes persist).
BASE
An alternative to ACID for distributed systems: Basically Available, Soft state, Eventually consistent. Trades strong consistency for availability.
Sharding
Splitting a database into smaller pieces (shards) distributed across multiple servers. Each shard holds a subset of the data.
Replication
Keeping copies of the same data on multiple servers. Improves read performance and provides fault tolerance if one server goes down.