DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Keep Calm and Column Wise
  • Accelerating Insights With Couchbase Columnar
  • JSON-Based Serialized LOB Pattern
  • Introduction to Couchbase for Oracle Developers and Experts: Part 2 - Database Objects

Trending

  • SEO Writing 101: A Simple Guide to Getting Found
  • The Twelve-Factor Agents: Building Production-Ready LLM Applications
  • Putting Guardrails Around What Your AI Agent Is Allowed to Touch
  • MCP vs REST/HTTP API vs Kafka: The Architect's Guide to Agentic AI Integration

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Query JSON Using SQL With Couchbase Query Workbench

Query JSON Using SQL With Couchbase Query Workbench

Would you like to query a JSON document database using SQL-like syntax? Couchbase has N1QL for you. Read on to learn more.

By 
Arun Gupta
·
Jan. 14, 16 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
8.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

You’d like to query a JSON document database using SQL-like syntax?

Couchbase has N1QL for you.

N1QL is a declarative query language that extends SQL for JSON. You can query data via native framework and language integration, a fluent API, or the JDBC/ODBC drivers.

N1QL enables you to query JSON documents without any limitations–sort, filter, transform, group, and combine data with a single query. That’s right. You can combine data from multiple documents with a JOIN. That flexible data model you were promised? This is it. You’re no longer limited to “single table” and “table per query” data models.

N1QL Tutorial is a great resource to learn the concepts of querying JSON documents using SQL-like syntax. Here are some examples:

SELECT children[0].fname AS cname
    FROM tutorial
       WHERE fname='Dave'

OR

SELECT fname, age, age/7 AS age_dog_years 
    FROM tutorial 
        WHERE fname = 'Dave'

OR

SELECT relation, COUNT(*) AS count
    FROM tutorial
        GROUP BY relation
            HAVING COUNT(*) > 1

OR

SELECT t.relation, COUNT(*) AS count, AVG(c.age) AS avg_age
    FROM tutorial t
    UNNEST t.children c
    WHERE c.age > 10
    GROUP BY t.relation
    HAVING COUNT(*) > 1
    ORDER BY avg_age DESC
    LIMIT 1 OFFSET 1

OR

SELECT
product.name, 
COUNT(reviews) AS reviewCount,
ROUND(AVG(reviews.rating),1) AS AvgRating,
category 
 FROM reviews AS reviews
JOIN product AS product 
 ON KEYS reviews.productId
UNNEST product.categories AS category
WHERE category = "Appliances"
GROUP BY category, product
ORDER BY AvgRating 
DESC LIMIT 3

So, are you ready to experiment with N1QL? You can certainly use CBQ tool.

Alternatively, you can use Couchbase Query Workbench. The query workbench provides a rich graphical user interface to prepare and execute simple to complex N1QL queries. It provides a convenient way to perform query development by enabling you to browse, create, and run N1QL statements, and view results.

Want to learn how to get started?

Learn all about it in this brief session with Eben Haber:


Ask your questions at Stack Overflow or Couchbase Forums.

You can also follow us at @couchbasedev and @couchbase.

JSON Database Workbench (AmigaOS) sql

Published at DZone with permission of Arun Gupta. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Keep Calm and Column Wise
  • Accelerating Insights With Couchbase Columnar
  • JSON-Based Serialized LOB Pattern
  • Introduction to Couchbase for Oracle Developers and Experts: Part 2 - Database Objects

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook