SPARQL Query Basics
SPARQL statements are constructed using the following basic query types, clauses, and other solution modifiers.
Standard Query Types
- SELECT: Run SELECT queries when you want to find and return all of the data that matches certain patterns.
- CONSTRUCT: Run CONSTRUCT queries when you want to create or transform data based on the existing data.
- ASK: Run ASK queries when you want to know whether a certain pattern exists in the data. ASK queries return only "true" or "false" to indicate whether a solution exists.
- DESCRIBE: Run DESCRIBE queries when you want to view the RDF graph that describes a particular resource.
Query Clauses
All queries may also include one or more of the following clauses:
- PREFIX Clause: The optional PREFIX clause declares the abbreviations that you want to use to reference URIs in the query.
- FROM Clause: The optional FROM clause defines the data sets or graphs to query. By default, if you do not specify the FROM clause, the scope of a query is limited to the default graph.
- WHERE Clause: The WHERE clause specifies the query pattern for data to match in the graphs or data sets specified in the query.
Solution Modifiers
The following optional query modifiers enable you to restrict, group, sort, or further refine query results:
- ORDER BY: This modifier sorts the result set in a particular order. It sorts query solution results based on the value of one or more variables.
- OFFSET: Using this modifier in conjunction with LIMIT and ORDER BY returns a slice of a sorted solution set, for example, for paging.
- LIMIT: This modifier puts an upper bound on the number of results returned by a query.
- GROUP BY: This modifier is used with aggregate functions and specifies the key variables to use to partition the solutions into groups. For information about the Graph Lakehouse GROUP BY clause extensions, see Advanced Grouping Sets.
- HAVING: This modifier is used with aggregate functions and further filters the results after applying the aggregates.