JOIN clauses allow records from multiple statements and/or named states to be combined, based on a relationship between certain attributes in these statements.
JOIN clauses, which conform to a subset of the SQL standard, do a join with the specified join condition. The join condition may be an arbitrary Boolean expression referring to the attributes in the FROM statement. The expression must be enclosed in parentheses.
The JOIN clause always modifies a FROM clause. Two named sources (one or both of which can be named states) can be indicated in the FROM clause. Fields must be dot-qualified to indicate which source they come from, except in queries from a single table.
Self-join is supported. Statement aliasing is required for self-join.
Both input tables must result from DEFINE or RETURN statements (that is, from intermediate results).
Any number of joins can be performed in a single statement.
FROM <statement1> [alias] [INNER,CROSS,LEFT,RIGHT,FULL] JOIN <statement2> [alias] ON (Boolean-expression) [JOIN <statementN> [alias] ON (Boolean-expression)]*where statement is either a statement or a named state. Note that you can put multiple JOIN clauses under a FROM clause, but there must be exactly one FROM clause in any statement.
EQL supports the following types of joins:
Keep in mind that if not used correctly, joins can cause the Dgraph to grow beyond available RAM because they can easily create very large results. For example, a CROSS JOIN of a result with 100 records and a result with 200 records would contain 20,000 records. Two best practices are to avoid CROSS JOIN if possible and to be careful with ON conditions so that the number of results are reasonable.
DEFINE EmployeeTotals AS SELECT ARB(DimEmployee_FullName) AS Name, SUM(FactSales_SalesAmount) AS Total FROM SaleState GROUP BY DimEmployee_EmployeeKey, ProductSubcategoryName; DEFINE SubcategoryTotals AS SELECT SUM(FactSales_SalesAmount) AS Total FROM SaleState GROUP BY ProductSubcategoryName; RETURN Stars AS SELECT EmployeeTotals.Name AS Name, EmployeeTotals.ProductSubcategoryName AS Subcategory, 100 * EmployeeTotals.Total / SubcategoryTotals.Total AS Pct FROM EmployeeTotals INNER JOIN SubcategoryTotals ON (EmployeeTotals.ProductSubcategoryName = SubcategoryTotals.ProductSubcategoryName) HAVING Pct > 10
DEFINE Days AS SELECT FactSales_OrderDateKey AS DateKey, DimEmployee_EmployeeKey AS EmployeeKey, ARB(DimEmployee_FullName) AS EmployeeName, SUM(FactSales_SalesAmount) AS DailyTotal FROM SaleState GROUP BY DateKey, EmployeeKey; RETURN CumulativeDays AS SELECT SUM(PreviousDays.DailyTotal) AS CumulativeTotal, Day.DateKey AS DateKey, Day.EmployeeKey AS EmployeeKey, ARB(Day.EmployeeName) AS EmployeeName FROM Days Day JOIN Days PreviousDays ON (PreviousDays.DateKey <= Day.DateKey) GROUP BY DateKey, EmployeeKey
DEFINE Totals AS SELECT SUM(FactSales_SalesAmount) AS Total FROM SaleState GROUP BY ProductSubcategoryName; DEFINE Top5 AS SELECT ARB(Total) AS Total FROM Totals GROUP BY ProductSubcategoryName ORDER BY Total DESC PAGE(0,5); RETURN Chart AS SELECT COALESCE(Top5.ProductSubcategoryName, 'Other') AS Subcategory, SUM(Totals.Total) AS Total FROM Totals LEFT JOIN Top5 ON (Totals.ProductSubcategoryName = Top5.ProductSubcategoryName) GROUP BY Subcategory
DEFINE Product AS SELECT ProductAlternateKey AS Key, ARB(ProductName) AS Name FROM SaleState GROUP BY Key; DEFINE RegionTrans AS SELECT ProductAlternateKey AS ProductKey, FactSales_SalesAmount AS Amount FROM SaleState WHERE DimSalesTerritory_SalesTerritoryRegion='United Kingdom'; RETURN Results AS SELECT Product.Key AS ProductKey, ARB(Product.Name) AS ProductName, COALESCE(SUM(RegionTrans.Amount), 0) AS SalesTotal, COUNT(RegionTrans.Amount) AS TransactionCount FROM Product LEFT JOIN RegionTrans ON (Product.Key = RegionTrans.ProductKey) GROUP BY ProductKey
DEFINE TopEmployees AS SELECT DimEmployee_EmployeeKey AS Key, ARB(DimEmployee_FullName) AS Name, SUM(FactSales_SalesAmount) AS SalesTotal FROM SaleState GROUP BY Key ORDER BY SalesTotal DESC PAGE (0,10); DEFINE TopProducts AS SELECT ProductAlternateKey AS Key, ARB(ProductName) AS Name, SUM(FactSales_SalesAmount) AS SalesTotal FROM SaleState GROUP BY Key ORDER BY SalesTotal DESC PAGE (0,10); DEFINE EmployeeProductTotals AS SELECT DimEmployee_EmployeeKey AS EmployeeKey, ProductAlternateKey AS ProductKey, SUM(FactSales_SalesAmount) AS SalesTotal FROM SaleState GROUP BY EmployeeKey, ProductKey HAVING [EmployeeKey] IN TopEmployees AND [ProductKey] IN TopProducts; RETURN Results AS SELECT TopEmployees.Key AS EmployeeKey, TopEmployees.Name AS EmployeeName, TopEmployees.SalesTotal AS EmployeeTotal, TopProducts.Key AS ProductKey, TopProducts.Name AS ProductName, TopProducts.SalesTotal AS ProductTotal, EmployeeProductTotals.SalesTotal AS EmployeeProductTotal FROM EmployeeProductTotals FULL JOIN TopEmployees ON (EmployeeProductTotals.EmployeeKey = TopEmployees.Key) FULL JOIN TopProducts ON (EmployeeProductTotals.ProductKey = TopProducts.Key)
DEFINE GlobalTotal AS SELECT SUM(FactSales_SalesAmount) AS GlobalTotal FROM SaleState GROUP; DEFINE SubcategoryTotals AS SELECT SUM(FactSales_SalesAmount) AS SubcategoryTotal FROM SaleState GROUP BY ProductSubcategoryName; RETURN SubcategoryContributions AS SELECT SubcategoryTotals.ProductSubcategoryName AS Subcategory, SubcategoryTotals.SubcategoryTotal / GlobalTotal.GlobalTotal AS Contribution FROM SubcategoryTotals CROSS JOIN GlobalTotal