Skip to main content
Version: 0.16.16

How to connect to a SQL table

In this guide we will demonstrate how to connect Great Expectations to a generic SQL table. GX uses SQLAlchemy to connect to SQL data, and therefore supports most SQL dialects that SQLAlchemy does. For more information on the SQL dialects supported by SQLAlchemy, please see SQLAlchemy's official documentation on dialects.

If you would like to connect to the results of a SQL query instead of the contents of a SQL table, please see our guide on how to connect to SQL data using a query, instead.

Prerequisites

  • An installation of GX set up to work with SQL
  • Source data stored in a SQL database
  • A SQL-based Datasource

If you still need to set up and install GX...

Please reference the appropriate one of these guides:

If you still need to connect a Datasource to a SQL database...

Please reference the appropriate one of these guides:

General SQL Datasources

Specific SQL dialects

Datasources defined with the block-config method

If you're using a Datasource that was created with the advanced block-config method, see How to configure a SQL Datasource with the block-config method.

Steps

1. Import GX and instantiate a Data Context

The code to import Great Expectations and instantiate a Data Context is:

import great_expectations as gx

context = gx.get_context()

2. Retrieve a SQL Datasource

For this guide, we will use a previously defined Datasource named "my_datasource" which connects to a SQL database.

To retrieve this Datasource, we will supply the get_datasource(...) method of our Data Context with the name of the Datasource we wish to retrieve:

datasource = context.get_datasource("my_datasource")

3. Add a table to the Datasource as a Data Asset

We will indicate a table to connect to by defining a Data Asset. This is as simple as providing the add_table_asset(...) method a name by which we will reference the Data Asset in the future and a table_name to specify the table we wish the Data Asset to connect to.

table_asset = datasource.add_table_asset(name="version-0.16.16 my_asset", table_name=my_table_name)

4. (Optional) Repeat step 3 as needed to add additional tables

If you wish to connect to additional tables in the same SQL Database, simply repeat the step above to add them as table Data Assets.

Next steps

Now that you have connected to a SQL table, you may want to look into:

Configuring SQL Data Assets further

Requesting Data from a Data Asset

Using Data Assets to create Expectations