SELECT and WHERE — without installing anything. This guide gives you a small, invented library table, asks you to write queries against it, and shows the exact expected results so you can check your own answers. No sign-up, no real database, no protected data: every row below was made up for this exercise.
The dataset
We will query abooks table with eight rows. Read each row as one book.
| book_id | title | author | genre | year | pages |
|---|---|---|---|---|---|
| 1 | The Red Planet | Ray Briggs | Science | 1998 | 320 |
| 2 | Rivers of Ink | Maya Rao | Fiction | 2005 | 288 |
| 3 | A Field Guide to Birds | Li Chen | Science | 2010 | 256 |
| 4 | The Silent Garden | Ana Souza | Fiction | 2015 | 412 |
| 5 | Numbers in Nature | Kavita Patel | Science | 2002 | 199 |
| 6 | Winter Traditions | Erik Johnson | History | 2018 | 340 |
| 7 | The Last Observatory | Mohan Rao | Science | 2021 | 384 |
| 8 | Tales from the Coast | Ruth Ortiz | Fiction | 1999 | 210 |
Two ideas to hold on to before you write anything:
SELECTchooses which columns to show.WHEREfilters which rows to keep.
A table can have hundreds of rows, but only rows that satisfy the WHERE condition come back.
Exercise 1 — Filter by one condition
Task: List the titles of all Science books. Query:SELECT title
FROM books
WHERE genre = 'Science';
Explanation: SELECT title says "give me only the title column." WHERE genre = 'Science' says "keep only rows whose genre equals Science." Text values go in single quotes.
Expected result (4 rows):
| title |
|---|
| The Red Planet |
| A Field Guide to Birds |
| Numbers in Nature |
| The Last Observatory |
Try it on paper first: scan the genre column, circle every Science row, then read off its title.
Exercise 2 — Combine conditions with AND
Task: List the title and year of Fiction books published in 2005 or later. Query:SELECT title, year
FROM books
WHERE year >= 2005 AND genre = 'Fiction';
Explanation: AND keeps a row only when both conditions are true. "2005 or later" is written year >= 2005. Tales from the Coast (1999) is Fiction but fails the year test; A Field Guide to Birds (2010) passes the year test but fails the genre test — neither comes back.
Expected result (2 rows):
| title | year |
|---|---|
| Rivers of Ink | 2005 |
| The Silent Garden | 2015 |
Exercise 3 — OR and ORDER BY (challenge)
Task: List Fiction books or books longer than 350 pages, with their genre and page counts, longest first. Query:SELECT title, genre, pages
FROM books
WHERE genre = 'Fiction' OR pages > 350
ORDER BY pages DESC;
Explanation: OR keeps a row when either condition is true. All three Fiction rows qualify through genre, and The Last Observatory (384 pages) qualifies through length. ORDER BY pages DESC sorts longest first (DESC means descending).
Expected result (4 rows, in this order):
| title | genre | pages |
|---|---|---|
| The Silent Garden | Fiction | 412 |
| The Last Observatory | Science | 384 |
| Rivers of Ink | Fiction | 288 |
| Tales from the Coast | Fiction | 210 |
OR to AND? You would keep only rows that are Fiction and longer than 350 pages — just The Silent Garden (412). AND narrows; OR widens.
Common mistakes to avoid
- Forgetting quotes on text:
WHERE genre = Scienceshould beWHERE genre = 'Science'. Numbers do not need quotes. - Using
ORwhen you meanAND:ORreturns more rows;ANDreturns fewer. - Missing
=vs>: "2005 or later" means>= 2005, not> 2005(which would drop the 2005 book). - Selecting a column you did not ask for:
SELECT titlereturns one column;SELECT title, yearreturns two.
Where to go next
Finished the three exercises? Keep going with the full course: PixoQuest SQL Basic teachesSELECT, joins, aggregation, INSERT, UPDATE, and DELETE through short interactive quizzes — no database install needed. When you want a real engine, try the free tools below. Full official syntax references:
- PostgreSQL SELECT documentation: https://www.postgresql.org/docs/current/sql-select.html
- SQLite SELECT documentation: https://www.sqlite.org/lang_select.html
- MDN, SQL Guide — Introduction: https://developer.mozilla.org/en-US/docs/Web/SQL/Guide/Introduction
Sources & further reading
- PostgreSQL documentation, SELECT: https://www.postgresql.org/docs/current/sql-select.html
- SQLite documentation, SELECT Syntax: https://www.sqlite.org/lang_select.html
- MDN Web Docs, SQL Guide: https://developer.mozilla.org/en-US/docs/Web/SQL/Guide/Introduction
The table and expected results in this article are invented example data written for this exercise; no real library, learner, or production data is used.