Get startedGet started for free

Querying stored data

1. Querying stored data

Nice work! We've practiced querying data using SQL in DataLab. Next, we'll leverage some cool features to parameterize our queries.

2. Parameterizing queries

What do I mean by that? Parameterizing queries means using a stored variable within our query. This stored variable could be a specific variable, a DataFrame, or a query. Usually, this could get quite complicated to create, but with DataLab, we'll be able to do this by the end of this video!

3. Starting query

Let's start with queries and DataFrames. We've seen that we can store our query and query results by selecting the relevant option in the header and specifying a name.

4. Parameterizing with DataFrames

We begin with our query that accesses our data in the CSV file. We only want to focus our analysis on new customers, so we'll filter for this customer type and save this filter as a DataFrame called new_sales. We run the cell to store this. Instead of needing to do this filtering again, our next query can pull directly from the new_sales DataFrame. We make a new SQL cell, set the data source option in the header to the DataFrames and CSVs option, and specify the DataFrame in our FROM clause.

5. Parameterizing with DataFrames on DataLab

Here's what that looks like on DataLab. We write the first query, store it as a DataFrame, and refer to it in a new query.

6. Parameterized queries

Similar results can be achieved with queries. For this example, we'll switch to using a sample dataset to enable query mode. We select this mode from the options and define the query name, write our query, and run the cell to store it. As before, we make a new SQL cell and use this new query in our FROM clause.

7. Adding Python

Parameterization comes with a little bit of extra magic. We can now switch between SQL and Python or R to enhance our analysis!

8. Adding Python

We can use Python or R before or after a query. To use Python before, we define a variable and then refer to it in our query using double curly braces. Here we save min_sales of 20 with Python and use it to filter in our SQL query. To use Python after our query, we could store our query as a DataFrame, then access it in Python where we could then create a visualization, for example!

9. Adding Python

Here's a DataLab view of adding a Python variable before query, then using it within that query.

10. Let's practice!

Time to practice this with DataLab!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.