In this SQL EXISTS operator tutorial has the following syntax for
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
Does the column_name
in the select statement of the subquery matter? From other answers it seems like 0
, 1
, *
, some arbitrary column name(s) have been used.
3
Answers
It does not matter.
If you look at the source, you will see that PostgreSQL removes the
SELECT
list in such subqueries. A short, standard conforming way is to use a constant likeIf I don’t have to write standard conforming code, I prefer the even shorter
Not only does it not matter, since version 9.4 it doesn’t even have to be there:
Demo:
EXISTS
checks for the existence of rows in the subquery, not columns in it. Rows with anything will work. Even empty rows with no columns.