Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Start with one small relational database and the loop CREATE TABLE → INSERT → SELECT. SQL queries read sets of related facts; statements such as INSERT, UPDATE and DELETE change them. The examples below use broadly familiar SQL, with dialect differences called out where they matter.

1. Choose a practice database

SQLite is the lowest-friction starting point. Install SQLite and run sqlite3 test.db in a terminal, or use SQLite’s browser-based fiddle for short experiments without a local installation. PostgreSQL’s introductory tutorial is a good next step when you need a server database; it assumes no particular Unix or programming background and covers tables, queries, joins, aggregates, updates and deletes.

SQL is standardized, but each database engine adds its own syntax and features. Treat every example as either portable SQL or label it for SQLite, PostgreSQL, Access or another specific engine before using it in production.

2. The SQL statement families

Family Purpose Common statements
DDL (data definition) Defines database objects CREATE TABLE
DML (data manipulation) Adds, changes or removes rows INSERT, UPDATE, DELETE
Query Reads rows without changing the database SELECT

A SELECT statement is read-only: it returns a result set and does not modify stored rows. Write statements require extra care, especially when a missing WHERE clause could affect every row.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

3. Create a table

CREATE TABLE defines columns and constraints. This example is portable across common teaching setups, although exact data types and constraint behavior can vary by engine.

CREATE TABLE customers (
  customer_id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  email TEXT UNIQUE
);
  • customer_id identifies each customer and is the primary key.
  • NOT NULL requires a value for name.
  • UNIQUE prevents duplicate non-null email values in engines that implement this constraint normally.

In SQLite, constraints are checked when rows are inserted or updated. A database can also have foreign keys, indexes and other objects; add those deliberately as your data model grows.

4. Insert rows

INSERT INTO customers (name, email)
VALUES ('Ada Lovelace', '[email protected]');

Name the columns explicitly. In SQLite, columns omitted from the column list receive their declared default, or NULL when no default exists. SQLite also supports inserting the result of a query:

INSERT INTO customers (name, email)
SELECT full_name, contact_email
FROM imported_customers;

Insert multiple rows with additional value tuples where your engine supports that standard form:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
INSERT INTO customers (name, email)
VALUES
  ('Grace Hopper', '[email protected]'),
  ('Katherine Johnson', '[email protected]');

5. Read data with SELECT

Read a table by assigning each clause a clear job:

Clause Job
SELECT Chooses output columns or expressions
FROM Chooses the source table or tables
WHERE Filters individual rows before grouping
ORDER BY Sorts the returned rows
SELECT customer_id, name
FROM customers
WHERE name LIKE 'A%'
ORDER BY name ASC;

This returns customer IDs and names beginning with “A”, sorted alphabetically. Avoid SELECT * in application queries when you know the required columns; an explicit column list is easier to review and remains stable when a table gains new columns.

Remove duplicates and limit results

SELECT DISTINCT email
FROM customers
ORDER BY email
LIMIT 20;

DISTINCT removes duplicate result values. LIMIT is common in SQLite and PostgreSQL but is dialect-sensitive: some systems use TOP or FETCH FIRST instead. Add a deterministic ORDER BY whenever which rows appear first matters.

6. Relate tables with JOIN

A join matches rows using a relationship, commonly a foreign key to another table’s primary key.

SELECT o.order_id, c.name
FROM orders AS o
JOIN customers AS c
  ON c.customer_id = o.customer_id;

JOIN without a modifier means INNER JOIN: only rows with a match on both sides are returned. A LEFT JOIN keeps every row from the left table and supplies nulls for columns with no matching right-hand row.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Join Rows returned Typical use
INNER JOIN Only matching rows Orders that have a known customer
LEFT JOIN All left-table rows, plus matches Every customer, including customers with no orders

Always write the join predicate. Omitting ON (or intentionally using a cross join) can multiply rows dramatically. When comparing join approaches, check readability, expected result cardinality, null handling, portability and the database’s execution plan.

7. Summarize rows with aggregates

Aggregate functions turn many rows into a summary, often one row per group.

SELECT customer_id, COUNT(*) AS order_count
FROM orders
GROUP BY customer_id
HAVING COUNT(*) >= 2;
  • WHERE decides which individual rows enter the grouping.
  • GROUP BY defines the groups.
  • HAVING filters the completed groups.

Common aggregate functions include COUNT, SUM, AVG, MIN and MAX. Be explicit about null behavior: for example, COUNT(column) ignores null values, while COUNT(*) counts rows.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

8. Update rows safely

Preview the target rows with a matching SELECT, then run the update with its deliberate predicate.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
SELECT customer_id, email
FROM customers
WHERE customer_id = 1;

UPDATE customers
SET email = '[email protected]'
WHERE customer_id = 1;

Without WHERE, the UPDATE changes every row. Verify the affected-row count and use a transaction when your engine supports transactional updates and you need an easy rollback.

9. Delete rows safely

SELECT customer_id, name
FROM customers
WHERE customer_id = 1;

DELETE FROM customers
WHERE customer_id = 1;

Again, omitting WHERE targets every row. Check the preview and affected-row count before committing. Foreign-key rules may reject a delete or require related rows to be handled first, depending on the database schema.

10. A repeatable beginner workflow

  1. Open SQLite with sqlite3 test.db, or use a browser fiddle.
  2. Create a small table with a primary key and the constraints you need.
  3. Insert two or three known rows.
  4. Run a simple SELECT, then add WHERE, ORDER BY, DISTINCT and a row limit one at a time.
  5. Create a second table and practice an INNER JOIN and a LEFT JOIN.
  6. Use GROUP BY with an aggregate, then compare WHERE and HAVING.
  7. Before every UPDATE or DELETE, run the equivalent SELECT, confirm the target set and use a transaction if appropriate.

11. Dialect and portability checks

  • SQLite, PostgreSQL and Microsoft Access do not expose exactly the same syntax or features.
  • LIMIT is not universal; SQL Server commonly uses TOP, while standard-style alternatives include FETCH FIRST.
  • PostgreSQL-specific features such as RETURNING should be labeled as PostgreSQL rather than presented as universal SQL.
  • SQLite documents some behavior as SQLite-specific, including implementation details around joins and other features.
  • Microsoft Access uses square brackets for identifiers containing spaces, such as [Order Date]; avoid spaces in new identifiers when you want easier portability.

Keep the target engine visible beside examples in your notes or code review. A query that is valid in one dialect may need different pagination, identifier quoting, data types or functions in another.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.