Cookies help us deliver our services. By using our services, you agree to our use of cookies. More information

Difference between revisions of "Space Race/MATCH Tutorial"

From NoSQLZoo
Jump to: navigation, search
 
(10 intermediate revisions by the same user not shown)
Line 6: Line 6:
  
 
[http://nosqlzoo.net/mw/visproject/visualisation.php Click here for 3d graph visualisation]
 
[http://nosqlzoo.net/mw/visproject/visualisation.php Click here for 3d graph visualisation]
 +
 +
==Cypher Cheat Sheet==
 +
[[Space_Race/Cypher_Cheat_Sheet]]
 +
==Database Schema==
 +
[[Space_Race/Schema]]
  
 
==Institutions==
 
==Institutions==
 
<div class="q nonum" data-lang="neo4j">
 
<div class="q nonum" data-lang="neo4j">
 
   <p>Use <code>MATCH</code> and <code>RETURN</code> to return nodes. Use <code>:</code> to specify what label(s) you wish the returned nodes to have. Not specifying any label will return ALL nodes</p>
 
   <p>Use <code>MATCH</code> and <code>RETURN</code> to return nodes. Use <code>:</code> to specify what label(s) you wish the returned nodes to have. Not specifying any label will return ALL nodes</p>
 +
<p>Using AS can alias the result column to whatever you wish, as long as it does not have spaces</p>
 
   <p class='strong'>Show Institutions instead of Astronauts</p>
 
   <p class='strong'>Show Institutions instead of Astronauts</p>
   <pre class="def"><nowiki>MATCH(n:Astronaut) RETURN n;</nowiki>
+
   <pre class="def"><nowiki>MATCH(n:Astronaut) RETURN n AS Astronauts;</nowiki>
 
</pre>
 
</pre>
<pre class="ans"><nowiki>MATCH(n:Institution) RETURN n; </nowiki>
+
<pre class="ans"><nowiki>MATCH(n:Institution) RETURN n AS Institutions; </nowiki>
 
</pre>
 
</pre>
 
</div>
 
</div>
 
==Born in Russia==
 
<div class="q nonum" data-lang="neo4j">
 
  <p>Use <code>WHERE</code> to specify conditions and the <code>.</code> operator to access individual properties of a node.</p>
 
  <p>The item in <code>[]</code> represents a <strong>relationship</strong> between nodes, in this case the relationship possessing the label "BORN_IN"</p>
 
  <p class='strong'>Show the Surnames of Astronauts born in Russia, and the full name of Russia rather than the USA. Hint: Use the ISO 3166-1 Alpha-2 code in all caps</p>
 
  <pre class="def"><nowiki>MATCH(n:Astronaut)-[:BORN_IN]->(c:Country) WHERE c.abbrev ='USA' RETURN n.surname, c.name;</nowiki>
 
</pre>
 
<pre class="ans"><nowiki>MATCH(n:Astronaut)-[:BORN_IN]->(c:Country) WHERE c.abbrev= 'RU' RETURN n.surname, c.name;</nowiki>
 
</pre>
 
</div>
 
 
  
 
==Neil Armstrong's Birthday==
 
==Neil Armstrong's Birthday==
Line 33: Line 27:
 
   <p>When simply matching based on properties of a node, use the <code>{}</code> with the format <code>{property:value}</code>
 
   <p>When simply matching based on properties of a node, use the <code>{}</code> with the format <code>{property:value}</code>
 
   <p class='strong'>Return Neil Armstrong's date of birth</p>
 
   <p class='strong'>Return Neil Armstrong's date of birth</p>
   <pre class="def"><nowiki>MATCH(n:Astronaut{surname:"Gagarin",first_name:"Yuri"})RETURN n.dob;</nowiki>
+
   <pre class="def"><nowiki>MATCH(n:Astronaut{surname:"Gagarin",first_name:"Yuri"})RETURN n.dob AS DateOfBirth;</nowiki>
 
</pre>
 
</pre>
<pre class="ans"><nowiki>MATCH(n:Astronaut{surname:"Armstrong",first_name:"Neil"})RETURN n.dob;</nowiki>
+
<pre class="ans"><nowiki>MATCH(n:Astronaut{surname:"Armstrong",first_name:"Neil"})RETURN n.dob AS DateOfBirth;</nowiki>
 
</pre>
 
</pre>
 
</div>
 
</div>
 
  
 
==The Young and Old==
 
==The Young and Old==
 
<div class="q nonum" data-lang="neo4j">
 
<div class="q nonum" data-lang="neo4j">
 
   <p><code>ORDER BY</code> works similarly to SQL, defaulting to ordering from low to high value. Adding <code>DESC</code> instead orders the result in descending order</p>
 
   <p><code>ORDER BY</code> works similarly to SQL, defaulting to ordering from low to high value. Adding <code>DESC</code> instead orders the result in descending order</p>
   <p class='strong'>Return the names of the Astronauts ordered by their date of birth from earlier to later</p>
+
   <p class='strong'>Return the names of the Astronauts ordered by their date of birth, from oldest to youngest.</p>
   <pre class="def"><nowiki>MATCH(n:Astronaut) RETURN n.first_name, n.surname ORDER BY n.surname;</nowiki>
+
   <pre class="def"><nowiki>MATCH(n:Astronaut) RETURN n.first_name AS FirstName, n.surname AS Surname ORDER BY n.surname;</nowiki>
 
</pre>
 
</pre>
<pre class="ans"><nowiki>MATCH(n:Astronaut) RETURN n.first_name, n.surname ORDER BY n.dob;</nowiki>
+
<pre class="ans"><nowiki>MATCH(n:Astronaut) RETURN n.first_name AS FirstName, n.surname As Surname ORDER BY n.dob;</nowiki>
 
</pre>
 
</pre>
 
</div>
 
</div>
Line 55: Line 48:
 
   <p> This can be checked using the <code>EXISTS()</code> function</p>
 
   <p> This can be checked using the <code>EXISTS()</code> function</p>
 
   <p class='strong'>Return the nicknames of the Astronauts who have them in reverse alphabetical order</p>
 
   <p class='strong'>Return the nicknames of the Astronauts who have them in reverse alphabetical order</p>
   <pre class="def"><nowiki>MATCH(n:Astronaut)WHERE EXISTS(n.middle_names) RETURN n.middle_names; </nowiki>
+
   <pre class="def"><nowiki>MATCH(n:Astronaut)WHERE EXISTS(n.middle_names) RETURN n.middle_names AS MiddleNames; </nowiki>
 +
</pre>
 +
<pre class="ans"><nowiki>MATCH(n:Astronaut)WHERE EXISTS(n.nickname) RETURN n.nickname AS Nicknames  ORDER BY n.nickname DESC;</nowiki>
 +
</pre>
 +
</div>
 +
 
 +
==Counting up the missions==
 +
<div class="q nonum" data-lang="neo4j">
 +
  <p>As in SQL, aggregate functions can be applied to the results of the MATCH query. Unlike SQL you do not need to group by other non-aggregate parameters </p>
 +
  <p>Functions include <code>count()</code>, <code>avg()</code>, <code>min()</code>, <code>max()</code> and many more</p>
 +
  <p class='strong'>Return the number of missions that took place in the Space Race.</p>
 +
  <pre class="def"><nowiki>MATCH(m:LaunchVehicle)RETURN count(m) AS Count; </nowiki>
 +
</pre>
 +
<pre class="ans"><nowiki>MATCH(m:Mission) RETURN count(m) AS Count;</nowiki>
 +
</pre>
 +
</div>
 +
 
 +
==Failure Rate==
 +
<div class="q nonum" data-lang="neo4j">
 +
  <p>Arithmetic and string operations can also be used in the return statement. Strings can be parsed in to integers or floats using the <code>toInteger()</code> or <code>toFloat()</code> functions</p>
 +
<p>The reverse can be done using the <code>toString()</code> function.
 +
  <p class='strong'>Return the name and percentage failure rate for each LaunchVehicle, ordered by number of failures in descending order.</p>
 +
  <pre class="def"><nowiki>MATCH(m:LaunchVehicle)RETURN m.name, toInteger(m.launches); </nowiki>
 
</pre>
 
</pre>
<pre class="ans"><nowiki>MATCH(n:Astronaut)WHERE EXISTS(n.nickname) RETURN n.nickname ORDER BY n.nickname DESC;</nowiki>
+
<pre class="ans"><nowiki>MATCH(m:LaunchVehicle) RETURN m.name AS Name, ((toFloat(m.failures)/toFloat(m.launches))*100) AS PercentageFailureRate ORDER BY PercentageFailureRate DESC;</nowiki>
 
</pre>
 
</pre>
 
</div>
 
</div>
  
 
{{Acknowledgements}}
 
{{Acknowledgements}}

Latest revision as of 08:53, 9 December 2019

Visualisation

Click here for 3d graph visualisation

Cypher Cheat Sheet

Space_Race/Cypher_Cheat_Sheet

Database Schema

Space_Race/Schema

Institutions

Use MATCH and RETURN to return nodes. Use : to specify what label(s) you wish the returned nodes to have. Not specifying any label will return ALL nodes

Using AS can alias the result column to whatever you wish, as long as it does not have spaces

Show Institutions instead of Astronauts

MATCH(n:Astronaut) RETURN n AS Astronauts;
MATCH(n:Institution) RETURN n AS Institutions; 

Neil Armstrong's Birthday

When simply matching based on properties of a node, use the {} with the format {property:value}

Return Neil Armstrong's date of birth

MATCH(n:Astronaut{surname:"Gagarin",first_name:"Yuri"})RETURN n.dob AS DateOfBirth;
MATCH(n:Astronaut{surname:"Armstrong",first_name:"Neil"})RETURN n.dob AS DateOfBirth;

The Young and Old

ORDER BY works similarly to SQL, defaulting to ordering from low to high value. Adding DESC instead orders the result in descending order

Return the names of the Astronauts ordered by their date of birth, from oldest to youngest.

MATCH(n:Astronaut) RETURN n.first_name AS FirstName, n.surname AS Surname ORDER BY n.surname;
MATCH(n:Astronaut) RETURN n.first_name AS FirstName, n.surname As Surname ORDER BY n.dob;

Nicknames

In Neo4j, rather than empty properties being assigned a value of NULL like in SQL, properties which are not assigned a value do not exist at all

This can be checked using the EXISTS() function

Return the nicknames of the Astronauts who have them in reverse alphabetical order

MATCH(n:Astronaut)WHERE EXISTS(n.middle_names) RETURN n.middle_names AS MiddleNames; 
MATCH(n:Astronaut)WHERE EXISTS(n.nickname) RETURN n.nickname AS Nicknames  ORDER BY n.nickname DESC;

Counting up the missions

As in SQL, aggregate functions can be applied to the results of the MATCH query. Unlike SQL you do not need to group by other non-aggregate parameters

Functions include count(), avg(), min(), max() and many more

Return the number of missions that took place in the Space Race.

MATCH(m:LaunchVehicle)RETURN count(m) AS Count; 
MATCH(m:Mission) RETURN count(m) AS Count;

Failure Rate

Arithmetic and string operations can also be used in the return statement. Strings can be parsed in to integers or floats using the toInteger() or toFloat() functions

The reverse can be done using the toString() function.

Return the name and percentage failure rate for each LaunchVehicle, ordered by number of failures in descending order.

MATCH(m:LaunchVehicle)RETURN m.name, toInteger(m.launches); 
MATCH(m:LaunchVehicle) RETURN m.name AS Name, ((toFloat(m.failures)/toFloat(m.launches))*100) AS PercentageFailureRate ORDER BY PercentageFailureRate DESC;

Acknowledgements

NoSQLZoo is made possible by the following open-source technologies:

jQuery JSHint CodeMirror MediaWiki MariaDB