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
  }
  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://qlever.dev/api/wikidata \
  --query - \
  --format csv \
  --to metaclasses-of-metaclasses

Success13.16 s1,822 bytes

Response bytes

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

Showing 5 of 14 rows.

metaclassmetaclassLabelinstanceCountexampleInstanceexampleInstanceLabel
http://www.wikidata.org/entity/Q24017414second-order class579http://www.wikidata.org/entity/Q100223784athletic shoe model
http://www.wikidata.org/entity/Q103997018direct anatomical metaclass85http://www.wikidata.org/entity/Q103812529organ type
http://www.wikidata.org/entity/Q205585Transport express régional29http://www.wikidata.org/entity/Q1538379TER Nord-Pas-de-Calais
http://www.wikidata.org/entity/Q140354262type of genre14http://www.wikidata.org/entity/Q104822033podcast genre
http://www.wikidata.org/entity/Q103994247medical metaclass13http://www.wikidata.org/entity/Q103995117medical entity type

Download results.csv

results.csv

{
  "@context": "https://w3id.org/ro/crate/1.3/context",
  "@graph": [
    {
      "@id": "ro-crate-metadata.json",
      "@type": "CreativeWork",
      "about": {
        "@id": "./"
      },
      "conformsTo": {
        "@id": "https://w3id.org/ro/crate/1.3"
      }
    },
    {
      "@id": "./",
      "@type": "Dataset",
      "additionalProperty": [
        {
          "@id": "#retrieval-duration"
        }
      ],
      "author": {
        "@id": "https://vitrify.iolanta.tech"
      },
      "datePublished": "2026-10-31T11:11:54.123193696+00:00",
      "description": "SPARQL retrieval materialized by Vitrify.",
      "hasPart": [
        {
          "@id": "results.csv"
        },
        {
          "@id": "query.rq"
        }
      ],
      "license": "No license asserted by Vitrify; consult the source endpoint's terms of use.",
      "name": "results.csv",
      "publisher": {
        "@id": "https://qlever.dev/api/wikidata"
      }
    },
    {
      "@id": "results.csv",
      "@type": "File",
      "contentSize": "1822",
      "encodingFormat": "text/csv",
      "sha256": "154baf48d0331da18c1769c6b52a6c00582ebf78d097962a71aa996ccbd2cde1"
    },
    {
      "@id": "query.rq",
      "@type": "File",
      "contentSize": "1306",
      "encodingFormat": "application/sparql-query",
      "sha256": "edb75482041c86b5f18389864f00cf5eb7da8a99f75753a3867a1ce1c788bbe0"
    },
    {
      "@id": "https://vitrify.iolanta.tech",
      "@type": "SoftwareApplication",
      "name": "Vitrify",
      "url": "https://vitrify.iolanta.tech",
      "version": "0.1.3"
    },
    {
      "@id": "https://qlever.dev/api/wikidata",
      "@type": "Organization"
    },
    {
      "@id": "#retrieval",
      "@type": "CreateAction",
      "actionStatus": {
        "@id": "http://schema.org/CompletedActionStatus"
      },
      "agent": {
        "@id": "https://vitrify.iolanta.tech"
      },
      "description": "HTTP 200; Accept: text/csv",
      "endTime": "2026-10-31T11:11:54.123193696+00:00",
      "identifier": "https://qlever.dev/api/wikidata",
      "instrument": {
        "@id": "https://vitrify.iolanta.tech"
      },
      "object": [
        {
          "@id": "https://qlever.dev/api/wikidata"
        },
        {
          "@id": "query.rq"
        }
      ],
      "result": {
        "@id": "results.csv"
      },
      "startTime": "2026-10-31T11:11:40.963789276+00:00"
    },
    {
      "@id": "#retrieval-duration",
      "@type": "PropertyValue",
      "name": "Retrieval duration",
      "propertyID": "retrieval-duration",
      "unitCode": "SEC",
      "value": 13.158168489
    }
  ]
}

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
  }
  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