Simple SQL Queries

Reading one table with SELECT, FROM and WHERE. Columns and computed expressions, with body mass index recomputed in SQL for 8,005 people; integer division and the order in which operators bind, where moving one pair of parentheses changes a count from 3,831 to 2,742; NULL and SQL's three-valued logic, which makes a condition and its negation together miss 1,231 people; how SQLite compares numbers with text and treats double quotes, and what the other dialects do; and the declarative nature of a query, shown by the same query running with two different plans. On the NHANES 2017-2018 database in SQLite.

A query reads one table with FROM, keeps the rows whose WHERE condition is true (not false, not NULL) and computes columns in SELECT: a computed body mass index, rounded to one decimal, matches the published one for all 8,005 NHANES 2017-2018 people measured. Integers divide by truncation in SQLite, PostgreSQL and SQL Server (7 / 2 is 3) but not in MySQL. AND binds before OR: moving one pair of parentheses changes a count from 3,831 to 2,742, a difference of exactly the 1,089 men aged 60 and over. Any comparison with NULL is unknown, so a condition and its negation together miss the 1,231 people with no income ratio; IS NULL finds them. An index changes the plan, not the rows returned.

A first query against the NHANES database of Relational Databases asks for the adults:

SELECT seqn, sex, age
FROM person
WHERE age >= 18
ORDER BY seqn
LIMIT 3;
seqn sex age
93705 female 66
93706 male 18
93708 female 66

The statement has the shape of almost every SQL query. FROM names the table to read. WHERE keeps the rows for which a condition is true. SELECT says which columns, or which values computed from them, to return. The last two lines, which sort the result and keep the first three rows, are covered in Special Operators and Clauses. Without them the query returns all 5,856 people aged 18 or over, in no promised order.

This article covers what can go in those three clauses: columns and expressions, operators and the order in which they bind, and conditions, including the behavior of NULL, which accounts for a large share of wrong answers from correct-looking SQL. The examples run in SQLite, the database built into Python, and the differences from PostgreSQL, MySQL and SQL Server are noted where they change a result.

SELECT and FROM

The SELECT list names the columns of the result, in the order wanted. SELECT * returns all columns of the table, which is convenient for a first look and fragile in code that is kept, because the result changes when a column is added to the table.

The list may contain expressions as well as columns. The examination table stores weight in kilograms, height in centimeters and the published body mass index, BMI, which is weight divided by the square of height in meters. The query can recompute it:

SELECT seqn, weight, height, bmi,
       weight / ((height / 100) * (height / 100)) AS bmi_computed
FROM exam;
seqn weight height bmi bmi_computed
93703 13.7 88.6 17.5 17.452
93704 13.9 94.2 15.7 15.664
93705 79.5 158.3 31.7 31.725

The expression is evaluated once for each row, with the column names replaced by that row's values. AS gives the computed column a name; without it the result column is named after the expression text. Rounded to one decimal, the computed value agrees with the published bmi for all 8,005 people who have a weight, a height and a BMI, the same check that Enriching Data makes in pandas.

The result of a query is itself a table, with named columns and rows, but it is not stored anywhere. That it is a table is what allows queries to be nested inside other queries, the subject of Subqueries.

Operators

Arithmetic

SQL has the usual arithmetic operators +, -, *, / and % (remainder), and their behavior on integers is the first place where dialects part ways.

Expression SQLite result
7 / 2 3
7 / 2.0 3.5
CAST(7 AS REAL) / 2 3.5
7 % 2 1
-7 / 2 -3

When both operands are integers, SQLite performs integer division, truncating toward zero (SQLite, n.d.-c). PostgreSQL and SQL Server do the same (PostgreSQL Global Development Group, n.d.-a; Microsoft, n.d.-a). MySQL does not: its / returns a decimal, 3.5000, and it has a separate operator, DIV, for integer division (MySQL, n.d.-a). The truncation is useful on purpose, age / 10 * 10 turns an age into its decade, and harmful by accident, when a proportion computed as a count divided by a count comes out as 0, an error shown in Aggregate Functions. Writing one operand as a real number, 2.0, or converting it with CAST, forces real division in every dialect.

