Home › SQL Tutorial

SQL Tutorial

Beginner to advanced5 min read1 queries run on MySQL 8.4

Learn SQL from your first SELECT to window functions, recursive CTEs, transactions and query tuning. Every lesson shows the sample table a query reads, the result it returns and a step-by-step explanation, and every query was run on a real MySQL 8.4 server. Most examples also have a Try it button that runs them in your browser, with nothing to install.

In this lesson
  • 69 lessons in 10 topics, from basic to advanced
  • 5 real-world SQL projects for your portfolio
  • Quizzes, exercises with solutions and interview-style FAQs
  • Written for developers, testers and data analysts

Start here

Your learning path
  1. Getting started: what SQL is, install MySQL, and meet the sample database used in every lesson.
  2. Query data: SELECT, WHERE, ORDER BY, LIKE, NULL and CASE.
  3. Change data: INSERT, UPDATE, DELETE and transactions.
  4. Summarise: aggregate functions, GROUP BY, HAVING and the string, number and date functions.
  5. Combine tables: joins, UNION, subqueries and CTEs.
  6. Advanced analysis: window functions, RANK and ROW_NUMBER, running totals and top N per group.
  7. Design and performance: constraints, normalization, indexes, EXPLAIN, views, procedures and triggers.
  8. Security: users and roles, GRANT and SQL injection.
  9. Projects: put it all together in five complete case studies.

Try a query now

This query reads the employees and departments tables of the sample database. Press Try it, change it, and run it again:

SQL
SELECT d.dept_name, COUNT(*) AS staff, MAX(e.salary) AS top_salaryFROM employees eJOIN departments d ON d.dept_id = e.dept_idGROUP BY d.dept_nameORDER BY staff DESC;
ResultMySQL 8.4
dept_namestafftop_salary
Engineering5125000
Sales3105000
Marketing288000
HR178000
4 rows in set
How this query works, step by step
  1. The join pairs each employee with their department.
  2. GROUP BY makes one row per department, and COUNT and MAX summarise each group.
  3. ORDER BY puts the largest department first.

All lessons

Getting Started

Querying Data

Modifying Data

Functions & Grouping

Joins & Set Operators

Subqueries & CTEs

Window Functions

Database Design

Performance & Objects

Security

SQL Projects

Five SQL projects

ProjectYou buildKey skills
E-commerce Sales Analysisrevenue KPIs, trends, top products, RFM, cohortsjoins, CTEs, LAG, NTILE, pivots
Library Management Systemavailability, overdue loans, fines, issue-book procedureLEFT JOIN, date functions, procedures
HR & Payroll Analyticsheadcount, attrition, current pay, raises, org chartlatest-row pattern, window functions, recursive CTE
Banking Transactionsreconciliation, statements, fraud rules, safe transfersACID transactions, locking, triggers, window frames
QA Test Results Trackerpass rates, regressions, flaky tests, defect metricsLAG, conditional aggregation, pivots

How this SQL tutorial works

  • Real output. Every result table on these pages was produced by running the query on MySQL 8.4 LTS, not typed by hand.
  • Same data everywhere. One small, realistic company database is used throughout, so you always know what the tables contain.
  • Step by step. Each important query has a numbered explanation of what the database does, in the order it does it.
  • Practice built in. Predict-the-output questions, exercises with hints and solutions, and a quiz in every lesson.
  • Try it. Examples marked SQL run in your browser on SQLite with the same data. Examples marked MySQL use MySQL-only features, so copy them into your own MySQL server.
Tip

Preparing for interviews? After the window functions and joins lessons, practise with the 150+ SQL scenario-based interview questions.

SQL tutorial FAQ

Is this SQL tutorial free?

Yes. All lessons, exercises, quizzes and projects are free, and you can run most examples in your browser without signing up or installing anything.

Which database does this tutorial use?

MySQL 8.4 LTS. The core SQL (SELECT, joins, GROUP BY, subqueries, window functions) works almost the same in PostgreSQL, SQL Server, Oracle and SQLite, and each lesson points out MySQL-specific syntax.

How long does it take to learn SQL?

Most beginners can write useful queries after the first two topics, in a few days. Working through all lessons and the five projects takes about four to six weeks at an hour a day.

Can I learn SQL without coding experience?

Yes. SQL reads close to English and this tutorial starts from zero, with every query explained step by step.

Is SQL useful for software testers?

Very. Testers use SQL to prepare test data, verify what an application stored, check constraints and analyse test results. The QA Test Results Tracker project is written for testers.