Is there a way in Postgres to fetch a list of rows having specific IDs from a table, using one query without any subqueries.
Table
id value
---------
0 | 12
2 | 345
3 | Alan
4 | 0.345
5 | 232333.2
I want fetch rows with IDs 2, 3, and 5 using just one query.
3
Answers
SELECT *
FROM Table
WHERE id IN (2, 3, 5);
You should use
IN
clauseYou can use the
IN
operator as shown in previous answers. However if your list of requested IDs can be empty this will not work becauseIN ()
is a syntax error.If you want to handle the empty list with the same query you can use the
ANY
function: