Skip to content

Metaclasses of metaclasses

The query finds explicitly typed metaclasses in Wikidata whose recorded direct instances are all explicitly typed metaclasses too. It requires at least one instance, ranks matches by distinct instance count, and shows one example instance per match. Metaclass typing uses instance of (P31), followed by the type's subclass of (P279) hierarchy to metaclass.

Wikidata calls a class of second-order classes a third-order class. The query also allows mixed or higher orders. It checks recorded truthy P31 values; missing metaclass typing excludes a candidate, and unrecorded instances cannot be checked. Ties and example instances are selected by URI.

printf '%s' 'PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?metaclass ?metaclassLabel ?instanceCount ?exampleInstance ?exampleInstanceLabel WHERE {
  {
    SELECT ?metaclass (COUNT(DISTINCT ?instance) AS ?instanceCount)
           (MIN(STR(?instance)) AS ?exampleInstanceIRI) WHERE {
      {
        SELECT DISTINCT ?metaclass WHERE {
          ?metaclass wdt:P31/wdt:P279* wd:Q19478619 .
        }
      }
      ?instance wdt:P31 ?metaclass .
      FILTER NOT EXISTS {
        ?counterexample wdt:P31 ?metaclass .
        FILTER NOT EXISTS {
          ?counterexample wdt:P31/wdt:P279* wd:Q19478619 .
        }
      }
    }
    GROUP BY ?metaclass
    ORDER BY DESC(?instanceCount) ?metaclass
    LIMIT 10
  }
  BIND(IRI(?exampleInstanceIRI) AS ?exampleInstance)
  OPTIONAL {
    ?metaclass rdfs:label ?englishLabel .
    FILTER(LANG(?englishLabel) = "en")
  }
  OPTIONAL {
    ?exampleInstance rdfs:label ?exampleEnglishLabel .
    FILTER(LANG(?exampleEnglishLabel) = "en")
  }
  BIND(COALESCE(?englishLabel, STRAFTER(STR(?metaclass), "entity/")) AS ?metaclassLabel)
  BIND(COALESCE(?exampleEnglishLabel, STRAFTER(STR(?exampleInstance), "entity/")) AS ?exampleInstanceLabel)
}
ORDER BY DESC(?instanceCount) ?metaclass
' |
  vitrify sparql --endpoint https://query.wikidata.org/sparql \
  --query - \
  --format csv \
  --to metaclasses-of-metaclasses

Failure65.55 s

Response bytes

metaclasses-of-metaclasses/
├── ro-crate-metadata.json
└── query.rq

Failure

endpoint returned HTTP 504 Gateway Timeout

{
  "@context": "https://w3id.org/ro/crate/1.2/context",
  "@graph": [
    {
      "@id": "ro-crate-metadata.json",
      "@type": "CreativeWork",
      "about": {
        "@id": "./"
      },
      "conformsTo": {
        "@id": "https://w3id.org/ro/crate/1.2"
      }
    },
    {
      "@id": "query.rq",
      "@type": "File",
      "contentSize": "1319",
      "encodingFormat": "application/sparql-query",
      "sha256": "ac06d1aed8d0069e851eb3167e416aa59e0da4de878a8c1e923e096ff6eafa31"
    },
    {
      "@id": "https://query.wikidata.org/sparql",
      "@type": "Organization"
    },
    {
      "@id": "https://vitrify.iolanta.tech",
      "@type": "SoftwareApplication",
      "name": "Vitrify",
      "url": "https://vitrify.iolanta.tech",
      "version": "0.1.1"
    },
    {
      "@id": "#retrieval-duration",
      "@type": "PropertyValue",
      "name": "Retrieval duration",
      "propertyID": "retrieval-duration",
      "unitCode": "SEC",
      "value": 65.550446695
    },
    {
      "@id": "#error",
      "@type": "Thing",
      "description": "endpoint returned HTTP 504 Gateway Timeout"
    },
    {
      "@id": "./",
      "@type": "Dataset",
      "additionalProperty": [
        {
          "@id": "#retrieval-duration"
        }
      ],
      "datePublished": "2026-10-16T08:01:42.664046613+00:00",
      "description": "SPARQL retrieval recorded by Vitrify.",
      "hasPart": [
        {
          "@id": "query.rq"
        }
      ],
      "license": "No license asserted by Vitrify; consult the source endpoint's terms of use.",
      "name": "SPARQL retrieval",
      "publisher": {
        "@id": "https://query.wikidata.org/sparql"
      }
    },
    {
      "@id": "#retrieval",
      "@type": "CreateAction",
      "actionStatus": {
        "@id": "http://schema.org/FailedActionStatus"
      },
      "agent": {
        "@id": "https://vitrify.iolanta.tech"
      },
      "description": "HTTP 504; Accept: text/csv",
      "endTime": "2026-10-16T08:01:42.664046613+00:00",
      "error": {
        "@id": "#error"
      },
      "identifier": "https://query.wikidata.org/sparql",
      "instrument": {
        "@id": "https://vitrify.iolanta.tech"
      },
      "object": [
        {
          "@id": "https://query.wikidata.org/sparql"
        },
        {
          "@id": "query.rq"
        }
      ],
      "startTime": "2026-10-16T08:00:37.095499896+00:00"
    }
  ]
}

Download ro-crate-metadata.json

ro-crate-metadata.json

PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?metaclass ?metaclassLabel ?instanceCount ?exampleInstance ?exampleInstanceLabel WHERE {
  {
    SELECT ?metaclass (COUNT(DISTINCT ?instance) AS ?instanceCount)
           (MIN(STR(?instance)) AS ?exampleInstanceIRI) WHERE {
      {
        SELECT DISTINCT ?metaclass WHERE {
          ?metaclass wdt:P31/wdt:P279* wd:Q19478619 .
        }
      }
      ?instance wdt:P31 ?metaclass .
      FILTER NOT EXISTS {
        ?counterexample wdt:P31 ?metaclass .
        FILTER NOT EXISTS {
          ?counterexample wdt:P31/wdt:P279* wd:Q19478619 .
        }
      }
    }
    GROUP BY ?metaclass
    ORDER BY DESC(?instanceCount) ?metaclass
    LIMIT 10
  }
  BIND(IRI(?exampleInstanceIRI) AS ?exampleInstance)
  OPTIONAL {
    ?metaclass rdfs:label ?englishLabel .
    FILTER(LANG(?englishLabel) = "en")
  }
  OPTIONAL {
    ?exampleInstance rdfs:label ?exampleEnglishLabel .
    FILTER(LANG(?exampleEnglishLabel) = "en")
  }
  BIND(COALESCE(?englishLabel, STRAFTER(STR(?metaclass), "entity/")) AS ?metaclassLabel)
  BIND(COALESCE(?exampleEnglishLabel, STRAFTER(STR(?exampleInstance), "entity/")) AS ?exampleInstanceLabel)
}
ORDER BY DESC(?instanceCount) ?metaclass

Download query.rq

query.rq