TC
code7 min read

SQL: The Language Every Database Speaks

Every app you use daily — your bank, your email, your social feed — stores and retrieves data using some form of SQL. Invented at IBM in the 1970s, Structured Query Language has outlived every programming trend for over 50 years. Understanding its core operations takes about 20 minutes. Mastering it takes a career.


The four core operations

SQL organises everything around four verbs, often called CRUD (Create, Read, Update, Delete):

-- Read data
SELECT name, email FROM users WHERE active = true;

-- Create data
INSERT INTO users (name, email) VALUES ('Jane', '[email protected]');

-- Update data
UPDATE users SET active = false WHERE last_login < '2025-01-01';

-- Delete data
DELETE FROM users WHERE id = 42;

SELECT is by far the most complex — it supports filtering, sorting, grouping, joining, and subqueries. The other three are straightforward once you understand WHERE clauses.


JOINs: combining tables

Real databases normalise data across multiple tables. A JOIN connects them:

Join typeReturnsWhen to use
INNER JOINOnly matching rowsYou need data that exists in both tables
LEFT JOINAll left rows + matching rightYou want all users, even those without orders
RIGHT JOINAll right rows + matching leftRarely used; same as LEFT JOIN with tables swapped
FULL JOINAll rows from both sidesYou need a complete picture of unmatched data

GROUP BY and aggregates

GROUP BY collapses rows into groups, and aggregate functions like COUNT, SUM, AVG, MIN, and MAX summarise each group:

SELECT department, COUNT(*) AS headcount, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
HAVING COUNT(*) > 5
ORDER BY avg_salary DESC;

Why formatting matters

In team codebases, SQL queries live in application code, migration files, and ad-hoc scripts. Compare these two versions of the same query:

-- Messy: hard to scan, easy to miss bugs
select u.name,o.total,o.created_at from users u join orders o on u.id=o.user_id where o.total>100 and u.active=true order by o.created_at desc limit 20;

-- Formatted: structure is visible at a glance
SELECT
  u.name,
  o.total,
  o.created_at
FROM users u
JOIN orders o ON u.id = o.user_id
WHERE o.total > 100
  AND u.active = true
ORDER BY o.created_at DESC
LIMIT 20;
Readable SQL catches bugs. When each clause sits on its own line, missing conditions, wrong joins, and accidental cross joins become visually obvious during code review.

SQL dialects

The SQL standard (ISO/IEC 9075) defines the language, but every database engine adds its own extensions:

  • PostgreSQL — the most standards-compliant, with rich JSON support, window functions, and CTEs
  • MySQL — popular for web apps, more forgiving with type coercion
  • SQLite — a single-file database embedded in mobile apps and browsers
  • SQL Server — Microsoft's enterprise offering with T-SQL extensions

NoSQL vs SQL

NoSQL databases (MongoDB, Redis, DynamoDB) trade structured schemas for flexibility. Use SQL when your data has clear relationships and you need transactional guarantees. Use NoSQL when your schema evolves rapidly, you need horizontal scaling, or your data is naturally document-shaped (JSON blobs, user profiles, event logs).

SQL is not a database — it's a language. Learning it once gives you access to dozens of database engines, from a 50KB SQLite file to a petabyte-scale data warehouse.

Try it yourself

Put what you learned into practice with our SQL Formatter.