---
title: "Text2SQL using Hugging Face Dataset Viewer API and Motherduck DuckDB-NSQL-7B"
description: "DuckDB-NSQL-7B, a Llama-2-7B model fine-tuned on general SQL and then DuckDB text-to-SQL pairs, can turn plain-language questions into DuckDB SQL that runs directly against Hugging Face datasets."
url: "https://swe.quest/articles/text2sql-using-hugging-face-dataset-viewer-api-and-motherduck-duckdb-nsql-7b-7024872a0cc79ab7"
canonical: "https://huggingface.co/blog/duckdb-nsql-7b"
source: "Hugging Face"
published: "2024-04-04"
difficulty: "medium"
kind: "How-to"
reading_minutes: 7
tags: ["llms","sql","analytics","python"]
---

# Text2SQL using Hugging Face Dataset Viewer API and Motherduck DuckDB-NSQL-7B

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2024-04-04, 7 min read, How-to. Original: [https://huggingface.co/blog/duckdb-nsql-7b](https://huggingface.co/blog/duckdb-nsql-7b)

## Summary

DuckDB-NSQL-7B, a Llama-2-7B model fine-tuned on general SQL and then DuckDB text-to-SQL pairs, can turn plain-language questions into DuckDB SQL that runs directly against Hugging Face datasets. The pipeline fetches a dataset's auto-converted parquet URL from the Dataset Viewer API and derives a CREATE TABLE schema by loading one row into DuckDB. It then prompts the quantized GGUF model through llama.cpp with the schema and question, rewrites the table name to the parquet URL, and executes the query in DuckDB. On the world-cities-geo dataset the generated query matches the results of the Viewer's search and filter APIs.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [SQL](https://swe.quest/tags/sql.md) (Languages)
- [Analytics & OLAP](https://swe.quest/tags/analytics.md) (Data & databases)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- SQL basics ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- Python ([easy posts on Python](https://swe.quest/tags/python.md?difficulty=easy))
- LLM inference basics ([easy posts on Inference & serving](https://swe.quest/tags/inference.md?difficulty=easy))

## Keywords

DuckDB-NSQL-7B, DuckDB, Hugging Face Dataset Viewer API, llama.cpp, GGUF, Parquet, MotherDuck, Llama 2

## Related posts

- [Introducing the SQL Console on Datasets](https://swe.quest/articles/introducing-the-sql-console-on-datasets-89aa5e3e1204ae2f.md): Hugging Face · 2024-09-17 · Medium · 5 min read · SQL, Analytics & OLAP, WebAssembly, Model training
  Hugging Face Hub datasets now have a one-click SQL Console that runs DuckDB WASM entirely in the browser against each dataset's Parquet files, with views created per split and config.
- [DuckDB: analyze 50,000+ datasets stored on the Hugging Face Hub](https://swe.quest/articles/duckdb-analyze-50-000-datasets-stored-on-the-hugging-face-hub-05ca0d0e06164c0a.md): Hugging Face · 2023-06-07 · Medium · 3 min read · Analytics & OLAP, SQL, Python
  Every public dataset on the Hugging Face Hub is now automatically converted to Parquet by the dataset viewer and can be queried with SQL through DuckDB.
- [Building a RAG pipeline for Google Analytics with ClickHouse and Amazon Bedrock](https://swe.quest/articles/building-a-rag-pipeline-for-google-analytics-with-clickhouse-and-amazon-bedrock-e2945701d51424da.md): ClickHouse · 2023-11-21 · Medium · 26 min read · RAG & retrieval, LLMs, Vector search, Analytics & OLAP, Cloud platforms
  A natural language interface over raw Google Analytics data stored in ClickHouse is built as a multi-step RAG pipeline that generates SQL from user questions using Amazon Bedrock models.
- [Visualizing BlueSky data with Evidence.dev](https://swe.quest/articles/visualizing-bluesky-data-with-evidence-dev-2a64305ef654ddec.md): ClickHouse · 2025-01-15 · Medium · 6 min read · Analytics & OLAP, SQL
  Evidence.dev builds dashboards from SQL and markdown, running queries at build time rather than at request time.
- [Generate embeddings with Neon AI Gateway](https://swe.quest/articles/generate-embeddings-with-neon-ai-gateway-8da1f214a3b9e4fc.md): Neon · 2026-10-02 · Medium · 7 min read · RAG & retrieval, Vector search, PostgreSQL, LLMs
  Neon AI Gateway now serves embedding models on its OpenAI-compatible /v1/embeddings endpoint using the same branch-scoped credential as chat, accepting up to 150 inputs per request.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
