For developers
SQL basics for beginners: SELECT, WHERE, JOIN and GROUP BY
SQL is how you ask a database questions. Learn tables, the six clauses behind most queries, how joins work and the beginner mistakes that return wrong answers.

SQL (Structured Query Language) is the standard language for asking questions of relational databases: which customers ordered last month, how many users signed up per country, which products are low on stock. Learning SQL basics is useful well beyond programming, for analysts, marketers and anyone who works with data.
This beginner guide explains how relational data is organised, the clauses behind most queries, how joins and grouping work, and the mistakes that most often produce wrong results.
How is data organised in a relational database?
A relational database stores data in tables, which look like spreadsheets with strict rules. Each table describes one kind of thing, such as customers or orders. Each row is one record, and each column is one attribute with a fixed type, such as text, integer or date.
Tables are linked by keys. A primary key, such as customer_id, uniquely identifies each row in its table. A foreign key is a column in another table, such as orders.customer_id, that points to that primary key. Splitting data into linked tables avoids repeating the same customer details on every order.
SQL works across many database systems, including well-known open-source ones such as PostgreSQL, MySQL, MariaDB and SQLite. Each adds its own extras, but the core language covered here is shared. SQLite runs from a single file and is an easy place to practise.
What are the building blocks of a SELECT query?
Most of the SQL you write will be SELECT queries that read data. A query is made of clauses, and while you write them in one order, the database logically processes them in a slightly different one.
For example: SELECT name, city FROM customers WHERE country = 'Canada' ORDER BY name LIMIT 10; returns the first ten Canadian customers alphabetically. Keywords are not case sensitive, but writing them in capitals makes queries easier to read.
- SELECT: which columns to return (or calculations on them).
- FROM: which table, or tables, to read.
- WHERE: which rows to keep, before any grouping.
- GROUP BY: which rows to combine into groups for totals or counts.
- HAVING: which groups to keep, after grouping.
- ORDER BY and LIMIT: how to sort the result and how many rows to return.
SQL rarely crashes on a logic mistake; it simply returns a wrong answer that looks plausible.
How do you filter rows with WHERE?
WHERE takes a condition and keeps only the rows where it is true. You can compare with =, <>, <, >, <= and >=, combine conditions with AND and OR, and use parentheses to make the order clear. IN checks a list of values, BETWEEN checks a range, and LIKE matches text patterns, where % stands for any run of characters.
Missing values are stored as NULL, which means 'unknown', and they behave differently. A comparison such as email = NULL is never true. Use IS NULL or IS NOT NULL instead. This trips up nearly every beginner at least once.
How do JOINs work?
A join combines rows from two tables using a matching column. To list each order with the customer's name, you join orders to customers on the customer ID: SELECT o.id, c.name FROM orders o JOIN customers c ON c.id = o.customer_id; The short letters are aliases that save typing.
The type of join decides what happens to rows that have no match.
- INNER JOIN (or plain JOIN): only rows with a match in both tables.
- LEFT JOIN: every row from the left table, with NULLs where the right table has no match. Useful for 'customers with no orders'.
- RIGHT JOIN: the mirror image of LEFT JOIN; most people rewrite these as LEFT JOINs.
- FULL OUTER JOIN: every row from both sides, matched where possible.
How do you count and total with GROUP BY?
Aggregate functions summarise many rows into one value: COUNT, SUM, AVG, MIN and MAX. On their own, they summarise the whole table. With GROUP BY, they summarise each group. SELECT country, COUNT(*) FROM customers GROUP BY country; returns one row per country with its customer count.
Every column in the SELECT list must either be in the GROUP BY or be inside an aggregate function. To filter on an aggregate, such as countries with more than 100 customers, use HAVING COUNT(*) > 100, not WHERE, because WHERE runs before the groups exist.
What mistakes do beginners make with SQL?
SQL rarely crashes on a logic mistake; it simply returns a wrong answer that looks plausible. These are the most common causes, and checking for them becomes a habit.
- Forgetting a WHERE clause on UPDATE or DELETE, which changes every row. Write the WHERE first and test it with a SELECT.
- Joining on the wrong column, or missing a join condition, which multiplies rows.
- Using = NULL instead of IS NULL.
- Counting rows after a join that duplicated them; check with COUNT(DISTINCT id).
- Building queries by pasting user input into SQL strings, which allows SQL injection. Use parameterised queries instead.
- Using SELECT * in application code, which breaks when columns change.
How should you practise SQL?
Install SQLite or run a local open-source database, load a small sample dataset, and ask yourself real questions about it: the busiest month, the top five products, customers who never ordered. Write each answer as a query and check the result makes sense by looking at a few rows by hand.
When queries get long, build them step by step. Start with FROM and a JOIN, check the rows, then add WHERE, then GROUP BY. Common table expressions, written with WITH, let you name intermediate steps and keep complex queries readable.
Frequently asked questions
How long does it take to learn SQL basics?
+
Most people can write useful SELECT, WHERE, JOIN and GROUP BY queries after a few days of practice. Window functions, indexing and query tuning take longer and come with real projects.
Is SQL a programming language?
+
SQL is a declarative query language: you describe the result you want and the database decides how to get it. It is not usually used to build whole applications, but it is a core skill for developers and analysts.
What is the difference between SQL and MySQL?
+
SQL is the language. MySQL is one database system that uses SQL, along with others such as PostgreSQL and SQLite. Each system has small differences in syntax and features.
What is the difference between WHERE and HAVING?
+
WHERE filters individual rows before grouping. HAVING filters groups after GROUP BY has run, so it can use aggregate results such as COUNT or SUM.
What is SQL injection?
+
SQL injection is an attack where user input is inserted into a query as code, letting an attacker read or change data. Prevent it by using parameterised queries or prepared statements, never by gluing strings together.
Is SQL still worth learning?
+
Yes. Relational databases have been central to software for decades and remain so. SQL skills carry over between systems, jobs and tools, including many data analysis tools that accept SQL.
Load a small dataset into SQLite and write ten queries about it this week. Once SELECT, WHERE, JOIN and GROUP BY feel natural, most everyday data questions become a few lines of SQL.
How we research: Guides are written from established sources and checked for accuracy. They are general information, not professional advice.
Written by
Cursorlane Editorial Team
Software & tools
Our writers are developers and heavy software users who test what they write about and update guides when the tools change.


