Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC-10010: Cheatsheet update #2973

Draft
wants to merge 7 commits into
base: release/7.2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 37 additions & 33 deletions modules/cheatsheets/Couchbase-N1QL-CheatSheet.adoc
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
= QUERYING WITH {sqlpp}
:author: Couchbase, Inc.
:title: Couchbase {sqlpp} Cheatsheet
:revnumber: 2
:sqlpp: SQL++
:revnumber: 3
:revdate: {docdate}
:source-highlighter: highlight.js
:highlightjsdir: highlight
:highlightjs-theme: foundation
:stylesheet: asciidoctor-pdf/css/asciidoctor.css;asciidoctor-pdf/css/document.css;cheatsheet.css
:stylesheet: +cheatsheet.css
:stylesdir: style
:imagesdir: images
:description: Created using asciidoctor-pdf.js — https://github.com/Mogztter/asciidoctor-pdf.js

image::CB Color w Black.svg[,100]

This cheatsheet uses the dataset found in the online {sqlpp} tutorial at http://query-tutorial.couchbase.com.
This cheatsheet uses the dataset found in the online tutorial at http://query-tutorial.couchbase.com.

[[basics]]
== Basics
== BASICS

[[basics-select]]
=== SELECT Statement

[source,sqlpp]
[source,n1ql]
----
SELECT count(*), state
FROM customer
Expand All @@ -36,16 +37,18 @@ The result set is grouped and ordered by state.
Output is limited to 5 documents, after skipping the first 5.

[[basics-arithmetic]]
=== SIMPLE ARITHMETIC
=== Simple Arithmetic

Normalized, rounded, and truncated ratings:

[source,sqlpp]
[source,n1ql]
----
SELECT
AVG(reviews.rating) / 5 AS normalizedRating,
ROUND((AVG(reviews.rating) / 5), 2) AS roundedRating,
TRUNC((AVG(reviews.rating) / 5), 3) AS truncRating
SELECT AVG(reviews.rating) / 5
AS normalizedRating,
ROUND((AVG(reviews.rating) / 5), 2)
AS roundedRating,
TRUNC((AVG(reviews.rating) / 5), 3)
AS truncRating
FROM reviews AS reviews
WHERE reviews.customerId = "customer62"
----
Expand All @@ -68,12 +71,12 @@ See the result set for this query below:
Try using other aggregation functions like SUM, MIN, and MAX.

[[basics-strings]]
=== STRING CONCATENATION AND MATCHING
=== String Concatenation and Matching

The || operator concatenates the first and last names to form the full name.
The LIKE operator filters customers with email addresses ending in .biz.

[source,sqlpp]
[source,n1ql]
----
SELECT firstName || " " || lastName AS fullName
FROM customer
Expand All @@ -85,7 +88,7 @@ WHERE emailAddress LIKE "%.biz"
"results": [
{
"fullName": "Joyce Murazik"
}, ...
}, // ...
]
----

Expand All @@ -98,19 +101,19 @@ The DISTINCT keyword enables you to remove duplicate results.

To count the number of unique customers who have purchased something:

[source,sqlpp]
[source,n1ql]
----
SELECT COUNT( DISTINCT customerId )
FROM purchases
----

[[basics-null-missing]]
=== NULL AND MISSING VALUES
=== NULL and MISSING Values

JSON documents can contain NULL values or omit fields entirely.
The NULL/MISSING operators let you test for these conditions.

[source,sqlpp]
[source,n1ql]
----
SELECT fname, children
FROM tutorial
Expand All @@ -120,12 +123,12 @@ WHERE children IS NULL
Now, try changing IS NULL to IS MISSING.

[[basics-indexes]]
=== INDEXES
=== Indexes

{sqlpp} uses indexes to perform queries.
You can create primary indexes and global secondary indexes.

[source,sqlpp]
[source,n1ql]
----
CREATE INDEX idx ON `customer`(`emailAddress`)
----
Expand All @@ -135,18 +138,19 @@ CREATE INDEX idx ON `customer`(`emailAddress`)

EXPLAIN shows how a statement will operate.

[source,sqlpp]
[source,n1ql]
----
EXPLAIN <Query Statement>
----

[[data]]
== Data Structures
== DATA STRUCTURES

[[data-arrays-objects]]
=== ARRAYS AND OBJECTS
=== Arrays and Objects

{sqlpp} supports nested JSON objects where you can use the dot “.” operator to access fields nested inside other objects as well as the bracket [index] to access elements inside an array.
{sqlpp} supports nested JSON arrays and objects.
You can use the dot “.” operator to access fields inside nested objects, and the bracket [index] to access elements inside an array.

For example, consider the following object:

Expand All @@ -165,17 +169,17 @@ ARRAY_APPEND(<array>,<value>) +
ARRAY_CONCAT(<array1>,<array2>)

[[data-collections]]
=== COLLECTION EXPRESSIONS
=== Range Predicates

A collection in {sqlpp} is an array-valued subpath or expression.
Collection predicates allow you to test a boolean condition over the elements of a collection.
{sqlpp} provides operators to work with arrays of nested objects.
Range predicates allow you to test a boolean condition over the elements of an array.

The ANY operator allows you to search through an array, returning TRUE when at least one match is found.
With the EVERY operator, every single element needs to match.

To search for purchase orders with a particular item purchased 5 times or more:

[source,sqlpp]
[source,n1ql]
----
SELECT *
FROM purchases
Expand All @@ -185,13 +189,13 @@ WHERE ANY item IN purchases.lineItems SATISFIES item.count >= 5 END
Try changing ANY to EVERY.

[[data-array-first]]
==== ARRAY and FIRST
=== Range Transformations

To map and filter elements of a collection, you can use the ARRAY and FIRST operators.
To map and filter elements of an array, you can use the ARRAY and FIRST operators.

To get an array of products for each purchase order:

[source,sqlpp]
[source,n1ql]
----
SELECT ARRAY item.product
FOR item IN purchases.lineItems END
Expand All @@ -202,7 +206,7 @@ FROM purchases
Changing ARRAY to FIRST will produce the first product in each purchase order.

[[joins]]
== Joins
== JOINS

[[join-nest-unnest]]
=== JOIN, NEST, and UNNEST
Expand All @@ -213,7 +217,7 @@ NEST produces a single result for each left-hand input, while the right-hand inp

To assemble a complete list of products purchased by a customer:

[source,sqlpp]
[source,n1ql]
----
SELECT c, pr
FROM purchases pu
Expand All @@ -226,7 +230,7 @@ The UNNEST clause allows you to take contents of a nested array and join them wi

To list products belonging to a particular category:

[source,sqlpp]
[source,n1ql]
----
SELECT p
FROM product p
Expand Down
Loading