ByeBuy.ai
BUILD YOUR ESCAPE ROUTE · ✦ CURSOR · HOST IT · ◫ SUPABASE · CONNECT IT · ↯ RELAY · BUILD YOUR ESCAPE ROUTE · ✦ CURSOR · HOST IT · ◫ SUPABASE · CONNECT IT · ↯ RELAY ·
← BYEBUY NOTES

September 12, 2026

SQL IS HOW YOU ASK A DATABASE A PRECISE QUESTION

SQL Is How You Ask a Database a Precise Question

In Class 20 you designed tables that remember: a companies table holds one row per company, a research_reports table holds one row per report, and a stable ID links each report to its company. The tables exist. Now the question is practical: how do you actually ask them something?

You ask in SQL — Structured Query Language. Think of it as writing a precise request slip and handing it to the database.

Tables hold records; SQL asks questions

A database table is a grid: columns name the fields, rows hold the records. If Sonariq has stored fifty research reports, you cannot usefully scroll through all of them every time. You need to say things like "show me the recent reports, newest first, just twenty of them" — and have the database return exactly that set.

That sentence is already almost SQL:

Plain EnglishSQL clause
Show me these fieldsSELECT
From this tableFROM
Keeping only rows where…WHERE
Newest firstORDER BY … DESC
Just twenty of themLIMIT

Here is one complete, safe query against Sonariq's reports table:

SELECT company_name, report_date, rating
FROM research_reports
WHERE report_date >= '2026-01-01'
ORDER BY report_date DESC
LIMIT 20;

Read it top to bottom in ordinary English: "From the research reports table, take rows dated 2026 or later, show me the company name, date, and rating, newest first, and stop after twenty rows."

That is the whole mental model for this lesson. If you can read that sentence, you can read SQL.

The five clauses, one at a time

SELECT — which columns. Name exactly the fields you want. SELECT * means "all columns": fine for a quick peek at an unfamiliar table, wasteful as a habit.

FROM — which table. Every query names its source. Get this wrong and nothing else matters, so confirm the table name first.

WHERE — which rows survive. WHERE report_date >= '2026-01-01' keeps only matching rows. Common forms: =, >, LIKE for partial text, IN for a list. Rows that fail the test do not appear in the answer.

ORDER BY — what order. No promised order unless you ask. DESC is newest first, ASC oldest first; add a second key for ties.

LIMIT — how many. LIMIT 20 caps the answer. During exploration this is protection, not politeness: a typo in WHERE turns a 5-row expectation into thousands without it. Make LIMIT a reflex.

A note on internal order (not your burden yet)

You read a query top to bottom: SELECT, FROM, WHERE, ORDER BY, LIMIT. Internally, the database works in a different order — roughly, it finds the table first, filters rows, then picks columns and sorts. Database engineers memorize that sequence because it affects performance and advanced queries.

You do not need to memorize it now. What matters is the reading skill: each clause answers one question (which fields, which table, which rows, which order, how many). If your question is clear in English, the SQL follows. When you ask AI to draft a query, that English sentence is what you hand over.

Result sets, NULLs, and LIMIT

A query answer is a result set: a temporary table of matching rows. Run the same query twice on unchanged data, get the same answer.

Two details that save confusion:

NULL means missing — not zero, not blank. A report with no rating yet holds NULL: the value was never entered. WHERE rating = 0 will not find it. Use WHERE rating IS NULL for "no rating recorded" and IS NOT NULL for "has a rating."

LIMIT belongs on exploratory queries, always. Remove it only when the product genuinely needs the full set, and then deliberately.

Read-aloud drill: three queries, increasing difficulty

Read each query aloud top to bottom, then check yourself against the gloss. Each one hides one trap — find it before you would run it.

Drill 1 — one filter, one sort.

SELECT company_name, report_date, rating
FROM research_reports
WHERE sector = 'Energy'
ORDER BY report_date DESC
LIMIT 10;

Gloss: "From the research reports table, keep only Energy rows, show company name, date, and rating, newest first, stop after ten rows."

Trap: if the draft said SELECT * instead of the three columns, it returns every column including the long summary text. Extra columns you did not ask for are still imprecision — ask the AI to name only the columns in your question.

Drill 2 — two filters, one sort.

SELECT company_name, report_date, rating
FROM research_reports
WHERE sector = 'Healthcare'
  AND rating = 'Buy'
ORDER BY report_date DESC
LIMIT 10;

Gloss: "Healthcare reports rated Buy, newest first, ten at most."

