VALUES(7)

VALUES(7) ๐Ÿ’–

okay bestie, welcome to the VALUES page from the PostgreSQL 18.6 Documentation โ€” yes that PostgreSQL, the one that's literally the moment. sit down, grab a strawberry ๐Ÿ“, we're gonna build a whole table out of thin air.


โœจ NAME โœจ

VALUES โ€” compute a set of rows ๐Ÿซถ


๐ŸŒธ SYNOPSIS ๐ŸŒธ

VALUES ( expression [, ...] ) [, ...]
    [ ORDER BY sort_expression [ ASC | DESC | USING operator ] [, ...] ]
    [ LIMIT { count | ALL } ]
    [ OFFSET start [ ROW | ROWS ] ]
    [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ]

look at that. that's the whole vibe. no cap. ๐Ÿ’…


๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

so VALUES computes a row value โ€” or a whole lil set of row values โ€” that you specify with value expressions. most of the time you're using it to cook up a โ€œconstant tableโ€ inside a bigger command, but ngl it absolutely can stand on its own. main character energy, it doesn't need a supporting cast. ๐Ÿ‘‘

when you specify more than one row, every single row has to have the same number of elements. not optional. the data types of the resulting table's columns get figured out by combining the explicit or inferred types of the expressions sitting in that column, using the exact same rules as UNION (peep Section 10.5 if you wanna go deeper).

and here's the slay: inside larger commands, VALUES is syntactically allowed anywhere that SELECT is. the grammar literally treats it like a SELECT, which means you can pull up with ORDER BY, LIMIT (or the equivalent FETCH FIRST), and OFFSET on a VALUES command. iconic behaviour fr fr. โœจ


๐Ÿ’œ PARAMETERS ๐Ÿ’œ

  • expression โ€” a constant or expression to compute and drop into the indicated place in the resulting table (aka the set of rows). ๐ŸŽ€ special case: in a VALUES list that shows up at the top level of an INSERT, an expression can be swapped out for DEFAULT to say "hey, use the destination column's default value here". but heads up โ€” DEFAULT cannot be used when VALUES appears in any other context. periodt.
  • sort_expression โ€” an expression or an integer constant that says how to sort the result rows. this expression can refer to the columns of the VALUES result as column1, column2, etc. for the full tea, see ORDER BY Clause in the SELECT(7) documentation. ๐ŸŒˆ
  • operator โ€” a sorting operator. deets live in ORDER BY Clause in the SELECT(7) documentation. ๐Ÿงš
  • count โ€” the maximum number of rows to return. more info in LIMIT Clause in the SELECT(7) documentation. โญ
  • start โ€” the number of rows to skip before you start returning rows. same deal, see LIMIT Clause in the SELECT(7) documentation. ๐Ÿ’ซ

๐ŸŽ€ NOTES ๐ŸŽ€

do NOT be out here writing VALUES lists with a massive number of rows. bestie. you will hit out-of-memory failures or just straight-up poor performance and it will not be cute. ๐Ÿ˜ญ

one exception though โ€” VALUES inside INSERT is a special case, because the desired column types are already known from the INSERT's target table and don't need to be inferred by scanning the VALUES list. so it can handle way larger lists than are practical anywhere else. ate. ๐Ÿ’…


๐Ÿ”ฅ EXAMPLES ๐Ÿ”ฅ

a bare VALUES command, no frills, just vibes:

VALUES (1, 'one'), (2, 'two'), (3, 'three');

this returns a table of two columns and three rows. it's effectively equivalent to:

SELECT 1 AS column1, 'one' AS column2
UNION ALL
SELECT 2, 'two'
UNION ALL
SELECT 3, 'three';

more usually though, VALUES is used inside a bigger SQL command. the most common use is in INSERT:

INSERT INTO films (code, title, did, date_prod, kind)
    VALUES ('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama');

in the context of INSERT, entries of a VALUES list can be DEFAULT to say the column default should be used right there instead of you specifying a value:

INSERT INTO films VALUES
    ('UA502', 'Bananas', 105, DEFAULT, 'Comedy', '82 minutes'),
    ('T_601', 'Yojimbo', 106, DEFAULT, 'Drama', DEFAULT);

VALUES can also be used anywhere a sub-SELECT might be written โ€” for example in a FROM clause:

SELECT f.*
  FROM films f, (VALUES('MGM', 'Horror'), ('UA', 'Sci-Fi')) AS t (studio, kind)
  WHERE f.studio = t.studio AND f.kind = t.kind;

UPDATE employees SET salary = salary * v.increase
  FROM (VALUES(1, 200000, 1.2), (2, 400000, 1.4)) AS v (depno, target, increase)
  WHERE employees.depno = v.depno AND employees.sales >= v.target;

okay important: an AS clause is required when VALUES is used in a FROM clause, exactly like it is for SELECT. it's not required that the AS clause names all the columns, but it's good practice to do it anyway. (the default column names for VALUES are column1, column2, etc. in PostgreSQL โ€” but lowkey these names might be different in other database systems, so don't @ me.) ๐Ÿ™ˆ

when VALUES is used in INSERT, all the values get automatically coerced to the data type of the corresponding destination column. when it's used in other contexts, you might have to specify the correct data type yourself. if the entries are all quoted literal constants, coercing the first one is enough to determine the assumed type for all of them:

SELECT * FROM machines
WHERE ip_address IN (VALUES('192.168.0.1'::inet), ('192.168.0.10'), ('192.168.1.43'));

๐Ÿ’ก Tip ๐Ÿ’ก

for simple IN tests, it's better to just rely on the list-of-scalars form of IN than to write a VALUES query like the one above. the list of scalars method needs less writing and is often more efficient. iykyk. โœจ

๐ŸŒˆ COMPATIBILITY ๐ŸŒˆ

VALUES conforms to the SQL standard. LIMIT and OFFSET are PostgreSQL extensions though โ€” see also under SELECT(7). ๐Ÿ‘‘


๐Ÿซถ SEE ALSO ๐Ÿซถ

INSERT(7), SELECT(7)


that's the whole page bestie, you understood it, you slayed it, go build a constant table ๐Ÿ’–

with love from the PostgreSQL 18.6 documentation, dated 2026 โ€” this was VALUES(7), and honestly? iconic. bye bestie! ๐ŸŽ€๐Ÿฆ‹โœจ