# Week 10 Sample Solution

## 1. READ

1. *What does the following query mean?*

```
MATCH (p:Product {brand: "Mega"})-[:CONTAINS]->(s:Sale)-[r:SOLD_IN {year: 2021}]->(sh:Shop)
RETURN DISTINCT sh
```

**Sample Answer**: This query is used to find shops that sold products of the brand "Mega" in the year 2021 without duplicated values.

## 2. UPDATE

2. *Compare Figure 1 with Figure 3. What's the difference between their results?*

**Sample Answer**: In Figure 1, the data type of "127.0" is a string. In Figure 3, the data type of 127.0 is float.

3. *Verify whether the price of product "BED\_3" has been correctly updated to 125.0.*

**Sample Answer**:&#x20;

```cypher
MATCH (p:Product {productID: "BED_3"})
RETURN p.productID, p.purchasePrice
```

4. *Choose a shop and update its size to a different value.*

**Sample Answer**: Assume the select shop's id is shop\_15, and the size will be updated to big.

```cypher
MATCH (s:Shop {shopID: "shop_15"})
SET s.shopSize = 'big'
RETURN s.shopID, s.shopSize
```

5. *Update the "unitPrice" property of all Sale nodes to float.*

**Sample Answer**:

```cypher
MATCH (s:Sale)
SET s.unitPrice = toFloat(s.unitPrice)
RETURN s.unitPrice
```

6. *Without changing the type of "quantity" for sale nodes, update the sale whose "SaleID" is "S000004969" to have a quantity of "5.0".*

**Sample Answer**:

```cypher
MATCH (s:Sale {saleID: "S000004969"})
SET s.quantity = 5.0
RETURN s.saleID, s.quantity
```

## 3. DELETE

7. *What is the difference between "DELETE" and "DETACH DELETE"?*

**Sample Answer**:

* **DELETE**: The `DELETE` clause is used to delete nodes, relationships or paths. But it is not possible to delete nodes with relationships connected to them without also deleting the relationships.
* **DETACH DELETE**: This command extends the functionality of `DELETE` by automatically removing any relationships that the node is involved in before deleting the node itself.

8. *Delete all nodes and relationships in the database.*

**Sample Answer**:

```cypher
MATCH (n)
DETACH DELETE n
```
