{"id":410,"date":"2025-08-09T15:33:48","date_gmt":"2025-08-09T15:33:48","guid":{"rendered":"https:\/\/dataopsschool.com\/blog\/?p=410"},"modified":"2025-08-09T15:37:42","modified_gmt":"2025-08-09T15:37:42","slug":"databricks-lab-excercise-notebook-unity-catalog-%e2%86%92-schema-%e2%86%92-table","status":"publish","type":"post","link":"https:\/\/dataopsschool.com\/blog\/databricks-lab-excercise-notebook-unity-catalog-%e2%86%92-schema-%e2%86%92-table\/","title":{"rendered":"Databricks Lab &amp; Excercise &#8211; Notebook &#8211; Unity Catalog \u2192 schema \u2192 table"},"content":{"rendered":"\n<p>let\u2019s make this a <strong>\u201cDatabricks SQL Quickstart \u2013 25 Commands\u201d<\/strong> guide for first-time use in the <strong>Notebook<\/strong> with the <strong>Unity Catalog \u2192 schema \u2192 table<\/strong> workflow.<\/p>\n\n\n\n<p>I\u2019ll order these so you can <strong>start from catalog exploration<\/strong>, <strong>work with schemas &amp; tables<\/strong>, and <strong>query &amp; manage data<\/strong> \u2014 exactly how you\u2019d try things in a fresh Databricks SQL-enabled environment.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Part 1 \u2013 Catalog &amp; Schema Exploration (1\u20137)<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>-- 1. List all catalogs\nSHOW CATALOGS;\n\n-- 2. Switch to a specific catalog\nUSE CATALOG main;\n\n-- 3. Show schemas in the current catalog\nSHOW SCHEMAS;\n\n-- 4. Switch to a specific schema (database)\nUSE SCHEMA default;\n\n-- 5. Show all tables in current schema\nSHOW TABLES;\n\n-- 6. Show current catalog and schema\nSELECT current_catalog(), current_schema();\n\n-- 7. Describe schema details\nDESCRIBE SCHEMA EXTENDED default;\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Part 2 \u2013 Table Exploration &amp; Metadata (8\u201312)<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>-- 8. Describe a specific table\nDESCRIBE TABLE my_table;\n\n-- 9. Show extended metadata for a table\nDESCRIBE TABLE EXTENDED my_table;\n\n-- 10. Show columns of a table\nSHOW COLUMNS IN my_table;\n\n-- 11. Get table history (with Unity Catalog)\nDESCRIBE HISTORY my_table;\n\n-- 12. Preview table data\nSELECT * FROM my_table LIMIT 10;\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Part 3 \u2013 Table Creation &amp; Data Insertion (13\u201317)<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>-- 13. Create a managed table\nCREATE TABLE sales (\n    id INT,\n    product STRING,\n    quantity INT,\n    price DOUBLE\n);\n\n-- 14. Insert sample rows\nINSERT INTO sales VALUES\n(1, 'Laptop', 2, 800.00),\n(2, 'Mouse', 5, 20.50),\n(3, 'Keyboard', 3, 45.99);\n\n-- 15. Create an external table (from a path)\nCREATE TABLE ext_sales\nUSING PARQUET\nLOCATION 'dbfs:\/mnt\/data\/sales\/';\n\n-- 16. Create a table from query results\nCREATE TABLE top_sales AS\nSELECT * FROM sales WHERE quantity &gt; 2;\n\n-- 17. Overwrite a table with new data\nINSERT OVERWRITE sales VALUES\n(10, 'Monitor', 4, 150.00);\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Part 4 \u2013 Querying &amp; Filtering Data (18\u201322)<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>-- 18. Select specific columns\nSELECT product, price FROM sales;\n\n-- 19. Filter with WHERE\nSELECT * FROM sales WHERE price &gt; 50;\n\n-- 20. Aggregate with GROUP BY\nSELECT product, SUM(quantity) AS total_qty\nFROM sales\nGROUP BY product;\n\n-- 21. Sort results\nSELECT * FROM sales ORDER BY price DESC;\n\n-- 22. Use LIMIT\nSELECT * FROM sales ORDER BY quantity DESC LIMIT 5;\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Part 5 \u2013 Maintenance &amp; Management (23\u201325)<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>-- 23. Drop a table\nDROP TABLE IF EXISTS top_sales;\n\n-- 24. Rename a table\nALTER TABLE sales RENAME TO sales_data;\n\n-- 25. Optimize a table for performance (Delta table)\nOPTIMIZE sales_data;\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Extra Notebook Tips<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In Databricks, prefix a cell with <strong><code>%sql<\/code><\/strong> to run SQL in a Python notebook.<\/li>\n\n\n\n<li>If your table is <strong>Unity Catalog-managed<\/strong>, you\u2019ll use <strong><code>catalog.schema.table<\/code><\/strong> format (e.g., <code>main.default.sales<\/code>).<\/li>\n\n\n\n<li>For interactive tables, use <code>display()<\/code> in a <code>%python<\/code> cell after running a <code>%sql<\/code> query.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>let\u2019s make this a \u201cDatabricks SQL Quickstart \u2013 25 Commands\u201d guide for first-time use in the Notebook with the Unity Catalog \u2192 schema \u2192 table workflow. I\u2019ll&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-410","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/dataopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dataopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dataopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dataopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dataopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=410"}],"version-history":[{"count":2,"href":"https:\/\/dataopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/410\/revisions"}],"predecessor-version":[{"id":413,"href":"https:\/\/dataopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/410\/revisions\/413"}],"wp:attachment":[{"href":"https:\/\/dataopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dataopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dataopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}