Master the fundamental SQL commands: create tables, insert data, query with filters, combine tables with JOINs, and aggregate results.
The command CREATE TABLE is part of the DDL (Data Definition Language), the subset of SQL responsible for defining the database structure. With it, you create tables and define columns, data types, and constraints. It's the first step before any operation on data.
NUMERIC(10,2) = up to 10 digits, 2 decimal places.When creating a table, you define constraints directly on the columns: PRIMARY KEY identifies the record, NOT NULL prevents empty values, UNIQUE prevents duplicates, REFERENCES creates a foreign key and DEFAULT defines a default value.
CREATE TABLE cliente (
id SERIAL PRIMARY KEY,
nome TEXT NOT NULL,
email TEXT UNIQUE
);
CREATE TABLE pedido (
id SERIAL PRIMARY KEY,
cliente_id INT REFERENCES cliente(id),
total NUMERIC(10,2) NOT NULL,
criado_em TIMESTAMP DEFAULT NOW()
);
After creating the structure, you need to populate the tables. The INSERT adds new records and the UPDATE modifies existing records. Together with DELETE, make up the operations CRUD (Create, Read, Update, Delete) — the foundation of any application.
One UPDATE without a clause WHERE modifies all the table records. Always include WHERE to limit the scope. Before running an UPDATE, run a SELECT with the same WHERE to confirm which records will be affected.
-- Inserindo um registro
INSERT INTO cliente (nome, email)
VALUES ('Ana Silva', 'ana@example.com');
-- Inserindo multiplos registros de uma vez
INSERT INTO cliente (nome, email)
VALUES ('Carlos Lima', 'carlos@example.com'),
('Maria Santos', 'maria@example.com');
-- Atualizando um registro especifico
UPDATE cliente
SET email = 'ana.silva@example.com'
WHERE id = 1;
O SELECT is the most commonly used SQL command. It reads data from tables and returns results filtered, sorted, and limited as you need. The clause WHERE filters which rows should appear in the result.
* for all of them (avoid in production).=, !=, >, <, LIKE, IS NULL, IN.ASC (default) or DESC.Combine conditions with AND (both must be true), OR (at least one must be true) and NOT (negates the condition). Use parentheses to control precedence when combining AND and OR.
-- Selecionar colunas especificas com filtro
SELECT nome, email
FROM cliente
WHERE email IS NOT NULL;
-- Filtrar, ordenar e limitar
SELECT *
FROM pedido
WHERE total > 100
ORDER BY criado_em DESC
LIMIT 10;
-- Combinando condicoes com AND e operadores
SELECT nome
FROM cliente
WHERE email LIKE '%example.com'
AND nome != 'Ana Silva';
In the relational model, data is distributed across multiple tables. The JOIN combines rows from two or more tables based on a relationship condition (usually foreign key = primary key). This is what gives SQL real power.
Use aliases (nicknames) for tables: FROM cliente c lets you reference it as c.nome instead of cliente.nome. This is especially useful when you join several tables and the columns have ambiguous names.
-- INNER JOIN: clientes que TEM pedidos
SELECT c.nome, p.total, p.criado_em
FROM cliente c
INNER JOIN pedido p ON p.cliente_id = c.id
ORDER BY p.total DESC;
-- LEFT JOIN: TODOS os clientes, mesmo sem pedidos
-- COALESCE substitui NULL por 0
SELECT c.nome, COALESCE(SUM(p.total), 0) AS total_gasto
FROM cliente c
LEFT JOIN pedido p ON p.cliente_id = c.id
GROUP BY c.nome;
Aggregate functions transform many rows into a single summarized result. The GROUP BY groups rows that share a common value, and aggregate functions operate on each group. That's how you generate reports and statistics directly in SQL.
WHERE filters individual rows before grouping. HAVING filters groups after aggregation. Example: "show customers who spent more than R$500" uses HAVING, because the total is calculated by SUM after GROUP BY.
-- Resumo por cliente: contagem, soma e media
SELECT cliente_id,
COUNT(*) AS num_pedidos,
SUM(total) AS total,
AVG(total) AS media
FROM pedido
GROUP BY cliente_id
HAVING SUM(total) > 500;
O DELETE permanently deletes records. It’s a powerful and dangerous command: without WHERE, it deletes everything of the table. In practice, many applications prefer the soft delete, which marks the record as removed instead of actually deleting it.
DELETE FROM tabela WHERE ... Permanently removes data. Use for temporary data or when regulations require it.deletado_em TIMESTAMP. Instead of deleting, use UPDATE and mark the date. The data stays in the database for auditing.-- SEGURO: sempre com WHERE
DELETE FROM pedido WHERE id = 5;
-- SOFT DELETE (recomendado)
-- Passo 1: adicione a coluna de controle
ALTER TABLE cliente ADD COLUMN deletado_em TIMESTAMP;
-- Passo 2: em vez de deletar, marque a data
UPDATE cliente SET deletado_em = NOW() WHERE id = 3;
-- Nas consultas, filtre os "deletados":
-- SELECT * FROM cliente WHERE deletado_em IS NULL;