SQL Basics: Query Any Database in 30 Minutes
The handful of SQL commands that handle most real work — explained without a computer science degree.
SQL is the language used to ask questions of data stored in a database. It has been standard for decades, it is readable, and a small subset of it covers the overwhelming majority of everyday tasks.
The mental model
Picture a table like a spreadsheet: rows are records, columns are fields. A customers table might have columns id, name, country and signup_date, with one row per customer.
SQL is how you select rows, choose columns, summarise groups and combine tables. That is essentially all of it.
Reading data
The core statement is SELECT.
SELECT name, country
FROM customers;
That returns two columns for every row. To narrow it down, add a condition with WHERE:
SELECT name
FROM customers
WHERE country = 'Egypt';
Useful conditions include =, <>, >, <, BETWEEN, LIKE (for text patterns) and IN (for a list of values). Combine them with AND, OR and NOT.
Sorting and limiting
SELECT name, signup_date
FROM customers
ORDER BY signup_date DESC
LIMIT 10;
ORDER BY sorts, DESC reverses it, and LIMIT returns only the first rows. This pattern — newest ten, top hundred — answers a surprising number of real questions.
Summarising
Aggregate functions collapse many rows into one number:
SELECT country, COUNT(*) AS customers
FROM customers
GROUP BY country
ORDER BY customers DESC;
GROUP BY defines the groups, and the aggregate — COUNT, SUM, AVG, MIN, MAX — produces a value for each. To filter the results rather than the rows, use HAVING:
SELECT country, COUNT(*) AS customers
FROM customers
GROUP BY country
HAVING COUNT(*) > 100;
WHERE filters rows before grouping; HAVING filters groups after.Combining tables
Real data is spread across tables. JOIN brings them together:
SELECT c.name, o.total
FROM customers c
JOIN orders o ON o.customer_id = c.id;
A plain JOIN (an inner join) keeps only rows that match on both sides. A LEFT JOIN keeps every row from the first table, filling in blanks where there is no match — useful for finding customers with no orders.
Changing data
Three statements alter tables, and all three deserve respect:
INSERT INTO table (columns) VALUES (...)adds rows.UPDATE table SET column = value WHERE conditionchanges rows.DELETE FROM table WHERE conditionremoves rows.
⚠️ Always write the WHERE clause first. An UPDATE or DELETE without one affects every row in the table, and there is no undo.
Habits that prevent disasters
- Select before you modify. Run the same conditions as a
SELECTand check what you are about to affect. - Use transactions. Wrap changes in
BEGINandCOMMIT, or roll back withROLLBACK, so a partial change cannot survive. - Work on a copy first when you are unsure.
- Read the numbers. If an update reports far more rows than expected, stop and investigate.
The bottom line
SELECT, WHERE, ORDER BY, LIMIT, GROUP BY, HAVING and JOIN are enough to answer most business questions. Learn those seven ideas and you can query almost any database.