SQL Tutorial

This SQL tutorial teaches you how to talk to relational databases using SQL, the Structured Query Language. SQL is the standard language for storing, reading, changing and analysing data kept in tables, and it is used by almost every company that keeps data, from small web shops to banks.

You do not need any previous programming experience. Every chapter explains one idea in plain English and gives you short examples that you can edit and run right here with the "Try it Yourself" button.

What is SQL?

A relational database stores data in tables. A table looks like a spreadsheet: it has named columns (such as City or Price) and rows, where each row is one record (one customer, one product, one order).

SQL is the language you use to ask the database questions and give it instructions. Instead of describing how to find the data step by step, you describe what you want, and the database engine works out the fastest way to get it. That is why SQL is called a declarative language.

Your First SQL Query

The query below asks the database for every row and every column in the Customers table. The star (*) means "all columns". Press "Try it Yourself", change the query, and run it again.

SELECT * FROM Customers;
CustomerID | CustomerName   | ContactName  | City       | Country
-----------+----------------+--------------+------------+---------
1          | Alpha Traders  | Ali Raza     | Lahore     | Pakistan
2          | Blue Sky Foods | Sara Khan    | Karachi    | Pakistan
3          | Cedar Market   | John Smith   | London     | UK
4          | Delta Supplies | Maria Garcia | Madrid     | Spain
5          | Evergreen Co   | Ahmed Ali    | Dubai      | UAE
6          | Fresh Mart     | Emma Brown   | Manchester | UK
7          | Golden Grains  | Usman Tariq  | Islamabad  | Pakistan
8          | Harbor Goods   | Liam Wilson  | New York   | USA
9          | Indigo Retail  | Olivia Davis | Chicago    | USA
10         | Jade Imports   | NULL         | Toronto    | Canada
(10 rows)

The Sample Database

All examples in this SQL tutorial use a small shop database with three tables. Every time you run an example you get a fresh copy, so you can update or delete rows without breaking anything.

  • Customers: CustomerID, CustomerName, ContactName, City, Country (10 rows)
  • Products: ProductID, ProductName, Category, Price, Stock (8 rows)
  • Orders: OrderID, CustomerID, ProductID, Quantity, OrderDate (10 rows)

Why Learn SQL?

  • It is one of the most requested skills for developers, data analysts and testers
  • The same core SQL works in MySQL, PostgreSQL, SQL Server, Oracle and SQLite
  • It lets you answer business questions in seconds, such as "which product sells most?"
  • Back-end frameworks like Laravel, Django and Node.js ORMs all generate SQL under the hood

What This SQL Tutorial Covers

We start with reading data: SELECT, WHERE, ORDER BY and LIMIT. Then we filter with AND, OR, IN, BETWEEN and LIKE, handle NULL values, and summarise data with COUNT, SUM, AVG, GROUP BY and HAVING. After that you will combine tables with JOINs, write subqueries, and finally create and change tables with CREATE TABLE, INSERT, UPDATE, DELETE, constraints, indexes and views.

The live examples run on SQLite. Where MySQL or SQL Server use different syntax, the text points it out so your knowledge carries over to any database.

SELECT ProductName, Price
FROM Products
ORDER BY Price DESC;
ProductName    | Price
---------------+------
Coffee Beans   | 22
Olive Oil      | 18.75
Almonds        | 15
Basmati Rice   | 12.5
Honey          | 11.5
Wheat Flour    | 8
Green Tea      | 6
Dark Chocolate | 4.25
(8 rows)

Frequently asked questions

Is SQL hard to learn for beginners?

No. Basic SQL reads almost like English, and most people can write useful SELECT queries after a day or two. Advanced topics such as joins and window functions take more practice.

Is SQL a programming language?

SQL is a domain-specific, declarative language for working with relational data. It is not a general-purpose language like Python, but it is a real language with its own syntax, and most vendor versions are even Turing complete.

Which database should I learn SQL on?

Any of them. SQLite, MySQL and PostgreSQL are free and share the same core syntax. Learn standard SQL first, then pick up the small differences of the database your job uses.

How long does it take to learn SQL?

You can learn the essentials covered in this tutorial in one to two weeks of regular practice. Becoming confident with complex reporting queries usually takes a few months of real-world use.