sqlcmd.net validated sql reference
intermediate joining MySQL MariaDB SQL Server PostgreSQL SQLite

Join Tables On A Range Condition

Join two tables using a comparison other than equality — such as `BETWEEN` or `<` — to match rows based on ranges rather than exact values.

Docker-validated Not currently validation-green

Assign each employee to the salary bracket they fall into

The ON clause uses BETWEEN instead of =. For each employee, the database checks every bracket row to see if the salary falls within [min_sal, max_sal]. Alice (55,000) matches Junior (0–60,000). Bob (85,000) matches Senior (60,001–100,000). Carol (120,000) matches Principal (100,001–999,999). Each employee matches exactly one bracket because the ranges do not overlap.

Rows loaded before the example query runs.
Setup
CREATE TABLE employees (name VARCHAR(50), salary INT);

INSERT INTO
  employees
VALUES
  ('Alice', 55000),
  ('Bob', 85000),
  ('Carol', 120000);

CREATE TABLE brackets (bracket VARCHAR(20), min_sal INT, max_sal INT);

INSERT INTO
  brackets
VALUES
  ('Junior', 0, 60000),
  ('Senior', 60001, 100000),
  ('Principal', 100001, 999999);
Shared across supported engines.
SQL
SELECT
  e.name,
  e.salary,
  b.bracket
FROM
  employees e
  JOIN brackets b ON e.salary BETWEEN b.min_sal AND b.max_sal
ORDER BY
  e.name;
Returned rows for the shared example.
namesalarybracket
Alice55000Junior
Bob85000Senior
Carol120000Principal

Identical syntax and result across all engines.

Where this command helps.

  • mapping a numeric value to the bracket or tier it falls into
  • joining time-series rows to the configuration that was active during that period

What the command is doing.

Most joins use equality (ON a.id = b.id), but the ON clause accepts any comparison expression. A non-equi join matches rows where a value from one table falls within a range defined by another table. Common uses include looking up a salary bracket, assigning a discount tier, or mapping a score to a category. Because non-equi joins can match each row in one table to multiple rows in the other, they can multiply output rows — use them with care and verify cardinality. The syntax is identical across all major engines.