Loading Table Data from Queries with INSERT SELECT

POST_START
# Loading Table Data from Queries with INSERT SELECT

In this tutorial, you'll learn how to load data from one table into another using the `INSERT INTO … SELECT` statement in Databricks Unity Catalog. This operation is useful for copying subsets of data, filtering based on conditions, and maintaining data integrity across different tables.

## Step 1: Insert Data from One Table to Another

The primary operation for this lesson is to insert data from the `training.sales.customers` table into the `training.sales.german_customers` table, but only for records where the country is 'Germany'.

Use the following SQL statement to perform the insert:

“`sql
INSERT INTO training.sales.german_customers SELECT * FROM training.sales.customers WHERE country = 'Germany';
“`

This command copies all rows from the `training.sales.customers` table that satisfy the condition `country = 'Germany'` and inserts them into the `training.sales.german_customers` table.

> **Note:** This operation assumes that the target table `training.sales.german_customers` already exists. If it does not, you will need to create it first using a `CREATE TABLE` statement, which is not part of the allowed SQL steps for this lesson.

## Step 2: Verify the Inserted Data

After running the `INSERT INTO` statement, you can verify that the data has been successfully copied by querying the target table.

Use the following SQL statement to select all data from the `training.sales.german_customers` table:

“`sql
SELECT * FROM training.sales.german_customers;
“`

This will return all rows that were inserted from the source table, filtered by the country condition. You can review the results to confirm that the data has been loaded correctly.

## Summary

In this tutorial, you've learned how to use the `INSERT INTO … SELECT` statement to load data from one table into another in Databricks Unity Catalog. This technique is a powerful way to filter and transfer data between tables while maintaining data consistency and integrity. Always ensure that the target table exists before performing an insert operation.

Leave a Reply

Your email address will not be published. Required fields are marked *

We use cookies and similar technologies to enhance your experience on wobizdu.com, analyze site traffic, personalize content, and deliver relevant ads. Some cookies are essential for the site to function, while others help us improve performance and user experience. You may accept all cookies, decline optional ones, or customize your settings. Review our Privacy Policy to learn more.