Skip to content

Commit 2e72210

Browse files
committed
Update SQL and Cypher queries per new versions
1 parent 617407a commit 2e72210

18 files changed

+61
-49
lines changed

cypher/q1.cypher

-4
This file was deleted.

cypher/q1a.cypher

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Return the name of the person who has the email "[email protected]"
2+
MATCH (p:Person)
3+
WHERE p.email = "[email protected]"
4+
RETURN p.name AS name;

cypher/q1b.cypher

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Return all the accounts owned by George
2+
MATCH (p:Person)-[o:Owns]->(a:Account)
3+
WHERE p.name = "George"
4+
RETURN p.name, a.account_id, a.balance;

cypher/q1c.cypher

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Return the names of all persons who made a direct transfer to George
2+
MATCH (p1:Person)-[o1:Owns]->(a1:Account)<-[t:Transfer]-(a2:Account)-[o2:Owns]-(p2:Person)
3+
WHERE p1.name = "George"
4+
RETURN p2.name AS persons;

cypher/q2.cypher

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
MATCH (p1:Person)-[o1:Owns]->(a1:Account)-[t:Transfer*..5]-(a2:Account)<-[o2:Owns]-(p2:Person)
2-
WHERE p1.email = "[email protected]" AND p2.email = "[email protected]"
1+
// Find all possible direct or indirect transfer path from George's account to Edward's account.
2+
MATCH (p1:Person)-[o1:Owns]->(a1:Account)-[t:Transfer* 1..5]->(a2:Account)<-[o2:Owns]-(p2:Person)
3+
WHERE p1.name = "George" AND p2.name = "Edward"
34
RETURN *, size(rels(t)) AS depth
45
ORDER BY depth;

cypher/q3.cypher

-3
This file was deleted.

cypher/q3a.cypher

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Find a shortest transfer path from George's account to Edward's account.
2+
MATCH (p1:Person)-[o1:Owns]->(a1:Account)-[t:Transfer* SHORTEST]->(a2:Account)<-[o2:Owns]-(p2:Person)
3+
WHERE p1.name = "George" AND p2.name = "Edward"
4+
RETURN *;

cypher/q3b.cypher

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Find all shortest Transfer paths from George's account to Edward's account.
2+
MATCH (p1:Person)-[o1:Owns]->(a1:Account)-[t:Transfer* ALL SHORTEST]->(a2:Account)<-[o2:Owns]-(p2:Person)
3+
WHERE p1.name = "George" AND p2.name = "Edward"
4+
RETURN *;

cypher/q4.cypher

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
MATCH (p1:Person)-[r* ALL SHORTEST]-(p2:Person)
2-
WHERE p1.email = "[email protected]" AND p2.email = "[email protected]"
3-
RETURN *, size(rels(r)) AS depth
4-
ORDER BY depth;
1+
// Find all indirect connections of any type between the Person nodes representing George and
2+
// Edward up to a specified path length
3+
MATCH (p1:Person)-[r* 1..5]-(p2:Person)
4+
WHERE p1.name = "George" and p2.name = "Edward"
5+
RETURN *;

cypher/q5.cypher

-8
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MATCH (a0:Account)-[t:Transfer]->(a:Account)
2-
WITH a, COUNT(t) AS in_degree
3-
ORDER BY in_degree DESC
2+
WITH a, COUNT(t.amount) AS total_amount
3+
ORDER BY total_amount DESC
44
LIMIT 3
55
MATCH (a)<-[o:Owns]-(p:Person)
6-
RETURN p.name AS person, a.account_id AS accountID, p.email AS email, in_degree;
6+
RETURN p.name AS person, a.account_id AS accountID, p.email AS email, total_amount;
File renamed without changes.

sql/q1.sql

-7
This file was deleted.

sql/q1a.sql

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SELECT name
2+
FROM Person
3+
WHERE email = '[email protected]';

sql/q1b.sql

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SELECT p.name, a.account_id, a.balance
2+
FROM Person p
3+
JOIN Account a ON p.id = a.id
4+
WHERE p.name = 'George';

sql/q1c.sql

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SELECT DISTINCT p2.name AS persons
2+
FROM Person p1
3+
JOIN Account a1 ON p1.id = a1.owner
4+
JOIN Transfer t ON a1.owner = t.target
5+
JOIN Account a2 ON t.source = a2.owner
6+
JOIN Person p2 ON a2.owner = p2.id
7+
WHERE p1.name = 'George';

