[SPARQL] SAPRQL commands

SPARQL commands


These are the basic commands of SPARQL

DISTINCT

Ensures the result set does not contain duplicate rows.

SELECT DISTINCT ?item ?itemLabel ?article ?titleLabel

Variable Declaration

Variables in SPARQL are prefixed with `?` and can be dynamically declared.

SELECT DISTINCT ?item ?itemLabel ?article
Variables can also be assigned within clauses like GROUP_CONCAT, FILTER, and OPTIONAL.

GROUP_CONCAT and GROUP BY

GROUP_CONCAT aggregates values into a single string separated by a delimiter, and GROUP BY groups results by specified variables.

(GROUP_CONCAT(DISTINCT ?genreLabel; separator=", ") AS ?genres)
GROUP BY ?item ?itemLabel ?article ?titleLabel

Triple Pattern

Used to retrieve values and bind them to variables or verify relationships in the format <subject> <predicate> <object>.

?item wdt:P31 wd:Q7889.
# This checks if ?item is an instance of wd:Q7889.
?item wdt:P136 ?genre.
# This assigns the P136 (genre) property to ?genre.

FILTER()

Adds conditions to filter query results.

FILTER(CONTAINS(LCASE(?label), "{search_keyword}"))

LANG()

Ensures the language of a value matches a specific language.

FILTER(LANG(?platformLabel) = "en")

CONTAINS()

Checks if a substring exists within a string.

FILTER(CONTAINS(LCASE(?platformLabel), "playstation 4"))

LCASE()

Converts strings to lowercase for case-insensitive matching.

LCASE(?platformLabel)

|| (OR)

Combines multiple conditions.

FILTER(CONTAINS(LCASE(?platformLabel), "playstation 4") ||
                CONTAINS(LCASE(?platformLabel), "playstation 5"))

OPTIONAL

Specifies optional patterns that may or may not exist in the data.

OPTIONAL {?item wdt:P136 ?genre.
          ?genre rdfs:label ?genreLabel.}

LIMIT

Restricts the number of results returned.

LIMIT 10

OFFSET

Skips a specified number of results, useful for pagination when used with LIMIT.

OFFSET 10 #This skips the first 10 items in the result set.
LIMIT 10 #This retrieves the next 10 items, starting from the 11th item.


Comments