SQL Tutorial
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.
- 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
- Getting started: what SQL is, install MySQL, and meet the sample database used in every lesson.
- Query data: SELECT, WHERE, ORDER BY, LIKE, NULL and CASE.
- Change data: INSERT, UPDATE, DELETE and transactions.
- Summarise: aggregate functions, GROUP BY, HAVING and the string, number and date functions.
- Combine tables: joins, UNION, subqueries and CTEs.
- Advanced analysis: window functions, RANK and ROW_NUMBER, running totals and top N per group.
- Design and performance: constraints, normalization, indexes, EXPLAIN, views, procedures and triggers.
- Security: users and roles, GRANT and SQL injection.
- 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:
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;| dept_name | staff | top_salary |
|---|---|---|
| Engineering | 5 | 125000 |
| Sales | 3 | 105000 |
| Marketing | 2 | 88000 |
| HR | 1 | 78000 |
- The join pairs each employee with their department.
GROUP BYmakes one row per department, andCOUNTandMAXsummarise each group.ORDER BYputs 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
| Project | You build | Key skills |
|---|---|---|
| E-commerce Sales Analysis | revenue KPIs, trends, top products, RFM, cohorts | joins, CTEs, LAG, NTILE, pivots |
| Library Management System | availability, overdue loans, fines, issue-book procedure | LEFT JOIN, date functions, procedures |
| HR & Payroll Analytics | headcount, attrition, current pay, raises, org chart | latest-row pattern, window functions, recursive CTE |
| Banking Transactions | reconciliation, statements, fraud rules, safe transfers | ACID transactions, locking, triggers, window frames |
| QA Test Results Tracker | pass rates, regressions, flaky tests, defect metrics | LAG, 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.
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.