Pending completion
Reading data
Now that we've created some data, it's time to explore it.
In this lesson, we'll cover how to use
The
SELECTstatement for both simple and advanced read operations, and how that compares to theRETURNstatement.The
LETstatement for setting parameters to use in our statements.The
LIVE SELECTstatement for reading data in real-time as changes are made to the underlying table.
Starting with the SQL select basics
Let's start with the foundations of a normal SQL SELECT statement.
Here are a few examples.
We can select everything from the product table with SELECT * FROM product. We can also just select specific fields by naming them, such as SELECT name FROM product. Finally, we can use AS to rename fields, such as changing name to product_name
You'll also find familiar SQL clauses such as WHERE, GROUP BY, ORDER BY and LIMIT.
WHERE
We use the WHERE clause to filter for specific records, such as products which have the black pink colour.
GROUP BY
GROUP BY is usually used alongside aggregate functions, which are functions that can either be used on their own or in a statement with GROUP BY that aggregates data. In this example we are using it to find the number of orders and sales amount for each product.
A simple example to show how GROUP BY works is this one using the count() function. Here we want to find the number of orders. It's important to be aware that when counting the number of records in a table, most relational databases don't require you to use a GROUP BY since the data will never be nested.
In SurrealQL however, there is a lot more flexibility to model nested data. Therefore, we can't make the same assumptions as a relational database. This means functions like count() always go record by record unless you specify GROUP ALL which will use the entire table as one group.
ORDER BY
ORDER BY is used to sort the records by a certain field. By default it sorts data in ascending order, but you can also specify DESC for descending order.
LIMIT
Finally, the LIMIT clause is used to limit the number of records we get back from our query.
Moving onto more advanced features
Now that we've covered the foundations that most SQL dialects have, let's look into things specific to SurrealQL.
Selecting a single record or a range of records
In most SQL dialects, you'd need to use the WHERE clause to filter by IDs or fields such as time.
In SurrealQL, the idiomatic way to fetch by record ID is to put the ID in the FROM clause rather than filtering with WHERE.
For a single record, WHERE id = person:… is valid SQL-style syntax and, since SurrealDB 3.0, the query planner optimises it to a direct key-value lookup, so the same path as FROM person:…. Even so, selecting directly by record ID is clearer and is the form we recommend.
For a range of records, WHERE id >= … AND id < … still tends to scan the table. Here you can use a record range in the FROM clause instead, because it reads only the keys in that range from the key-value store.
SurrealQL also makes it easy to select what you need from a record, such as in cases where you want to select everything, but omit certain fields from a record.
Working with objects and arrays
The SELECT statement in SurrealQL is extremely flexible, with many advanced features. You can find a more comprehensive list of these features in our documentation, but for now, let's look at a few examples of how to work with objects and arrays.
For selecting and traversing objects and arrays and arrays of objects, we can use the dot and bracket notation.
Here are some notes on what those queries just did.
We can select the first colour in the colours array using
colours[0]We select
updated_atfrom the time object usingtime.updated_atTo select the first URL in the images array of objects, we use a combination of both,
images[0].url
Object and array functions
Object and array functions are useful for many things such as deduplicating results similar to SELECT DISTINCT in most SQL dialects.
There are a few ways of doing this in SurrealQL:
Using
array::distinct(), if the fields are not nested.Using
array::flatten(), if the fields are nested.Using
array::group(), if you want to call flatten and distinct using a single method call.
Importantly, there is way to count things without using aggregate functions, if what you're counting is either an array or object. As then we can use array::len() or object::len() and get both aggregated data and non-aggregated data in one query.
You can find many more useful functions in our documentation.
Tempfiles
There might be times where you want to run large analytics that have the potential to cause an out-of-memory error (OOM). That is where the TEMPFILES clause comes in, allowing you to process the query in temporary files on disk rather than in memory.
This significantly reduces memory usage, though it's likely to also result in slower performance.
Using LET parameters and subqueries
Subqueries function in similar ways as you'd expect from SQL dialects, such as using them in the SELECT, FROM or WHERE clauses.
We'll cover them in more detail in part 2 on relational style joins.
An alternative to using subqueries is often through using common table expressions (CTEs).
SurrealQL does not use typical CTEs, but we can use the LET statement to cover those use cases. We can also use the LET statement to parameterise our queries, either directly as static values, or dynamic values in combination with the type functions.
The LET statement can do a lot more than what we've covered here, as you can use it in almost every statement in SurrealQL and you can see more examples of that in our documentation.
How SELECT compares to RETURN
Aside from the SELECT statement, you can also use the RETURN statement for reading data. A RETURN on its own just returns the value that follows it, and technically is almost never needed. That's because the value returned from a statement is its output, whether it is preceded by RETURN or not.
One situation where RETURN does make a difference though is when you want to use it to return a value from inside a block and skip the rest.
Otherwise, you still might prefer to use it for the sake of readability. Even when adding RETURN before a value, it ends up being shorter than the combination of FROM ONLY and SELECT VALUE using the SELECT statement. I'd encourage you to experiment with the queries, removing the ONLY and or VALUE clause to see how the result changes.
You can see more examples of how to use the RETURN statement in code blocks, functions and transactions in our documentation.
Going real-time with LIVE SELECT
LIVE SELECT allows us to unlock streaming data magic, through what we call live queries.
Live queries read data in real time, as changes are made to the underlying table.
When you run the LIVE SELECT in the CLI it will return a UUID, which is the live query unique ID. The UUID is used to keep track of the various live queries you have running and to stop them using the KILL statement.
Since Surrealist has live query listening built in, a LIVE SELECT will instead display the option to track the live query that you have just created.
Once you make changes to the table the live query is listening to, in our case the product table, you can see the live results updating.
You can either receive the changes in our normal format or use the JSON PATCH format by specifying LIVE SELECT DIFF FROM product.
You can see more details about live queries in our documentation.
Summary
Let's quickly summarise what we've learned before I see you in the next lesson.
The
SELECTstatementStarts with the foundation of SQL, using familiar clauses such as
WHERE,GROUP BY,ORDER BYandLIMIT.Has more advanced features, such as selecting directly from a record ID or a record range in the
FROMclause — the idiomatic way to avoid table scans when fetching by ID. It can also be used to work with objects and arrays.Can use the
TEMPFILESclause for processing the query in temporary files on disk rather than in memory. This is typically used for very large queries that might otherwise give an out-of-memory error (OOM).
The
RETURNstatementCan
RETURNany value, from strings and numbers to entire code blocks and query results.Can be used instead of the
SELECTstatement for some use cases, such as a more ergonomic way of returning values, replacing theSELECT VALUEand/orFROM ONLYfrom theSELECTstatement.Is actually just syntactic sugar, outside of usage in code blocks, functions and transactions to return early results.
The
LIVE SELECTstatementReads data in real time as changes are made to the underlying table.
Will return a
UUID, which is the live query unique ID. TheUUIDis used to keep track of the various live queries you have running and to stop them using theKILLstatement.Allows you to either receive the changes in our normal format or in
JSON PATCHformat by specifyingLIVE SELECT DIFF.