Skip to content
IGCSE·Tuition
Computer Science · Lessons

Write a simple SELECT filter

A query is only a short sentence, yet the order of its three parts and the choice of operator decide what you get back.

On this page
  1. How do you build a query?
  2. Worked example
  3. The mistake to watch for
  4. Check yourself
  5. Where this leads next

A SELECT query chooses which fields to show (SELECT), which table to use (FROM) and which records to keep (WHERE). The result is a new, smaller table.

This is the core skill in databases and query reasoning. The same Plants table from the first lesson is used here.

PlantIDNameTypePriceStockIndoor
P01FernFoliage12.508TRUE
P02CactusSucculent6.0025TRUE
P03RoseFlowering15.000FALSE
P04MintHerb4.5040FALSE
P05OrchidFlowering28.003TRUE
P06BasilHerb5.0012FALSE
P07AloeSucculent9.5015TRUE
P08HibiscusFlowering18.006FALSE

How do you build a query?

Write it in the same order every time:

SELECT field1, field2
FROM table
WHERE condition

Use * after SELECT to show every field. Put text values in quotation marks and leave numbers bare. The comparison operators are =, <> (not equal), <, <=, > and >=.

To sort the result, add ORDER BY field at the end (ascending by default, DESC for descending).

Worked example

Question: list the name and price of every plant that costs more than 10.00, cheapest first.

Step 1, choose fields to show: Name, Price.

Step 2, choose the table: Plants.

Step 3, write the condition: Price > 10.

Step 4, add the sort: ORDER BY Price.

SELECT Name, Price
FROM Plants
WHERE Price > 10
ORDER BY Price

Step 5, test every record. Fern 12.50 passes, Cactus 6.00 fails, Rose 15.00 passes, Mint 4.50 fails, Orchid 28.00 passes, Basil 5.00 fails, Aloe 9.50 fails, Hibiscus 18.00 passes.

Step 6, sort by price: 12.50, 15.00, 18.00, 28.00.

NamePrice
Fern12.50
Rose15.00
Hibiscus18.00
Orchid28.00

Four rows come back. Checking all eight records, not only the likely ones, is what keeps the answer right.

The mistake to watch for

A common slip is to put the tested field in SELECT instead of WHERE.

Mistaken answer: SELECT Price > 10 FROM Plants

This asks the database to display a comparison, not to filter records.

The correction: SELECT lists columns to display; WHERE holds the test. A second slip is leaving quotation marks off text, as in Type = Herb.

Check yourself

1. Write a query for the names of all herbs.

Show answer
SELECT Name
FROM Plants
WHERE Type = "Herb"

Result: Mint, Basil.

2. How many rows does SELECT * FROM Plants WHERE Stock < 10 return?

Show answer

Stock under 10: Fern (8), Rose (0), Orchid (3), Hibiscus (6). That is 4 rows.

3. What changes if you write Price >= 28 instead of Price > 28?

Show answer

> 28 returns no rows, as no price is above 28.00. >= 28 returns Orchid (28.00).

Where this leads next

Next, combine conditions with correct Boolean grouping, then try the module practice set. Run your own queries in the read-only SQL practice lab.

If you want a teacher to check how you build each query, see our online one-to-one Computer Science tuition.

Updated:

Your next step

If you can read queries but freeze when asked to write one from scratch, a one-to-one teacher can give you a repeatable template and mark each part as you build it.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80. Other fees, schedules and ongoing arrangements are confirmed directly with your teacher after the trial class.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parent or guardian? Enquire here

9,000+ students helped through our service