Ever need to map codes to labels without creating a whole new table? In YugabyteDB, you can use the SQL VALUES
clause like a temporary, inline table, making your queries cleaner and more self-contained.
Example:
Suppose you have an orders
table like this:
CREATE TABLE orders (
id SERIAL PRIMARY KEY,
status_code INT
);
INSERT INTO orders (status_code)
VALUES (1), (2), (3), (2), (1);
Now you want to map those numeric status codes to human-friendly labels like:
1 → ‘Pending’
2 → ‘Shipped’
3 → ‘Delivered’
Solution Using VALUES
as a Table:
SELECT o.id, o.status_code, s.label AS status_label
FROM orders o
JOIN (
VALUES
(1, 'Pending'),
(2, 'Shipped'),
(3, 'Delivered')
) AS s(code, label)
ON o.status_code = s.code;
Runing the SELECT:
yugabyte=# SELECT o.id, o.status_code, s.label AS status_label
yugabyte-# FROM orders o
yugabyte-# JOIN (
yugabyte(# VALUES
yugabyte(# (1, 'Pending'),
yugabyte(# (2, 'Shipped'),
yugabyte(# (3, 'Delivered')
yugabyte(# ) AS s(code, label)
yugabyte-# ON o.status_code = s.code;
id | status_code | status_label
----+-------------+--------------
5 | 1 | Pending
1 | 1 | Pending
4 | 2 | Shipped
2 | 2 | Shipped
3 | 3 | Delivered
(5 rows)
Why it’s cool:
- You don’t need a permanent
lookup
table. - Great for small, static mappings or ad-hoc transformations.
- Makes queries more self-contained (no external joins or dependencies).
Whether you’re doing quick reporting, lightweight ETL, or testing logic inline, VALUES
tables are a powerful to add to your SQL toolkit.
Have Fun!