Some notations differ more. String concatenation is || in standard SQL, SQLite, PostgreSQL and Oracle, while MySQL uses the function CONCAT: in MySQL's default mode || is a (deprecated) spelling of logical OR, and it concatenates only when the PIPES_AS_CONCAT mode is switched on (MySQL, n.d.-b). There is no standard exponentiation operator: ^ raises to a power in PostgreSQL (PostgreSQL Global Development Group, n.d.-a), means bitwise exclusive or in MySQL and SQL Server (MySQL, n.d.-c; Microsoft, n.d.-b), and is a syntax error in SQLite ("unrecognized token"). The function POWER(x, y) is the more portable spelling, and in SQLite is available when its math functions are compiled in, as they are in the Python used for these articles.

Comparison and logic

The comparison operators are =, <> (also written !=), <, <=, > and >=. Equality is a single =, not Python's ==, although SQLite accepts both. Comparisons work on text as well as numbers, but text compares character by character, so '10' < '9' is true: the character 1 sorts before 9. A number stored as text sorts like text, which is one reason column types matter.

The logical operators are AND, OR and NOT. They combine conditions, and the order in which they bind decides what a condition means. In SQLite the order from tightest to loosest is: unary minus and the other prefix operators, then COLLATE, then the concatenation || (with the JSON operators -> and ->>), then *, / and %, then + and -, then the bitwise operators, then ESCAPE, then the comparisons (with <, <=, > and >= binding more tightly than = and <>), then NOT, then AND, then OR (SQLite, n.d.-c). Arithmetic binds before comparison, so weight / 2 > 40 compares half the weight with 40, as intended. The trap is AND and OR. Suppose the question is how many people are women or girls who are either 60 and over or under 18:

SELECT COUNT(*) FROM person
WHERE age >= 60 OR age < 18 AND sex = 'female';      -- 3,831

SELECT COUNT(*) FROM person
WHERE (age >= 60 OR age < 18) AND sex = 'female';    -- 2,742

The first query binds AND first and means "60 and over, or a girl under 18": the 2,150 people aged 60 and over of both sexes plus the 1,681 girls, 3,831 in all. Only the parenthesized version answers the question. Both run without error, and the wrong one returns a plausible number. Parentheses cost nothing and remove the question of precedence entirely; it is good practice to use them whenever AND and OR are mixed.

WHERE and three-valued logic

WHERE keeps a row when its condition is true. The SQLite documentation states the rule precisely: "Rows are excluded from the result if the WHERE clause evaluates to either false or NULL" (SQLite, n.d.-e). The word NULL in that sentence is the key to this section. A condition in SQL can have three values, not two.

Comparisons with NULL

NULL marks a missing value (Relational Databases describes the kinds of missing it stands for). Any comparison with it returns NULL, read as "unknown", because nothing is known about the missing value. That includes comparing NULL with NULL:

Expression Result
NULL = NULL NULL
NULL <> NULL NULL
NULL IS NULL 1 (true)
1 + NULL NULL
NULL * 0 NULL

Arithmetic with NULL is also NULL, even multiplication by zero. When the SQLite developers set out to match other engines' handling of NULL, they tested PostgreSQL, Oracle, DB2, SQL Server, MySQL and others, and on these cases, and on the logic below, found them in agreement apart from one old version of MySQL (SQLite, n.d.-d). The consequence for queries is immediate. The ratio of family income to the poverty line is missing for 1,231 people, but a query that looks for them with = finds none:

SELECT COUNT(*) FROM person WHERE income_ratio = NULL;    -- 0
SELECT COUNT(*) FROM person WHERE income_ratio IS NULL;   -- 1,231

income_ratio = NULL is unknown for every row, so no row is kept. Testing for a missing value needs the operators IS NULL and IS NOT NULL, which always return true or false.

The truth tables

When conditions are combined, AND, OR and NOT extend to the third value by a consistent rule: if the result would be the same whatever the unknown value turned out to be, the result is that value; otherwise it is unknown. TRUE OR unknown is true, because an OR with one true side is true either way. FALSE AND unknown is false. TRUE AND unknown is unknown, because the answer depends on the missing value. SQLite computes the full tables (1 is true, 0 false):

p q p AND q p OR q NOT p
1 1 1 1 0
1 0 0 1 0
1 NULL NULL 1 0
0 1 0 1 1
0 0 0 0 1
0 NULL 0 NULL 1
NULL 1 NULL 1 NULL
NULL 0 0 NULL NULL
NULL NULL NULL NULL NULL

SQLite's documentation notes that its operators handle NULL "in accordance with the SQL92 standard" (SQLite, n.d.-c).