Trap: this draft is missing its LIMIT in the wild — without it, a typo like rating = 'buy' (lowercase, matching nothing) or a missing filter returns the whole table instead of ten rows. No LIMIT on an exploratory query means do not run it yet.

Drill 3 — text search with a tie-break sort.

SELECT company_name, report_date, rating
FROM research_reports
WHERE summary LIKE '%grid%'
  AND report_date >= '2026-01-01'
ORDER BY report_date DESC, company_name ASC
LIMIT 20;

Gloss: "2026 reports whose summary mentions 'grid', newest first with ties broken alphabetically by company, twenty at most."

Trap: LIKE '%grid%' matches any mention anywhere, including "off-grid" or "gridlock" — broader than "about the power grid." Before running, ask: is partial-text matching what I meant, or did I want an exact sector = 'Energy' filter? Vague match, vague answer.

Practice: cover the gloss, read only the SQL, and reconstruct the English question aloud. Then name the trap. If you can do all three without peeking, you can read simple queries.

Let AI explain, draft, and sample before you run

The safe beginner workflow:

1. State the question in English. "Company name, date, and rating of reports from 2026 onward, newest first, twenty at most." 2. Name the table and fields. "Table: research_reports. Fields: company_name, report_date, rating." 3. Ask for three things: a read-only SELECT, a one-sentence explanation, and the expected result shape (columns, row count). 4. Read the draft before running it. Every field and filter must match your question. It must start with SELECT with no changing words (Lesson 21.4 names them). 5. Run it somewhere safe — a development database or sample dataset — and compare the first rows against expectations.

A reusable prompt:

I have a table called research_reports with columns
company_name, report_date, rating, sector, summary.
Business question: [your sentence here].
Write a read-only SELECT query with a LIMIT, explain
in one sentence what it returns, and describe the
expected columns and row count. Do not write any
query that changes data.

AI writes the syntax quickly; you own the question. If the English is vague ("show me the good reports"), the SQL will be vague too. Precision in, precision out.

Visual: English → SQL → table

ENGLISH                        SQL                          RESULT SET
"company name, date,           SELECT company_name,         company_name | report_date | rating
 rating of 2026+                report_date, rating          -------------+-------------+-------
 reports, newest first,        FROM research_reports         Acme Corp    | 2026-08-14  | Buy
 twenty at most"               WHERE report_date >=          Beta Inc     | 2026-07-02  | Hold
                                '2026-01-01'                 ...
                               ORDER BY report_date DESC
                               LIMIT 20;

Draw one arrow from each English phrase to its SQL clause, and one arrow from each clause to the part of the result table it controls. When something looks wrong in the answer, that mapping tells you which clause to fix.

Practical exercise: three questions, three drafts

1. Pick a table you know — research_reports or a table from your own Class 20 sketch. Write down its exact table name and five real column names. 2. Write three plain-English questions, each using at least one filter, one sort, and a limit. Example: "Show titles and dates of healthcare reports rated Buy, newest first, ten at most." 3. Paste each question into an AI assistant with the safe prompt above. Ask for read-only drafts only. 4. For each draft, check field-by-field: does every SELECT column appear in your question? Does every WHERE condition match a condition you stated? Is there a LIMIT? Does it start with SELECT?

Finish line: three English questions plus three AI-drafted queries, each annotated with one line confirming the field/filter match.

Verify: cover the English question and read only the SQL — can you reconstruct the question? If not, the draft drifted.

Common failure mode: accepting a draft that selects extra columns or adds a filter you never asked for. Extra precision you did not request is still imprecision — ask the AI to remove it.

Check your understanding

1. What does each of SELECT, FROM, WHERE, ORDER BY, and LIMIT control? 2. Why should exploratory queries almost always include a LIMIT? 3. What is a NULL, and why does WHERE rating = 0 miss unrated reports? 4. Before running an AI-drafted query, which three things should you confirm?

ARTICLE DISCUSSION

JOIN THE
CONVERSATION.

0 COMMENTS

BYEBUY ACCOUNT ACCESS

Sign in

Use your account to save routes and make the catalogue yours.

Enter your email and we’ll send a secure sign-in link and code.

NEW ROUTES ADDED WEEKLY · 9,235 CATALOGUE ENTRIES · BUILD · DEPLOY · QUERY · STACK · SAY BYE TO BUY · NEW ROUTES ADDED WEEKLY · 9,235 CATALOGUE ENTRIES · BUILD · DEPLOY · QUERY · STACK · SAY BYE TO BUY ·