Snowflake SQL Flashcards
How do you create a database in Snowflake?
To create a database in Snowflake, use the SQL command: CREATE DATABASE database_name;
Example: CREATE DATABASE my_database;
What is the syntax for creating a table in Snowflake?
The syntax for creating a table is: CREATE TABLE table_name (column1 datatype, column2 datatype, …);
Example: CREATE TABLE my_table (id INT, name VARCHAR);
Explain how to insert data into a Snowflake table.
To insert data into a Snowflake table, use the INSERT INTO statement: INSERT INTO table_name (column1, column2, …) VALUES (value1, value2, …);
Example: INSERT INTO my_table (id, name) VALUES (1, ‘John’);
How do you perform a SELECT query in Snowflake?
To perform a SELECT query, use the SELECT statement: SELECT column1, column2 FROM table_name WHERE condition;
Example: SELECT id, name FROM my_table WHERE id = 1;
What are the different types of joins available in Snowflake?
The types of joins include INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN.
Example: SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id;
Describe how to use the UPDATE statement in Snowflake.
The UPDATE statement modifies existing data in a table: UPDATE table_name SET column1 = value1 WHERE condition;
Example: UPDATE my_table SET name = ‘Jane’ WHERE id = 1;
What is the purpose of the MERGE statement?
The MERGE statement allows you to perform insert, update, or delete operations based on a join condition between two tables.
Example: MERGE INTO target_table USING source_table ON target_table.id = source_table.id WHEN MATCHED THEN UPDATE SET target_table.name = source_table.name;
How do you delete data from a table in Snowflake?
To delete data, use the DELETE statement: DELETE FROM table_name WHERE condition;
Example: DELETE FROM my_table WHERE id = 1;
Explain the concept of window functions in Snowflake.
Window functions perform calculations across a set of table rows related to the current row, using an OVER clause.
Example: SELECT id, name, SUM(salary) OVER (PARTITION BY department) FROM employees;
How do you handle NULL values in Snowflake SQL?
NULL values can be handled using functions like COALESCE, NVL, or IS NULL/IS NOT NULL conditions.
Example: SELECT * FROM my_table WHERE name IS NULL;
What are the aggregate functions supported by Snowflake?
Aggregate functions include COUNT, SUM, AVG, MIN, MAX, etc.
Example: SELECT COUNT(*), AVG(salary) FROM employees;
How do you create a stored procedure in Snowflake?
Stored procedures can be created using the CREATE PROCEDURE statement, specifying the procedure logic in JavaScript.
Example: CREATE PROCEDURE my_procedure() RETURNS STRING LANGUAGE JAVASCRIPT AS $$ return ‘Hello’; $$;
What is the role of user-defined functions (UDFs) in Snowflake?
UDFs allow you to create custom functions in SQL or JavaScript to encapsulate reusable logic.
Example: CREATE OR REPLACE FUNCTION my_function(param INT) RETURNS INT LANGUAGE JAVASCRIPT AS $$ return param * 2; $$;
How can you optimize a query in Snowflake?
Query optimization can be done by analyzing the query plan, using appropriate indexes, partitioning, and optimizing SQL logic.
Example: EXPLAIN SELECT * FROM my_table WHERE id = 1;
What is the use of the ANALYZE statement?
The ANALYZE statement is used to gather statistics on tables and indexes to help optimize query performance.
Example: ANALYZE TABLE my_table COMPUTE STATISTICS;