HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
SQL Queries: SELECT, WHERE and ORDER BY
  1. 1SELECT and FROM: choosing the fields
  2. 2WHERE: choosing the records
  3. 3ORDER BY: sorting the output
  1. Computer Science
  2. /SQL Queries: SELECT, WHERE and ORDER BY
  3. /SELECT and FROM: choosing the fields

Course 9.2·Lesson 1 of 3

SELECT and FROM: choosing the fields

SQL is the language we use to ask a database for data. A script starts with SELECT, which names the fields to show, and FROM, which names the table. You write such scripts, read them, and work out their exact output.

about 25 min1 simulation1 practice question
Lesson 1 of 3Show all lessonsHide
SQL Queries: SELECT, WHERE and ORDER BY
  1. 1SELECT and FROM: choosing the fields
  2. 2WHERE: choosing the records
  3. 3ORDER BY: sorting the output

Try the code and the simulations beside the lesson.

Asking a database a question

A librarian wants a list of the titles and the authors of all the books. She does not want the page counts or the book IDs. How does she ask?

She writes a short script in SQL (structured query language). SQL is the language that almost every database understands. A script that asks a database for data is called a query.

Here is the library's table, Books. We use it in all three lessons of this course.

table
BookID  Title        Author   Genre    Pages  OnLoan
K1      Night Train  Adeyemi  Mystery  320    TRUE
K2      Green Hills  Silva    Nature   180    FALSE
K3      Lost Key     Adeyemi  Mystery  256    FALSE
K4      Deep Blue    Haddad   Nature   410    TRUE
K5      Iron Bridge  Kowal    History  288    TRUE
K6      Snow Fox     Silva    Nature   144    FALSE

SELECT and FROM

The two words every query needs are SELECT and FROM.

  • SELECT is followed by the field names to show, with a comma between them.
  • FROM is followed by the name of the table.
  • A semicolon ; ends the script.
sql
SELECT Title, Author
FROM Books;

The output:

table
Title        Author
Night Train  Adeyemi
Green Hills  Silva
Lost Key     Adeyemi
Deep Blue    Haddad
Iron Bridge  Kowal
Snow Fox     Silva
The Books table drawn as a grid with the fields BookID, Title, Author, Genre, Pages and OnLoan. The Title and Author columns are shaded green. An arrow labelled 'SELECT Title, Author' points to a smaller output table on the right with only those two columns and all six books, from Night Train, Adeyemi to Snow Fox, Silva.

Read the script out loud: "show the title and the author, from the table Books". The output has the two fields we asked for and every record, because nothing in the script leaves a record out.

The order of the fields

The fields come out in the order they are written after SELECT, not in the order of the table.

sql
SELECT Pages, Title
FROM Books;

The output:

table
Pages  Title
320    Night Train
180    Green Hills
256    Lost Key
410    Deep Blue
288    Iron Bridge
144    Snow Fox

The records come out in the order they are stored in the table. The third lesson shows how to sort them.

Writing a script

  • Write the keywords SELECT and FROM in capitals. It makes the script easy to read.
  • Spell each field name exactly as it is in the table: Title, not Book title.
  • Put a comma between two field names, but not after the last one.
  • A script can be on one line or on several lines. Many people put each keyword on a new line.

Practise with new numbers

Try it in Python

The program builds the Books table with executemany. Then it runs a script, prints the names of the output fields and prints each output row. SQLite shows TRUE as 1 and FALSE as 0.

Read it first. Then run it. Does the output match the table in this lesson?

1. Change the script to SELECT BookID, Genre FROM Books;. Predict the first output row, then run it.

2. Leave out the comma: SELECT BookID Genre FROM Books;. What happens? SQLite reads Genre as a new name for the column, so the output is wrong without an error.

Common mistakes

  • A missing comma between fields. SELECT Title Author is not the list Title, Author.
  • A comma after the last field. SELECT Title, Author, FROM Books is an error.
  • A field name spelled differently from the table. The database cannot find it.
  • Thinking SELECT chooses records. SELECT chooses the fields (the columns). The next lesson chooses records.

Remember these

  • SELECT names the fields to output, in the order you want them.
  • FROM names the table.
  • With no condition, every record is output, in the order of the table.

Check yourself

Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.

  1. Write a script that outputs the Genre and the Title of every book, in that order.
  1. How many rows and how many columns does SELECT BookID, Pages, OnLoan FROM Books; output?
  1. What is the first row of the output of SELECT Author, Pages FROM Books;?
  1. Find the error: SELECT Title, FROM Books;
  1. Which keyword names the table in a query?

References

  • Cambridge International, IGCSE Computer Science 0478 syllabus (exams in 2026, 2027 and 2028), section 9 Databases, 9.4: read, understand and complete SQL scripts to query data in a single table (SELECT, FROM).

Enroll free in SQL Queries: SELECT, WHERE and ORDER BY

Every lesson is open to read. Enrolling also lets you:

  • Mark lessons done and carry on where you stopped.
  • Take the tests of this lesson and its course.
  • Count the course towards the certificate for its chapter.
No payment. You can leave at any time.

Problems (3)

A stationery cupboard keeps its pens in a table Pens…Easy · High School (9–10) · by StemPy Lab
A student wants the name and the price of every product in a shop table called Products. He writes…Medium · High School (9–10) · by StemPy Lab
A film club keeps this table, Films. Complete the script so that its output is the rating and then…Easy · High School (9–10) · by StemPy Lab
Next: lesson 2WHERE: choosing the records
← SQL Queries: SELECT, WHERE and ORDER BY
← PreviousWHERE: choosing the records →

Discussion (0)

Log in to join the discussion.

Loading…

1 Python program
5 questions to check yourself

This lesson is free to read. Enroll free to keep your place and take the tests.