Snowflake SQL Flashcards

1
Q

How do you create a database in Snowflake?

A

To create a database in Snowflake, use the SQL command: CREATE DATABASE database_name;

Example: CREATE DATABASE my_database;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the syntax for creating a table in Snowflake?

A

The syntax for creating a table is: CREATE TABLE table_name (column1 datatype, column2 datatype, …);

Example: CREATE TABLE my_table (id INT, name VARCHAR);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain how to insert data into a Snowflake table.

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you perform a SELECT query in Snowflake?

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the different types of joins available in Snowflake?

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Describe how to use the UPDATE statement in Snowflake.

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the purpose of the MERGE statement?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you delete data from a table in Snowflake?

A

To delete data, use the DELETE statement: DELETE FROM table_name WHERE condition;

Example: DELETE FROM my_table WHERE id = 1;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain the concept of window functions in Snowflake.

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you handle NULL values in Snowflake SQL?

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the aggregate functions supported by Snowflake?

A

Aggregate functions include COUNT, SUM, AVG, MIN, MAX, etc.

Example: SELECT COUNT(*), AVG(salary) FROM employees;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you create a stored procedure in Snowflake?

A

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’; $$;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the role of user-defined functions (UDFs) in Snowflake?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can you optimize a query in Snowflake?

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the use of the ANALYZE statement?

A

The ANALYZE statement is used to gather statistics on tables and indexes to help optimize query performance.

Example: ANALYZE TABLE my_table COMPUTE STATISTICS;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Describe the use of materialized views in Snowflake.

A

Materialized views store the results of a query physically, allowing faster query performance on complex calculations.

Example: CREATE MATERIALIZED VIEW my_view AS SELECT id, COUNT(*) FROM my_table GROUP BY id;

17
Q

How do you grant and revoke privileges in Snowflake?

A

Privileges can be granted and revoked using the GRANT and REVOKE statements.

Example: GRANT SELECT ON my_table TO role_name;

18
Q

What is the purpose of the SHOW command?

A

The SHOW command is used to display information about Snowflake objects like tables, views, and schemas.

Example: SHOW TABLES;

19
Q

How do you handle transactions in Snowflake?

A

Transactions in Snowflake can be managed using the BEGIN, COMMIT, and ROLLBACK statements.

Example: BEGIN TRANSACTION; INSERT INTO my_table (id, name) VALUES (1, ‘Alice’); COMMIT;

20
Q

What are the best practices for writing efficient SQL queries in Snowflake?

A

Best practices include using proper indexing, avoiding SELECT *, minimizing data transfer, and writing optimized SQL logic.

Example: SELECT id, name FROM my_table WHERE id = 1;