SQL EXISTS
SQL EXISTS

SQL EXISTS

Status
Done

MySQL EXISTS Operator

In MySQL, the EXISTS operator is used in conjunction with a subquery to test for the existence of rows returned by the subquery. It returns true if the subquery returns one or more rows, and false otherwise.

SELECT 
      col_names
FROM 
      table_name
WHERE 
      [NOT] EXISTS (subquery)

The NOT operator negates the EXISTS operator. In other words, the NOT EXISTS returns true if the subquery returns no row, otherwise it returns false.

💡
Note that you can use SELECT *SELECT columnSELECT a_constant, or anything in the subquery. The results are the same because MySQL ignores the select list that appeared in the SELECT clause.

The subquery's specific columns selected (col_names) don't matter to EXISTS. It only cares about whether any rows are returned.

MySQL SELECT EXISTS examples 1

MySQL SELECT EXISTS examples 2

EXIST VS JOIN

MySQL UPDATE EXISTS