SQL for beginners starts with five commands, SELECT, WHERE, ORDER BY, GROUP BY and JOIN, and you can learn them in a couple of weeks by practising on a small sample database. SQL, short for Structured Query Language, is how you ask questions of data stored in tables. Data analysts, testers, developers and business teams all use it daily. This step-by-step tutorial builds one small table, then walks you through the queries you will actually use at work, with examples you can run for free.
Key takeaways
- SQL is used to read, filter, sort, summarise and combine data stored in relational database tables.
- You can practise without installing anything by using a free online SQL editor, or install SQLite, MySQL or PostgreSQL locally.
- Learn queries in this order: SELECT, WHERE, ORDER BY, aggregate functions, GROUP BY with HAVING, then JOINs.
- Always test UPDATE and DELETE with a matching SELECT first, because a missing WHERE clause changes every row.
- Practise on real-looking datasets such as sales or student marks, since interview tasks use the same patterns.
What is SQL and why learn it?
A relational database stores data in tables made of rows and columns, much like a spreadsheet. SQL is the standard language for working with those tables. Popular databases such as MySQL, PostgreSQL, SQL Server, Oracle and SQLite all use SQL, with small differences in extra features.
For a data analyst, SQL is usually the first technical skill checked in interviews, ahead of Power BI or Python. If you are wondering whether it even counts as programming, read is SQL a programming language.
Step 1: Set up a place to practise
You have three easy options:
- Online editor: search for a free online SQL playground. Nothing to install, ideal for your first week.
- SQLite: a tiny file-based database. Tools such as DB Browser for SQLite give you a simple window to run queries.
- MySQL or PostgreSQL: full database servers used in companies. Install one when you are comfortable with the basics.
The examples below use standard SQL that works in all of them.
Step 2: Create a table and add data
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer VARCHAR(50),
city VARCHAR(30),
product VARCHAR(30),
quantity INT,
price DECIMAL(10,2),
order_date DATE
);
INSERT INTO orders VALUES
(1, 'Asha', 'Pune', 'Laptop', 1, 55000, '2026-01-05'),
(2, 'Rahul', 'Mumbai', 'Mouse', 3, 500, '2026-01-06'),
(3, 'Asha', 'Pune', 'Keyboard', 1, 1500, '2026-01-10'),
(4, 'Imran', 'Bengaluru', 'Laptop', 2, 52000, '2026-02-02'),
(5, 'Priya', 'Mumbai', 'Monitor', 1, 12000, '2026-02-14'),
(6, 'Rahul', 'Mumbai', 'Laptop', 1, 58000, '2026-03-01');
CREATE TABLE defines the columns and their data types. INSERT INTO adds rows. The PRIMARY KEY makes sure every order_id is unique.
Step 3: Read data with SELECT
SELECT * FROM orders;
SELECT customer, product, price FROM orders;
The star returns every column. In real work, list only the columns you need, because it is faster and clearer.
Step 4: Filter rows with WHERE
SELECT * FROM orders WHERE city = 'Mumbai';
SELECT * FROM orders WHERE price > 10000 AND city <> 'Pune';
SELECT * FROM orders WHERE product IN ('Mouse', 'Keyboard');
SELECT * FROM orders WHERE customer LIKE 'A%';
SELECT * FROM orders WHERE order_date BETWEEN '2026-01-01' AND '2026-01-31';
Operators you will use most
- = and <> for equal and not equal
- >, <, >= and <= for comparisons
- AND, OR and NOT to combine conditions
- IN for a list of values, BETWEEN for a range, LIKE for patterns
- IS NULL and IS NOT NULL for missing values
Step 5: Sort and limit results
SELECT customer, price FROM orders ORDER BY price DESC;
SELECT customer, price FROM orders ORDER BY price DESC LIMIT 3;
ORDER BY sorts ascending by default; add DESC for descending. LIMIT works in MySQL, PostgreSQL and SQLite. SQL Server uses SELECT TOP 3 instead.
Step 6: Summarise with aggregate functions
SELECT COUNT(*) AS total_orders,
SUM(quantity * price) AS revenue,
AVG(price) AS avg_price,
MIN(price) AS cheapest,
MAX(price) AS costliest
FROM orders;
Aggregate functions collapse many rows into one value. AS gives the result column a readable name, called an alias.
Step 7: Group data with GROUP BY and HAVING
SELECT city, SUM(quantity * price) AS revenue
FROM orders
GROUP BY city
ORDER BY revenue DESC;
SELECT customer, COUNT(*) AS orders_placed
FROM orders
GROUP BY customer
HAVING COUNT(*) > 1;
GROUP BY makes one row per group. WHERE filters rows before grouping; HAVING filters groups after grouping. This difference is one of the most common interview questions.
Step 8: Combine tables with JOIN
Real databases split data across tables. Add a customers table:
CREATE TABLE customers (
customer VARCHAR(50) PRIMARY KEY,
segment VARCHAR(20)
);
INSERT INTO customers VALUES
('Asha', 'Student'), ('Rahul', 'Business'),
('Imran', 'Business'), ('Meena', 'Student');
-- Only customers who have orders
SELECT o.order_id, o.customer, c.segment, o.product
FROM orders o
INNER JOIN customers c ON o.customer = c.customer;
-- Every customer, even without orders
SELECT c.customer, c.segment, o.product
FROM customers c
LEFT JOIN orders o ON o.customer = c.customer;
Types of JOIN
| JOIN | Returns |
|---|---|
| INNER JOIN | Only rows that match in both tables |
| LEFT JOIN | All rows from the left table, plus matches from the right |
| RIGHT JOIN | All rows from the right table, plus matches from the left |
| FULL OUTER JOIN | All rows from both tables, matched where possible |
In the LEFT JOIN example, Meena appears with an empty product because she has no orders, and Priya is missing because she is not in the customers table.
Step 9: Change data safely
-- Check first
SELECT * FROM orders WHERE order_id = 2;
UPDATE orders SET quantity = 4 WHERE order_id = 2;
DELETE FROM orders WHERE order_id = 5;
Always run the SELECT with the same WHERE clause before an UPDATE or DELETE. Without WHERE, every row in the table is changed or removed.
Order of a SQL query
You write clauses in this order: SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT. The database logically processes them as FROM and JOIN, then WHERE, GROUP BY, HAVING, SELECT, ORDER BY and LIMIT. Knowing this explains why you cannot use a SELECT alias inside WHERE in most databases.
A two-week practice plan
- Days 1 to 3: SELECT, WHERE, ORDER BY and LIMIT on one table.
- Days 4 to 6: aggregate functions, GROUP BY and HAVING.
- Days 7 to 10: JOINs across two or three tables.
- Days 11 to 14: subqueries, CASE WHEN and a first look at window functions such as ROW_NUMBER.
After that, move to interview practice with our SQL interview questions for data analysts, and see where SQL fits in the data analyst roadmap.
Frequently asked questions
Can I learn SQL without a coding background?
Yes. SQL reads close to plain English and you only need a few commands to be useful, so many analysts from commerce and arts backgrounds learn it first.
How long does it take to learn SQL for beginners?
With an hour of daily practice, most beginners are comfortable with SELECT, GROUP BY and JOINs in two to four weeks. Advanced topics take longer.
Which database should I use to learn SQL?
Start with SQLite or an online editor, then move to MySQL or PostgreSQL. The core SQL you learn works across all of them.
Is SQL enough to get a data analyst job?
SQL is essential but usually not enough on its own. Combine it with Excel, a BI tool such as Power BI and basic statistics.
Find more guides in our data analytics category.
