← Back to SQL, page 23

sql-0451 · Easy fresher

A filter salary BETWEEN 100000 AND 60000 returns no rows. What is wrong?

BETWEEN a AND b means value >= a AND value <= b, so the lower bound must come first. With the bounds reversed, no value can satisfy both conditions. Swap them, or compute the bounds with min() and max() when they come from user input.

Full answer
sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

The query as written:

sql
SELECT name, salary FROM employees WHERE salary BETWEEN 100000 AND 60000;
namesalary

(0 rows)

No salary is both at least 100000 and at most 60000, so the result is empty and no error is raised. Range filters built from two user-entered values hit this whenever the user enters them in the other order.

A corrected version:

sql
SELECT name, salary FROM employees WHERE salary BETWEEN 60000 AND 100000 ORDER BY salary DESC;
namesalary
Ravi95000
Mei95000
Tom70000
Sara62000

With the bounds in order, Ravi, Mei, Tom and Sara are returned. For user input, BETWEEN min(:a, :b) AND max(:a, :b) (SQLite's scalar min and max) or LEAST and GREATEST in PostgreSQL protect against reversed values. PostgreSQL also offers BETWEEN SYMMETRIC, which accepts bounds in either order (not run here).

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

SELECT name, salary FROM employees WHERE salary BETWEEN 100000 AND 60000;

SELECT name, salary FROM employees WHERE salary BETWEEN 60000 AND 100000 ORDER BY salary DESC;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • BETWEEN needs low then high
  • Reversed bounds silently return nothing
  • Normalise user bounds with min and max
  • PostgreSQL has BETWEEN SYMMETRIC

Common mistakes

  • Passing user-entered bounds unchecked
  • Assuming BETWEEN is order-independent

Follow-ups

  • Is BETWEEN inclusive at both ends?
  • How would you write an exclusive upper bound?

Tips

  • Expand BETWEEN into two comparisons