Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NOT EXISTS together with SELECT NULL

I'm trying to figure out what this SQL query is doing, more specifically in the part that starts after NOT EXISTS:

SELECT
    order_num,
    MIN(order_date)
FROM
    orders
WHERE
    order_date >= '01.01.2019'
    AND
    NOT EXISTS
    (
        SELECT
            NULL
        FROM
            result
        WHERE
            unique_id = '201895'
            AND
            result = order_num
    )
GROUP BY
    order_num
like image 829
Skywalker Avatar asked Nov 05 '25 04:11

Skywalker


1 Answers

SELECT NULL still returns rows from the query. It does the same as if it was:

[...] EXISTS( SELECT 1 FROM [...]

or

[...] EXISTS( SELECT Id FROM [...]

It's just one way to make it clear that the value is not used.

like image 66
Ortiga Avatar answered Nov 07 '25 18:11

Ortiga