loading...

March 29, 2026

SQL for Beginners: A Complete Foundation Guide

Most of the world’s data lives in databases — from hospital records to supply chain transactions. SQL is the language that unlocks all of it. This guide covers everything you need to go from zero to writing your first real queries, explained clearly with working code examples throughout.

What Is SQL and Why Does It Matter?

SQL — pronounced either “S-Q-L” or “sequel” — stands for Structured Query Language. It is the most widely used language for working with data stored in databases, and it has been for decades.

Here’s the simple version of how it works: you write a piece of code called a SQL query that asks a database for specific information. The database processes that request and sends back exactly what you asked for.

What makes SQL worth learning is how universally it shows up. Almost every organisation that deals with data — and that’s virtually all of them — stores it in a database that SQL can talk to. Customer transactions, inventory records, financial data, supply chain movements — it all lives in databases. SQL is what gets you to it.

Worth knowing

SQL is significantly easier to learn than general-purpose programming languages like Python or Java. It’s designed for one specific purpose — working with data — which keeps the learning curve manageable.

If you’ve seen job descriptions asking for SQL experience and wondered what it actually means in practice, the answer is straightforward: being able to write queries to pull, filter, sort, and summarise data without depending on anyone else to do it for you.

Understanding Databases and Tables

Before writing SQL, you need to understand what you’re querying. A database is a system for storing and managing data that lets you store data reliably, retrieve it efficiently, and manipulate it systematically.

The most common type is a relational database, where data is organised into tables. Each table represents a specific type of thing — customers, products, orders, and so on.

Rows and Columns

Tables work like this:

TABLE

Represents one type of entity — e.g. all products, all customers, all orders

COLUMN

One specific attribute — e.g. price, name, rating, date

ROW

One individual record — e.g. a single product, one customer, one transaction

CELL

The value where a specific row and column intersect — e.g. $29.99

An e-commerce company, for example, might have a products table, a customers table, an orders table, and an order_items table — each holding a different category of information, all linked together.

Databases vs Spreadsheets

If tables look familiar, it’s because they resemble spreadsheets. But databases are built for an entirely different scale and purpose:

Rule of thumb

Use spreadsheets for small datasets and personal analysis. Use databases — and SQL — when you need to handle millions of rows, support multiple simultaneous users, or power applications and automated reporting.

 
Writing Your First SQL Queries

Every SQL query starts with SELECT. It tells the database which data you want to retrieve.

Selecting all columns

The * wildcard means “give me everything” — all columns from the specified table.

				
					SELECT *
FROM products;
				
			

This returns every row and every column from the products table. Useful for exploration, but not always what you need.

Selecting specific columns

In practice, you rarely need everything. List the columns you want, separated by commas:

				
					SELECT id, name, rating
FROM products;
				
			

This returns only three columns: idname, and rating — in exactly that order. Specifying columns keeps your queries fast and your results easy to read.

Common mistake

SQL is precise. If you misspell a column name — writing ratings instead of rating, for example — you’ll get an error. Read error messages carefully; they usually tell you exactly what went wrong and often suggest a fix.

Breaking down the syntax

Here’s what each part of a basic query does:

SELECT

Specifies which columns to retrieve

*

Wildcard meaning “all columns”

FROM

Specifies which table to query

;

Marks the end of the SQL statement

One important thing to note: the result of any query is a temporary copy of the data. It isn’t stored anywhere automatically. If you need to keep it, you have to save it explicitly — to a file, a new table, or a reporting tool.

Sorting and Limiting Results

Retrieving data is only half the job. Often, you need it in a specific order, or you only want to see the top or bottom N records.

Sorting with ORDER BY

Use ORDER BY to sort results by any column. By default, SQL sorts in ascending order (lowest to highest):

				
					SELECT id, name, rating
FROM products
ORDER BY rating;
				
			

To flip the order — highest to lowest — add DESC:

				
					SELECT id, name, rating