A condition and its negation do not cover everything

In two-valued logic, every row satisfies either a condition or its negation. In SQL, rows where the condition is unknown satisfy neither:

SELECT COUNT(*) FROM person WHERE income_ratio < 1;          -- 1,837
SELECT COUNT(*) FROM person WHERE NOT (income_ratio < 1);    -- 6,186
SELECT COUNT(*) FROM person
WHERE income_ratio < 1 OR NOT (income_ratio < 1);            -- 8,023

1,837 people live below the poverty line and 6,186 at or above it, 8,023 together; the remaining 1,231 of the 9,254 are in neither group, and even the condition "below or not below" excludes them. The same thing happens with <>. A query for everyone whose income ratio is not at the top code of 5 returns 6,815 rows and silently drops the 1,231 people whose ratio is unknown. Whether they belong in the answer is a question about the analysis, not about SQL, but it has to be asked. Writing income_ratio <> 5 OR income_ratio IS NULL, or in SQLite the shorter income_ratio IS NOT 5, includes them (8,046 rows). The standard spelling of the last form is IS DISTINCT FROM, which PostgreSQL supports and SQLite accepts from version 3.39.0 (SQLite, n.d.-f); the SQLite 3.38.4 in the Python used for this site's tests rejects it with a syntax error. SQLite's compact IS and IS NOT between two values are an extension (SQLite, n.d.-c).

The practical rule is to ask, for every condition on a column that can be NULL, which side the NULL rows should fall on, and to say so in the query. Special Operators and Clauses shows the same trap with NOT IN, where it can make a query return nothing at all.

Literals, quotes and types

Text literals take single quotes: sex = 'male'. In standard SQL, double quotes are for identifiers, names of tables and columns that would otherwise be illegal, such as a column called "All Cause". SQLite accepts double-quoted strings too, for compatibility with early MySQL, and this has a consequence its developers regret. If a double-quoted name does not match any column, SQLite treats it as a string. A misspelled column in double quotes is therefore not an error:

SELECT COUNT(*) FROM person WHERE "sexx" = 'male';   -- 0, no error
SELECT COUNT(*) FROM person WHERE sexx = 'male';     -- Error: no such column: sexx

The first query compares the string 'sexx' with 'male' for every row and returns 0. The SQLite documentation calls the feature a "misfeature" that lures newcomers "into the bad habit of using double-quoted string literals" (SQLite, n.d.-a). PostgreSQL would reject both queries with an error about the unknown column. Single quotes for strings and double quotes only for identifiers is the portable habit.

Comparisons between numbers and text follow each product's type rules. In SQLite, 1 = '1' is false, because an integer is not a string; the documentation notes that "every other major SQL database engine says this is true" (SQLite, n.d.-a). But when one side is a column with a numeric type, SQLite first converts a text operand that looks like a number, so age = '80' and age = 80 both find the 427 people recorded as 80 (SQLite, n.d.-b). The safe practice is not to rely on either behavior and to compare numbers with numbers.

The order in which clauses take effect

A query is written SELECT ... FROM ... WHERE ..., but its parts take effect in a different order. Logically, FROM comes first and produces the input rows; WHERE filters them; then SELECT computes the output columns for each surviving row; then ORDER BY sorts and LIMIT cuts. The SQLite documentation describes this sequence, with the caution that it "is purely illustrative" and that no engine "is required to follow this or any other specific process" (SQLite, n.d.-e); what is fixed is the result, not the procedure. Aggregate Functions adds grouping to the sequence and draws it in full.

The order explains a rule of standard SQL: a column alias defined in SELECT cannot be used in WHERE, because WHERE takes effect before the alias exists. PostgreSQL rejects the following query, its documentation stating that an output column's name can be used in ORDER BY and GROUP BY "but not in the WHERE or HAVING clauses" (PostgreSQL Global Development Group, n.d.-b); SQLite, as an extension, accepts it and returns the four people whose computed BMI exceeds 70:

SELECT seqn, weight / ((height / 100) * (height / 100)) AS bmi_computed
FROM exam
WHERE bmi_computed > 70;

The portable form repeats the expression in the WHERE clause, or computes it in a subquery or common table expression and filters outside (Subqueries). An alias can be used in ORDER BY in every dialect, because sorting comes after SELECT.

Declarative queries and query plans

