Run real SQL queries in your browser. No install needed.
📑 SQL Reference — All Topics
SELECT, WHERE, ORDER BY, LIMIT, LIKE, IN — the basics.
INNER/LEFT/RIGHT JOIN, GROUP BY, HAVING, COUNT, SUM.
Window functions, CTEs, subqueries, indexes, EXPLAIN.
Declarative: you describe WHAT data you want, the engine decides HOW.
No SQL runtime exists — you pick an engine (Postgres, MySQL, SQLite) first.
SELECT and FROM — the two words every query starts with.
Comparisons, BETWEEN, IN, and LIKE — decide which rows even qualify.
Without ORDER BY, row order isn't guaranteed at all.
INNER JOIN drops unmatched rows; LEFT JOIN keeps them, as NULLs.
Correlated subqueries re-run per outer row — fast queries can quietly slow down.
WHERE can't filter on COUNT(*) — that's exactly what HAVING is for.
Aggregates that don't collapse rows — OVER, PARTITION BY, running totals.
WITH names a subquery for clarity, and can even reference itself.
SQLite lets you insert text into an INTEGER column — type affinity, not strict typing.
DROP TABLE has no undo, no trash can, and no confirmation dialog.
Foreign keys make an orphaned reference literally impossible at the DB level.
Rules enforced by the database itself, no matter which app is writing to it.
Every index speeds up reads and slows down writes — always both, never just one.
A crashed bank transfer that debits but never credits — exactly what this prevents.
Four separate promises — miss one, and you don't really have transactions.
Editing a customer's address in five different rows means the schema isn't normalized.
A saved query, not a snapshot — re-run fresh, live, every single time.
The one place SQL stops being one language — PL/pgSQL, T-SQL, and MySQL differ.
Fires with no app code calling it — powerful, and easy to hide bugs inside.
Wrapping an indexed column in a function often defeats the index entirely.
EXPLAIN ANALYZE turns "this feels slow" into a specific, fixable line.
WHERE column = NULL never matches a single row, ever — by design.
UNION dedupes; UNION ALL skips that check and is almost always faster.
One-to-many, many-to-many, and a design checklist before writing any app code.
A single quote in a login form has bypassed authentication — real breaches, not theory.
SQL (Structured Query Language) is a declarative domain-specific language for managing and querying data in relational database management systems (RDBMS), based on Edgar Codd's relational model, standardised as ISO/IEC 9075, and used by all major databases including PostgreSQL, MySQL, SQLite, SQL Server, and Oracle.
SELECT — the core statement
SELECT name, salary, department
FROM employees
WHERE salary > 50000
ORDER BY salary DESC
LIMIT 10;
-- Aggregates
SELECT department, COUNT(*) AS headcount, AVG(salary) AS avg_sal
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;JOINs
-- INNER JOIN: only rows that match in both tables
SELECT e.name, d.department_name
FROM employees e
INNER JOIN departments d ON e.department_id = d.id;
-- LEFT JOIN: all employees, even those with no department
SELECT e.name, d.department_name
FROM employees e
LEFT JOIN departments d ON e.department_id = d.id;CREATE, INSERT, UPDATE, DELETE
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
salary NUMERIC(10,2) CHECK (salary > 0),
dept_id INTEGER REFERENCES departments(id)
);
INSERT INTO employees (name, salary) VALUES ('Priya', 85000);
UPDATE employees SET salary = salary * 1.10 WHERE dept_id = 3;
DELETE FROM employees WHERE active = FALSE;CTEs and Window Functions
-- CTE
WITH high_earners AS (
SELECT * FROM employees WHERE salary > 80000
)
SELECT department, COUNT(*) FROM high_earners GROUP BY department;
-- Window function
SELECT name, salary,
RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rank_in_dept,
SUM(salary) OVER (ORDER BY hire_date) AS running_total
FROM employees;