sql/q2.sql

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
WITH RECURSIVE TransferPaths AS (
2-
-- Base case: Start with the person with one of the emails as the source
2+
-- Base case: Start with transfers FROM George
33
SELECT p1.id AS start_id, p2.id AS end_id, [(a1.owner, a2.owner, t.amount)] AS path,
44
1 AS depth
55
FROM Person p1
66
JOIN Account a1 ON p1.id = a1.owner
7-
JOIN Transfer t ON a1.owner = t.source OR a1.owner = t.target
8-
JOIN Account a2 ON (t.source = a2.owner OR t.target = a2.owner) AND a2.owner != a1.owner
7+
JOIN Transfer t ON a1.owner = t.source -- Only FROM source
8+
JOIN Account a2 ON t.target = a2.owner -- Only TO target
99
JOIN Person p2 ON a2.owner = p2.id
10-
WHERE p1.email = '[email protected]'
10+
WHERE p1.name = 'George'
1111

1212
UNION ALL
1313

14-
-- Recursive case: Traverse up to N relationships deep
14+
-- Recursive case: Follow only outgoing transfers
1515
SELECT tp.start_id, p2.id AS end_id, list_append(tp.path, (a1.owner, a2.owner, t.amount)), tp.depth + 1 AS depth
1616
FROM TransferPaths tp
1717
JOIN Account a1 ON tp.end_id = a1.owner
18-
JOIN Transfer t ON a1.owner = t.source OR a1.owner = t.target
19-
JOIN Account a2 ON (t.source = a2.owner OR t.target = a2.owner) AND a2.owner != a1.owner
18+
JOIN Transfer t ON a1.owner = t.source -- Only FROM source
19+
JOIN Account a2 ON t.target = a2.owner -- Only TO target
2020
JOIN Person p2 ON a2.owner = p2.id
2121
WHERE tp.depth < 5
2222
)
23-
-- Select the paths where the end person has the target email
2423
SELECT start_id, end_id, path, depth
2524
FROM TransferPaths, Person p
26-
WHERE end_id = p.id and p.email = '[email protected]'
25+
WHERE end_id = p.id AND p.name = 'Edward'
2726
ORDER BY depth;

sql/q3.sql

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
WITH RECURSIVE TransferPaths AS (
2-
-- Base case: Start with the person with one of the emails as the source
2+
-- Base case: Start with transfers FROM George
33
SELECT p1.id AS start_id, p2.id AS end_id, [(a1.owner, a2.owner, t.amount)] AS path,
44
1 AS depth
55
FROM Person p1
66
JOIN Account a1 ON p1.id = a1.owner
7-
JOIN Transfer t ON a1.owner = t.source OR a1.owner = t.target
8-
JOIN Account a2 ON (t.source = a2.owner OR t.target = a2.owner) AND a2.owner != a1.owner
7+
JOIN Transfer t ON a1.owner = t.source -- Only FROM source
8+
JOIN Account a2 ON t.target = a2.owner -- Only TO target
99
JOIN Person p2 ON a2.owner = p2.id
10-
WHERE p1.email = '[email protected]'
10+
WHERE p1.name = 'George'
1111

1212
UNION ALL
1313

14-
-- Recursive case: Traverse up to N relationships deep
14+
-- Recursive case: Follow only outgoing transfers
1515
SELECT tp.start_id, p2.id AS end_id, list_append(tp.path, (a1.owner, a2.owner, t.amount)), tp.depth + 1 AS depth
1616
FROM TransferPaths tp
1717
JOIN Account a1 ON tp.end_id = a1.owner
18-
JOIN Transfer t ON a1.owner = t.source OR a1.owner = t.target
19-
JOIN Account a2 ON (t.source = a2.owner OR t.target = a2.owner) AND a2.owner != a1.owner
18+
JOIN Transfer t ON a1.owner = t.source -- Only FROM source
19+
JOIN Account a2 ON t.target = a2.owner -- Only TO target
2020
JOIN Person p2 ON a2.owner = p2.id
2121
WHERE tp.depth < 5
2222
)
23-
-- Select the paths where the end person has the target email
2423
SELECT start_id, end_id, path, depth
2524
FROM TransferPaths, Person p
26-
WHERE end_id = p.id and p.email = '[email protected]'
25+
WHERE end_id = p.id AND p.name = 'Edward'
2726
ORDER BY depth
2827
LIMIT 1;

0 commit comments

Comments
 (0)