Learn SQL: 24 free interactive lessons
3 to 5 minute lessons with a real SQL database running in your browser. Write the query, press Run, see the answer. No installation, no account.
Do it in the browser: a real SQL tool, a short mission and instant feedback. No install. Optional certificate at the end of the course.
Open the courseLessons
- Your first SELECTA database stores data in tables: rows are records, columns are fields. SELECT is how you ask a table for its data.
- Choosing columnsMost of the time you only need a few columns. List them after SELECT, separated by commas, in the order you want them.
- Filtering rows with WHEREWHERE keeps only the rows that meet a condition. Text values go between single quotes.
- Sorting and limitingORDER BY sorts the result; LIMIT keeps only the first rows. Together they answer 'top N' questions.
- AND, OR and NOTReal questions have several conditions. AND requires all of them, OR at least one, NOT inverts one.
- IN and BETWEENTwo shortcuts keep conditions short: IN checks a list of values, BETWEEN checks a range, ends included.
- Text patterns with LIKELIKE matches text against a pattern: % stands for any number of characters, _ for exactly one.
- Missing values: NULLNULL means 'unknown' or 'not filled in'. It is not zero and not empty text, and it needs its own operators.
- Counting rowsAggregate functions turn many rows into one value. COUNT is the simplest: how many rows match?
- SUM, AVG, MIN and MAXThe other everyday aggregates add up, average and find extremes. You can use several in the same SELECT.
- GROUP BYGROUP BY splits the rows into groups and runs the aggregate once per group, producing one row per group.
- Filtering groups with HAVINGWHERE filters rows before grouping; HAVING filters groups after the aggregates are computed.
- INNER JOINData is split across tables and linked by keys. A JOIN puts the pieces back together, matching rows where the keys are equal.
- LEFT JOINLEFT JOIN keeps every row of the left table, even without a match. Missing values on the right come back as NULL.
- Joining several tablesQuestions often cross three or four tables. Chain one JOIN per table, each with its own ON condition.
- A table joined to itselfSome tables point to themselves: each staff member has a manager who is also staff. Join the table twice with two aliases.
- SubqueriesA query inside another query. The inner query runs first and its result feeds the outer one.
- WITH: naming a stepA common table expression (CTE) gives a name to an intermediate result. Long queries become a sequence of readable steps.
- CASE: conditions inside SELECTCASE works like an if/else that produces a value for each row. Use it to label, bucket or translate data.
- Window functionsWindow functions compute across related rows without collapsing them. Ranking inside a group is the classic use.
- CREATE TABLETables are defined once with their columns and types. A primary key identifies each row; a foreign key points to another table.
- INSERTINSERT adds new rows. List the columns, then the values in the same order.
- UPDATE and DELETEUPDATE changes existing rows and DELETE removes them. Both use WHERE to choose which rows; forget it and every row is affected.
- Indexes and query plansAn index is a sorted lookup structure, like the index of a book. It makes searches on a column fast at the cost of some space and slower…