SQL Basics Tutorial for Developers

SQL basics tutorial hero graphic with SQL query window, database icon and result table

SQL Basics Tutorial for Developers

This SQL basics tutorial focuses on the core skills developers use daily: creating tables, inserting data, querying with SELECT, updating rows, deleting safely and joining tables. We will also touch on indexes and EXPLAIN so you can keep queries fast as your data grows.

In this SQL tutorial you will:

  • Understand how SQL fits into a typical app or analytics stack
  • Create a database and tables for a small demo schema
  • Write basic SELECT, INSERT, UPDATE and DELETE statements
  • Join tables and aggregate data with GROUP BY
  • Add simple indexes and inspect query plans with EXPLAIN

For deeper database work you can pair this article with the
MariaDB tutorial for developers,
Linux basics for developers,
PHP backend basics tutorial,
Python data engineering tutorial,
and your e-commerce stack in the
Adobe Commerce Magento 2 tutorial.


1. SQL basics tutorial: where SQL lives in your stack

SQL is the language relational databases use to define, query and manipulate data. Whether you are running MariaDB, MySQL, PostgreSQL or a cloud database, the same core SQL ideas apply:

  • Tables store rows of structured data.
  • Columns define the types and meaning of each field.
  • Primary keys uniquely identify rows.
  • Foreign keys link related tables together.

In most applications, your code (PHP, Python, Node, Java) talks to SQL through a driver or ORM, but understanding the raw language makes debugging and performance tuning much easier.


2. Set up a database and connect

This SQL basics tutorial uses a simple MariaDB/MySQL-style environment. The commands are the same for many engines, with only small differences in syntax.

2.1 Create a database and user

From a MariaDB or MySQL prompt (see the MariaDB tutorial for exact install steps):

CREATE DATABASE sql_tutorial
  CHARACTER SET utf8mb4
  COLLATE utf8mb4_unicode_ci;

CREATE USER 'sql_user'@'localhost' IDENTIFIED BY 'sql_pass';

GRANT ALL PRIVILEGES ON sql_tutorial.* TO 'sql_user'@'localhost';
FLUSH PRIVILEGES;

Then switch into the new database:

USE sql_tutorial;

3. Create tables for a simple app

To make the rest of this SQL basics tutorial concrete, we will create a tiny schema for a notes app: one table for users and one for notes.

3.1 Define tables

CREATE TABLE users (
  id          BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  email       VARCHAR(255) NOT NULL,
  name        VARCHAR(255) NOT NULL,
  created_at  DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uniq_users_email (email)
);

CREATE TABLE notes (
  id          BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id     BIGINT UNSIGNED NOT NULL,
  title       VARCHAR(255) NOT NULL,
  body        TEXT,
  created_at  DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_notes_user_id (user_id),
  CONSTRAINT fk_notes_user
    FOREIGN KEY (user_id) REFERENCES users(id)
      ON DELETE CASCADE
);

These statements define the shape of your data and enforce basic integrity, such as requiring each note to belong to a valid user.


4. INSERT, SELECT, UPDATE, DELETE

Once tables exist, most of your SQL work boils down to four verbs: INSERT, SELECT, UPDATE and DELETE. The examples below cover the everyday patterns you will see in logs, debugging sessions and ORMs.

4.1 Insert data

INSERT INTO users (email, name)
VALUES
  ('alice@example.com', 'Alice'),
  ('bob@example.com', 'Bob');

INSERT INTO notes (user_id, title, body)
VALUES
  (1, 'Welcome', 'Welcome to the SQL basics tutorial'),
  (1, 'Checklist', 'Set up database, write queries, add indexes'),
  (2, 'Ideas', 'Experiment with a new analytics stack');

4.2 Select data

-- all users
SELECT id, email, name
FROM users
ORDER BY created_at DESC;

-- notes for a specific user
SELECT id, title, created_at
FROM notes
WHERE user_id = 1
ORDER BY created_at DESC;

4.3 Update and delete data

-- update a user's name
UPDATE users
SET name = 'Alice Cooper'
WHERE email = 'alice@example.com';

-- delete a single note safely
DELETE FROM notes
WHERE id = 3;

Always make your WHERE clauses as specific as possible when deleting or updating, especially in production.


5. Joins and aggregates

Joins and aggregates are where SQL really shines. This part of the SQL basics tutorial shows how to combine tables and compute simple metrics.

5.1 Inner join users and notes

SELECT
  u.email,
  n.title,
  n.created_at
FROM notes n
INNER JOIN users u ON u.id = n.user_id
ORDER BY n.created_at DESC;

This query returns only notes that have a matching user.

5.2 Count notes per user

SELECT
  u.email,
  COUNT(n.id) AS note_count
FROM users u
LEFT JOIN notes n ON n.user_id = u.id
GROUP BY u.id, u.email
ORDER BY note_count DESC;

LEFT JOIN ensures you still see users even if they have zero notes.


6. Simple indexes and EXPLAIN

Indexes are critical to performance, which is why they appear in any serious SQL basics tutorial. A good index lets the database look up rows quickly instead of scanning entire tables.

6.1 Add an index on created_at

Suppose you frequently query recent notes:

SELECT id, user_id, title, created_at
FROM notes
WHERE created_at >= NOW() - INTERVAL 7 DAY
ORDER BY created_at DESC;

You can help this query by indexing the created_at column:

CREATE INDEX idx_notes_created_at
  ON notes (created_at);

6.2 Use EXPLAIN to inspect the plan

EXPLAIN
SELECT id, user_id, title, created_at
FROM notes
WHERE created_at >= NOW() - INTERVAL 7 DAY
ORDER BY created_at DESC;

Look for which index is used and how many rows the engine expects to examine. As data volume grows, checking EXPLAIN and adding the right indexes is one of the highest-leverage skills you can develop.


7. Compact SQL cheat sheet

To close this SQL basics tutorial, here is a small cheat sheet summarizing the core commands we used. You can keep this near your terminal or in a README.

Category Examples Purpose Relative Usage
DDL (schema) CREATE TABLE, ALTER TABLE Define and evolve structure
CRUD INSERT, SELECT, UPDATE, DELETE Work with data rows
Joins & aggregates JOIN, GROUP BY, COUNT, SUM Combine tables and compute metrics
Performance CREATE INDEX, EXPLAIN Tune query speed

With these patterns in hand, you can read and write most everyday queries you encounter in logs, migrations and analytics jobs. From here, you can deepen your SQL skills with window functions, CTEs and more advanced query design while still building on the foundations from this SQL basics tutorial.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top