FROM products
ORDER BY rating DESC;
				
			

Limiting results with LIMIT

Once sorted, use LIMIT to cap how many rows are returned. This is how you answer questions like “what are the top 10 highest-rated products?”

				
					SELECT id, name, rating
FROM products
ORDER BY rating DESC
LIMIT 10;
				
			
Best practice

Always use LIMIT when exploring data. Many cloud databases charge by the volume of data scanned — an unchecked query on a large table can be slow and expensive. Limiting your results is good hygiene even when you expect a small dataset.

The order of these keywords matters. SQL expects them in this sequence: SELECT → FROM → ORDER BY → LIMIT. Putting them in the wrong order will throw an error.

DISTINCT and Column Aliases

Removing duplicates with DISTINCT

When you want to know what unique values exist in a column, SELECT alone won’t give you a clean answer — it returns everything, duplicates included. If a product category like “Books” appears across 50 rows, you’ll see “Books” 50 times.

DISTINCT fixes this. Place it immediately after SELECT:

				
					SELECT DISTINCT category
FROM products;
				
			

The query returns each category exactly once — no repetition. This is useful any time you need to understand the range of values in a column: product categories, customer cities, order statuses, and so on.

Renaming columns with AS

Raw column names aren’t always meaningful to someone reading your results. The AS keyword lets you assign a descriptive alias to any column in your output:

				
					SELECT DISTINCT category AS unique_categories
FROM products;
				
			

The column header in your results will now show unique_categories instead of category. Importantly, this only affects the display — the actual column name in the database is unchanged.

Naming convention

Write aliases in snake_case — lowercase letters with underscores between words. So unique_categories, not UniqueCategories or uc. It’s the accepted convention and makes your code easier for others to read.

A note on SQL style

SQL keywords work in both uppercase and lowercase — select and SELECT do the same thing. The convention, however, is to write keywords in uppercase. It makes it immediately obvious which parts of a query are SQL commands versus table or column names, and it makes your code easier to scan quickly.

Working with PostgreSQL

SQL is a standard, but different database systems implement it with minor variations. The most common ones you’ll encounter are PostgreSQL, MySQL, SQL Server, and BigQuery. The fundamentals covered in this guide apply to all of them — the differences are mostly in edge cases and specific features.

I’ll be working primarily in PostgreSQL, which is one of the most powerful and widely used open-source database systems, particularly well suited for data analysis and reporting.

A few PostgreSQL specifics worth knowing

Limiting rows

PostgreSQL uses LIMIT 10. SQL Server uses SELECT TOP(10) instead.

Default port

PostgreSQL runs on port 5432 by default.

Case sensitivity

PostgreSQL is NOT case-sensitive for column names, table names, or keywords.

Popular clients

pgAdmin (free, official), DBeaver, TablePlus, DataGrip, psql (command line)

How it all connects

When you query a database in the real world, two systems are involved. The database server is the machine that stores and processes your data. The database client is the tool you use to write queries and see results — pgAdmin, DBeaver, or similar. Multiple people can connect to the same server at once, each running independent queries. This is called client-server architecture, and it’s how virtually all production databases work.

To connect to a PostgreSQL database at an organisation, you’ll need a server address, port number, database name, username, and password — typically provided by a database administrator or IT team.

Quick Reference Summary

Here’s every keyword covered in this guide, with a one-line description of what it does:

KeywordWhat it doesExample
SELECTSpecifies which columns to retrieveSELECT name, price
*Wildcard — selects all columnsSELECT *
FROMSpecifies which table to queryFROM products
ORDER BYSorts results by a column (ASC by default)ORDER BY rating DESC
DESCReverses sort order to highest → lowestORDER BY price DESC
LIMITCaps the number of rows returnedLIMIT 10
DISTINCTReturns only unique values, removes duplicatesSELECT DISTINCT category
ASAssigns a display alias to a columncategory AS unique_categories
Posted in Beginner, PostgreSQL, SQL