SELECT
Retrieve specific variables from your query results. SELECT is the most common query form in SPARQL, returning data in a table format.
Understanding SELECT
SELECT specifies which variables to include in the query results. Variables in SPARQL
start with ? (or $) and get bound to values from the data.
| Syntax | Description |
|---|---|
SELECT * | Return all variables |
SELECT ?var1 ?var2 | Return specific variables |
SELECT (?x AS ?newName) | Rename a variable |
Basic SELECT Examples
SELECT *
WHERE {
?country wdt:P31 wd:Q6256 ;
wdt:P36 ?capital .
}
LIMIT 5
The asterisk (*) returns all variables mentioned in the WHERE clause.
This is useful for exploration but less efficient for production queries.
SELECT ?country ?countryLabel
WHERE {
?country wdt:P31 wd:Q6256 .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 10
Explicitly listing variables is preferred. It makes queries clearer and ensures you only transfer the data you need.
SELECT with Computed Values
SELECT ?country (?countryLabel AS ?name)
WHERE {
?country wdt:P31 wd:Q6256 .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 10
The AS keyword creates an alias for a variable. This is useful for
making output column names more readable or for computed expressions.
SELECT ?country ?countryLabel ?population (?population / 1000000 AS ?popMillions)
WHERE {
?country wdt:P31 wd:Q6256 ;
wdt:P1082 ?population .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY DESC(?population)
LIMIT 10
You can include expressions in SELECT to compute new values. The expression result
must be given a name using AS.
Key Entities Used in Examples
| Entity | Description |
|---|---|
wd:Q6256 | Country |
wdt:P31 | Instance of (type) |
wdt:P36 | Capital |
wdt:P1082 | Population |