Skip to content

SQL Basics

The SQL language: statements, clauses, data types, and how to read and write queries.

What you'll learn

  • What SQL is and where it comes from
  • The main categories of SQL statements: DDL, DML, DQL, DCL
  • Common data types: INTEGER, TEXT, DATE, BOOLEAN, DECIMAL
  • How to write your first SELECT, INSERT, UPDATE, and DELETE

Concept

What Is SQL?

SQL (Structured Query Language) is the standard language for interacting with relational databases. It was developed in the 1970s at IBM and has been standardized by ANSI/ISO. Despite minor dialect differences between PostgreSQL, MySQL, and SQLite, the core syntax is the same across all of them — learn it once and it transfers everywhere.

Statement Categories

SQL statements fall into four groups:

  • DDL (Data Definition Language) — CREATE, ALTER, DROP — defines the schema
  • DML (Data Manipulation Language) — INSERT, UPDATE, DELETE — modifies data
  • DQL (Data Query Language) — SELECT — reads data
  • DCL (Data Control Language) — GRANT, REVOKE — manages permissions

Common Data Types

| Type | Example | Use case | |------|---------|----------| | INTEGER / SERIAL | 42 | Whole numbers, IDs | | VARCHAR(n) / TEXT | 'hello' | Strings | | DECIMAL(p,s) | 19.99 | Money, precise decimals | | DATE / TIMESTAMP | 2025-01-15 | Dates and times | | BOOLEAN | TRUE | True/false flags |

Your First Statements

A SELECT reads rows, an INSERT adds a row, an UPDATE changes existing rows, and a DELETE removes rows. Every SQL statement ends with a semicolon. Keywords are conventionally written in uppercase for readability, though SQL is case-insensitive for keywords.

Example

              -- DQL: read all columns from the products table
SELECT * FROM products;

-- DML: insert a new product
INSERT INTO products (name, price, in_stock)
VALUES ('Wireless Mouse', 29.99, TRUE);

-- DML: update a product's price
UPDATE products SET price = 24.99 WHERE name = 'Wireless Mouse';

-- DML: delete a product
DELETE FROM products WHERE name = 'Wireless Mouse';
            

These four statements cover the core of day-to-day SQL. Always use a WHERE clause with UPDATE and DELETE to avoid affecting every row.

Try it

  • SQL Formatter

    Paste SQL to auto-format and validate syntax.

  • SQL Escaper

    Escape special characters in SQL string literals.

Common mistakes

The mistake

Running UPDATE or DELETE without a WHERE clause

The fix

Without WHERE, every row is affected. Always include a WHERE clause, and test with SELECT first to preview affected rows.

The mistake

Forgetting the semicolon at the end of a statement

The fix

Some tools require it, some do not. Always end statements with a semicolon to avoid ambiguity, especially in scripts with multiple statements.

Practice SQL Basics

5 exercises

Practice SQL Basics

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors