Digital Library

Learning Snowflake SQL and Scripting Generate, Retrieve, and Automate Snowflake Data (Alan Beaulieu)(Z-Library)

Alan Beaulieu

Learning Snowflake SQL and Scripting Generate, Retrieve, and Automate Snowflake Data (Alan Beaulieu)(Z-Library)

Author Alan Beaulieu

data
Language English

To help you on the path to becoming a Snowflake pro, this concise yet comprehensive guide reviews fundamentals and best practices for Snowflake's SQL and Scripting languages. Developers and data professionals will learn how to generate, modify, and query data in the Snowflake relational database management system as well as how to apply analytic functions for reporting. Author Alan Beaulieu also shows you how to create scripts, stored functions, and stored procedures to return data sets using Snowflake Scripting. This book is ideal whether you're new to databases and need to run queries or reports against a Snowflake database, or transitioning from databases such as Oracle, SQL Server, or MySQL to cloud-based platforms. With this book, you will: • Generate and modify Snowflake data using Insert, Update, Delete • Query data in Snowflake using Select, including joining multiple tables, using subqueries, and grouping • Apply analytic functions for performing subtotals, grand totals, row comparisons, and other reporting functionality • Build scripts combining SQL statements with looping, if-then-else, and exception handling • Learn how to build stored procedures and functions • Use stored procedures to return data sets

