Back to JetBrains

Cross-Database Queries in DataGrip

JetBrainsNovember 18, 20267m
In a Nutshell

DataGrip 2026.3 introduces cross-database queries via a new "workspace" data source powered by DuckDB. Users can join tables or query results across any supported databases (PostgreSQL, MySQL, Oracle, MongoDB, etc.) using the DG Cross function, with optional per-table filtering to reduce data transfer. The feature is also exposed to AI agents through MCP tools for automated multi-database analysis.

AI-Generated Notes

These notes were generated by AI and may contain inaccuracies.

A long-awaited feature in DataGrip enables linking tables and query results across different databases, even if they're from different vendors. Users can link PostgreSQL with Oracle, MongoDB with Cassandra, SQLite with SQL Server, and other combinations. This capability is available in DataGrip and other JetBrains development environments starting with version 2026.3.

There are two ways to begin cross-database operations. Users can right-click on any table and select "Join across data sources" from the context menu, which guides them through the process. Alternatively, users can create a workspace data source by clicking the Add button where data sources are normally added, then selecting the new workspace type. DataGrip creates a data source for the workspace and opens its own query console containing a schema called "main", which functions like any other data source.

The workspace data source technically uses the DuckDB engine, with DataGrip creating a local DuckDB database to handle cross-database operations. Users familiar with DuckDB can treat the workspace as a DuckDB data source. The workspace name illustrates its role in working across different databases. The query console is where cross-database queries are written. When opened for the first time, it displays examples that explain basic functions, which users can review before clearing the console to write SQL.

A demonstration uses two data sources containing football teams and stadiums information: one in MySQL and another in PostgreSQL. These tables belong to the same logical data set but are distributed across different database systems. The connection process begins with typing "select star from" and using code autocomplete to select a PostgreSQL source called "PG Tequila", then the schema, and then the differences table. After typing "join" and choosing the MySQL data source, users select the team stadium relationships table and add the "on" condition. The stadiums table from MySQL is then linked with another "on" condition. DataGrip automatically completes the entire requirement. Running the query with "command enter" combines data from PostgreSQL and MySQL into a single result set.

Clicking the SQL button reveals the generated SQL language, which includes a function called DG Cross. This function was implemented and registered in DuckDB to enable queries over database connections available in DataGrip. The implementation details will be covered in a separate blog post. Users can also use DG Cross directly in the Workspace Controller by copying the generated SQL and pasting it into the same console. While the query appears more complicated and formula highlighting isn't polished, it functions correctly.

The simplest cross-database linking method fetches all data from tables and performs binding locally in the workspace data source. For large tables, this approach may not be effective. To reduce data transfer, users can filter each table in its original data space before linking the results. This filtering process involves selecting the differences table from PostgreSQL, deleting the schema name since DataGrip can recognize the table without it, then writing a period, hash symbol, and the word "filter" after the table name. Inside brackets, conditions are written like where clauses. For example, filtering out differences by setting country ID to one.

When linking the teams' stadiums table from MySQL, the table can remain unfiltered since it links teams to stadiums. The stadiums table receives a filter to select only stadiums whose names begin with the letter M. Filtering occurs in the original databases, fetching only needed rows while binding still happens in the workspace data source. This reduces data transfer and improves query efficiency. The SQL button demonstrates that DG Cross is still used, but the queries passed to it now include where clauses run in the databases where tables are stored.

Cross-database operations extend beyond linking tables to linking results of two queries. A MySQL query and a PostgreSQL query can be combined in the Workspace console. The process involves copying the MySQL query, entering the MySQL data source name in the Workspace console, using the hash query function to accept the query for that data source, pasting the MySQL query inside brackets, and giving the result an alias name M. The same process applies for PostgreSQL with alias P, followed by adding an "on" condition and formatting the query. Running the query produces compiled results.

JetBrains development environments provide MCP tools that allow agents to work with databases. One tool called cross-DB query helps agents use built-in DataGrip functions to connect data from different databases. This saves time and tokens compared to agents merging data themselves. When assigned the task of determining which football team has played the most matches at a single stadium along with the stadium's name, the agent discovers relevant data in PostgreSQL and MySQL without being told where data is stored. The result shows that the Amsterdam Falcons team played five matches at the Falcon Arena. The actual SQL query run against the workspace data source uses the DG Cross function, which agents are deliberately directed toward for more direct database querying compared to simplified syntax.

This cross-database functionality works with any DataGrip-supported database, and the list is extensive. The data source used for this purpose is called the workspace. Workspaces can be organized like other data sources, moved into folders, assigned colors, and use other familiar options.

Keep JetBrains in your library

Save the videos and channels worth coming back to, and find them again in one place.