What is SQL?
A practical introduction to SQL — what it is, how queries are structured, the most important commands, and how to read and write basic queries.
What is SQL?
SQL (Structured Query Language, pronounced "sequel" or "S-Q-L") is the standard language for interacting with relational databases. It was developed at IBM in the 1970s based on Edgar Codd's relational model, standardized by ANSI in 1986, and remains the dominant way to store and query structured data today.
Virtually every major database — PostgreSQL, MySQL, SQLite, SQL Server, Oracle, BigQuery, Snowflake — speaks SQL. The core syntax is the same across all of them, with dialect differences mainly in functions, data types, and advanced features.
SQL is a declarative language: you describe what data you want, not how to fetch it. The database's query planner figures out the most efficient execution path.
Tables, rows, and columns
Data in a relational database lives in tables — think of a spreadsheet where each column has a fixed name and data type, and each row is one record. A real database has many tables, and they're connected through shared IDs called foreign keys.
-- users table id | name | email | created_at ----+---------------+----------------------+------------ 1 | Ada Lovelace | ada@example.com | 2024-01-05 2 | Alan Turing | alan@example.com | 2024-01-12 -- orders table id | user_id | amount | status ----+---------+--------+--------- 101 | 1 | 49.99 | shipped 102 | 2 | 12.00 | pending
The four core operations (CRUD)
SQL maps to Create, Read, Update, Delete — the four operations every data system needs:
SELECT — read data
-- Get all columns from users SELECT * FROM users; -- Get specific columns with a filter SELECT name, email FROM users WHERE id = 1; -- Sort and limit SELECT name, created_at FROM users ORDER BY created_at DESC LIMIT 10;
INSERT — add rows
INSERT INTO users (name, email, created_at)
VALUES ('Grace Hopper', 'grace@example.com', NOW());UPDATE — modify rows
-- Always include a WHERE clause — without it, every row is updated UPDATE users SET email = 'ada.lovelace@example.com' WHERE id = 1;
DELETE — remove rows
-- Always include WHERE — without it, every row is deleted DELETE FROM users WHERE id = 2;
JOINs — combining tables
The most powerful feature of SQL is the JOIN — combining rows from two tables based on a matching column. There are four types:
-- Get each order with the customer's name SELECT users.name, orders.amount, orders.status FROM orders INNER JOIN users ON orders.user_id = users.id; -- name | amount | status -- ---------------+--------+--------- -- Ada Lovelace | 49.99 | shipped -- Alan Turing | 12.00 | pending
Aggregates and GROUP BY
Aggregate functions compute a single value from multiple rows. Combined with GROUP BY, they let you summarize data by category:
-- Common aggregate functions SELECT COUNT(*) AS total_orders, SUM(amount) AS total_revenue, AVG(amount) AS avg_order_value, MAX(amount) AS largest_order, MIN(amount) AS smallest_order FROM orders; -- Group orders by status SELECT status, COUNT(*) AS count, SUM(amount) AS revenue FROM orders GROUP BY status;
Frequently asked questions
- Is SQL a programming language?
- SQL is a domain-specific language, not a general-purpose programming language. It's declarative — you describe what data you want, and the database figures out how to retrieve it. Some databases extend SQL with procedural logic (PL/pgSQL in PostgreSQL, T-SQL in SQL Server), but standard SQL itself has no loops or conditionals.
- What's the difference between SQL and NoSQL?
- SQL databases store data in tables with a fixed schema and use joins to relate data. NoSQL databases (MongoDB, DynamoDB, Redis, Cassandra) use flexible document, key-value, or graph models instead. SQL excels at structured relational data with complex queries. NoSQL trades query flexibility for horizontal scaling and schema flexibility.
- What does NULL mean in SQL?
- NULL represents the absence of a value — it's not zero, not an empty string, not false. NULL is "unknown." This has surprising implications: NULL = NULL is false (use IS NULL instead). Any arithmetic with NULL returns NULL. Aggregate functions like COUNT(*) include NULLs, but SUM() and AVG() ignore them.
- What is an index, and do I need one?
- An index is a data structure that speeds up lookups on a column at the cost of extra storage and slower writes. Without an index, a database scans every row to find matches (a "full table scan"). Add indexes on columns you filter by frequently (WHERE, JOIN, ORDER BY). Primary keys are always indexed automatically.