SQL (Structured Query Language) is the standard language for working with data in relational databases. You use it to ask questions of your data, such as “which customers live in California?”, and to add, change, or delete records. Almost every database, from PostgreSQL and MySQL to SQL Server and SQLite, understands it.
SQL is one of the most useful skills in technology. Data analysts use it to answer business questions, developers use it to store application data, and data engineers use it to move and transform data between systems. It has been around since the 1970s and is still everywhere, because the basic idea, storing data in tables and asking for exactly what you need, works.
This is the first part of the Learn SQL series. It explains what SQL and relational databases are, the main kinds of SQL commands, how to set up a free practice database, and how to run your first queries. Every part of the series uses the same small sample database, so you can follow along from start to finish.
What Is a Database?
A database is an organized collection of data that’s built to be stored, searched, and updated efficiently and safely, even by many people at once. A relational database organizes that data into tables that are related to each other.
Think of a table as a well-organized spreadsheet:
- A table holds one kind of thing, such as customers, products, or orders.
- Each row (also called a record) is one item: one customer, one product, one order.
- Each column (also called a field) is one piece of information about it, such as a name, price, or date. Every value in a column has the same data type, such as text, number, or date.
Here’s the customers table from the sample database used throughout this series:
customer_id | first_name | last_name | email | city | state | signup_date
-------------+------------+-----------+--------------------------+-----------+-------+-------------
1 | Maria | Lopez | [email protected] | El Centro | CA | 2025-01-15
2 | James | Carter | [email protected] | San Diego | CA | 2025-02-03
3 | Sofia | Nguyen | [email protected] | Phoenix | AZ | 2025-02-20
4 | Daniel | Brooks | | Yuma | AZ | 2025-03-08
5 | Ana | Martinez | [email protected] | El Centro | CA | 2025-04-12
6 | Kevin | Patel | [email protected] | Las Vegas | NV | 2025-05-30
7 | Laura | Chen | [email protected] | | NV | 2025-06-18
8 | Omar | Haddad | [email protected] | San Diego | CA | 2025-07-04
The blank email for Daniel and the blank city for Laura are NULL, which means “no value.” You’ll learn how to work with NULLs in Part 2.
Keys Connect the Tables
What makes a database relational is that tables point to each other:
- A primary key uniquely identifies each row. In
customers, it’scustomer_id: no two customers share one. - A foreign key is a column that refers to a primary key in another table. Each row in
ordershas acustomer_idthat points to the customer who placed it.
Here are Maria’s orders. The customer_id of 1 links each order to her row, and product_id links to the products table:
order_id | customer_id | product_id | quantity | order_date | status
----------+-------------+------------+----------+------------+---------
101 | 1 | 1 | 1 | 2025-08-02 | shipped
103 | 1 | 3 | 2 | 2025-08-11 | shipped
112 | 1 | 5 | 1 | 2025-11-02 | pending
Storing each customer once and referring to them by ID avoids repeating their name and email in every order, which keeps the data consistent. Database Design explains how to plan tables this way.
What Does SQL Look Like?
SQL reads almost like English. This query asks for the name and city of customers in California:
SELECT first_name, last_name, city
FROM customers
WHERE state = 'CA';
first_name | last_name | city
------------+-----------+-----------
Maria | Lopez | El Centro
James | Carter | San Diego
Ana | Martinez | El Centro
Omar | Haddad | San Diego
Notice what’s missing: you don’t tell the database how to find the rows, which files to open, or which loop to run. You describe what you want, and the database figures out the fastest way to get it. That’s why SQL is called a declarative language.
A few conventions you’ll see throughout the series:
- Keywords like
SELECT,FROM, andWHEREare written in capitals by convention, but SQL doesn’t require it;selectworks too. - Text values go in single quotes:
'CA'. Double quotes mean something different in most databases. - A semicolon ends each statement.
- Line breaks and spaces don’t matter, so put each clause on its own line to keep queries readable.
- Comments start with
--and are ignored by the database.
The Main Types of SQL Commands
SQL commands are often grouped into five families:
| Group | What it does | Main commands |
|---|---|---|
| Query (DQL) | Reads data | SELECT |
| Data manipulation (DML) | Adds, changes, and removes rows | INSERT, UPDATE, DELETE |
| Data definition (DDL) | Creates and changes the structure | CREATE, ALTER, DROP |
| Data control (DCL) | Manages permissions | GRANT, REVOKE |
| Transaction control (TCL) | Groups changes so they succeed or fail together | BEGIN, COMMIT, ROLLBACK |
Most of your time, especially as an analyst, goes into SELECT. This series starts there, then covers changing data and, later, designing and managing databases.
SQL Databases: One Language, Several Dialects
SQL is a standard, but each database adds its own extras and has small differences, called dialects. The most common:
| Database | Typical use | Cost |
|---|---|---|
| PostgreSQL | Web apps, analytics, general purpose | Free, open source |
| MySQL / MariaDB | Websites, including WordPress | Free, open source |
| SQL Server | Business systems on Microsoft platforms | Free editions; paid for production |
| SQLite | Mobile apps, desktop apps, small sites, learning | Free, public domain |
| Oracle | Large enterprise systems | Commercial |
Most of what you learn works the same in all of them: SELECT, WHERE, JOIN, GROUP BY, and so on. The differences are mostly in functions, data types, and a few keywords. This series shows PostgreSQL first and notes the others where they differ.
Set Up a Practice Database
Download the sample database file: shop.sql. It creates three small tables (customers, products, and orders) and fills them with data. It works in PostgreSQL, MySQL, SQLite, and SQL Server.
Pick whichever setup suits you:
SQLite (easiest, nothing to configure): install SQLite (it’s already on macOS and most Linux systems), then run these commands in a terminal from the folder where you saved the file:
sqlite3 shop.db < shop.sql sqlite3 shop.dbYou’ll see a
sqlite>prompt where you can type queries. See What Is SQLite? for a full introduction.PostgreSQL: install it, create a database, and load the file with
psql -d shop -f shop.sql. PostgreSQL for Beginners walks through the setup.MySQL: run MySQL in Docker and load the file, as shown in MySQL with Docker.
A graphical tool: DB Browser for SQLite, pgAdmin, DBeaver, and MySQL Workbench let you run the same SQL in a window instead of a terminal.
The file deletes and recreates the three tables each time you run it, so you can reload it whenever you want a fresh start.
Your First Queries
With the database loaded, try these.
See everything in a table. The * means “all columns”:
SELECT * FROM products;
product_id | name | category | price
------------+--------------+-------------+--------
1 | Laptop | Electronics | 899.00
2 | Monitor | Electronics | 249.00
3 | Keyboard | Accessories | 45.00
4 | Mouse | Accessories | 25.00
5 | Desk | Furniture | 320.00
6 | Office Chair | Furniture | 180.00
Count rows:
SELECT COUNT(*) AS customers FROM customers;
customers
-----------
8
Summarize by group, for example customers per state:
SELECT state, COUNT(*) AS customers
FROM customers
GROUP BY state
ORDER BY customers DESC, state;
state | customers
-------+-----------
CA | 4
AZ | 2
NV | 2
Combine tables. Which orders are still pending, and who placed them?
SELECT c.first_name, o.order_id, o.order_date
FROM orders o
JOIN customers c ON c.customer_id = o.customer_id
WHERE o.status = 'pending'
ORDER BY o.order_date;
first_name | order_id | order_date
------------+----------+------------
Daniel | 108 | 2025-09-22
Maria | 112 | 2025-11-02
Don’t worry if GROUP BY and JOIN look new; they each get their own part of this series. The point is how much you can answer with a few lines.
SQL vs. Excel
If you already use spreadsheets, SQL will feel familiar. The difference is scale and reliability:
| Excel | SQL database | |
|---|---|---|
| Size | Up to about a million rows per sheet, and slows down well before that | Millions or billions of rows |
| Many users at once | Limited | Built for it |
| Data rules | Easy to type anything anywhere | Enforced types, required fields, and keys |
| Repeatability | Steps are manual clicks | A query can be saved and rerun on fresh data |
| Best for | Quick analysis, small data, sharing with non-technical people | Storing data for applications, large datasets, shared sources of truth |
They work well together: many analysts pull data with SQL and present it in Excel or Power BI. The Excel for Data Analysis series covers the spreadsheet side.
Who Uses SQL?
- Data analysts query databases to answer questions and build reports. See What Is a Data Analyst?
- Developers store and retrieve application data, often through libraries like SQLAlchemy.
- Data engineers build pipelines that move and transform data. See What Does a Data Engineer Do?
- Business users run reports in tools that use SQL behind the scenes.
Tips for Learning SQL
- Practice on real questions. Instead of memorizing syntax, ask “which product sold the most?” and work out the query.
- Start small. Get a simple query working, then add one clause at a time.
- Read the error message. Most SQL errors point to the exact word or line that’s wrong.
- Check your results. Count rows and spot-check values; a query that runs isn’t necessarily correct.
- Be careful with changes.
SELECTis always safe.UPDATEandDELETEchange data permanently, so practice them on the sample database, not on real data.
Frequently Asked Questions
Is SQL a programming language?
It’s a query language rather than a general-purpose programming language. You use it to describe the data you want, not to build applications on its own. It’s often used alongside languages like Python, JavaScript, or PHP.
How do you pronounce SQL?
Both “S-Q-L” (ess-cue-el) and “sequel” are common and correct.
How long does it take to learn SQL?
You can learn the basics, SELECT, WHERE, ORDER BY, and simple aggregates, in a few days of practice. Becoming comfortable with joins, subqueries, and window functions usually takes a few weeks of regular use.
Which database should I learn first?
SQLite is the easiest to start with because there’s nothing to set up. PostgreSQL is a great next step and closely follows the SQL standard. Most of what you learn transfers to MySQL and SQL Server.
Is SQL still worth learning?
Yes. Relational databases still store most business data, and SQL remains one of the most requested skills for data analysts, data engineers, and back-end developers.
What’s the difference between SQL and MySQL?
SQL is the language. MySQL is one database system that uses it, like PostgreSQL, SQL Server, and SQLite.
Conclusion
SQL is the language for asking questions of relational databases, which store data in tables connected by keys. With a handful of keywords, SELECT, FROM, and WHERE, you can already find specific records, count them, and summarize them.
Load the sample database and run the queries above. Then continue with the next part of the series, SQL SELECT Basics: WHERE, ORDER BY, LIMIT, and DISTINCT, where you’ll learn to filter, sort, and shape query results.
