Basic

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.

SyntaxDescription
SELECT *Return all variables
SELECT ?var1 ?var2Return specific variables
SELECT (?x AS ?newName)Rename a variable

Basic SELECT Examples

Select All Variables
Run ↗
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 Specific Variables
Run ↗
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

Rename Variables with AS
Run ↗
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 with Expressions
Run ↗
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

EntityDescription
wd:Q6256Country
wdt:P31Instance of (type)
wdt:P36Capital
wdt:P1082Population