20% off any Coddy plan with codecodingforweb20

20% off any Coddy plan with code20% off any plan · codecodingforweb20How to use it
energy 30/30

SQL Fundamentals for Beginners: The Queries You Actually Need

SELECT, JOINs, GROUP BY, CTEs and safe updates — the SQL you actually need, with simple examples you can copy and run.

SQL has been around for decades and still sits underneath almost every app, report and dashboard you use. Frameworks and AI tools often write queries for you, but someone has to read them, check them and fix them when the numbers look wrong. That's why SQL is one of the most useful skills a beginner can pick up — and one of the fastest to learn.

All examples below use two simple tables: customers (id, name, country) and orders (id, customer_id, total, created_at).

What is SQL used for?

  • Reading data from databases like PostgreSQL, MySQL and SQLite.
  • Reports and analytics — totals, averages, trends by month or country.
  • Backend development — every web app stores its data somewhere, and ORMs generate SQL under the hood.
  • Data and AI work — preparing and cleaning data before it goes into charts or models.

How do you read data with SELECT?

Every query starts with three words: which columns, from which table, under which condition.

sql
SELECT name, country
FROM customers
WHERE country = 'Germany'
ORDER BY name;

It reads almost like a sentence: select these columns from this table where this is true, sorted by name. Use LIMIT 10 at the end when you only want a quick look at the first rows.

How do JOINs combine tables?

Real data is split across tables. A JOIN connects them through a shared column — here, orders.customer_id points to customers.id.

sql
SELECT c.name, o.total, o.created_at
FROM orders o
JOIN customers c ON c.id = o.customer_id
WHERE o.total > 100;
JOIN typeWhat it returns
INNER JOINOnly rows that match in both tables
LEFT JOINAll rows from the left table, plus matches from the right
RIGHT JOINAll rows from the right table, plus matches from the left
FULL JOINAll rows from both tables, matched where possible

How do you summarise data with GROUP BY?

Aggregate functions — COUNT, SUM, AVG, MIN, MAX — turn many rows into one summary per group.

sql
SELECT c.country,
       COUNT(*)     AS orders,
       SUM(o.total) AS revenue
FROM orders o
JOIN customers c ON c.id = o.customer_id
GROUP BY c.country
HAVING COUNT(*) > 5
ORDER BY revenue DESC;

WHERE filters rows before grouping; HAVING filters the groups after. If you know JavaScript, GROUP BY works a lot like reduce() — it collapses rows into summaries.

How do CTEs make long queries readable?

A CTE (common table expression) lets you name a step and use it like a table. Long queries become a series of small, readable parts.

sql
WITH customer_totals AS (
  SELECT customer_id, SUM(total) AS spent
  FROM orders
  GROUP BY customer_id
)
SELECT c.name, t.spent
FROM customer_totals t
JOIN customers c ON c.id = t.customer_id
WHERE t.spent > (SELECT AVG(spent) FROM customer_totals);

This finds customers who spent more than the average customer — in two clear steps instead of one tangled query.

How do you change data safely?

Reading is half the job. These three statements write data:

sql
INSERT INTO customers (name, country)
VALUES ('Ana Silva', 'Portugal');

UPDATE customers
SET country = 'Spain'
WHERE id = 42;

DELETE FROM orders
WHERE id = 1001;

Why do indexes matter?

Without an index, the database reads every row to find what you asked for. An index works like the index at the back of a book.

sql
CREATE INDEX idx_orders_customer ON orders (customer_id);

Index the columns you filter and join on most often. You don't need to worry about indexes in your first weeks, but it helps to know why a query on a large table is slow.

How do you keep queries secure?

Never build SQL by pasting user input into a string — that's how SQL injection happens. Use parameterised queries, where the database receives the value separately from the query:

python
# Unsafe: user input becomes part of the SQL
cursor.execute(f"SELECT * FROM customers WHERE name = '{name}'")

# Safe: the value is passed as a parameter
cursor.execute("SELECT * FROM customers WHERE name = %s", (name,))

How to practise SQL as a beginner

Start with SELECT and WHERE on a small dataset, then add one concept at a time: sorting, joins, grouping, CTEs. Write every query yourself and predict the result before you run it.

Coddy has an interactive SQL course where you write real queries in the browser and get instant feedback, with nothing to install. It's free to start, with a daily energy limit on the free plan. SQL pairs especially well with Python if you're heading towards data work — see our Python roadmap.

Common questions

Is SQL still worth learning when AI can write queries? Yes. AI writes queries quickly, but you still need to read them, spot a wrong join and understand why a report shows the wrong numbers. That requires knowing SQL. Our guide to learning with AI as a tutor shows how to use it without skipping the thinking.

Which database should a beginner practise on? SQLite or PostgreSQL. SQLite needs no setup at all, and almost everything you learn transfers to other databases.

How long does it take to learn SQL basics? About a week of focused practice covers SELECT, WHERE, JOIN, GROUP BY and basic aggregates — enough for most everyday reporting.

Should I learn SQL or Python first? If your goal is data analysis, SQL first is a fast win, then Python to automate and visualise. Our guide on which language to learn first compares the options.

Keep reading

Found this useful? Share it

Related Articles

20% off any Coddy plan

Enter codingforweb20 at checkout on the Coddy website for 20% off Plus, Pro and Lifetime, monthly or yearly. The code does not work in the iOS or Android app.

codingforweb20Code checked on 18 October 2026 · How to use it
Start free — no card →