-
Notifications
You must be signed in to change notification settings - Fork 65
/
02-query.cypher
132 lines (103 loc) · 3.59 KB
/
02-query.cypher
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
////////////////////////////////////////////////////////////
// ================ Querying For Nodes ================== //
////////////////////////////////////////////////////////////
// All nodes //
MATCH (n) RETURN n
// All nodes with specific label //
MATCH (player:PLAYER) RETURN player
// Properies //
MATCH (player:PLAYER) RETURN player.name, player.height
////////////////////////////////////////////////////////////
// =============== Filtering For Nodes ================== //
////////////////////////////////////////////////////////////
// Nodes where name is LeBron James //
MATCH (player:PLAYER)
WHERE player.name = "LeBron James"
RETURN player
// Nodes where name is LeBron James //
MATCH (player:PLAYER {name: "LeBron James"})
RETURN player
// Nodes where name is not LeBron James
MATCH (player:PLAYER)
WHERE player.name <> "LeBron James"
RETURN player
// Nodes where height is greater than or equal to 2
MATCH (player:PLAYER)
WHERE player.height >= 2
RETURN player
// Nodes where height is less than 2
MATCH (player:PLAYER)
WHERE player.height < 2
RETURN player
// Nodes with a BMI larger than 25
MATCH (player:PLAYER)
WHERE (player.weight / (player.height * player.height)) > 25
RETURN player
// Nodes with a BMI not larger than 25
MATCH (player:PLAYER)
WHERE NOT (player.weight / (player.height * player.height)) > 25
RETURN player
// Nodes with a weight larger than 100 and a height smaller than 2
MATCH (player:PLAYER)
WHERE player.weight >= 100 AND player.height <= 2
RETURN player
// Nodes with height greater than 2.1 or weight greater than 120
MATCH (player:PLAYER)
WHERE player.weight >= 120 OR player.height >= 2.1
RETURN player
// Limit
MATCH (player:PLAYER)
WHERE player.height >= 2
RETURN player
LIMIT 3
// Skip
MATCH (player:PLAYER)
WHERE player.height >= 2
RETURN player
SKIP 1
LIMIT 3
// Orderby
MATCH (player:PLAYER)
WHERE player.height >= 2
RETURN player
SKIP 1
ORDER BY player.height DESC
LIMIT 3
// Query for multiple nodes
MATCH (coach:COACH), (player:PLAYER)
RETURN coach, player
////////////////////////////////////////////////////////////
// ============== Querying Relationships ================ //
////////////////////////////////////////////////////////////
// GET ALL LAKER PLAYERS //
MATCH (player:PLAYER) - [:PLAYS_FOR] -> (team:TEAM)
WHERE team.name = "LA Lakers"
RETURN player, team
// GET ALL LAKER OR MAVERICKS PLAYERS //
MATCH (player:PLAYER) - [:PLAYS_FOR] -> (team:TEAM)
WHERE team.name = "LA Lakers" OR team.name = team.name = "Dallas Mavericks"
RETURN player, team
// GET ALL PLAYERS THAT MAKE MORE THE 35M //
MATCH (player:PLAYER) - [contract :PLAYS_FOR] -> (team:TEAM)
WHERE contract.salary >= 35000000
RETURN player
// GET ALL OF LEBRONS TEAMMATES THAT MAKE MORE THAN 40M //
MATCH (lebron:PLAYER {name: "LeBron James"}) - [:TEAMMATES] -> (teammate:PLAYER)
MATCH (teammate) - [contract:PLAYS_FOR] -> (:TEAM)
WHERE contract.salary >= 40000000
RETURN teammate
////////////////////////////////////////////////////////////
// ==================== Aggregates ====================== //
////////////////////////////////////////////////////////////
// GET PLAYERS AND NUMBER OF GAMES PLAYED //
MATCH (player:PLAYER) - [gamePlayed:PLAYED_AGAINST] - (team:TEAM)
RETURN player.name, COUNT(gamePlayed)
// GET PLAYERS AND POINTS PER GAME //
MATCH (player:PLAYER) - [gamePlayed:PLAYED_AGAINST] - (team:TEAM)
RETURN player.name, AVG(gamePlayed.points)
// GET HIGHEST SCORING PLAYER IN THE LAKERS //
MATCH (player:PLAYER) - [:PLAYS_FOR] - (:TEAM {name: "LA Lakers"})
MATCH (player) - [gamePlayed:PLAYED_AGAINST] - (:TEAM)
RETURN player.name, AVG(gamePlayed.points) AS ppg
ORDER BY ppg DESC
LIMIT 1