SQL Practice: SELECT and WHERE, Worked Step by Step

← Community · SQL·By Aman Raj·Published ·9 min read·843 words

You can learn the two most-used SQL keywords — 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 a books table with eight rows. Read each row as one book.

book_idtitleauthorgenreyearpages
1The Red PlanetRay BriggsScience1998320
2Rivers of InkMaya RaoFiction2005288
3A Field Guide to BirdsLi ChenScience2010256
4The Silent GardenAna SouzaFiction2015412
5Numbers in NatureKavita PatelScience2002199
6Winter TraditionsErik JohnsonHistory2018340
7The Last ObservatoryMohan RaoScience2021384
8Tales from the CoastRuth OrtizFiction1999210

Two ideas to hold on to before you write anything:

  • SELECT chooses which columns to show.
  • WHERE filters 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):
titleyear
Rivers of Ink2005
The Silent Garden2015

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):
titlegenrepages
The Silent GardenFiction412
The Last ObservatoryScience384
Rivers of InkFiction288
Tales from the CoastFiction210
Check yourself: What if you changed 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 = Science should be WHERE genre = 'Science'. Numbers do not need quotes.
  • Using OR when you mean AND: OR returns more rows; AND returns 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 title returns one column; SELECT title, year returns two.

Where to go next

Finished the three exercises? Keep going with the full course: PixoQuest SQL Basic teaches SELECT, 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.

FAQ

What is the difference between SELECT and WHERE in SQL?

SELECT chooses which columns to return. WHERE filters which rows survive the query — only rows satisfying the condition come back.

How do AND and OR differ in a WHERE clause?

AND keeps a row only when every condition is true, so it narrows results. OR keeps a row when either condition is true, so it widens them.

Do text values need quotes in SQL queries?

Yes — text goes in single quotes, as in WHERE genre = 'Science'. Numbers do not need quotes.

What does ORDER BY pages DESC do?

It sorts result rows by the pages column in descending order — longest books first. ASC is the default ascending direction.

Frequently asked questions

What is the difference between SELECT and WHERE in SQL?

SELECT chooses which columns to return; WHERE filters which rows survive the query. Only rows satisfying the WHERE condition come back.

How do AND and OR differ in a WHERE clause?

AND keeps a row only when every condition is true and narrows results; OR keeps a row when either condition is true and widens them.

Do text values need quotes in SQL queries?

Yes — text goes in single quotes, as in WHERE genre = 'Science', while numbers do not need quotes.

What does ORDER BY pages DESC do?

It sorts the result rows by the pages column in descending order — longest books first — with ASC as the default ascending direction.

SQLSELECTWHEREpracticebeginners

Published under the PixoQuest editorial policy.