A query describes a result, not a method. The database's query planner chooses the method, and SQLite will report its choice when a query is prefixed with EXPLAIN QUERY PLAN. For the query

SELECT seqn, bmi FROM exam WHERE bmi > 70;

the plan on the table as built is SCAN exam: read every one of the 8,704 rows and test each. After CREATE INDEX exam_bmi ON exam (bmi), the plan for the unchanged query becomes SEARCH exam USING COVERING INDEX exam_bmi (bmi>?): jump to the first entry above 70 in the sorted index and read from there. Both plans return the same four people (in different orders, as Relational Databases showed). On a table of millions of rows the difference in time can be large, and it is obtained without changing the query. This is the practical meaning of Codd's data independence, and the reason a data scientist can usually write the query that states the question and leave the plan to the database. When a query is slow, EXPLAIN QUERY PLAN (EXPLAIN in PostgreSQL and MySQL) is the first place to look.

Comments and layout

Two hyphens start a comment that runs to the end of the line, and /* ... */ encloses a comment of any length. Commenting out a line is a common way to try a query with and without a condition:

SELECT seqn, age      -- the person and age
FROM person
-- WHERE age >= 18
ORDER BY age DESC, seqn;

Keywords are not case-sensitive and line breaks are ignored, so layout is for readers. The convention used in these articles, keywords in capitals and each clause on its own line, makes the structure of a query visible at a glance. A semicolon ends a statement; Python's sqlite3 runs one statement per call and does not need it.

Worked problems

The three problems prove the logic behind the article's examples and show one more failure of the same kind. The review-pass block of simple-sql-queries.py checks each on SQLite 3.49.1.

Problem 1 (the three-valued truth tables as minimum and maximum). Show that SQL's AND, OR and NOT are the minimum, the maximum and the complement when true, unknown and false are given the values 1, ½ and 0, and use this to prove that a condition, its negation and its NULL test split any table exactly.

Approach. The rule stated with the truth tables is: if the result would be the same whatever the unknown value turned out to be, the result is that value; otherwise it is unknown. Check the rule against the formulas case by case, then read the consequences off the formulas.

AND. If either operand is false, the conjunction is false whatever the other is, and the minimum is 0. If both are true, both give 1. Otherwise neither is false and at least one is unknown, so the minimum is ½; and replacing the unknown operands by true gives true while replacing one of them by false gives false, so the rule gives unknown as well. OR. The same argument with the roles of true and false exchanged gives the maximum. NOT. Negation exchanges true and false and leaves unknown unknown, which is $1 - p$. SQLite's results agree with the formulas on all nine pairs of operands. These are the tables of the strong three-valued logic that Kleene (1952, §64) sets out for predicates that may be undefined, crediting it to a paper of his from 1938.

Consequences. Two of the usual laws survive and one does not. De Morgan's laws hold, because $1 - \min(p, q) = \max(1 - p, 1 - q)$, and SQLite confirms NOT (p AND q) and NOT p OR NOT q agree in all nine cases. The law of the excluded middle fails: $\max(p, 1 - p) = \tfrac{1}{2}$ when $p = \tfrac{1}{2}$, so p OR NOT p is unknown for a row where p is unknown, which is why the third query of the article's section on negation still excluded 1,231 people. For any condition $p$ and any row, exactly one of p, NOT p and (p) IS NULL is true, since the row's value of $p$ is exactly one of 1, 0 and ½. Counting rows therefore gives, for every condition,

$$\#\{p\} + \#\{\text{NOT } p\} + \#\{p \text{ IS NULL}\} = N.$$

For income_ratio < 1 the three counts are 1,837, 6,186 and 1,231, and for income_ratio <> 5 they are 6,815, 1,208 and 1,231; both sum to the 9,254 people. The identity is a cheap check to run on any important filter.

Problem 2 (what the misplaced parentheses cost). The two queries of the section on comparison and logic returned 3,831 and 2,742. Prove that the difference is exactly the number of men aged 60 and over.

Approach. Write $A$ for age >= 60, $B$ for age < 18 and $C$ for sex = 'female'. Neither column is ever NULL here, so ordinary two-valued logic applies. Because AND binds first, the first query is $A \lor (B \land C)$; the second is $(A \lor B) \land C$.

