Key takeaways
- SQL is the standard language for querying and managing data in relational databases.
- Most analysis uses a small set of commands: SELECT, WHERE, ORDER BY, GROUP BY, JOIN.
- Learning SQL is one of the fastest ways to become useful in a data role.
- Practise on real datasets — writing queries is a skill you build by doing.
If you want to work with data, SQL is the first skill to learn. Data analysts use it every day, data scientists use it to pull training data, developers use it to build applications, and many marketers and product managers use it to answer their own questions without waiting for someone else.
This SQL tutorial for beginners covers the essential commands with simple examples. By the end, you will be able to read and write the kinds of queries used in real analysis work.
What is SQL?
SQL (Structured Query Language) is a language for storing, retrieving and managing data in relational databases. A relational database organises data into tables made of rows and columns — similar to spreadsheets, but designed to handle much larger amounts of data reliably and to link related tables together.
Popular databases that use SQL include PostgreSQL, MySQL, SQLite, Microsoft SQL Server and cloud data warehouses. Each has small differences in syntax, but the core commands below work almost everywhere.
Our sample data
The examples use two simple tables: customers and orders.
| Table | Columns |
|---|---|
| customers | id, name, country, signup_date |
| orders | id, customer_id, amount, order_date |
1. SELECT: choose the columns you want
SELECT retrieves data from a table. List the columns you want, or use * for all columns.
SELECT name, country
FROM customers; Good habit: select only the columns you need instead of SELECT *. Your queries will be faster and easier to read.
2. WHERE: filter rows
WHERE keeps only the rows that match a condition. Combine conditions with AND and OR.
SELECT name, signup_date
FROM customers
WHERE country = 'Vietnam'
AND signup_date >= '2026-01-01'; Other useful filters include IN ('Vietnam', 'Japan'), BETWEEN 10 AND 50, LIKE 'A%' for text patterns and IS NULL for missing values.
3. ORDER BY and LIMIT: sort and shorten results
SELECT id, amount
FROM orders
ORDER BY amount DESC
LIMIT 10; This returns the ten largest orders. Use ASC for ascending order (the default) and DESC for descending.
4. Aggregate functions: COUNT, SUM, AVG, MIN, MAX
Aggregate functions summarise many rows into a single value.
SELECT COUNT(*) AS total_orders,
SUM(amount) AS revenue,
AVG(amount) AS average_order
FROM orders; The AS keyword gives a column a readable name, called an alias.
5. GROUP BY and HAVING: summarise by category
GROUP BY splits rows into groups and calculates an aggregate for each group. HAVING filters the groups after aggregation.
SELECT customer_id,
COUNT(*) AS orders,
SUM(amount) AS total_spent
FROM orders
GROUP BY customer_id
HAVING SUM(amount) > 500
ORDER BY total_spent DESC; Remember the difference: WHERE filters rows before grouping; HAVING filters groups after grouping.
Recommended free courseData Analysis with Python, Pandas and Visualization View course 6. JOIN: combine related tables
Real databases split information across tables to avoid duplication. JOIN brings it back together using a shared column — here, orders.customer_id matches customers.id.
SELECT c.name, c.country, o.amount, o.order_date
FROM orders AS o
JOIN customers AS c
ON o.customer_id = c.id; | Join type | What it returns |
|---|---|
| INNER JOIN | Only rows that have a match in both tables |
| LEFT JOIN | All rows from the left table, plus matches from the right (or NULL) |
| RIGHT JOIN | All rows from the right table, plus matches from the left |
| FULL OUTER JOIN | All rows from both tables, matched where possible |
For example, a LEFT JOIN from customers to orders finds customers who have never ordered — their order columns will be NULL.
How SQL actually runs a query
SQL is written in one order but logically processed in another. Knowing this explains many beginner errors:
FROMandJOIN— gather the tablesWHERE— filter rowsGROUP BY— form groupsHAVING— filter groupsSELECT— choose and calculate columnsORDER BY— sortLIMIT— restrict the number of rows
Beyond the basics
Once you are comfortable with the commands above, learn these next:
- Subqueries and CTEs (
WITHclauses) to break complex queries into readable steps. - CASE expressions for conditional logic inside queries.
- Window functions such as
ROW_NUMBER()and running totals. - Date functions for monthly and weekly reporting.
- Data modification with
INSERT,UPDATEandDELETE— carefully!
How to practise SQL
- Install SQLite or PostgreSQL locally, or use a free browser-based SQL playground.
- Download a public dataset and write ten questions you want to answer about it.
- Recreate reports you already know from work or spreadsheets using SQL.
- Combine SQL with Python and pandas for deeper analysis and charts.
Planning a data career? Read data analyst vs data scientist to see how SQL fits into each role.
Conclusion
SQL is one of the most practical skills in tech. With just a handful of commands — SELECT, WHERE, ORDER BY, GROUP BY and JOIN — you can answer most everyday business questions directly from a database. Practise regularly on real data, and SQL will quickly become one of your most valuable tools.
Frequently asked questions
How long does it take to learn SQL?
Most beginners can learn the core SQL commands in two to four weeks of regular practice. Becoming fluent with joins, subqueries and window functions takes a few months of real-world use.
Is SQL a programming language?
SQL is a domain-specific language designed for working with data in relational databases. It is not a general-purpose language like Python, but it is one of the most widely used languages in tech.
Which SQL database should beginners use?
SQLite is the easiest to start with because it needs no server, while PostgreSQL is a popular, powerful choice for learning skills used in professional environments.
Do data analysts need SQL?
Yes. SQL is one of the most important skills for data analysts because most business data is stored in databases and data warehouses.



