NEWFree courses in AI, data, cloud and cybersecurity — start learning today
Data Science

SQL for Beginners: The Essential Commands Every Data Analyst Needs

Learn SQL from scratch with clear examples: SELECT, WHERE, ORDER BY, GROUP BY, HAVING, aggregate functions and JOINs, plus how to practise effectively.

SQL for Beginners — illustration
Table of contents
  1. What is SQL?
  2. Our sample data
  3. 1. SELECT: choose the columns you want
  4. 2. WHERE: filter rows
  5. 3. ORDER BY and LIMIT: sort and shorten results
  6. 4. Aggregate functions: COUNT, SUM, AVG, MIN, MAX
  7. 5. GROUP BY and HAVING: summarise by category
  8. 6. JOIN: combine related tables
  9. How SQL actually runs a query
  10. Beyond the basics
  11. How to practise SQL
  12. Conclusion
  13. Frequently asked questions

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.

TableColumns
customersid, name, country, signup_date
ordersid, 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 VisualizationBeginner · 6 weeks · 4–6 hours/week 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 typeWhat it returns
INNER JOINOnly rows that have a match in both tables
LEFT JOINAll rows from the left table, plus matches from the right (or NULL)
RIGHT JOINAll rows from the right table, plus matches from the left
FULL OUTER JOINAll 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:

  1. FROM and JOIN — gather the tables
  2. WHERE — filter rows
  3. GROUP BY — form groups
  4. HAVING — filter groups
  5. SELECT — choose and calculate columns
  6. ORDER BY — sort
  7. LIMIT — restrict the number of rows

Beyond the basics

Once you are comfortable with the commands above, learn these next:

  • Subqueries and CTEs (WITH clauses) 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, UPDATE and DELETE — 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.

ST
SmartTechWorld Editorial Team

Our editorial team writes practical, beginner-friendly guides to help people learn technology skills and build careers in tech. Last updated November 6, 2026.