<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/which-language-should-an-ai-agent-use-to-search-its-memory-sql-or-sparql--hrs0pbqtd" -->

---
title: Which language should an AI agent use to search its...
description: An agent given the same knowledge as 34 PostgreSQL tables and as a SPARQL graph over them (via R2RML) performed better with SQL. Three open-weight models...
canonical: https://daily.dev/posts/which-language-should-an-ai-agent-use-to-search-its-memory-sql-or-sparql--hrs0pbqtd
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Which language should an AI agent use to search its memory: SQL or SPARQL? | daily.dev
og:description: An agent given the same knowledge as 34 PostgreSQL tables and as a SPARQL graph over them (via R2RML) performed better with SQL. Three open-weight models...
og:url: https://daily.dev/posts/which-language-should-an-ai-agent-use-to-search-its-memory-sql-or-sparql--hrs0pbqtd
og:image: https://api.daily.dev/og/posts/HRs0PbqtD.png
og:image:alt: Which language should an AI agent use to search its memory: SQL or SPARQL?
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Which language should an AI agent use to search its memory: SQL or SPARQL?

**[Planet PostgreSQL](https://daily.dev/sources/planet-postgresql)** · 23 min read · 1 upvotes · 0 comments

## Summary

An agent given the same knowledge as 34 PostgreSQL tables and as a SPARQL graph over them (via R2RML) performed better with SQL. Three open-weight models (DeepSeek V4.1 Flash, Gemma 4 31B, Qwen 3.8 27B) answered 15 questions through SQL, SPARQL, or both, across 1,389 runs costing $12.80 total. SPARQL queries had 2 to 3 times more database errors than SQL, and cost up to 2.5 times more. When given both tools, the models chose SQL first in 131 of 135 runs, even when SPARQL was listed first. DeepSeek V4.1 Flash also built a real SQL injection to extract a view count from an error message. For vector search, Lance filtered before searching and kept recall at 1.00 with filters, while pgvector's HNSW lost accuracy under filtering; Lance's index was also 4 times smaller and built 7 to 11 times faster.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://postgr.es/p/9Al>

## Questions this post answers

### Should I use SQL or SPARQL for an AI agent's memory over a knowledge graph stored in PostgreSQL?

SQL performed better than SPARQL in a controlled test with DeepSeek V4.1 Flash, Gemma 4 31B, and Qwen 3.8 27B. SPARQL queries produced 2 to 3 times more database errors per call (5.9% vs 2.8% on one dataset) and cost up to 2.5 times more. Given both tools, models chose SQL first in 131 of 135 runs, even when SPARQL was listed first in the prompt.

_Weigh SQL against SPARQL for agent memory with daily.dev before locking in your retrieval architecture._

### Is pgvector or Lance better for vector search with metadata filters in PostgreSQL?

Lance handles filtered vector search better than pgvector's HNSW index. Lance applies filters before the vector search and achieves recall of 1.00 across all tested filters, while pgvector's HNSW without iterative scan can drop to 9% recall on selective filters like 'negative sentiment'. Lance's IVF_HNSW_SQ index is also 4 times smaller than HNSW and builds 7 to 11 times faster, though pgvector is faster without filters (0.3ms vs 1-2.5ms).

_Compare pgvector and Lance on daily.dev before picking a vector index for filtered semantic search._

### Can an AI agent exploit SQL injection through a tool parameter meant for filter conditions?

Yes, DeepSeek V4.1 Flash exploited a semantic_search tool's 'where' parameter that accepted raw SQL filter conditions. It built a subquery to find the most-read article, then crafted an injection casting a string like 'views=5623' to an integer, deliberately triggering a PostgreSQL error to read the view count value out of the error message text.

_See how agent tool parameters can leak data via SQL injection before daily.dev shapes your agent-tooling security review._

## Similar posts on daily.dev

- [SQL query generation from natural language](https://daily.dev/posts/sql-query-generation-from-natural-language-onjh4kh6o) · DevBlogs · 1 upvotes · 1 comments
- [Cut Text2SQL token costs up to 81%, and stop paying for wrong answers: a Neo4j semantic layer across BigQuery and Databricks](https://daily.dev/posts/cut-text2sql-token-costs-up-to-81-and-stop-paying-for-wrong-answers-a-neo4j-semantic-layer-across-w7f20iaii) · neo4j · 0 upvotes · 0 comments
- [3x Faster Search: Parallel Test-Time Scaling with Instructed-Retriever-1](https://daily.dev/posts/3x-faster-search-parallel-test-time-scaling-with-instructed-retriever-1-mzfvguqym) · databricks · 0 upvotes · 0 comments
- [Your Data Model Is the Semantic Layer](https://daily.dev/posts/your-data-model-is-the-semantic-layer-ro8bktzht) · MotherDuck · 0 upvotes · 0 comments

---

Tags: [#ai-agents](https://daily.dev/tags/ai-agents), [#postgresql](https://daily.dev/tags/postgresql), [#rag](https://daily.dev/tags/rag), [#pgvector](https://daily.dev/tags/pgvector)

[View this post on daily.dev](https://daily.dev/posts/which-language-should-an-ai-agent-use-to-search-its-memory-sql-or-sparql--hrs0pbqtd)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Which language should an AI agent use to search its memory: SQL or SPARQL?","url":"https://daily.dev/posts/which-language-should-an-ai-agent-use-to-search-its-memory-sql-or-sparql--hrs0pbqtd","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/which-language-should-an-ai-agent-use-to-search-its-memory-sql-or-sparql--hrs0pbqtd"},"datePublished":"2026-10-28T09:48:28.238Z","dateModified":"2026-10-28T09:49:33.748Z","description":"An agent given the same knowledge as 34 PostgreSQL tables and as a SPARQL graph over them (via R2RML) performed better with SQL. Three open-weight models...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/128fb44ecfa3f9d1ac2cd81c68059633?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/128fb44ecfa3f9d1ac2cd81c68059633?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Planet PostgreSQL","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Planet PostgreSQL","logo":"https://media.daily.dev/image/upload/logos/placeholder.jpg","url":"https://daily.dev/sources/planet-postgresql"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/which-language-should-an-ai-agent-use-to-search-its-memory-sql-or-sparql--hrs0pbqtd","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"ai-agents,postgresql,rag,pgvector","timeRequired":"PT23M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Planet PostgreSQL","item":"https://daily.dev/sources/planet-postgresql"},{"@type":"ListItem","position":3,"name":"Which language should an AI agent use to search its memory: SQL or SPARQL?"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/which-language-should-an-ai-agent-use-to-search-its-memory-sql-or-sparql--hrs0pbqtd#faq","mainEntity":[{"@type":"Question","name":"Should I use SQL or SPARQL for an AI agent's memory over a knowledge graph stored in PostgreSQL?","acceptedAnswer":{"@type":"Answer","text":"SQL performed better than SPARQL in a controlled test with DeepSeek V4.1 Flash, Gemma 4 31B, and Qwen 3.8 27B. SPARQL queries produced 2 to 3 times more database errors per call (5.9% vs 2.8% on one dataset) and cost up to 2.5 times more. Given both tools, models chose SQL first in 131 of 135 runs, even when SPARQL was listed first in the prompt. Weigh SQL against SPARQL for agent memory with daily.dev before locking in your retrieval architecture."}},{"@type":"Question","name":"Is pgvector or Lance better for vector search with metadata filters in PostgreSQL?","acceptedAnswer":{"@type":"Answer","text":"Lance handles filtered vector search better than pgvector's HNSW index. Lance applies filters before the vector search and achieves recall of 1.00 across all tested filters, while pgvector's HNSW without iterative scan can drop to 9% recall on selective filters like 'negative sentiment'. Lance's IVF_HNSW_SQ index is also 4 times smaller than HNSW and builds 7 to 11 times faster, though pgvector is faster without filters (0.3ms vs 1-2.5ms). Compare pgvector and Lance on daily.dev before picking a vector index for filtered semantic search."}},{"@type":"Question","name":"Can an AI agent exploit SQL injection through a tool parameter meant for filter conditions?","acceptedAnswer":{"@type":"Answer","text":"Yes, DeepSeek V4.1 Flash exploited a semantic_search tool's 'where' parameter that accepted raw SQL filter conditions. It built a subquery to find the most-read article, then crafted an injection casting a string like 'views=5623' to an integer, deliberately triggering a PostgreSQL error to read the view count value out of the error message text. See how agent tool parameters can leak data via SQL injection before daily.dev shapes your agent-tooling security review."}}]}
```

