# Understanding not and not-join

**URL:** <https://forum.datomic.com/t/understanding-not-and-not-join/1119>\
**Category:** General\
**Created:** [August 7, 2019, 11:44pm UTC](https://forum.datomic.com/t/understanding-not-and-not-join/1119 "2019-08-07T23:44:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![t\_xiong](https://avatars.discourse-cdn.com/v4/letter/t/59ef9b/32.png) [@t\_xiong](https://forum.datomic.com/u/t_xiong)\
**Post date:** [August 7, 2019, 11:44pm UTC](https://forum.datomic.com/t/understanding-not-and-not-join/1119/1 "2019-08-07T23:44:57Z")

</div>

Trying to understand the use of [`not` and `not-join`](https://docs.datomic.com/on-prem/query.html#not-caluses) in queries.

### Multiple `not` Clauses

The following raises`:db.error/insufficient-binding [?release] not bound in not clause`:

```clojure
 (d/q '[:find (count ?artist) .
       :where [?artist :artist/name]
       (not
        [?release :release/artists ?artist]
        [?release :release/year 1970])]
     db
     "John Lennon")

```

I’m trying to understand why via the description in [`How Not Clauses Work`](https://docs.datomic.com/on-prem/query.html#how-not-clauses-work):

> One can understand _not_ clauses as if they turn into subqueries where all of the variables and sources unified by the negation are propagated to the subquery. The results of the subquery are removed from the enclosing query via set difference.

Subqueries of what? Of the set of results returned by the query without the `not` clause? Would it be correct to understand that because the query without the `not` clause does not contain` ?release`, the `not` clause cannot refer to it?

### `not` and `not-join`

The following gives `4124`:

```auto
(d/q '[:find (count ?artist) .
       :where [?artist :artist/name]
       [?release :release/artists ?artist]
       (not
        [?release :release/year 1970])]
     db
     "John Lennon")

```

While this gives `3263`:

```auto
(d/q '[:find (count ?artist) .
       :where [?artist :artist/name]
       (not-join [?artist]
                 [?release :release/artists ?artist]
                 [?release :release/year 1970])]
     db
     "John Lennon")

```

It seems to me the two queries are the same–where do they differ?

---

<div class="post-metadata">

**Author:** ![marshall](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/marshall/32/48_2.png) [@marshall](https://forum.datomic.com/u/marshall)\
**Post date:** [August 14, 2019, 4:47pm UTC](https://forum.datomic.com/t/understanding-not-and-not-join/1119/2 "2019-08-14T16:47:11Z")

</div>

The key difference between your two queries is that the first not clause removes `?release`s from consideration in the outer query, while the not-join clause removes `?artist`s from consideration.

The `not` clause says “remove all releases with :release/year of 1970 from consideration for the rest of the query”, while the `not-join` says “remove artists that are referred to by a release with :release/year of 1970” from consideration for the rest of the query.

In other words, the first query is asking for all artists except those that have releases _only_ in 1970. The second query is asking for all artists except those that have _at least one_ release in 1970.
