---
title: "SQL: Selecting, Filtering and Ordering Rows"
description: "Asking a database for the rows and columns you want with SELECT, WHERE and ORDER BY. The query says what is wanted, and the database decides how to fetch it."
canonical: https://lightmysky.com/learn/computing/sql-selecting-filtering-and-ordering-rows-mt_lDVVG2AKzs
source: https://lightmysky.com/learn/computing/sql-selecting-filtering-and-ordering-rows-mt_lDVVG2AKzs.md
retrieved: 2026-09-12
---

> **Agent view.** This is the Markdown twin of the page, for tools and assistants.
> When to use this site, and the call that answers each job: https://lightmysky.com/agent-instructions.md
> API description (OpenAPI 3.1): https://lightmysky.com/openapi.json · Authentication: https://lightmysky.com/auth.md
> Pricing: https://lightmysky.com/pricing.md · Catalog: https://lightmysky.com/llms.txt · Full catalog: https://lightmysky.com/llms-full.txt
> Every machine-readable file on this domain: https://lightmysky.com/.well-known/ai-catalog.json
> Ask for Markdown with `Accept: text/markdown`, a `.md` address, or `?mode=agent`.

# SQL: Selecting, Filtering and Ordering Rows

Asking a database for the rows and columns you want with SELECT, WHERE and ORDER BY. The query says what is wanted, and the database decides how to fetch it.

Subject: Computing · Area: Computer Systems · Ages 17 to 18
Page: https://lightmysky.com/learn/computing/sql-selecting-filtering-and-ordering-rows-mt_lDVVG2AKzs

## Ready when they can

- Write a query that returns named columns for the rows matching a condition
- Combine conditions with AND, OR and NOT and predict which rows survive
- Sort results and limit them, and say what changes when the order is reversed

## Lesson: Ask the database a precise question

SQL is the language for asking questions of relational databases. SELECT names the columns you want, FROM names the table, and WHERE keeps only the rows that pass your test. The query says what you want, and the database decides how to fetch it.

**Example.** A library keeps a Books table with title, author, and borrowed_on columns. SELECT title, author FROM Books WHERE borrowed_on > '2025-06-01' returns just those columns for books borrowed after that date. ORDER BY title sorts the survivors by title, and LIMIT 10 trims the answer to the first ten.

Conditions combine with AND, OR, and NOT. AND keeps rows passing both sides, OR keeps rows passing either side, and NOT flips a test to keep the opposite rows. To predict an answer, apply the WHERE tests row by row and keep only rows where the whole condition holds.

**Tip.** Read every query as a pipeline: pick columns, keep rows, sort, then cut. Reversing the sort swaps the first and last rows, which is a handy check that ordering controls the output. Remember the tables must exist before any SELECT runs, because queries name tables and columns from the schema.

**Recap.** Name your columns, filter your rows, then sort and trim the answer.

## Practice

8 questions on this page, each with its working shown.

## Needs first

- [Boolean Expressions and Comparison Operators](https://lightmysky.com/learn/computing/boolean-expressions-and-comparison-operators-mt_0aIh-DPCZ4)
- [Sorting and Filtering a Table to Answer a Question](https://lightmysky.com/learn/computing/sorting-and-filtering-a-table-to-answer-a-question-mt_6-UzZXVWwV)
- [Normalisation to Third Normal Form](https://lightmysky.com/learn/computing/normalisation-to-third-normal-form-mt_cbRXVW6KxX)

## Opens up

- [SQL: Joining Tables and Summarising Groups](https://lightmysky.com/learn/computing/sql-joining-tables-and-summarising-groups-mt_0QPZDE2KuS)
- [SQL at Depth: Subqueries, Set Operations and Views](https://lightmysky.com/learn/computing/sql-at-depth-subqueries-set-operations-and-views-mt_SLKUlC-7or)
