Edit

Share via


Tutorial: Query the graph by using GQL

Note

This feature is currently in public preview. This preview is provided without a service-level agreement, and isn't recommended for production workloads. Certain features might not be supported or might have constrained capabilities. For more information, see Supplemental Terms of Use for Microsoft Azure Previews.

In this tutorial step, you query your graph by using GQL (Graph Query Language) in the code editor. GQL provides powerful querying capabilities for complex graph patterns and analysis.

Switch to code editor mode

Follow these steps to switch to the code editor and start querying your graph by using GQL:

  1. Go to your graph's home page.

  2. Select Code editor from the top menu.

    Screenshot showing result of selecting 'Code editor'.

Run a basic query

  1. Enter a GQL query into the input field. For example, count all orders:

    MATCH (n:`Order`) RETURN count(n) AS num_orders
    
  2. Select Run query to execute the query.

This query finds all nodes with the Order label, counts them, and returns the total as num_orders. It's a simple way to confirm your graph has data. The following image shows the result of the query:

Screenshot showing the result of running a GQL query to count all orders.

Recreate the query builder query in GQL

In the previous tutorial step, you used the query builder to find what products a specific customer purchased. Here's the same query written in GQL:

MATCH (c:Customer)-[:purchases]->(o:`Order`)-[:`contains`]->(p:`Product`)
FILTER c.fullName = 'Carla Adams'
RETURN c.fullName, o, p.productName

This query:

  1. Matches the pattern Customer → purchases → Order → contains → Product
  2. Filters for the customer named "Carla Adams"
  3. Returns the customer's full name, order details, and product names

The following image shows the result of the query (only a portion of the returned data is shown).

Screenshot showing the result of running a GQL query to find products purchased by Carla Adams.

Run a complex query

You can run more complex queries that combine matching graph patterns, filtering, aggregation, sorting, and limiting:

MATCH (v:Vendor)-[:produces]->(p:`Product`)->(sc:`ProductSubcategory`)->(c:`ProductCategory`), 
      (o:`Order`)-[:`contains`]->(p)
FILTER c.subCategoryName = 'Touring Bikes'
LET vendorName = v.vendorName, subCategoryName = sc.subCategoryName
RETURN vendorName, subCategoryName, count(DISTINCT p) AS num_products, count(o) AS num_orders
GROUP BY vendorName, subCategoryName
ORDER BY num_orders DESC
LIMIT 5

This query:

  1. Matches a pattern that connects vendors to products through the supply chain, and orders to products.
  2. Filters for products in the 'Touring Bikes' category.
  3. Defines variables for vendor and subcategory names.
  4. Returns the vendor name, subcategory name, distinct product count, and order count.
  5. Groups results by vendor and subcategory.
  6. Orders results by order count in descending order.
  7. Limits results to the top 5.

In summary, it shows the top five vendors supplying products in the 'Touring Bikes' category, along with how many products they supply and how many orders those products have.

Screenshot showing the result of running a GQL query to find the top five vendors supplying products in the 'Touring Bikes' category.

For more information about GQL language support, see:

Next step