Solution. By distribution, $(A \lor B) \land C = (A \land C) \lor (B \land C)$. Both disjuncts imply $A \lor (B \land C)$, so every row the second query keeps, the first keeps too. A row kept by the first and not the second satisfies $A \lor (B \land C)$ but neither $A \land C$ nor $B \land C$. It cannot satisfy $B \land C$, so it satisfies $A$; and since it fails $A \land C$, it fails $C$. Conversely a row satisfying $A \land \lnot C$ is kept by the first query and fails both disjuncts of the second. The difference is therefore exactly $A \land \lnot C$, the men aged 60 and over. On the data there are 1,089 of them, and $3{,}831 - 2{,}742 = 1{,}089$. The second count splits as well: $A$ and $B$ cannot both hold, so $(A \land C) \lor (B \land C)$ is a disjoint union of 1,061 women aged 60 and over and 1,681 girls, 2,742 in all. The proof holds on any data; the numbers only confirm it.

Problem 3 (numbers stored as text). The section on comparisons noted that '10' < '9'. What happens to queries on age if the column is stored as text, and what restores the right answers?

The experiment. Copy seqn and age into a table whose age column is declared TEXT, with every age converted to a string, and rerun three queries. MAX(age) returns '9', not '80', because text compares character by character and '9' sorts after every string beginning with 1 to 8. Sorted, the ages begin '0', '1', '10', '11'. The condition age > 18 counts 7,224 people where the numeric column gives 5,712.

Why the count changes by 1,512. SQLite's comparison rules convert the operands before comparing (SQLite, n.d.-b). A column with TEXT affinity compared with a number that has no affinity turns the number into text, so age > 18 becomes the text comparison age > '18', and the two queries return the same 7,224. As text, every two-digit age from 19 up still exceeds '18', and 10 to 17 still do not. The one-digit ages 2 to 9 are the difference: each is greater than '18' as a string, because its first character exceeds 1, and 1,512 people have such an age. The count rises by exactly that number.

What works. Equality is unaffected in this direction, since age = 80 converts 80 to '80' and still finds the 427 people recorded at 80. CAST(age AS INTEGER) > 18 restores the count of 5,712, and so does declaring the column INTEGER, which converts the text to numbers as it is stored. The lesson is the one stated in the section on types: compare numbers with numbers, and give numeric data a numeric type, or a STRICT table that enforces it (Relational Databases).

Open questions

  • When does the index pay? The plan for bmi > 70 switched from a scan to an index search when an index existed. Timing both plans, 30 runs each, on copies of exam enlarged 10 to 1,000 times, and for less selective conditions such as bmi > 25, would show where the planner's choice changes and whether it is the faster one.
  • Do the other dialects return what their documentation says? The dialect notes in this article come from documentation, not from running the queries. Running the arithmetic, concatenation, NULL and quoting examples in PostgreSQL, MySQL and SQL Server, in containers pinned to stated versions, would confirm them or correct them.
  • How often does an analysis condition fall on a nullable column without saying where its NULLs go? A scan of the SQL in this repository's experiment scripts for WHERE conditions on columns that hold NULLs, checked with the counting identity of Problem 1, would measure how common the silent exclusion is in practice.

Where it leads

A query reads a table with FROM, keeps rows whose condition is true, not merely not false, with WHERE, and computes its columns in SELECT; the database chooses the method. Special Operators and Clauses adds the operators IN, BETWEEN, LIKE and CASE and the clauses that shape a result, DISTINCT, ORDER BY and LIMIT, and shows the three-valued logic of this article at work in NOT IN. The index that changed the plan here is a sorted structure much like the dictionary of a search engine's inverted index; how databases build and choose indexes is a subject of its own.

In Python

import sqlite3

con = sqlite3.connect("nhanes.db")
rows = con.execute("""
    SELECT seqn, sex, age
    FROM person
    WHERE age >= ? AND income_ratio IS NOT NULL
""", (18,)).fetchall()

con.execute("EXPLAIN QUERY PLAN SELECT seqn FROM exam WHERE bmi > 70").fetchall()

The ? is a placeholder for a value supplied separately, the safe way to put Python values into a query, explained with the rest of the sqlite3 interface in Queries in Python. The numbers in this article are computed by tools/experiments/data-science/simple-sql-queries.py in the site's repository, which builds the database from the public NHANES files with sqldata.py.

References

Reviewed: verified, 23 Nov 2026 · citations checked, 23 Nov 2026 · editorial review, 23 Nov 2026 · 5 revisions