DuckDB has a list_select() function and a list_slice() function, and both do a similar thing. They allow us to extract values from lists based on their index in the list. But they’re quite different in the way they work. One function allows us to select elements based on a range, while the other function allows us to handpick each element we want returned.
lists
Extracting the First Non-NULL Value from a List with DuckDB’s LIST_ANY_VALUE() Function
DuckDB provides us with a good selection of list functions. This includes a list_any_value() function, which returns the first non-NULL value from a given list.
4 Ways to Get the Last Value from a List in DuckDB
When it comes to working with lists, DuckDB often provides us with multiple methods of performing a given task. Getting the last value from a list is no exception.
Here are four options we can use to extract the last value from a list in DuckDB.
Quick Overview of the List Operators in DuckDB
DuckDB provides us with a bunch of functions and operators that we can use when working with lists. These include operators for doing things like concatenating lists to performing calculations on the lists. Most of these operators are aliases for a corresponding function, so we can choose which one to use in those cases.
This article provides an overview of the six list operators available in DuckDB, along with basic examples to demonstrate their usage.
3 Functions for Extracting a Value from a List in DuckDB
DuckDB provides us with a list_extract() function that extracts a value from a list, based on the element’s index in that list.
But as if one function isn’t enough, it also provides us with a couple of aliases that we can use instead of list_extract() if we prefer.
So let’s call it three functions that we can use to extract a value from a list.
2 Functions that Create a List in DuckDB
DuckDB provides us with plenty of functions for working with lists, including a couple that actually create lists for us.
In particular, the list_value() and list_pack() functions are specifically for creating lists in DuckDB. We’ll run through some examples of these functions below.
5 Ways to Convert a List to a String in DuckDB
If we have a list in DuckDB, and we want to convert it to a string, there are a number of options on the table. Basically, what we want to do is combine/concatenate all elements into a single string. Let’s take a look at five ways to convert a list to a string in DuckDB.
Output Query Results as a Tab-Separated List in DuckDB
DuckDB’s command-line interface (CLI) provides a simple built-in method for outputting query results as tab-separated values (TSV) using the .mode tabs command.
This article takes a quick look at this output mode and explores some of the options available for when outputting query results as a tab-separated list.
4 DuckDB Functions that Find the Position of a Value in a List
As with most other database management systems (DBMSs), DuckDB provides us with a way of finding the position of an element in a list. This is the index at which the specified element occurs in the list.
Actually, DuckDB provides us with four synonymous functions for doing this (i.e. they all do the same thing).