---
title: How to Query RDF Data
description: Learn to query RDF graphs using SPARQL and JavaScript patterns
---

# How to Query RDF Data

This guide shows you how to query RDF data in UNRDF using SPARQL and JavaScript patterns.

## Prerequisites

- UNRDF installed (`@unrdf/core` and `@unrdf/oxigraph`)
- Basic understanding of RDF triples
- Familiarity with SPARQL (helpful but not required)

## Simple Pattern Matching

Query triples matching a specific pattern:

```javascript
import { createStore, DataFactory } from '@unrdf/oxigraph'

const { namedNode } = DataFactory
const store = createStore()

// Find all triples with specific subject
const matches = store.match({
  subject: namedNode('http://example.org/person/alice')
})

for (const quad of matches) {
  console.log(quad.predicate.value, '→', quad.object.value)
}
```

## SPARQL Queries

Execute SPARQL SELECT queries:

```javascript
const query = `
  PREFIX foaf: <http://xmlns.com/foaf/0.1/>

  SELECT ?name ?email WHERE {
    ?person foaf:name ?name .
    ?person foaf:mbox ?email .
  }
  ORDER BY ?name
`

const results = store.query(query)

for (const binding of results) {
  console.log(binding.get('name').value, binding.get('email').value)
}
```

## Filtering Results

Add filter conditions:

```javascript
const query = `
  PREFIX ex: <http://example.org/>

  SELECT ?name ?age WHERE {
    ?person ex:name ?name .
    ?person ex:age ?age .
    FILTER (?age >= 18)
  }
`
```

## Aggregation

Count, group, and aggregate:

```javascript
const query = `
  PREFIX ex: <http://example.org/>

  SELECT ?company (COUNT(?employee) AS ?count) WHERE {
    ?employee ex:worksFor ?company .
  }
  GROUP BY ?company
  HAVING (COUNT(?employee) > 5)
`
```

## Streaming Results

For large result sets, use streaming:

```javascript
import { queryToStream } from '@unrdf/streaming'

const stream = queryToStream(store, query)

stream.on('data', (binding) => {
  console.log(binding.get('name').value)
})

stream.on('end', () => {
  console.log('Query complete')
})
```

## Performance Tips

1. **Use indexes** - Oxigraph automatically indexes quads
2. **Limit results** - Add `LIMIT 100` for testing
3. **Stream large datasets** - Use `queryToStream` instead of collecting all results
4. **Filter early** - Put FILTER clauses early in the query

## Common Patterns

### Find all properties of a resource

```javascript
const resourceProps = store.match({
  subject: namedNode('http://example.org/resource/123')
})
```

### Find resources by type

```javascript
const query = `
  PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
  PREFIX ex: <http://example.org/>

  SELECT ?resource WHERE {
    ?resource rdf:type ex:Person .
  }
`
```

### Traverse relationships

```javascript
const query = `
  PREFIX ex: <http://example.org/>

  SELECT ?person ?friend ?friendOfFriend WHERE {
    ?person ex:knows ?friend .
    ?friend ex:knows ?friendOfFriend .
    FILTER (?person != ?friendOfFriend)
  }
`
```

## Related

- 📚 [Tutorial: Getting Started](/tutorials/getting-started)
- 📖 [Reference: Core API](/reference/api/core)
- 💡 [Explanation: RDF Fundamentals](/explanation/rdf-fundamentals)
