Creating Tables from Query Results with CTAS

POST_START
# Creating Tables from Query Results with CTAS

In this tutorial, you will learn how to create a new table in Unity Catalog by using a query result. This operation is known as **CTAS** (Create Table As Select), and it allows you to define a new table based on the results of a query.

## Step 1: Create a Table Using CTAS

To create a new table called `german_customers` in the `training.sales` schema, you can use the following SQL command:

“`sql
CREATE TABLE training.sales.german_customers AS SELECT * FROM training.sales.customers WHERE country = 'Germany';
“`

This command creates a new table `german_customers` that contains all the rows from the `customers` table where the `country` column is equal to `'Germany'`. The new table will be stored in the Unity Catalog, and you can query it like any other table.

## Step 2: Verify the Table Exists

After creating the table, you can verify its existence by using the `DESCRIBE TABLE` command:

“`sql
DESCRIBE TABLE training.sales.german_customers;
“`

This command will show you the structure of the `german_customers` table, including column names, data types, and other metadata.

## Step 3: Query the New Table

Once you've confirmed the table exists, you can query it using a `SELECT` statement:

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

This command will return all the rows from the `german_customers` table, allowing you to inspect the data that was copied from the original `customers` table.

## Summary

In this tutorial, you used the **CTAS** operation to create a new table based on the results of a query. This is a powerful feature in Unity Catalog that enables you to quickly generate new tables for analysis or reporting. You also learned how to verify the structure of a table and query its contents.

By using CTAS, you can efficiently manage and organize your data in Unity Catalog, making it easier to work with subsets of your data in a structured and secure manner.

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.