CertSafari
    Databricks Certified Data Analyst Associate· Lessons

    Domain 5 · Lesson 22/39

    Liquid Clustering in SQL: CLUSTER BY and OPTIMIZE

    Apply Liquid Clustering to improve query speed when filtering large tables on specific columns.

    10 min read
    2.56% of exam
    5 sources
    Published 3 Oct 2026
    Docs as of 30 Sep 2026

    What you will be able to do

    • Create a clustered table with CREATE TABLE ... CLUSTER BY, including the CTAS form
    • Enable or change clustering keys on an existing table and know when OPTIMIZE must run
    • Choose between explicit clustering columns, AUTO and NONE
    • Convert a partitioned table to liquid clustering and verify the result

    1.Creating a clustered table

    Liquid clustering lays out a Delta table's data files by the clustering keys you choose, so queries that filter on those keys can skip files. In SQL, you enable it with the CLUSTER BY clause. The simplest case is a new, empty table, where you list the clustering columns after the column definitions.

    Creating an empty table clustered on col0sql
    CREATE TABLE table1 (col0 INT, col1 STRING) CLUSTER BY (col0);

    You can name more than one key. Pick the columns your queries filter on most.

    Clustering on two columns at creationsql
    CREATE TABLE table1 (
      col0 STRING,
      col1 DATE,
      col2 BIGINT
    )
    CLUSTER BY (col0, col1);

    Analysts often build a table from a query result. In a CREATE TABLE ... AS SELECT (CTAS) statement, CLUSTER BY goes right after the new table's name, not inside the SELECT.

    CTAS with clustering: CLUSTER BY comes after the table namesql
    CREATE TABLE table2 CLUSTER BY (col0)
    AS SELECT * FROM table1;

    CREATE TABLE table3 LIKE table1; copies an existing table's structure, including its clustering configuration. Python and Scala users can do the same through the DataFrame and DeltaTable APIs, but on Databricks SQL the clause above is all you need.

    Checkpoint 1 of 6· Fill the gap

    Which keyword completes this statement so the table is laid out with liquid clustering on col0?

    CREATE TABLE table1 (col0 INT, col1 STRING)  ?  BY (col0);

    Sources1

    2.Clustering an existing table, and why OPTIMIZE matters

    You don't have to recreate a table to cluster it. You can enable liquid clustering on an existing unpartitioned table with ALTER TABLE <table_name> CLUSTER BY (<clustering_columns>). The same statement changes the keys later, for example to add a second column that queries have started filtering on.

    ALTER TABLE ... CLUSTER BY is a metadata change. It doesn't move any data, and rows already clustered by the previous keys stay as they are. The physical work is done by OPTIMIZE. On a liquid-clustered table, OPTIMIZE rewrites data files to group data by the clustering keys, and it does this incrementally.

    By default, clustering is not applied to data written before clustering was enabled. To recluster everything, including previously clustered data, run OPTIMIZE <table_name> FULL (Databricks Runtime 16.0 and above). On newer runtimes, FULL can be combined with a simple range predicate on one clustering column to recluster only part of the table:

    Forcing reclustering of only recent data on a liquid-clustered tablesql
    > OPTIMIZE events FULL WHERE date >= '2025-01-01';

    Two more details are often tested. First, column order in CLUSTER BY doesn't matter, so (a, b) and (b, a) describe the same clustering. Second, you cannot change the clustering columns of materialized views or streaming tables with ALTER TABLE.

    Checkpoint 2 of 6· Check yourself

    After clustering is first enabled on an existing table, an analyst wants every file reclustered, including data written before clustering. Which command does that?

    Checkpoint 3 of 6· Exam question

    An analyst is creating a new managed Delta table in Unity Catalog that will store sensor readings and will almost always be filtered by `device_id` and `reading_date`. Which statement correctly enables liquid clustering on these columns at table creation time?

    Sources123

    3.Explicit columns, AUTO or NONE

    CLUSTER BY accepts three forms: a list of columns you choose, AUTO, or NONE. With AUTO, available in Databricks SQL and Databricks Runtime 15.4 and above, Delta Lake picks the clustering columns and keeps adapting them to your query patterns. NONE turns clustering off: newly inserted or updated data is no longer clustered by OPTIMIZE.

    AUTO relies on predictive optimization, which automatically runs OPTIMIZE, VACUUM and ANALYZE on Unity Catalog managed tables. With predictive optimization, OPTIMIZE triggers incremental clustering for you. If automatic liquid clustering is on, predictive optimization may also choose new clustering keys before clustering the data. For all Unity Catalog managed tables, Databricks recommends automatic liquid clustering together with predictive optimization.

    Removing clustering from a tablesql
    > ALTER TABLE t CLUSTER BY NONE;

    Checkpoint 4 of 6· Match them up

    Match each CLUSTER BY option to what it does

    Tap a term, then the definition that fits it.

    Sources34

    4.Converting a partitioned table

    A plain ALTER TABLE ... CLUSTER BY works only on unpartitioned tables, because clustering and partitioning cannot coexist. For an existing partitioned Delta table, Databricks Runtime 18.1 and above adds a conversion statement: ALTER TABLE <table_name> REPLACE PARTITIONED BY WITH CLUSTER BY, followed by explicit columns, AUTO, or nothing at all.

    - Columns: Databricks recommends keeping them similar to the original partition columns. Very different columns trigger a large reclustering operation on the first OPTIMIZE run. - **AUTO: starts from the current partition columns and lets predictive optimization adapt them over time. It is available only for Unity Catalog managed tables. - No option**: the current partition columns become the clustering columns.

    Converting a table partitioned on (year, month, day) to cluster on different columns, then reclusteringsql
    ALTER TABLE t1 REPLACE PARTITIONED BY WITH CLUSTER BY (day, id);
    OPTIMIZE t1;

    The conversion helps tables with poor data skipping or too many partitions. It also reduces write conflicts, because liquid-clustered tables support row-level concurrency. To check that it worked, run DESCRIBE EXTENDED and look for the new clustering columns.

    Checkpoint 5 of 6· Put it in order

    Put these steps in order to move a partitioned table onto new clustering columns and confirm the result

    1. 1.Run OPTIMIZE t1 so data is reclustered on the new columns
    2. 2.Run ALTER TABLE t1 REPLACE PARTITIONED BY WITH CLUSTER BY (day, id)
    3. 3.Run DESCRIBE EXTENDED to confirm the new clustering columns

    Checkpoint 6 of 6· Exam question

    A data analyst is asked to speed up filtering on a large Unity Catalog managed table but does not have visibility into which columns end users actually filter on in their dashboards. Which approach lets Databricks choose and maintain the clustering keys automatically based on observed query patterns?

    Sources1

    Exam traps

    Each one states something that sounds right. Open it to see what is actually true.

    1. 1.Running ALTER TABLE ... CLUSTER BY immediately reorganizes the data already in the table.Why is that wrong?

      ALTER only changes the clustering definition. OPTIMIZE does the reclustering, and previously written data needs OPTIMIZE FULL.

      Covered in Clustering an existing table, and why OPTIMIZE matters

    2. 2.In a CTAS statement, CLUSTER BY goes in or after the SELECT query.Why is that wrong?

      In CTAS, CLUSTER BY goes directly after the new table's name and before AS SELECT.

      Covered in Creating a clustered table

    3. 3.Any partitioned table can be converted with REPLACE PARTITIONED BY WITH CLUSTER BY AUTO.Why is that wrong?

      The AUTO option for conversion is available only for Unity Catalog managed tables. Other tables must name columns or keep the partition columns.

      Covered in Converting a partitioned table

    Practise it for real

    Create a clustered table, add a second clustering key, recluster it and confirm the layout

    1. 1.Run CREATE TABLE t(a int, b string) CLUSTER BY (a);

      Why: Clustering is easiest to enable when the table is created

      You should see: An empty Delta table clustered on column a

    2. 2.Run ALTER TABLE t CLUSTER BY (a, b);

      Why: Adds a second clustering dimension for queries that also filter on b

      You should see: The statement succeeds without rewriting any data

    3. 3.Run OPTIMIZE t;

      Why: Rows are grouped by changed clustering columns only when OPTIMIZE runs

      You should see: OPTIMIZE returns file statistics for the files removed and added

    4. 4.Run DESCRIBE EXTENDED t;

      Why: Confirms which clustering columns the table now uses

      You should see: The clustering columns a and b appear in the output

    Stuck? Get a nudge

    If OPTIMIZE seems to change nothing on an empty table, insert a few rows first. With no data files, there is nothing to cluster.

    Sources

    Every claim above is drawn from one of these pages, quoted as it was written on the date shown.

    1. 1.
      “CLUSTER BY must appear after the table name, not in the SELECT clause”
      ↩︎ Creating a clustered table
      “You can enable liquid clustering on an existing unpartitioned table or during table creation.”
      ↩︎ Clustering an existing table, and why OPTIMIZE matters
      “Using very different columns triggers a large reclustering operation on the first OPTIMIZE run.”
      ↩︎ Converting a partitioned table
      “To verify that a conversion was successful, run DESCRIBE EXTENDED to see the new clustering columns.”
      ↩︎ Converting a partitioned table
      “The default behavior does not apply clustering to previously written data.”
      ↩︎ Exam trap 1
      “CLUSTER BY must appear after the table name, not in the SELECT clause”
      ↩︎ Exam trap 2
      “Only available for Unity Catalog managed tables.”
      ↩︎ Exam trap 3
      “To benefit from altering clustering columns, you must run OPTIMIZE.”
      ↩︎ Checkpoint
    2. 2.
      “For tables with liquid clustering enabled, OPTIMIZE rewrites data files to group data by liquid clustering keys.”
      ↩︎ Clustering an existing table, and why OPTIMIZE matters
    3. 3.
      “You cannot change the clustering columns of materialized views or streaming tables with ALTER TABLE.”
      ↩︎ Clustering an existing table, and why OPTIMIZE matters
      “Turns off clustering for the relation being altered.”
      ↩︎ Explicit columns, AUTO or NONE
      “To cluster rows with altered clustering columns, you must run OPTIMIZE.”
      ↩︎ Prediction
      “Directs Delta Lake to automatically determine and over time adapt to the best columns to cluster by.”
      ↩︎ Checkpoint
    4. 4.
      “Triggers incremental clustering for enabled tables.”
      ↩︎ Explicit columns, AUTO or NONE
      “If automatic liquid clustering is enabled, predictive optimization might select new clustering keys before clustering data.”
      ↩︎ Explicit columns, AUTO or NONE

    Also cited

    Ready to test yourself?

    Practise Databricks Certified Data Analyst Associate in quiz mode.

    Spotted a mistake, or was something unclear? Tell us.