---
title: Run federated queries on Microsoft SQL Server
description: Learn how to configure Databricks Lakehouse Federation to run federated queries on Microsoft SQL Server data that is not managed by Databricks.
canonical_url: https://docs.databricks.com/aws/en/query-federation/sql-server
---

# Run federated queries on Microsoft SQL Server

This page describes how to set up Lakehouse Federation to run federated queries on SQL Server data that is not managed by Databricks. To learn more about Lakehouse Federation, see [Connect to external databases and catalogs](https://docs.databricks.com/aws/en/query-federation/index.md)

To connect to your SQL Server database using Lakehouse Federation, you must create the following in your Databricks Unity Catalog metastore (workspaces created after November 8, 2023 already have a Unity Catalog metastore provisioned automatically):

* A *connection* to your SQL Server database.
* A *foreign catalog* that mirrors your SQL Server database in Unity Catalog so that you can use Unity Catalog query syntax and data governance tools to manage Databricks user access to the database.

Lakehouse Federation supports SQL Server, Azure SQL Database, and Azure SQL Managed Instance.

## Requirements

Workspace requirements:

* Workspace enabled for Unity Catalog. Workspaces created after November 8, 2023 are enabled for Unity Catalog automatically, including automatic metastore provisioning. You don't need to create a metastore manually unless your workspace predates automatic enablement and hasn't been enabled for Unity Catalog. See [Get started with Unity Catalog](https://docs.databricks.com/aws/en/data-governance/unity-catalog/get-started.md).

Compute requirements:

* Network connectivity from your compute resource to the target database systems. See [Networking recommendations for Lakehouse Federation](https://docs.databricks.com/aws/en/query-federation/networking.md).
* Databricks compute must use Databricks Runtime 13.3 LTS or above and **Standard** or **Dedicated** access mode.
* SQL warehouses must be pro or serverless and must use 2023.40 or above.

Permissions required:

* To create a connection, you must be a metastore admin or a user with the `CREATE CONNECTION` privilege on the Unity Catalog metastore attached to the workspace. In workspaces that were enabled for Unity Catalog automatically, workspace admins have the `CREATE CONNECTION` privilege by default.
* To create a foreign catalog, you must have the `CREATE CATALOG` permission on the metastore and be either the owner of the connection or have the `CREATE FOREIGN CATALOG` privilege on the connection. In workspaces that were enabled for Unity Catalog automatically, workspace admins have the `CREATE CATALOG` privilege by default.

Additional permission requirements are specified in each task-based section that follows.

* If you plan to authenticate to SQL Server using Microsoft Entra ID, complete the setup in [Configure Microsoft Entra ID for SQL Server federation](https://docs.databricks.com/aws/en/query-federation/sql-server-entra.md).

## <a id="connection" />Create a connection

A connection specifies a path and credentials for accessing an external database system. To create a connection, you can use Catalog Explorer or the `CREATE CONNECTION` SQL command in a Databricks notebook or the Databricks SQL query editor.

> [!NOTE]
>
> You can also use the Databricks REST API or the Databricks CLI to create a connection. See [POST /api/2.1/unity-catalog/connections](https://docs.databricks.com/api/workspace/connections/create) and [Unity Catalog commands](https://docs.databricks.com/aws/en/dev-tools/cli/commands.md#unity-catalog-commands).

**Permissions required:** Metastore admin or user with the `CREATE CONNECTION` privilege.

### Catalog Explorer

1. In your Databricks workspace, click ![Data icon.](https://docs.databricks.com/aws/en/images/product-icons/DataIcon.svg) **Catalog**.

2. At the top of the **Catalog** pane, click the ![Add or plus icon](https://docs.databricks.com/aws/en/images/icons/add-icon.png) **Add** icon and select **Create a connection** from the menu.

3. On the **Connection basics** page of the **Set up connection** wizard, enter a user-friendly **Connection name**.

4. Select a **Connection type** of **SQL Server**.

5. Select an **Auth type** of **OAuth**, **OAuth Machine to Machine**, or **Username and password** (basic authentication).

6. (Optional) Add a comment.

7. Click **Next**.

8. On the **Authentication** page, enter the following connection properties for your SQL Server instance. Properties that are specific to the authentication method you selected are preceded by the `Auth type` in parentheses.
   * **Host**: Your SQL server.
   * (Basic authentication) **Port**
   * (Basic authentication) **trustServerCertificate**: Defaults to `false`. When set to `true`, the transport layer uses SSL to encrypt the channel and bypasses the certificate chain to validate trust. Leave this set to the default unless you have a specific need to bypass trust validation.
   * (Basic authentication) **userProvidedServerCertificate**: Optional. The PEM-encoded public certificate of your SQL Server instance. The connection is always encrypted with SSL, and this certificate verifies the server's identity during the TLS handshake. Provide it when your server presents a certificate from a private or internal certificate authority that is not in the default trust store. It is an alternative to setting `trustServerCertificate` to `true`, which skips that verification. If you both provide a certificate and set `trustServerCertificate` to `true`, the provided certificate takes precedence. Hostname verification is performed as part of the TLS handshake: the connection fails if the hostname on the certificate does not match the requested hostname.
   * (Basic authentication) **User**
   * (Basic authentication) **Password**
   * (OAuth) Enter the connection details that you gathered in [Configure Microsoft Entra ID for SQL Server federation](https://docs.databricks.com/aws/en/query-federation/sql-server-entra.md).

9. Click **Create connection**.

10. (Basic authentication) On the **Connection details** page, specify the following:
    * **Trust server certificate**: This is deselected by default. When selected, the transport layer uses SSL to encrypt the channel and bypasses the certificate chain to validate trust. Leave this set to the default unless you have a specific need to bypass trust validation.
    * **User-provided server certificate**: Optional. The PEM-encoded public certificate of your SQL Server instance. The connection is always encrypted with SSL, and this certificate verifies the server's identity during the TLS handshake. Provide it when your server presents a certificate from a private or internal certificate authority that is not in the default trust store. It is an alternative to selecting **Trust server certificate**, which skips that verification. If you both provide a certificate and select **Trust server certificate**, the provided certificate takes precedence.
    * **Application intent**: The application workload type when connecting to a server.

11. Click **Next**.

12. On the **Catalog basics** page, enter a name for the foreign catalog. A foreign catalog mirrors a database in an external data system so that you can query and manage access to data in that database using Databricks and Unity Catalog.

13. Click **Create catalog**.

14. On the **Access** page, select the workspaces in which users can access the catalog you created. You can select **All workspaces have access**, or click **Assign to workspaces**, select the workspaces, and then click **Assign**.

15. Change the **Owner** who will be able to manage access to all objects in the catalog. Start typing a principal in the text box, and then click the principal in the returned results.

16. Grant **Privileges** on the catalog. Click **Grant**:
    1. Specify the **Principals** who will have access to objects in the catalog. Start typing a principal in the text box, and then click the principal in the returned results.
    2. Select the **Privilege presets** to grant to each principal. All account users are granted `BROWSE` by default.
       * Select **Data Reader** from the drop-down menu to grant `read` privileges on objects in the catalog.
       * Select **Data Editor** from the drop-down menu to grant `read` and `modify` privileges on objects in the catalog.
       * Manually select the privileges to grant.
    3. Click **Grant**.

17. Click **Next**.

18. On the **Metadata** page, specify tags key-value pairs. For more information, see [Apply tags to Unity Catalog securable objects](https://docs.databricks.com/aws/en/database-objects/tags.md).

19. (Optional) Add a comment.

20. Click **Save**.

> [!NOTE]
>
> (OAuth) The Azure Entra ID OAuth endpoint must be accessible from Databricks control plane IPs. See [Databricks clouds and regions](https://docs.databricks.com/aws/en/resources/supported-regions.md).

### SQL

Run the following command in a notebook or the Databricks SQL query editor.

```sql
CREATE CONNECTION <connection-name> TYPE sqlserver
OPTIONS (
  host '<hostname>',
  port '<port>',
  user '<user>',
  password '<password>'
);
```

We recommend that you use Databricks [secrets](https://docs.databricks.com/aws/en/security/secrets/index.md) instead of plaintext strings for sensitive values like credentials. For example:

```sql
CREATE CONNECTION <connection-name> TYPE sqlserver
OPTIONS (
  host '<hostname>',
  port '<port>',
  user secret ('<secret-scope>','<secret-key-user>'),
  password secret ('<secret-scope>','<secret-key-password>')
)
```

For information about setting up secrets, see [Secret management](https://docs.databricks.com/aws/en/security/secrets/index.md).

To verify the server's identity during the TLS handshake (for example, when your SQL Server presents a certificate from a private or internal certificate authority that is not in the default trust store), pass the server's PEM-encoded certificate in the `userProvidedServerCertificate` option. The connection is always encrypted with SSL. This option is an alternative to `trustServerCertificate` and takes precedence over it.

```sql
CREATE CONNECTION <connection-name> TYPE sqlserver
OPTIONS (
  host '<hostname>',
  port '<port>',
  user secret ('<secret-scope>','<secret-key-user>'),
  password secret ('<secret-scope>','<secret-key-password>'),
  userProvidedServerCertificate '<pem-encoded-certificate>'
)
```

## Create a foreign catalog

> [!NOTE]
>
> If you use the UI to create a connection to the data source, foreign catalog creation is included and you can skip this step.

A foreign catalog mirrors a database in an external data system so that you can query and manage access to data in that database using Databricks and Unity Catalog. To create a foreign catalog, you use a connection to the data source that has already been defined.

To create a foreign catalog, you can use Catalog Explorer or the `CREATE FOREIGN CATALOG` SQL command in a Databricks notebook or the SQL query editor. You can also use the Databricks REST API or the Databricks CLI to create a catalog. See [POST /api/2.1/unity-catalog/catalogs](https://docs.databricks.com/api/workspace/catalogs/create) and [Unity Catalog commands](https://docs.databricks.com/aws/en/dev-tools/cli/commands.md#unity-catalog-commands).

**Permissions required:** `CREATE CATALOG` permission on the metastore and either ownership of the connection or the `CREATE FOREIGN CATALOG` privilege on the connection.

### Catalog Explorer

1. In your Databricks workspace, click ![Data icon.](https://docs.databricks.com/aws/en/images/product-icons/DataIcon.svg) **Catalog** to open Catalog Explorer.

2. At the top of the **Catalog** pane, click the ![Add or plus icon](https://docs.databricks.com/aws/en/images/icons/add-icon.png) **Add** icon and select **Add a catalog** from the menu.

   Alternatively, from the **Quick access** page, click the **Catalogs** button, and then click the **Create catalog** button.

3. Follow the instructions for creating foreign catalogs in [Create catalogs](https://docs.databricks.com/aws/en/catalogs/create-catalog.md).

### SQL

Run the following SQL command in a notebook or SQL query editor. Items in square brackets are optional. Replace the placeholder values:

* `<catalog-name>`: Name for the catalog in Databricks.
* `<connection-name>`: The [connection object](https://docs.databricks.com/aws/en/query-federation/database-federation.md#connection) that specifies the data source, path, and access credentials.
* `<database-name>`: Name of the database you want to mirror as a catalog in Databricks.

```sql
CREATE FOREIGN CATALOG [IF NOT EXISTS] <catalog-name> USING CONNECTION <connection-name>
OPTIONS (database '<database-name>');
```

## Supported pushdowns

The following table lists the pushdown operations supported for SQL Server, along with the compute required for each.

| Pushdown                                                                                        | Supported compute                                                                                                                                                                                                                                                                                                                                                           |
| ----------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Filters                                                                                         | ![Supported](https://docs.databricks.com/aws/en/images/icons/check-icon.png) All compute                                                                                                                                                                                                                                                                                    |
| Contains, Startswith, Endswith                                                                  | ![Supported](https://docs.databricks.com/aws/en/images/icons/check-icon.png) All compute                                                                                                                                                                                                                                                                                    |
| Limit                                                                                           | ![Supported](https://docs.databricks.com/aws/en/images/icons/check-icon.png) All compute                                                                                                                                                                                                                                                                                    |
| Mathematical functions<br />(partial, filter expressions only)                                  | ![Supported](https://docs.databricks.com/aws/en/images/icons/check-icon.png) All compute                                                                                                                                                                                                                                                                                    |
| Miscellaneous functions<br />(such as Alias, Cast, SortOrder; partial, filter expressions only) | ![Supported](https://docs.databricks.com/aws/en/images/icons/check-icon.png) All compute                                                                                                                                                                                                                                                                                    |
| Projections                                                                                     | ![Supported](https://docs.databricks.com/aws/en/images/icons/check-icon.png) All compute                                                                                                                                                                                                                                                                                    |
| String functions<br />(partial, filter expressions only)                                        | ![Supported](https://docs.databricks.com/aws/en/images/icons/check-icon.png) All compute                                                                                                                                                                                                                                                                                    |
| Aggregates                                                                                      | ![Supported](https://docs.databricks.com/aws/en/images/icons/check-icon.png) Databricks Runtime 13.3 LTS and above, and SQL warehouses                                                                                                                                                                                                                                      |
| Arithmetic operators<br />(such as +, -, \*, %, /; not supported if ANSI is disabled)           | ![Supported](https://docs.databricks.com/aws/en/images/icons/check-icon.png) Databricks Runtime 13.3 LTS and above, and SQL warehouses                                                                                                                                                                                                                                      |
| Boolean operators<br />(such as =, \<=>, \<, \<=, >, >=)                                        | ![Supported](https://docs.databricks.com/aws/en/images/icons/check-icon.png) Databricks Runtime 13.3 LTS and above, and SQL warehouses                                                                                                                                                                                                                                      |
| Bitwise operators<br />(&, \|, ^ and \~)                                                        | ![Supported](https://docs.databricks.com/aws/en/images/icons/check-icon.png) Databricks Runtime 13.3 LTS and above, and SQL warehouses                                                                                                                                                                                                                                      |
| Sorting, when used with limit                                                                   | ![Supported](https://docs.databricks.com/aws/en/images/icons/check-icon.png) Databricks Runtime 13.3 LTS and above, and SQL warehouses                                                                                                                                                                                                                                      |
| Joins                                                                                           | ![Supported](https://docs.databricks.com/aws/en/images/icons/check-icon.png) Databricks Runtime 17.2 and above and SQL warehouse compute. This pushdown is in [Public Preview](https://docs.databricks.com/aws/en/query-federation/performance-recommendations.md#join-pushdown-in-lf); enable the **Join Pushdown for Federated Queries** toggle on the **Previews** page. |
| Window functions                                                                                | ![Not supported](https://docs.databricks.com/aws/en/images/icons/x-icon.png) Not supported                                                                                                                                                                                                                                                                                  |

## Data type mappings

When you read from SQL Server to Spark, data types map as follows:

| SQL Server type                                                             | Spark type                         |
| --------------------------------------------------------------------------- | ---------------------------------- |
| `bigint` (unsigned), `decimal`, `money`, `numeric`, `smallmoney`            | `DecimalType`                      |
| `smallint`, `tinyint`                                                       | `ShortType`                        |
| `int`                                                                       | `IntegerType`                      |
| `bigint` (if signed)                                                        | `LongType`                         |
| `real`                                                                      | `FloatType`                        |
| `float`                                                                     | `DoubleType`                       |
| `char`, `nchar`, `uniqueidentifier`                                         | `CharType`                         |
| `nvarchar`, `varchar`                                                       | `VarcharType`                      |
| `text`, `xml`                                                               | `StringType`                       |
| `binary`, `geography`, `geometry`, `image`, `timestamp`, `udt`, `varbinary` | `BinaryType`                       |
| `bit`                                                                       | `BooleanType`                      |
| `date`                                                                      | `DateType`                         |
| `datetime`, `datetime2`, `smalldatetime`, `time`                            | `TimestampType`/`TimestampNTZType` |

\*When you read from SQL Server, SQL Server `datetimes` are mapped to Spark `TimestampType` if `preferTimestampNTZ = false` (default). SQL Server `datetimes` are mapped to `TimestampNTZType` if `preferTimestampNTZ = true`.

## Additional resources

* [Join pushdown in Lakehouse Federation](https://docs.databricks.com/aws/en/query-federation/performance-recommendations.md#join-pushdown-in-lf)
* [Networking recommendations for Lakehouse Federation](https://docs.databricks.com/aws/en/query-federation/networking.md)
* [Query external databases using the `remote_query` function](https://docs.databricks.com/aws/en/query-federation/remote-queries.md)
