Math Functions
Perform numeric calculations, rounding, and mathematical operations on Wikidata values.
Math Function Reference
SPARQL provides various mathematical functions for numeric calculations.
| Function | Description | Example |
|---|---|---|
ABS(x) |
Absolute value | ABS(-5) → 5 |
ROUND(x) |
Round to nearest integer | ROUND(3.7) → 4 |
CEIL(x) |
Round up to integer | CEIL(3.2) → 4 |
FLOOR(x) |
Round down to integer | FLOOR(3.9) → 3 |
RAND() |
Random number 0-1 | RAND() → 0.573... |
+, -, *, / |
Basic arithmetic | 10 / 3 → 3.333... |
Basic Arithmetic
Perform calculations on numeric properties.
Calculate Population Density
Divide population by area to get density:
SELECT ?country ?countryLabel ?population ?area ?density WHERE {
?country wdt:P31 wd:Q6256 . # instance of: country
?country wdt:P1082 ?population . # population
?country wdt:P2046 ?area . # area in km²
FILTER(?area > 0) # avoid division by zero
BIND(?population / ?area AS ?density)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY DESC(?density)
LIMIT 50
Price Per Unit Calculation
Calculate ratios and unit prices:
SELECT ?building ?buildingLabel ?height ?floors ?heightPerFloor WHERE {
?building wdt:P31/wdt:P279* wd:Q11303 . # skyscraper
?building wdt:P2048 ?height . # height in meters
?building wdt:P1101 ?floors . # number of floors
FILTER(?floors > 0)
BIND(?height / ?floors AS ?heightPerFloor)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY DESC(?height)
LIMIT 50
Rounding Functions
Control decimal precision with ROUND, CEIL, and FLOOR.
Round to Nearest Integer
Clean up calculated values:
SELECT ?city ?cityLabel ?population ?area ?densityRounded WHERE {
?city wdt:P31/wdt:P279* wd:Q515 . # city
?city wdt:P1082 ?population .
?city wdt:P2046 ?area .
FILTER(?area > 0 && ?population > 1000000)
BIND(ROUND(?population / ?area) AS ?densityRounded)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY DESC(?densityRounded)
LIMIT 30
Ceiling and Floor
Round up or down:
SELECT ?mountain ?mountainLabel ?elevation ?elevationKm WHERE {
?mountain wdt:P31 wd:Q8502 . # mountain
?mountain wdt:P2044 ?elevation . # elevation in meters
FILTER(?elevation > 7000)
BIND(CEIL(?elevation / 1000) AS ?elevationKm) # Round up to km
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY DESC(?elevation)
LIMIT 30
Absolute Value
Use ABS() to get magnitude regardless of sign.
Temperature Extremes
Find places with extreme temperatures (hot or cold):
SELECT ?city ?cityLabel ?latitude ?absLatitude WHERE {
?city wdt:P31/wdt:P279* wd:Q515 . # city
?city wdt:P1082 ?population .
?city wdt:P625 ?coord .
BIND(geof:latitude(?coord) AS ?latitude)
BIND(ABS(?latitude) AS ?absLatitude)
FILTER(?population > 100000)
FILTER(?absLatitude > 60) # Far from equator (north or south)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY DESC(?absLatitude)
LIMIT 30
Difference from Average
Find how far values deviate from a reference:
SELECT ?person ?personLabel ?birthYear ?yearsFrom1900 WHERE {
?person wdt:P106 wd:Q170790 . # mathematician
?person wdt:P569 ?birthDate .
BIND(YEAR(?birthDate) AS ?birthYear)
BIND(ABS(?birthYear - 1900) AS ?yearsFrom1900)
FILTER(?yearsFrom1900 <= 50) # Within 50 years of 1900
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY ?yearsFrom1900
LIMIT 50
Random Sampling
Use RAND() to get random samples of data.
Random Selection
Get a random sample of items:
SELECT ?painting ?paintingLabel ?creatorLabel WHERE {
?painting wdt:P31 wd:Q3305213 . # painting
?painting wdt:P170 ?creator .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY RAND()
LIMIT 10
Random with Conditions
Random sample within constraints:
SELECT ?writer ?writerLabel ?birthYear WHERE {
?writer wdt:P106 wd:Q36180 . # writer
?writer wdt:P27 wd:Q142 . # country: France
?writer wdt:P569 ?birthDate .
BIND(YEAR(?birthDate) AS ?birthYear)
FILTER(?birthYear >= 1800 && ?birthYear <= 1900)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY RAND()
LIMIT 5
Percentage Calculations
Calculate percentages and proportions.
Percentage of Total
Calculate what percentage each value represents:
SELECT ?country ?countryLabel ?population ?percentOfWorld WHERE {
BIND(8000000000 AS ?worldPop) # Approximate world population
?country wdt:P31 wd:Q6256 .
?country wdt:P1082 ?population .
BIND(ROUND(?population * 10000 / ?worldPop) / 100 AS ?percentOfWorld)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY DESC(?population)
LIMIT 20
Ratio Calculations
Compare two quantities:
SELECT ?country ?countryLabel ?gdp ?population ?gdpPerCapita WHERE {
?country wdt:P31 wd:Q6256 . # country
?country wdt:P2131 ?gdp . # nominal GDP
?country wdt:P1082 ?population .
FILTER(?population > 0)
BIND(ROUND(?gdp / ?population) AS ?gdpPerCapita)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY DESC(?gdpPerCapita)
LIMIT 30
Unit Conversions
Convert between different units of measurement.
Meters to Feet
Convert elevation from meters to feet:
SELECT ?mountain ?mountainLabel ?elevationM ?elevationFt WHERE {
?mountain wdt:P31 wd:Q8502 . # mountain
?mountain wdt:P2044 ?elevationM . # elevation in meters
FILTER(?elevationM > 8000)
BIND(ROUND(?elevationM * 3.28084) AS ?elevationFt)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY DESC(?elevationM)
Kilometers to Miles
Convert distance units:
SELECT ?river ?riverLabel ?lengthKm ?lengthMi WHERE {
?river wdt:P31 wd:Q4022 . # river
?river wdt:P2043 ?lengthKm . # length in km
FILTER(?lengthKm > 3000)
BIND(ROUND(?lengthKm * 0.621371) AS ?lengthMi)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY DESC(?lengthKm)
LIMIT 20
Conditional Calculations
Combine math with IF statements for conditional logic.
Categorizing by Size
Assign size categories based on numeric values:
SELECT ?city ?cityLabel ?population ?sizeCategory WHERE {
?city wdt:P31/wdt:P279* wd:Q515 .
?city wdt:P17 wd:Q30 . # USA
?city wdt:P1082 ?population .
BIND(
IF(?population > 1000000, "Major City",
IF(?population > 500000, "Large City",
IF(?population > 100000, "Medium City",
"Small City"))) AS ?sizeCategory)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY DESC(?population)
LIMIT 50