title: Emulating a graph query with standard SQL
body: <p>I was wondering if there was any way to emulate a graph pattern match using straight SQL, perhaps with a help of a recursive CTE.</p>
  <p>Here's an example from using the <code>MATCH</code> <a href="https://docs.microsoft.com/en-us/sql/t-sql/queries/match-sql-graph?view=sql-server-ver15" rel="nofollow noreferrer">Cypher-like syntax in SQL Server</a>:</p>
  <blockquote>
  <p>Find 2 people who are both friends with same person</p>
  </blockquote>
  <pre><code>SELECT Person1.name AS Friend1, Person2.name AS Friend2
  FROM Person Person1, friend friend1, Person Person2, 
          friend friend2, Person Person0
  WHERE MATCH(Person1-(friend1)-&gt;Person0&lt;-(friend2)-Person2);
  </code></pre>
  <p>I know this can be done by doing a bunch of joins, but I was wondering if there's another way to do it in this concise pattern, perhaps with the use of a recursive CTE (without having access to the SQL-Server <code>MATCH</code> clause).</p>
  <hr />
  <p>Here is an updated example although it will requires a few steps to import it:</p>
  <ul>
  <li>Products: <a href="http://data.neo4j.com/northwind/products.csv" rel="nofollow noreferrer">http://data.neo4j.com/northwind/products.csv</a></li>
  <li>Categories: <a href="http://data.neo4j.com/northwind/categories.csv" rel="nofollow noreferrer">http://data.neo4j.com/northwind/categories.csv</a></li>
  <li>Suppliers: <a href="http://data.neo4j.com/northwind/suppliers.csv" rel="nofollow noreferrer">http://data.neo4j.com/northwind/suppliers.csv</a></li>
  </ul>
  <p>Emulating the query of &quot;Which categories of food does each supplier supply?&quot;</p>
  <pre><code>MATCH (s:Supplier)--&gt;(:Product)--&gt;(c:Category)
  RETURN s.companyName as Company, collect(distinct c.categoryName) as Categories
  </code></pre>
  <p>Or, if expressed more sql-like:</p>
  <pre><code>SELECT Company, CategoryName, Categories
  FROM table
  MATCH (s:Supplier)--&gt;(:Product)--&gt;(c:Category)
  </code></pre>
  <p>This is taken from the Neo4j example --  <a href="https://neo4j.com/developer/guide-importing-data-and-etl/" rel="nofollow noreferrer">https://neo4j.com/developer/guide-importing-data-and-etl/</a>.</p>
  <p>Basically, instead of hardcoding all the JOINs (which I know how to do) I'd like to create something like a function or table-function, which could be called something like:</p>
  <pre><code>SELECT Field1, Field2, ...
  FROM MATCH_TABLE_FUNCTION(table.field1, {'--&gt;'|'--','&lt;--'}, table.field2, ...})
  </code></pre>
  <p>I know it's not an easy ask, but hopefully we'll get some creative answers here.</p>