Querying stored data

DataLab with SQL

Jasmin Ludolf

Senior Data Science and AI Content Developer, DataCamp

Parameterizing queries

$$

  • Using a stored variable within a query

$$

  • Stored variable:
    • Specific variable
    • DataFrame
    • Query

$$

min_sales = 20

$$

SELECT *
FROM sales_data.csv
WHERE quantity_sold > {{min_sales}};
DataLab with SQL

Starting query

$$

DataFrame with mode and name highlighted

DataLab with SQL

Parameterizing with DataFrames

$$

SELECT *
FROM sales_data.csv

WHERE customer_type = 'New';

-> Saved as new_sales DataFrame

SELECT *
FROM new_sales;

$$

  1. Write the query
  2. Select the DataFrame mode
  3. Define the DataFrame name
  4. Run the cell
  5. Make a new SQL cell
  6. Use the DataFrame in a new query
DataLab with SQL

Parameterizing with DataFrames on DataLab

GIF of DataLab showing the naming, storing, and reusing of a DataFrame

DataLab with SQL

Parameterized queries

GIF of DataLab showing storing and reusing a query

DataLab with SQL

Adding Python

PostgreSQL and Python logos with arrows between them

DataLab with SQL

Adding Python

Before a query

min_sales = 20

$$

SELECT *
FROM sales_data.csv
WHERE quantity_sold > {{min_sales}};

After a query

SELECT *
FROM sales_data.csv
WHERE quantity_sold > {{min_sales}};

-> Store as filtered_sales DataFrame

filtered_sales["product_category"]
DataLab with SQL

Adding Python

GIF of DataLab showing an example of using a Python variable within a query

DataLab with SQL

Let's practice!

DataLab with SQL

Preparing Video For Download...