SQL SELECT: Your First Query
Write your first SQL query with SELECT and FROM, read a table in a real SQLite database right in your browser, and see the result instantly.
SELECT * FROM heroes;The essential SQL syntax on one page. Every snippet comes from a lesson of our interactive Learn SQL course — click "Practise" to write it yourself and get instant feedback.
Last updated on .
Write your first SQL query with SELECT and FROM, read a table in a real SQLite database right in your browser, and see the result instantly.
SELECT * FROM heroes;Pick exactly the columns you need in SQL, set their order, rename them with AS aliases and calculate new values like price * 3 in a real database.
SELECT name, level FROM heroes;Filter rows in SQL with WHERE. Compare values with =, <>, < and >, combine conditions with AND, OR and NOT, and group them with parentheses.
SELECT name, level FROM heroes WHERE level > 18;Sort SQL results with ORDER BY, ASC and DESC, sort by several columns, and return just the rows you need with LIMIT and OFFSET.
SELECT name, gold FROM heroes ORDER BY gold DESC;Match text patterns with SQL LIKE and its wildcards, test lists with IN and ranges with BETWEEN, and find missing values with IS NULL.
SELECT name FROM items WHERE name LIKE 'Scroll%';Count rows with SQL COUNT(*), see why COUNT(column) skips NULLs, remove duplicates with DISTINCT and count unique values with COUNT(DISTINCT).
SELECT COUNT(*) AS veterans FROM heroes WHERE level >= 15;Add up values with SQL SUM, average them with AVG, find the smallest and largest with MIN and MAX, and round the results with ROUND.
SELECT SUM(gold) AS total_gold, AVG(gold) AS avg_gold FROM heroes;Summarise data per group with SQL GROUP BY. Count, sum and average for each category, group by several columns and sort the groups.
SELECT class, SUM(gold) AS total_gold
FROM heroes
GROUP BY class;Learn the difference between SQL WHERE and HAVING. Filter rows before grouping, and filter groups by their COUNT or SUM after GROUP BY.
SELECT region, COUNT(*) AS quests
FROM quests
GROUP BY region
HAVING COUNT(*) >= 2;Learn why data is split into tables, how foreign keys link them, and how SQL JOIN … ON combines rows from two tables into one result.
SELECT heroes.name AS hero, guilds.name AS guild, guilds.city
FROM heroes
JOIN guilds ON heroes.guild_id = guilds.id;Join three or more tables in SQL. Give tables short aliases, follow foreign keys through a link table, and combine heroes, quests and items.
SELECT h.name AS hero, g.name AS guild
FROM heroes AS h
JOIN guilds AS g ON h.guild_id = g.id;Use SQL LEFT JOIN to keep every row of the first table, get NULLs for missing matches, and find rows without a match with WHERE … IS NULL.
SELECT h.name AS hero, g.name AS guild
FROM heroes h
LEFT JOIN guilds g ON h.guild_id = g.id;Add new rows with SQL INSERT INTO … VALUES, insert several rows in one statement, and let SQLite fill in default values and auto-numbered ids.
INSERT INTO guilds (name, city, founded)
VALUES ('Storm Riders', 'Saltcliff', 2025);Change data with SQL UPDATE … SET … WHERE, update several columns at once, use expressions like gold = gold + 100 and avoid the missing-WHERE disaster.
UPDATE heroes
SET level = 10
WHERE id = 4;Remove rows with SQL DELETE FROM … WHERE, check the rows first with SELECT, and delete related rows in the right order so no orphans are left behind.
DELETE FROM items
WHERE type = 'scroll' AND price < 50;Create your own SQL tables with CREATE TABLE, choose SQLite data types (INTEGER, TEXT, REAL) and give every row an id with INTEGER PRIMARY KEY.
CREATE TABLE taverns (
id INTEGER PRIMARY KEY,
name TEXT,
city TEXT,
rooms INTEGER,
ale_price REAL
);Protect your data with SQL constraints: NOT NULL, UNIQUE, DEFAULT and CHECK in CREATE TABLE, the errors they raise, and a table that rejects bad rows.
CREATE TABLE bounties (
id INTEGER PRIMARY KEY,
target TEXT NOT NULL UNIQUE,
reward INTEGER NOT NULL CHECK (reward > 0),
status TEXT NOT NULL DEFAULT 'open'
);Link SQL tables with foreign keys (REFERENCES), switch on enforcement in SQLite, and change a schema with ALTER TABLE ADD COLUMN and DROP TABLE.
CREATE TABLE purchases (
id INTEGER PRIMARY KEY,
hero_id INTEGER NOT NULL REFERENCES heroes(id),
item_id INTEGER NOT NULL REFERENCES items(id),
quantity INTEGER NOT NULL DEFAULT 1,
bought_on TEXT NOT NULL
);Put a query inside a query: SQL subqueries with IN, NOT IN, EXISTS and scalar subqueries like gold > (SELECT AVG(gold) …), with an interactive exercise.
SELECT name, gold
FROM heroes
WHERE gold > (SELECT AVG(gold) FROM heroes);Add if-then-else logic to SQL with CASE WHEN … THEN … ELSE … END. Label rows, sort in a custom order and count conditionally inside aggregates.
SELECT name, level,
CASE
WHEN level >= 20 THEN 'veteran'
WHEN level >= 10 THEN 'adept'
ELSE 'novice'
END AS tier
FROM heroes;Break a big SQL query into named steps with the WITH clause. Learn common table expressions (CTEs), chain several CTEs and see why they beat nesting.
WITH guild_gold AS (
SELECT guild_id, SUM(gold) AS total
FROM heroes
WHERE guild_id IS NOT NULL
GROUP BY guild_id
)
SELECT g.name, gg.total
FROM guild_gold gg
JOIN guilds g ON g.id = gg.guild_id
WHERE gg.total > 1500
ORDER BY gg.total DESC;Learn SQL window functions: OVER, PARTITION BY, ROW_NUMBER, RANK and running totals with SUM() OVER, to rank and compare rows without losing them.
SELECT name, class, gold,
MAX(gold) OVER (PARTITION BY class) AS class_best
FROM heroes;