LIMIT
Control the size of your result sets. LIMIT restricts the number of rows returned, essential for large datasets and quick previews.
Understanding LIMIT
The LIMIT clause restricts the maximum number of results returned by a query. This is essential when working with large datasets like Wikidata, where queries can potentially return millions of rows.
Key entities used in examples:
wdt:P31 (instance of) links an item to its type, while
wd:Q515 (city) and wd:Q6256 (country) are common type values.
Other properties include wdt:P1082 (population), wdt:P2046 (area),
wdt:P2044 (elevation), wdt:P571 (inception), and
wdt:P106 (occupation). Example values include wd:Q5 (human) and
wd:Q82955 (mathematician).
| Syntax | Description | Use Case |
|---|---|---|
LIMIT n |
Return at most n results | Quick previews, testing |
ORDER BY ... LIMIT n |
Get top n sorted results | Leaderboards, rankings |
LIMIT n OFFSET m |
Skip m, return n | Pagination |
Basic LIMIT Examples
SELECT ?country ?countryLabel
WHERE {
?country wdt:P31 wd:Q6256 .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
LIMIT 10
Returns only 10 country entities, giving a quick preview without loading the full dataset.
SELECT ?city ?cityLabel ?population
WHERE {
?city wdt:P31 wd:Q515 ;
wdt:P1082 ?population .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
LIMIT 5
Restricts output to five cities to keep results small while inspecting structure and fields.
LIMIT with ORDER BY
When combined with ORDER BY, LIMIT becomes a powerful tool for finding top/bottom N results - creating rankings, leaderboards, and extremes.
SELECT ?country ?countryLabel ?population
WHERE {
?country wdt:P31 wd:Q6256 ;
wdt:P1082 ?population .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
ORDER BY DESC(?population)
LIMIT 10
Sorts countries by population (descending) and keeps only the top 10.
SELECT ?country ?countryLabel ?area
WHERE {
?country wdt:P31 wd:Q6256 ;
wdt:P2046 ?area .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
ORDER BY ?area
LIMIT 5
Orders by area ascending to surface the smallest countries, then limits to five.
SELECT ?mountain ?mountainLabel ?elevation
WHERE {
?mountain wdt:P31 wd:Q8502 ;
wdt:P2044 ?elevation .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
ORDER BY DESC(?elevation)
LIMIT 10
Ranks mountains by elevation (descending) and returns the highest 10.
Practical LIMIT Patterns
SELECT ?uni ?uniLabel ?founded
WHERE {
?uni wdt:P31 wd:Q3918 ;
wdt:P571 ?founded .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
ORDER BY ?founded
LIMIT 10
Uses ascending founding date to surface the earliest universities, limited to 10.
SELECT ?type ?typeLabel ?example ?exampleLabel
WHERE {
VALUES ?type { wd:Q6256 wd:Q515 wd:Q3918 }
{
SELECT ?type (SAMPLE(?item) AS ?example) WHERE {
?item wdt:P31 ?type .
}
GROUP BY ?type
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Samples one example item per type, then limits the output to the four categories listed.
Best Practices
Always use LIMIT when exploring new queries to avoid timeouts and excessive data transfer. Start with small limits, then increase once you know the query works correctly.
# Start with LIMIT 5 to test, then increase
SELECT ?item ?itemLabel ?value
WHERE {
?item wdt:P31 wd:Q5 ;
wdt:P106 wd:Q82955 ;
wdt:P569 ?value .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
LIMIT 5 # Change to 100 or more once verified
Starts with LIMIT 5 to validate the query quickly before increasing the cap.