Format PDF
Size 8.2 MB
16
Views
0
Downloads
0.00
Total Donations
(First 20 pages)

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Page 1
Learning Snowflake SQL and Scripting Generate, Retrieve, and Automate Snowflake Data Alan Beaulieu
Page 2
DATA “A valuable reference for professionals and a great starting place for Snowflake SQL beginners.” —Ed Crean Senior Solutions Architect, Snowflake “Lays a strong foundation for new learners and thoroughly covers the distinctive SQL features of Snowflake.” —Pankaj Gupta Principal Data Engineer, Discover Financial Services USA Learning Snowflake SQL and Scripting Twitter: @oreillymedia linkedin.com/company/oreilly-media youtube.com/oreillymedia To help you on the path to becoming a Snowflake pro, this concise yet comprehensive guide reviews fundamentals and best practices for Snowflake’s SQL and scripting languages. Developers and data professionals will learn how to generate, modify, and query data in the Snowflake relational database management system as well as how to apply analytic functions for reporting. Author Alan Beaulieu also shows you how to create scripts, stored functions, and stored procedures to return data sets using Snowflake Scripting. This book is ideal whether you’re new to databases and need to run queries or reports against a Snowflake database or you’re transitioning from databases such as Oracle, SQL Server, or MySQL to cloud-based platforms. With this book, you will: • Generate and modify Snowflake data using INSERT, UPDATE, and DELETE • Query data in Snowflake using SELECT, including joining multiple tables, using subqueries, and grouping • Apply analytic functions for performing subtotals, grand totals, row comparisons, and other reporting functionality • Build scripts combining SQL statements with looping, if-then-else, and exception handling • Learn how to build stored procedures and functions • Use stored procedures to return data sets Alan Beaulieu has been designing, building, and implementing custom database applications for over 25 years. Author of Learning SQL and Mastering Oracle SQL, Alan has written an online course on SQL for the University of California and runs a consulting company that specializes in database design and development in the fields of financial services and telecommunications. US $79.99 CAN $99.99 ISBN: 978-1-098-14032-8
Page 3
Alan Beaulieu Learning Snowflake SQL and Scripting Generate, Retrieve, and Automate Snowflake Data Boston Farnham Sebastopol TokyoBeijing
Page 4
978-1-098-14032-8 LSI Learning Snowflake SQL and Scripting by Alan Beaulieu Copyright © 2024 Alan Beaulieu. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com. Acquisitions Editor: Andy Kwan Development Editor: Corbin Collins Production Editor: Katherine Tozer Copyeditor: nSight, Inc. Proofreader: Tove Innis Indexer: Ellen Troutman-Zaig Interior Designer: David Futato Cover Designer: Karen Montgomery Illustrator: Kate Dullea October 2023: First Edition Revision History for the First Edition 2023-10-03: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781098140328 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Learning Snowflake SQL and Scripting, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. The views expressed in this work are those of the author and do not represent the publisher’s views. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
Page 5
Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix 1. Query Primer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Query Basics 1 Query Clauses 5 The select Clause 6 The from Clause 8 The where Clause 10 The group by Clause 11 The having Clause 12 The qualify Clause 14 The order by Clause 15 The limit Clause 17 Wrap-Up 19 Test Your Knowledge 19 2. Filtering. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Condition Evaluation 21 Using Parentheses 22 Using the not Operator 22 Condition Components 23 Equality Conditions 24 Inequality Conditions 24 Range Conditions 25 Membership Conditions 26 Matching Conditions 27 Null Values 29 iii
Page 6
Filtering Using Snowsight 32 Wrap-Up 39 Test Your Knowledge 39 3. Joins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 What Is a Join? 41 Table Aliases 43 Inner Joins 44 Outer Joins 46 Cross Joins 47 Joining Three or More Tables 48 Joining a Table to Itself 50 Joining the Same Table Twice 52 Wrap-Up 54 Test Your Knowledge 54 4. Working with Sets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Set Theory Primer 57 The union Operator 60 The intersect Operator 62 The except Operator 63 Set Operation Rules 65 Sorting Compound Query Results 65 Set Operation Precedence 66 Wrap-Up 68 Test Your Knowledge 68 5. Creating and Modifying Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Data Types 71 Character Data 71 Numeric Data 73 Temporal Data 73 Other Data Types 76 Creating Tables 79 Populating and Modifying Tables 80 Deleting Data 82 Modifying Data 85 Merging Data 87 Wrap-Up 90 Test Your Knowledge 90 iv | Table of Contents
Page 7
6. Data Generation, Conversion, and Manipulation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Working with Character Data 91 String Generation and Manipulation 91 String Searching and Extracting 93 Working with Numeric Data 95 Numeric Functions 96 Numeric Conversion 97 Number Generation 98 Working with Temporal Data 100 Date and Timestamp Generation 101 Manipulating Dates and Timestamps 102 Date Conversion 105 Wrap-Up 106 Test Your Knowledge 107 7. Grouping and Aggregates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Grouping Concepts 109 Aggregate Functions 111 count() Function 113 min(), max(), avg(), and sum() Functions 113 listagg() Function 114 Generating Groups 115 Multicolumn Grouping 115 Grouping Using Expressions 116 Generating Rollups 118 Filtering on Grouped Data 123 Filtering with Snowsight 124 Wrap-Up 128 Test Your Knowledge 128 8. Subqueries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Subqueries Defined 131 Subquery Types 132 Uncorrelated Subqueries 132 Correlated Subqueries 137 Subqueries as Data Sources 140 Subqueries in the from Clause 140 Common Table Expressions 141 Wrap-Up 145 Test Your Knowledge 145 Table of Contents | v
Page 8
9. From Clause Revisited. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Hierarchical Queries 147 Time Travel 150 Pivot Queries 151 Random Sampling 153 Full Outer Joins 154 Lateral Joins 156 Table Literals 157 Wrap-Up 159 Test Your Knowledge 159 10. Conditional Logic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 What Is Conditional Logic? 163 Types of Case Expressions 165 Searched Case Expressions 165 Simple Case Expressions 166 Uses for Case Expressions 167 Pivot Operations 167 Checking for Existence 168 Conditional Updates 169 Functions for Conditional Logic 171 iff() Function 171 ifnull() and nvl() Functions 172 decode() Function 173 Wrap-Up 174 Test Your Knowledge 174 11. Transactions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 What Is a Transaction? 177 Explicit and Implicit Transactions 177 Related Topics 180 Finding Open Transactions 180 Isolation Levels 181 Locking 181 Transactions and Stored Procedures 183 Wrap-Up 183 Test Your Knowledge 183 12. Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 What Is a View? 185 Creating Views 185 vi | Table of Contents
Page 9
Using Views 188 Why Use Views? 188 Data Security 188 Data Aggregation 193 Hiding Complexity 195 Considerations When Using Views 196 Wrap-Up 197 Test Your Knowledge 197 13. Metadata. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 information_schema 201 Working with Metadata 206 Schema Discovery 206 Deployment Verification 208 Generating Administration Scripts 209 get_ddl() Function 210 account_usage 212 Wrap-Up 217 Test Your Knowledge 217 14. Window Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Windowing Concepts 219 Data Windows 219 Partitioning and Sorting 222 Ranking 223 Ranking Functions 224 Top/Bottom/Nth Ranking 225 Qualify Clause 230 Reporting Functions 232 Positional Windows 235 Other Window Functions 239 Wrap-Up 240 Test Your Knowledge 241 15. Snowflake Scripting Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 A Little Background 243 Scripting Blocks 244 Scripting Statements 247 Value Assignment 247 if 249 case 251 Table of Contents | vii
Page 10
Cursors 254 Loops 256 Exceptions 263 Wrap-Up 268 Test Your Knowledge 268 16. Building Stored Procedures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 Why Use Stored Procedures? 269 Turning a Script into a Stored Procedure 269 Stored Procedure Execution 272 Stored Procedures in Action 274 Returning Result Sets 277 Dynamic SQL 281 Wrap-Up 284 Test Your Knowledge 284 17. Table Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 User-Defined Functions 287 What Is a Table Function? 288 Writing Your Own Table Functions 288 Using Built-In Table Functions 292 Data Generation 293 Flattening Rows 294 Finding and Retrieving Query Results 297 Wrap-Up 298 Test Your Knowledge 298 18. Semistructured Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 Generating JSON from Relational Data 299 Storing JSON Documents 304 Querying JSON Documents 311 Wrap-Up 314 Test Your Knowledge 314 A. Sample Database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 B. Solutions to Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 viii | Table of Contents
Page 11
Preface Welcome to Learning Snowflake SQL and Scripting. Perhaps you are brand new to databases and will need to run queries or reports against a Snowflake database. Or perhaps, like myself, you have been working with databases such as Oracle, SQL Server, or MySQL for years, and your company has begun transitioning to cloud- based platforms. Whatever the case, this book strives to empower you with a detailed understanding of Snowflake’s SQL implementation so that you can be as effective as possible. To help put things in context, I’ll start with a brief history of databases, starting with the introduction of relational databases in the 1980s and leading up to the availability of cloud-based database platforms such as Snowflake. If you’re ready to jump right into learning SQL, feel free to move on to Chapter 1, but you should read “Setting Up a Sample Database” on page xii if you want to create your own database with which to experiment. Relational Database Primer Computerized database systems have been around since the 1960s, but for the pur‐ poses of this book, let’s start with the introduction of relational databases, which started hitting the market in the 1980s with products such as Oracle, Sybase, SQL Server (Microsoft), and Db2 (IBM). Relational databases are based on rows of data stored in tables, and related rows stored in different tables are linked using redundant values. For example, the ACME Wholesale customer can be identified using customer ID 123 in the Customer table, and any of ACME’s orders in the Orders table would also be identified using customer ID 123. A table, such as Customer or Orders mentioned above, is comprised of multiple col‐ umns, such as name, address, and telephone number. One or more of these columns is used to uniquely identify each row in the table (known as the primary key). For the example database used for this book, there is a Customer table whose primary key ix
Page 12
consists of a single column named custkey, and every row in the Customer table must have a unique custkey value. There is also an Orders table that includes the col‐ umn custkey to reference the customer who placed the order. The Orders.custkey column is referred to as a foreign key to the Customer table and must contain a value that exists in the Customer.custkey column. Table P-1 shows a quick recap of the ter‐ minology introduced so far. Table P-1. Terms and definitions Column An individual piece of data Row A set of related columns Table A set of rows Primary key One or more columns that can be used as a unique identifier for each row in a table Foreign key One or more columns that can be used together to identify a single row in another table So far, we’ve discussed the use of redundant column values to link tables via primary and foreign keys, but there are rules regarding the storage of redundant values. For example, it is perfectly fine for the Orders table to include a column to hold values of Customer.custkey, but it is not okay to include other columns from the Customer table, such as the name or address columns. If you were looking at a row in the Orders table and wanted to know the name and address of the customer who placed the order, you should go get these values from the Customer table rather than storing the customer’s name and address in the Orders table. The process of designing a database to ensure that each independent piece of information is in only one place (except for foreign keys) is known as normalization. Normalization rules also apply to individual columns, in that a column should hold only a single independent value. One common example would be a mailing address, which is comprised of multiple elements, such as street, city, state, and zip code. A normalized design would therefore include multiple columns, as demonstrated in Table P-2. Table P-2. Sample address columns Address1 3 Maple Street Address2 Suite 305 City Anytown State TX Zip code 12345 x | Preface
Page 13
Companies often have multiple databases used for different purposes, and the degree of normalization can vary greatly. A database used exclusively by a company’s ship‐ ping department, for example, may include a single address column used to print shipping labels, which for the example above might contain the value "3 Maple Street, Suite 305, Anytown, TX 12345." It may also be the case that the shipping database is refreshed daily from a central, normalized database. Snowflake First launched in 2014, Snowflake is a cloud-based, full-featured, relational database. Snowflake databases can be hosted on any of the three major cloud platforms (Ama‐ zon AWS, Microsoft Azure, and Google Cloud), which allows customers with existing cloud deployments to stick with what they know. Both storage and compute engines can be scaled on demand, and Snowflake’s software as a service (SaaS) model frees companies from the need to hire legions of network, server, and database administra‐ tors, allowing organizations to focus on their core business. There are many ways to interact with Snowflake, but for the purposes of this book I suggest you use Snowflake’s browser-based graphical tool named Snowsight, which is an excellent tool and is regularly updated and enhanced. Read Snowsight’s online documentation for an overview of its capabilities. What Is SQL? Structured query language (SQL) is the language originally developed for querying and manipulating data in relational databases. The SQL language has evolved to han‐ dle complex data, such as JavaScript Object Notation (JSON) documents, allowing easier integration between SQL and procedural languages, such as Java. The SQL language is comprised of several groups of commands, as shown in Table P-3. Table P-3. SQL command categories Category Usage Examples Schema statements Creating and modifying database structures Create table, Create index, Alter table Data statements Querying and manipulating data Select, Insert, Update, Delete, Merge Transaction statements Creating and ending transactions Commit, Rollback Preface | xi
Page 14
You may also see schema statements classified as data definition language (DDL), and data statements classified as data manipulation language (DML). The schema state‐ ments are used to create or alter tables, indexes, views, and various other database structures. Once these structures are in place, you will use the data statements to insert, modify, and delete rows in your tables, and to retrieve data. While you will see some schema statements used in this book, the vast majority of examples cover the data statements, which, though few in number, are rich and pow‐ erful statements worthy of in-depth study. SQL is a nonprocedural language, meaning that you define what you want done, but not how to do it. For example, if you are running a report that lists the top ten cus‐ tomers per geographic region, you would write a select statement that sums sales for each customer, but it would be up to the database server to determine how best to retrieve the data. There are generally multiple ways to generate a particular set of results, and some are more efficient than others, so it is left to the database server to determine how to pull data from multiple tables in an efficient manner. What Is SQL Scripting? If you have programmed with a procedural language such as Java, C#, or Go, you are familiar with such programming constructs as looping, if-then-else, and exception handling. SQL, being a nonprocedural language, has none of these constructs. To bridge this gap, most database platforms provide both a nonprocedural SQL imple‐ mentation along with a procedural language that includes both the SQL data state‐ ments such as select and insert along with all of the usual procedural programming constructs. Oracle, for example, provides the PL/SQL procedural lan‐ guage, while Microsoft provides the Transact-SQL language. Snowflake provides the Snowflake Scripting language, which allows you to declare variables, incorporate looping and if-then-else statements, and detect and handle exceptions. Snowflake Scripting language will be covered in Chapters 15, 16, and 17 of this book. Setting Up a Sample Database The nice people at Snowflake have provided several sample databases so that potential customers can gain experience with their SQL implementation. One of the sample databases, TPCH_SF1, is used for the majority of the examples in this book. However, since the TPCH_SF1 database is quite large (over 8.7 million rows of data), I chose to use a small subset (about 330,000 rows) of TPCH_SF1. You will have two options for setting up your own sample database (see “Sample Database Setup” on page xiv), which will depend on whether the TPCH_SF1 sample database is still being made available by Snowflake. xii | Preface
Page 15
The sample database contains eight tables containing information about customer orders of a set of parts provided by a set of suppliers, a real-life example of which might be a company that sells automobile parts made by other companies. Appen‐ dix A contains a visual representation of these tables along with the relationships between the tables. If you want to run the example queries in this book, setting up a free 30-day Snow‐ flake account is very easy. Once your account is active, you can follow my instruc‐ tions for setting up your sample database. Setting Up a Snowflake Account One of the great things about SaaS is that there is generally nothing that needs to be installed locally. All of your interactions with Snowflake will be through a standard browser of your choice. Here are the steps needed to create your own account: 1. Go to www.snowflake.com and click the START FOR FREE button on the top right of the page. 2. Enter your first name, last name, email, company name, role, and country. Click CONTINUE. 3. Choose the Standard edition and choose one of the three cloud providers. A drop-down will appear allowing you to choose the closest cloud node. Check the box to agree to the terms and conditions and click GET STARTED. 4. An email will be sent asking you to activate your account. Click on CLICK TO ACTIVATE in the email. 5. A tab will open in your browser asking you to choose a username and password. After choosing, you will be asked to log in. 6. Your account page will appear in your browser. That’s all there is to it. You will have 30 days to experiment, after which you will need to provide a credit card to continue. You can track your costs under Admin>Usage so you don’t have any unwanted surprises. If you exceed the 30-day trial period and want to continue, here are a couple tips to help keep the costs down: 1. When working in Snowflake, the set of compute resources attached to your ses‐ sion is referred to as the virtual warehouse. You have your choice of anything from a very small warehouse (X-Small) all the way up to the 4X-Large ware‐ house. Make sure you choose the X-Small warehouse when working with the sample database for this book. 2. After a configurable period of inactivity, your warehouse will be shut down. The default setting is 10 minutes, but you can reduce it to as little as 1 minute. I sug‐ gest setting it 3 to 4 minutes. Preface | xiii
Page 16
Both the warehouse size and auto-suspend period can be modified by choosing the Admin>Warehouses menu, clicking on your warehouse name (which is named COMPUTE_WH by default), and then clicking the Edit menu option in the top right corner, as shown in Figure P-1. Figure P-1. Editing warehouse settings Sample Database Setup No matter which of the options you choose for creating your sample database tables, there are a couple of things you will need to do first. Create a worksheet In Snowsight, worksheets are where you will interact with your database. You can cre‐ ate different worksheets for different purposes, so let’s create a worksheet called Learning_Snowflake_SQL. To do so, click Worksheets in the left-hand menu, then click the “+” button at the top right and choose SQL Worksheet. A new worksheet tab will appear and will be given a default name based on the current date/time. You can click the menu next to the worksheet name and choose Rename, at which point you can name it LEARNING_SNOWFLAKE_SQL, as shown in Figure P-2. You can use this worksheet to run your SQL commands, starting with the create database statement in the next section. xiv | Preface
Page 17
Figure P-2. Renaming a worksheet Create your database Now that you have a worksheet, you can start entering commands. The first task will be creating your sample database, as shown in Figure P-3. Figure P-3. Create a new database After typing create database learning_sql into your worksheet, click the Run button (the white arrow with a blue background at the top right) to execute your command. Your database will be created and a schema named Public will be created by default. This is where the tables for your sample database will be created. Preface | xv
Page 18
Sample Database Option #1: Copy from TPCH_SF1 In order to choose this option, which is the simpler of the two methods, you must first check to see which Snowflake sample databases are available. To do so, choose the Data>Databases menu option to see the list of available databases. If you see TPCH_SF1 under the SNOWFLAKE_SAMPLE_DATA database, you’re in luck, as shown in Figure P-4. Figure P-4. Sample database listing The next section describes how to copy the data from TPCH_SF1 into your own database. Create and populate the tables Before executing the commands to create your tables, you will need to specify the database and schema in which you will be working, as shown in Figure P-5. xvi | Preface
Page 19
Figure P-5. Setting the database and schema After entering the use schema command and pressing the Run button, you’re ready to create your tables. Here’s the set of commands: create table region as select * from snowflake_sample_data.tpch_sf1.region; create table nation as select * from snowflake_sample_data.tpch_sf1.nation; create table part as select * from snowflake_sample_data.tpch_sf1.part where mod(p_partkey,50) = 8; create table partsupp as select * from snowflake_sample_data.tpch_sf1.partsupp where mod(ps_partkey,50) = 8; create table supplier as with sp as (select distinct ps_suppkey from partsupp) select s.* from snowflake_sample_data.tpch_sf1.supplier s inner join sp on s.s_suppkey = sp.ps_suppkey; create table lineitem as select l.* from snowflake_sample_data.tpch_sf1.lineitem l inner join part p on p.p_partkey = l.l_partkey; create table orders as Preface | xvii
Page 20
with li as (select distinct l_orderkey from lineitem) select o.* from snowflake_sample_data.tpch_sf1.orders o inner join li on o.o_orderkey = li.l_orderkey; create table customer as with o as (select distinct o_custkey from orders) select c.* from snowflake_sample_data.tpch_sf1.customer c inner join o on c.c_custkey = o.o_custkey; This script can also be found at my GitHub page. Once you have loaded these eight create table commands into your worksheet, you can run them individually by highlighting and executing each statement, or you can run all of them in a single execution by dropping down the menu on the right side of the Run button and choosing Run All, as shown in Figure P-6. Figure P-6. Choosing Run All option from Run menu Whether you run them one at a time or all together, the end result should be eight new tables in the Public schema of your Learning_SQL database. If you run into problems, you can simply start again by re-creating the database via create or replace database learning_sql, which will drop any existing tables. xviii | Preface
The above is a preview of the first 20 pages. Register to read the complete e-book.

Support Author

0.00
Total Amount (¥)
0
Donation Count
Please enter an amount Minimum ¥1

You will be redirected to Alipay to complete payment, then return here.

Recommended for You

Loading recommended books...
Failed to load, please try again later
Back to List