SQL has a little bit different logic system than normally is taught at schools. The simple two value logic has only the values: `true`

and `false`

. Additionally there are logical operators like `or`

, `and`

, `not`

. There are also operators returning these values like `=`

, `<>`

, `in`

.

All that combined allows for some complicated searches like:

```
SELECT *
FROM tab
WHERE a = 'b' AND (x <> 'c' OR y in ('a', 'b'));
```

An example from the last bug I fixed:

```
SELECT *
FROM x
WHERE id = :id1 AND id <> :id2;
```

The values of `:id1`

and `:id2`

are passed by a function using a complicated logic. Generally I wanted to have results like this:

```
WITH DATA AS (SELECT 1 AS id)
SELECT id
FROM data
WHERE id = 1 AND id <> 2;
id
----
1
(1 row)
```

For some values of the `:id2`

argument the query returned no rows at all, even that there was a row with `id=1`

.

The two-valued logic in databases is generally useless. In SQL there is something more: there is `NULL`

. This is also called a three-valued logic. The rules are a little bit different and sometimes it’s too easy to make mess.

## The Boolean Values

In the two-valued logic we have only `true`

and `false`

. Then the results of some operations are: