Week 10 - Cypher Queries
Last updated
Last updated
This week's lab focuses on writing and understanding Cypher queries. Let's use the database that we created last week as the example.
Write queries that find each of the following:
All nodes in the database.
All products in the database.
Up to 5 products in the database.
All products whose prices are greater than 20. We will talk about toFloat()
in
All sales which contain a product with the brand "Mega".
Alternatively, If you don't need to reference the "Product" node itself, you can simplify the query by removing "p".
All shops that have sales of a product with the brand "Mega".
Same query as 6, but return the path.
In this example dataset, our relationships don't have properties. However, what if we need to query based on a property of a relationship? Let's assume there's a "year" property on the "SOLD_IN" relationship. Here's how you can modify the query to include this relationship property:
If there's a "year" property on the "SOLD_IN" relationship, the query should be:
The output of the above query should be (no changes, no records) as there is no "year" property on the "SOLD_IN" relationship.
Your turn: What does Query 8 mean?
Find the path with a length of 4 from the product with ID "TAB_1" to the product with ID "TSH_9":
Sometimes, records such as product price might need updating. Instead of uploading a new CSV file, we can write queries to update the information directly in the database.
Updates a product's price to a different price.
Check the current price of the product with ID: "BED_3" and its type:
Alternatively, you can return the node and hover over the property to see its type:
You can see that the purchasePrice
is stored as a string. This is because when we created nodes from a CSV file (see Week9), we did not specify the price as a float, so Neo4j automatically treated every variable as a string.
Two methods to change the type:
For example:
In this example, toFloat()
is used to convert the purchasePrice
property from a string to a float during node creation.
Method 2: Updating the type aftering creating:
Your turn: compare Figure 1 with Figure 3. What's the difference between their results?
Now, let's update the price:
Your turn:
Verify whether the price of product "BED_3" has been correctly updated to 125.0.
Choose a shop and update its size to a different value.
Update the "unitPrice" property of all Sale nodes to float.
Without changing the type of "quantity" for sale nodes, update the sale whose "SaleID" is "S000004969" to have a quantity of "5.0".
Deletes a specific shop in the database
Deletes a relationship between a specific Shop and a specific Sale
Your turn:
Delete all nodes and relationships in the database.
For more information on path queries:
Method 1: Changing the type when creating the database from a CSV file, you can use conversion functions such as toInteger()
, toFloat()
, toString()
, toBoolean()
. You can learn more about these functions in the Neo4j documentation:
Remove the "subcategory" property from all product nodes.
What is the difference between "DELETE" and "DETACH DELETE":