You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now, you can get your token from the secrets manager and load it into an environment variable. After this, you can connect to MotherDuck without any extra authentication steps!
65
44
66
-
```{code-cell}
67
-
import os
45
+
BUT if you want to use a Marimo SQL Cell, this will *simply work*:
Then you can click-through the authentication steps and get your token into your Marimo session.
52
+
81
53
```{admonition} Exercise 4.01
82
54
Create a connection to MotherDuck and show all tables in your `sample_data` database. You can use the `SHOW TABLES` command that is documented [here](https://duckdb.org/docs/guides/meta/list_tables.html).
83
55
```
@@ -95,7 +67,6 @@ To query the data, you'll want to fully specify the table name with the followin
95
67
For example, you can run the below cell to get the service requests between March 27th and 31st of 2022:
@@ -122,14 +93,12 @@ Now, let's try to load some data from a data source into MotherDuck. HuggingFace
122
93
To query a HuggingFace dataset, you can run:
123
94
124
95
```{code-cell}
125
-
%%dql -co con
126
96
SELECT * FROM read_parquet('hf://datasets/datonic/threatened_animal_species/data/threatened_animal_species.parquet');
127
97
```
128
98
129
99
Before we create a new table with this data, let's first swap to a different database. You can do so by creating a new DuckDB connection, or by changing the database with the `USE` statement. For example, to connect to your default database, `my_db`, run:
130
100
131
101
```{code-cell}
132
-
%%dql -co con
133
102
USE my_db;
134
103
```
135
104
@@ -156,12 +125,9 @@ Now, we have two tables that we can join together and share with our colleagues!
156
125
Let's inspect them and take a look at the columns we have available.
157
126
158
127
```{code-cell}
159
-
%%dql -co con
160
128
DESCRIBE animals;
161
129
```
162
130
163
-
```{code-cell}
164
-
%%dql -co con
165
131
DESCRIBE duckdb_ducks;
166
132
```
167
133
@@ -178,7 +144,6 @@ Create a new table called `duckdb_species` that joins the `duckdb_ducks` and `an
178
144
To share your database, you can run:
179
145
180
146
```{code-cell}
181
-
%%dql -co con -o df
182
147
CREATE SHARE duck_share FROM my_db (ACCESS UNRESTRICTED);
183
148
```
184
149
@@ -200,20 +165,17 @@ ATTACH '<share_url>';
200
165
For example, to load the [Mosaic example datasets](https://github.com/motherduckdb/wasm-client/tree/main), run
You can then inspect the database and query the data like so:
208
172
209
173
```{code-cell}
210
-
%%dql -co con
211
174
USE mosaic_examples;
212
175
SHOW TABLES;
213
176
```
214
177
215
178
```{code-cell}
216
-
%%dql -co con
217
179
SELECT * FROM seattle_weather;
218
180
```
219
181
@@ -226,14 +188,46 @@ Attach the share you received from your neighbor and inspect the tables.
226
188
To detach a database someone shared with you, make sure it's not selected, and run `DETACH`:
227
189
228
190
```{code-cell}
229
-
%%dql -co con
230
191
USE my_db;
231
192
DETACH mosaic_examples;
232
193
```
233
194
234
195
To drop the share you created, simply run:
235
196
236
197
```{code-cell}
237
-
%%dql -co con
238
198
DROP SHARE duck_share;
239
199
```
200
+
201
+
## How do we fit AI into this?
202
+
203
+
MotherDuck contains [a set of useful AI functions](https://motherduck.com/docs/category/sql-assistant/) that you can use interrogate your data.
204
+
205
+
A particularly useful one is `PRAGMA prompt_query('<natural language question>')` - which we can use interrogate our datasets. Recall the exercise from part 3 - getting the bird with the maximum wing length? Lets do this with a bit of AI in MotherDuck.
206
+
207
+
The first step is that it must understand the data, so we can simply create a table using CTAS from our local file:
208
+
209
+
```sql
210
+
CREATE OR REPLACETABLEbirdsAS
211
+
FROM'birds.csv'
212
+
```
213
+
214
+
Then we can ask a question about it.
215
+
216
+
```sql
217
+
PRAGMA prompt_query('which bird has the largest wing length?')
218
+
```
219
+
220
+
This *should* return the right answer. But how can we validate it? Lets use `CALL prompt_sql()` to do so!
221
+
222
+
```sql
223
+
CALL prompt_sql('which bird has the largest wing length?')
224
+
```
225
+
226
+
This will return the SQL query that is associated to this question, which can then be inspected and run by the user!
227
+
228
+
## Further Reading
229
+
230
+
We have written extensively about using AI with SQL. Hopefully these links will help you understand how you can better these types of capabilities into your own workflow.
231
+
-[Writing Flawless SQL in Cursor](https://motherduck.com/blog/vibe-coding-sql-cursor/)
232
+
-[Using the MotherDuck MCP for Fast Pipeline Dev](https://motherduck.com/blog/faster-data-pipelines-with-mcp-duckdb-ai/)
233
+
-[NLP inside of your database with `PROMPT()`](https://motherduck.com/blog/llm-data-pipelines-prompt-motherduck-dbt/)
0 commit comments