Wrong Results

I have to find the results of top N items base on purchase orders for Orders of Northwind dataset.
Screenshot 2021-05-29 163233

I used the query:
MATCH (c:Customer)-[:PURCHASED]->(o:Order)-[:ORDERS]->(p:Product)
RETURN p.productID AS ProductID, p.productName AS ProductName, COUNT(*) AS NumberOfOrders
ORDER BY NumberOfOrders DESC

But the results from neo4j are wrong.

Can anyone help me to find the correct solution? (I will appreciate)

Also I should remove from the top N list of recommended items those items which ALFKI has purchased

I used the below sets of queries:
Query 1:
MATCH (c:Customer)-[:PURCHASED]->(o:Order)-[:ORDERS]->(p:Product) WHERE c.customerID="ALFKI"



Query 2:
MATCH (c:Customer)-[:PURCHASED]->(o:Order)-[:ORDERS]->(p:Product)
RETURN p.productID AS ProductID, p.productName AS ProductName, COUNT(*) AS NumberOfOrders
ORDER BY NumberOfOrders DESC

But I would like to find and examine another one solution that used nested queries.


Would you mind posting the results you are expecting as well as the results you are getting?



1 Like

Hi Matt

I would like to thank you for your response.

Wrong results:

Expecting results:

Also I would like to ask you if you can fins an alternative solution for the below queries:

Hi chragr19,

So your query and results look correct. You can double check the values used in the sample Northwinds database here:


As for your second query, it sounds like you just need to exclude products that a particular customer has ordered. You don't need to, and almost always do not want to delete those orders from the graph. Using a <> operator will include all values not equal to the value in the where condition.

match (c:Customer)-[:PURCHASED]->(o:Order)-[orders:ORDERS]->(p:Product)
where c.customerID <> 'ALFKI'
return p.productID, p.productName, count(o.orderID) as num_orders
order by num_orders desc
limit 20

Please let me know if I'm misunderstanding something but these should help.

For details of the sample Northwinds database in the Neo4j browser type:

:play Northwinds



1 Like


Thank you for your support.

For first query my tutor said that his results are the correct ones. I spend some days without manage to retrieve the results he want. I did not get more help from him. Generally the all situation with him is a little bit straight.

For the second query we should remove from the list all the products that ALFKI purchased. With the above query we do not remove the items just remove ALFKI orders from count.

I really grateful for your help. I will wait for your further feedback.

Thank you,

Ah I see. So for the second query we want exclude all instances of items that ALFKI has ordered, regardless of which customer created the order / items? Would you like a query? One way off the top of my head to achieve this is to first match all orders -> items from ALFKI. Then using the COLLECT function you can store the itemIds in list. You can then use that list in the another match and exclude items included in that list. I can help write the query, however I wasn't sure if you wanted to give it a shot your self first.

Thanks !


1 Like


I follow your advice and manage to find another solution by using the COLLECT clause!!

Thank you for your support and help. I am glad to meet such people, who still help each other.

Your are the best!!


1 Like

No problem, glad it worked!

1 Like