Skip to content

SQL Tutorial for Beginners: SELECT, JOIN, GROUP BY Explained Simply

SQL Tutorial for Beginners: Why SQL Still Matters

Whether you’re working through an SQL tutorial for beginners as a student or stepping into a data analyst role, SQL is the language that lets you query and analyze relational databases. Nearly every company stores structured data in a database system, and anyone who wants to make sense of that data needs SQL. In this article, you’ll learn the three most important SQL commands – SELECT, JOIN, and GROUP BY – through hands-on examples, no prior experience required.

SQL stands for Structured Query Language and was developed in the 1970s. Today it is the industry standard for communicating with relational databases such as MySQL, PostgreSQL, and SQLite. The core concepts are consistent across systems – once you understand them, you can apply them anywhere. For a broader foundation, check out our guide on SQL Basics.

Entwickler schreibt SQL-Abfragen auf einem Laptop / Developer writing SQL queries on a laptop

SELECT: Retrieving Data from a Table

The SELECT statement is the cornerstone of every SQL query. It tells the database exactly which columns you want to retrieve. Think of it like a search filter on an enormous spreadsheet – you specify what you need, and the database delivers only that.

Here’s a simple example: imagine a table called customers with the columns name, city, and age. To retrieve every customer’s name and city, you would write:

SELECT name, city FROM customers;

If you want to see all columns at once, use the asterisk wildcard: SELECT * FROM customers;. You can narrow down results with the WHERE clause – for example, to find only customers from Chicago: SELECT * FROM customers WHERE city = 'Chicago';. This way you pull exactly the data you actually need.

Tabellarische Daten mit Zeilen und Spalten als Veranschaulichung einer Datenbanktabelle

Handy Additions to SELECT

  • ORDER BY: Sorts your results, e.g. by age ascending (ORDER BY age ASC)
  • LIMIT: Caps the number of rows returned (LIMIT 10)
  • DISTINCT: Returns each unique value only once, removing duplicates (SELECT DISTINCT city FROM customers)

SQL Tutorial for Beginners: JOIN – Connecting Two Tables

In the real world, data is rarely stored in a single table. Related information is typically spread across multiple tables – a design principle called normalization. To query that data together, you use the JOIN command.

Suppose you have a customers table and a separate orders table with the columns customer_id, product, and amount. An INNER JOIN links both tables through the shared customer ID and returns only rows that have a matching entry in both tables:

SELECT customers.name, orders.product, orders.amount FROM customers INNER JOIN orders ON customers.id = orders.customer_id;

The result shows you each customer paired with their ordered product and amount. Beyond INNER JOIN, there are several other join types that return different sets of data. If you want to explore how databases fit into a broader analytical workflow, take a look at our Introduction to Data Science.

The Most Common JOIN Types at a Glance

  • INNER JOIN: Returns only rows that have a match in both tables
  • LEFT JOIN: Returns all rows from the left table, even when there is no match in the right table
  • RIGHT JOIN: Returns all rows from the right table, regardless of the left
  • FULL OUTER JOIN: Combines LEFT and RIGHT JOIN – all rows from both tables are included
Zwei Puzzleteile verbinden sich – Metapher für SQL JOIN im SQL Tutorial / Two puzzle pieces connecting – metaphor for SQL JOIN and SQL tutorial

GROUP BY: Grouping and Aggregating Data

GROUP BY is especially powerful when you want to summarize and analyze data. It groups all rows that share the same value in a given column and lets you apply aggregate functions – calculations that operate across multiple rows at once.

Let’s say you want to know the total revenue generated by each customer. You can use the SUM() function together with GROUP BY:

SELECT customer_id, SUM(amount) AS total_revenue FROM orders GROUP BY customer_id;

The result is a clean table where each customer appears exactly once, alongside their total revenue. Combine GROUP BY with HAVING to filter the grouped results further. HAVING works just like WHERE, but it applies after the grouping step: HAVING SUM(amount) > 500 shows only customers whose total exceeds 500.

Commonly Used Aggregate Functions

  • COUNT(): Counts the number of rows in a group
  • SUM(): Adds up all values in a column
  • AVG(): Calculates the average value
  • MIN() / MAX(): Returns the smallest or largest value

Putting It All Together: Building a Complex SQL Query

The real power of SQL emerges when you combine these commands. Suppose you want to see all customers from New York, ranked by their total spending, but only those who have ordered more than $1,000 worth of products.

SELECT customers.name, SUM(orders.amount) AS total_revenue FROM customers INNER JOIN orders ON customers.id = orders.customer_id WHERE customers.city = 'New York' GROUP BY customers.name HAVING SUM(orders.amount) > 1000 ORDER BY total_revenue DESC;

This single query weaves together JOIN, WHERE, GROUP BY, HAVING, and ORDER BY into a powerful analysis. If you’d like to take your data skills further, our Python Tutorial is a great next step – Python and SQL pair exceptionally well in practice. For background on the language itself, Wikipedia’s SQL article is a solid reference.

Conclusion: Why Learning SQL Is Worth Your Time

This SQL tutorial for beginners has walked you through SELECT, JOIN, and GROUP BY – the three commands that form the backbone of almost every database analysis. The more you practice them, the more naturally they’ll come.

The best way to learn SQL is to work with real data as often as possible. Free browser-based tools like DB Fiddle or SQLiteOnline let you run queries instantly without any setup. Ready to go further? Explore our articles on Machine Learning Basics – because the path from raw database data to a working AI application always starts with clean, well-structured queries.

Image
Niklas Lang

I have been working as a machine learning engineer and software developer since 2020 and am passionate about the world of data, algorithms and software development. In addition to my work in the field, I teach at several German universities, including the IU International University of Applied Sciences and the Baden-Württemberg Cooperative State University, in the fields of data science, mathematics and business analytics.

My goal is to present complex topics such as statistics and machine learning in a way that makes them not only understandable, but also exciting and tangible. I combine practical experience from industry with sound theoretical foundations to prepare my students in the best possible way for the challenges of the data world.

Cookie Consent with Real Cookie Banner