# Help with a 'siblings' query

**URL:** <https://forum.datomic.com/t/help-with-a-siblings-query/1582>\
**Category:** Client API\
**Created:** [August 16, 2020, 2:55pm UTC](https://forum.datomic.com/t/help-with-a-siblings-query/1582 "2020-08-16T14:55:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![miwal](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/miwal/32/404_2.png) [@miwal](https://forum.datomic.com/u/miwal)\
**Post date:** [August 16, 2020, 2:55pm UTC](https://forum.datomic.com/t/help-with-a-siblings-query/1582/1 "2020-08-16T14:55:37Z")

</div>

I’m trying to find `lessons` in my schema which belong to the same `series` and which are of higher `rank` than the given lesson. But the below query is returning _ **all** _ lessons of higher rank, regardless of which series they belong to. I’m somewhat lost as to why this is the case.

Below, :lesson/series is a ref & :lesson/rank is an integer.

```
(defn higher-siblings [l-idf]
  "Return id and rank of higher siblings."
  (d/q '[:find ?sibling ?rank
         :in $ l
         :where
         [?l :lesson/rank ?r]
         [?l :lesson/series ?s]
         [?sibling :lesson/series ?s]
         [?sibling :lesson/rank ?rank]
         [(> ?rank ?r)]]
    (d/db conn) l-idf))

```

My problem: the query seems to be binding `?sibling` regardless of the `?s` of `?l`.

Intuitively that suggests an ‘and’ is missing, but can’t quite reconcile that with the fact that since these are logic variables, I thought ‘and’ should be implied in some sense.

Is this anything to do with the fact that I have multiple where clauses referencing the same ‘a’ value?

Just for interest, I also have a composite tuple to make rank+series unique, so another form, which gives the same result, is:

```
      '[:find ?sibling ?rank
        :in $ l
        :where
        [?l :lesson/rank+series ?tup]
        [(untuple ?tup) [?r ?s]]
        [?sibling :lesson/series ?s]
        [?sibling :lesson/rank ?rank]
        [(> ?rank ?r)]]
```

---

<div class="post-metadata">

**Author:** ![miwal](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/miwal/32/404_2.png) [@miwal](https://forum.datomic.com/u/miwal)\
**Post date:** [August 17, 2020, 12:46pm UTC](https://forum.datomic.com/t/help-with-a-siblings-query/1582/2 "2020-08-17T12:46:02Z")

</div>

**Solved** : By incrementally breaking down the query into smaller and smaller pieces and testing it each time, until the error simply stared me in the face, I discovered there’s a missing `?` on the variable in the `:in` clause in the listing above. I was using the variable name `l` in surrounding code, where it’s not a logic variable, from where the typo must have slipped in.

As such, the input variable was not being bound to anything, and the query was returning a superset of the results I thought I was querying for.

(Side note: could an unbound :in variable not throw a warning maybe? Note I’m currently still on client-pro 0.9.41, in case that’s been added since.)
