🌱BeginnerFiltering & Sorting

BETWEEN, Column Aliases, and Table Aliases

Use BETWEEN for range checks and AS aliases for readable queries.

Overview

BETWEEN provides an inclusive range check: val BETWEEN low AND high is equivalent to val >= low AND val <= high. It works on numbers, dates, and strings. NOT BETWEEN excludes the range. Aliases (AS) give a temporary name to columns or tables. Column aliases appear in result headers and can be used in ORDER BY. Table aliases shorten table names and are required when joining a table to itself (self-join). The AS keyword is optional you can write col alias or table_name alias directly.

Key Points

  • •BETWEEN low AND high inclusive on both ends
  • •NOT BETWEEN the inverse: outside the range
  • •Column alias: SELECT price * 1.1 AS price_with_tax
  • •Column alias usable in ORDER BY but not WHERE
  • •Table alias: FROM orders o shorten long names
  • •Table alias required for self-joins

Examples

BETWEEN for ranges

-- Numeric range (inclusive)
SELECT * FROM products WHERE price BETWEEN 10 AND 50;

-- Date range
SELECT * FROM orders
WHERE created_at BETWEEN '2024-01-01' AND '2024-12-31';

-- NOT BETWEEN
SELECT * FROM products WHERE price NOT BETWEEN 10 AND 100;

Column and table aliases

-- Column alias
SELECT
  first_name || ' ' || last_name  AS full_name,
  salary * 12                      AS annual_salary,
  UPPER(department)                AS dept
FROM employees;

-- Table alias (shorter)
SELECT o.id, o.total, c.name AS customer
FROM orders o
JOIN customers c ON c.id = o.customer_id;

Exercises

Write SQL to solve each exercise. Your progress is saved automatically.

Loading editor…

Completed: 0 / 5 exercises

Knowledge Check

Question 1 of 4

Is BETWEEN inclusive or exclusive of the boundary values?