DevDocsDev Docs
SQL Fundamentals

SQL Fundamentals

Master the core SQL syntax including SELECT, INSERT, UPDATE, DELETE, JOINs, subqueries, and aggregations

SQL (Structured Query Language) is the standard language for managing and manipulating relational databases. This section covers the foundational concepts that apply across all major database platforms.

Cross-Platform

The SQL fundamentals covered here work across PostgreSQL, MySQL, and SQL Server with minimal syntax differences.

How SQL Queries Work

When you execute a SQL query, the database follows a specific order of operations—different from how you write the query:

Understanding this execution order helps you write better queries and debug issues:

OrderClausePurpose
1FROM / JOINIdentify tables and combine rows
2WHEREFilter rows before grouping
3GROUP BYGroup rows for aggregation
4HAVINGFilter groups after aggregation
5SELECTChoose columns and compute expressions
6DISTINCTRemove duplicate rows
7ORDER BYSort the result set
8LIMIT / OFFSETRestrict returned rows

Common Mistake

You cannot use column aliases from SELECT in WHERE because WHERE is evaluated before SELECT.

Quick Navigation

SQL Statement Categories

Data Query Language - Retrieve data from the database

SELECT column1, column2
FROM table_name
WHERE condition
ORDER BY column1;

DQL is the most commonly used category. The SELECT statement and all its clauses fall under DQL.

Data Manipulation Language - Modify data in tables

-- Insert new data
INSERT INTO users (name, email) VALUES ('John', 'john@example.com');

-- Update existing data
UPDATE users SET status = 'active' WHERE id = 1;

-- Delete data
DELETE FROM users WHERE status = 'inactive';

Data Definition Language - Define database structure

-- Create table
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100)
);

-- Modify table
ALTER TABLE users ADD COLUMN email VARCHAR(255);

-- Remove table
DROP TABLE users;

Data Control Language - Control access permissions

-- Grant permissions
GRANT SELECT, INSERT ON users TO app_user;

-- Revoke permissions
REVOKE DELETE ON users FROM app_user;

Sample Database Schema

Throughout this documentation, we'll use this sample e-commerce schema:

Learning Path

Start with SELECT

Learn how to retrieve data from single tables with filtering, sorting, and limiting results.

Go to SELECT Statements →

Master JOINs

Combine data from multiple tables using different JOIN types.

Go to JOINs →

Aggregate Data

Use GROUP BY and aggregate functions to summarize data.

Go to Aggregations →

Write Subqueries

Nest queries within queries for complex data retrieval.

Go to Subqueries →

Use Window Functions

Perform calculations across related rows without grouping.

Go to Window Functions →

Quick Reference

Basic SELECT Template

SELECT 
    column1,
    column2,
    aggregate_function(column3) AS alias
FROM table1
JOIN table2 ON table1.id = table2.foreign_id
WHERE condition
GROUP BY column1, column2
HAVING aggregate_condition
ORDER BY column1 DESC
LIMIT 10 OFFSET 20;

Common Operators

OperatorDescriptionExample
=Equal toWHERE status = 'active'
<> or !=Not equal toWHERE status <> 'deleted'
>, <, >=, <=ComparisonWHERE price > 100
BETWEENRange (inclusive)WHERE price BETWEEN 10 AND 50
INMatch any in listWHERE status IN ('a', 'b')
LIKEPattern matchingWHERE name LIKE 'John%'
IS NULLCheck for NULLWHERE phone IS NULL
AND, ORLogical operatorsWHERE a = 1 AND b = 2
NOTNegate conditionWHERE NOT status = 'deleted'

Pattern Matching

PatternMatches
%Any sequence of characters
_Any single character
LIKE 'a%'Starts with 'a'
LIKE '%a'Ends with 'a'
LIKE '%a%'Contains 'a'
LIKE 'a_b''a' + any char + 'b'

On this page