Skip to content

Instantly share code, notes, and snippets.

@JAForbes
Last active June 6, 2021 04:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JAForbes/994e5c6e4478c555dd93 to your computer and use it in GitHub Desktop.
Save JAForbes/994e5c6e4478c555dd93 to your computer and use it in GitHub Desktop.
Table literal in SQLite
select * from (
-- define the column names
select 0 as a, 0 as b
-- join the definition row with all the values
union
-- define the values
values
(4,5),
(5,6),
(6,7),
(7,8),
(9,10),
(11,12)
)
-- skip the definition row
limit -1 offset 1;
--> /*
"a" "b"
=== ===
"4" "5"
"5" "6"
"6" "7"
"7" "8"
"9" "10"
"11" "12"
*/
@ronburk
Copy link

ronburk commented Aug 31, 2018

Stumbled across this and it helped increase my understanding. I learned that a SELECT can just be VALUES, I think, meaning that
select * from ( values(1,2) ); produces 1|2. Next, I think I learned that I can get the column headings I want with Common Table Expression, like:

.headers on
with Literal(a,b) as ( values(1,2) ) select * from Literal;

which produces

a|b
1|2

Which may be completely irrelevant to what you were doing, but helped me get where I was going, so thanks for this gist!

@chrismwendt
Copy link

To create multiple rows, keep appending tuples:

sqlite> SELECT * FROM (VALUES (1, 'hi'), (2, 'there'));
column1  column2
-------  -------
1        hi
2        there

Found this in the grammar for SELECT https://www.sqlite.org/lang_select.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment