---
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-11-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

You have split facts into tables. SQL lets you request data from them. These examples use SQLite. The schema lists table and column names. SELECT names columns to return; FROM names the table. WHERE tests each row and returns it only when the condition is true. Requesting fewer columns does not delete stored data.

**Example.** Books has title, author and borrowed_on columns. borrowed_on stores dates as YYYY-MM-DD text, with no NULLs. SELECT title, author FROM Books WHERE borrowed_on > '2025-05-01' ORDER BY title ASC LIMIT 3; returns up to three matching rows. The date itself is excluded by >. Write these clauses in this order: SELECT, FROM, WHERE, ORDER BY, LIMIT.

AND needs both conditions to be true. OR needs at least one to be true, including when both are true. NOT reverses true and false. Parentheses show which tests belong together. SQL uses = to compare values. Quote text values, as in borrowed = 'yes'. If returned stores only 0 or 1, NOT (returned = 1) keeps rows with 0.

*(drawing: A row must pass both conditions to pass an AND test.)*

ORDER BY sorts matching rows. ASC, the default, is ascending; DESC is descending. Text order follows the database's comparison rules. Add a unique book_id to break title ties. LIMIT n returns at most n rows. Without ORDER BY, you cannot guarantee which rows a limit will return. To predict results, find matching rows, sort, then limit. This describes the result, not the engine's physical execution plan.

*(drawing: ASC gives Apple, Birch, Cedar. LIMIT 2 keeps Apple and Birch. DESC with LIMIT 2 keeps Cedar and Birch.)*

A query that names a table needs that table and its columns to exist. For an unknown-column error, compare the name with the schema. An empty table can return zero rows. For an unexpected empty result, check the selected database, stored values and filter before changing it. NULL means a missing value. Comparisons with NULL usually give unknown. WHERE keeps only true results; NOT unknown remains unknown.

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

*SQL lesson, questions and study cards written for this dataset. The query examples use SQLite.*

## Practice

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

## Needs first

- [Normalisation to Third Normal Form](https://lightmysky.com/learn/computing/normalisation-to-third-normal-form-mt_cbRXVW6KxX)

## Optional connections

- [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)

## 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)
