SQL

One language for (almost) every database.

What is it?

SQL (Structured Query Language) is the language you use to talk to relational databases - MySQL, PostgreSQL, and SQLite all speak it, with only minor dialect differences. Data lives in tables (think spreadsheets: rows of records, columns of fields), and SQL is how you ask questions of it: "give me every order from this customer, newest first."

It's one of the best-value skills in software: learn it once and it transfers to virtually every database, every language, and every job - it has been the standard since the 1970s and isn't going anywhere.


When to use it

Whenever your app stores structured data - users, posts, orders, anything with consistent fields and relationships between records. That's the vast majority of applications. The main alternative, document databases like Firebase, trade SQL's strict structure and powerful queries for flexibility and real-time sync.


Core concepts

  • Tables, rows, columns - a users table has one row per user and columns like id, name, email. Each column has a type.
  • The big four statements - SELECT reads data, INSERT adds rows, UPDATE changes them, DELETE removes them. Most daily work is these four.
  • WHERE, ORDER BY, LIMIT - filter which rows you get, sort them, and cap how many come back.
  • Primary and foreign keys - every row gets a unique id; other tables reference it (orders.user_id) to model relationships.
  • JOINs - combine related tables in one query: every order together with the name of the user who placed it.

A taste of the code

-- Every order over $50, with the customer's name,
-- newest first
SELECT users.name, orders.total, orders.created_at
FROM orders
JOIN users ON users.id = orders.user_id
WHERE orders.total > 50
ORDER BY orders.created_at DESC
LIMIT 10;

SQL is declarative: you describe what you want, and the database figures out how to fetch it efficiently.


Your first steps

  1. Start with SQLite - it's a real SQL database with zero setup, perfect for practicing.
  2. Create a table and practice the big four: INSERT a few rows, SELECT them back, UPDATE one, DELETE one.
  3. Add WHERE, ORDER BY, and aggregate functions (COUNT, SUM, GROUP BY) to your queries.
  4. Model a two-table relationship - users and their posts - and write your first JOIN.
  5. Use it from code: connect from Python, PHP, or Node.js, always with prepared statements (never paste user input into a query string).

Common beginner mistakes

  • UPDATE or DELETE without WHERE. It changes every row. Run the same condition as a SELECT first, and use a transaction when you can.
  • Comparing with = NULL. Nothing equals NULL, not even NULL. Use IS NULL and IS NOT NULL.
  • Building queries by joining strings with user input. Use parameterised queries in your application code - always.
  • SELECT * in application code. Name the columns you need; it's clearer, faster and doesn't break when the table changes.
  • Selecting columns that aren't grouped. With GROUP BY, every selected column must be grouped or aggregated (COUNT, SUM, MAX...).

Where to practise

The fastest way to practise is SQLite: it needs no server, and tools like DB Browser for SQLite let you run queries against a file on your own computer. Browser-based SQL playgrounds also let you experiment without installing anything.

When you want a "real" database server, free tiers from Neon or Supabase give you hosted PostgreSQL with a SQL editor in the browser. On shared hosting, phpMyAdmin gives you a SQL tab for MySQL.


← Back to Stack Picker All primers Compare databases