GCSE · Computer Science · OCR · Spec J277

SQL: SELECT, FROM, WHERE

A database can hold thousands of records and you want four of them. SQL asks for exactly those four, using three keywords.

Two queries, one table

Watch a query cut the table down

Two queries, one table of music tracks. Each press of Next shows what is left after one more part of the query has done its job.

Before each Next, predict what will be left.

SELECT Title, Plays
FROM tblTracks
WHERE Genre = "Rock";

SELECT Title, Artist
FROM tblTracks
WHERE Plays > 300;

Result

TitleArtistGenrePlays
Paper MoonThe SpringsPop420
StaticIron GateRock310
Lemon SkyThe SpringsRock150
Night BusMia ColePop95
EchoThe SpringsPop260
GravelIron GateRock500

Output

 

Step 1: Query 1. FROM tblTracks names the table: six records (rows), four fields (columns). Which tracks do you think will survive WHERE Genre = "Rock"?

1 / 6

Cover the result with your hand, predict, then press Next.

Step 1 of 6: Query 1. FROM tblTracks names the table: six records (rows), four fields (columns). Which tracks do you think will survive WHERE Genre = "Rock"?.

Watch out: WHERE can test a field that SELECT never shows. In query 2, Plays decides which records stay, but it is not in the final result.

AND or OR?

Overlap or everything?

Same six tracks. For each one, tick which conditions it meets: Genre = "Pop", Artist = "The Springs", both, or neither.

  • A Genre = "Pop"
  • B Artist = "The Springs"
  1. Paper Moon (The Springs, Pop)
  2. Static (Iron Gate, Rock)
  3. Lemon Sky (The Springs, Rock)
  4. Night Bus (Mia Cole, Pop)
  5. Echo (The Springs, Pop)
  6. Gravel (Iron Gate, Rock)

Predict, then check

A table tblDownloads logs the Time of each download. The query is SELECT * FROM tblDownloads WHERE Time BETWEEN "06:00" AND "07:00";

One download was logged at exactly 07:00. Does this query return it?

Find the faulty line

One small slip

Ella wants the title and artist of every track in the Indie genre from tblTracks. Which line of her query is wrong?

Ella's query — which line goes wrong?

What do you think?

Is SELECT * always the safe bet?

Sam needs the titles of the Pop tracks, nothing else. He writes: SELECT * FROM tblTracks WHERE Genre = "Pop";

Which is closest to what you think about Sam's query?
How sure are you?

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Three keywords, three jobs: pick the columns, name the table, choose the rows.

What you need to know

  • SQL stands for Structured Query Language: you use it to search, retrieve, insert, delete and update records in a database.
  • SELECT is followed by the field names (columns) you want to display, separated by commas.
  • FROM is followed by the name of the table those fields come from.
  • WHERE is followed by the criteria a record must meet to be returned, such as WHERE Genre = "Rock".
  • Have a goIn SELECT Title FROM tblTracks WHERE Artist = "Iron Gate"; which part decides the rows you get back, and which decides the columns?

    WHERE decides the rows; SELECT decides the columns (just Title).

    SELECT sounds as if it picks the records, but it only chooses which fields appear; the WHERE criteria decide which records make it.

  • Text you are searching for goes in speech marks, and the statement ends with a semicolon.
  • Comparison operators: equal to (=), greater than (>), less than (<), greater or less than or equal to (>=, <=), and not equal to.
  • Have a goYour friend Jas swears WHERE Plays > 300 and WHERE Plays >= 300 always give exactly the same records. Find the one kind of record that proves Jas wrong.

    A record with exactly 300 plays: >= keeps it, > drops it.

    Greater than on its own leaves out the value itself; "or equal to" is what lets 300 in, so the two only differ at that exact value.

  • AND is true only if both conditions are true; OR is true if any of the conditions is true.
  • Have a goA table has 4 Pop tracks and 3 Rock tracks, and no track is both. How many records does WHERE Genre = "Pop" OR Genre = "Rock" return?

    7

    OR keeps a record if any condition is true, so every Pop track and every Rock track comes back; reading OR as "pick one of the groups" gives 4 or 3.

  • BETWEEN is true when a value is within the stated range, and the two end values count.
  • SELECT * returns every field in the table, so save it for when you need all of them.

The big picture

SQL (Structured Query Language) is used to communicate with a database. In a query, SELECT lists the fields to show, FROM names the table, and WHERE gives the criteria a record must meet. Comparison operators and the logical operators AND, OR and BETWEEN make the criteria more precise.

Key points

1SELECT picks the columns, FROM picks the table, WHERE picks the rows.
2Text in speech marks, a semicolon to finish.
3AND needs both conditions true (the overlap); OR needs just one (everything in either group).
4BETWEEN keeps both end values.
5Use SELECT * only when every field is genuinely needed.

Worked example

Problem

Using tblTracks (Title, Artist, Genre, Plays), write a query to show the Title and Genre of every Iron Gate track with more than 400 plays.

⚠ Watch out

Leaving the speech marks off text. WHERE Genre = Rock looks fine at a glance, but the text being searched for must be in speech marks: WHERE Genre = "Rock"; and the statement ends with a semicolon.

🧠

Memory hook

Columns, table, rows: SELECT, FROM, WHERE, in that order. Then AND squeezes, OR spreads: adding an AND condition can never give you more records, and adding an OR condition can never give you fewer.

✓

Check yourself

Cover the page. Say what SELECT, FROM and WHERE each choose. Then write a query for the Artist of every Pop track in tblTracks with fewer than 300 plays, and say which tracks it returns.

Flashcards

(13)
What does SQL stand for?
Structured Query Language: a language used to communicate with a database.
Four things SQL can do with records
Search for and retrieve records, insert new records, delete records, update records.
What follows the keyword SELECT?
The names of the fields you want to display, separated by commas.
What follows the keyword FROM?
The name of the table the fields are selected from.
What follows the keyword WHERE?
The criteria a record must meet, e.g. Genre = "Rock".
How do you write text you are searching for?
Inside speech marks, e.g. Artist = "The Springs".
How does an SQL statement end?
With a semicolon ;
What does SELECT * return?
All of the fields (columns) in the table, for the records that meet the criteria.
When should you use SELECT *?
Only when you need all the fields. Unneeded data can frustrate the person working with the results.
Name the six comparison operators
Equal to, greater than, less than, greater than or equal to, less than or equal to, not equal to.
When is AND true?
Only when both conditions are true.
When is OR true?
When any of the conditions is true.
Does BETWEEN include the end values?
Yes. BETWEEN is true for a value within the range, including both end values.

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More OCR GCSE Computer Science topics

See the full OCR Computer Science curriculum →

How this lesson was checked. This OCR GCSE Computer Science (specification J277)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 3 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.