What if you could protect sensitive data row by row without juggling multiple copies?
Why Row access policies in Snowflake? - Purpose & Use Cases
Imagine you have a big spreadsheet with sensitive data for many teams. You want each team to see only their own rows. So, you try to copy the spreadsheet multiple times, each with filtered data for each team.
This manual way is slow and confusing. You must update many copies every time data changes. Mistakes happen easily, and sensitive data might leak if you forget to filter correctly.
Row access policies let you set rules once that automatically hide or show rows based on who is looking. No need to copy data or manage many versions. The system enforces access safely and quickly.
CREATE VIEW team_a_data AS SELECT * FROM data WHERE team = 'A'; CREATE VIEW team_b_data AS SELECT * FROM data WHERE team = 'B';
CREATE ROW ACCESS POLICY team_policy AS (user_role STRING) RETURNS BOOLEAN -> user_role = CURRENT_ROLE(); ALTER TABLE data ADD ROW ACCESS POLICY team_policy ON (team);
You can securely share one dataset with many users, each seeing only their allowed rows, without extra copies or manual filtering.
A company shares sales data with regional managers. Each manager sees only their region's sales, thanks to row access policies, keeping data safe and easy to manage.
Manual filtering by copying data is slow and risky.
Row access policies automate row-level security with simple rules.
This keeps data safe, reduces errors, and saves time.
Practice
row access policy in Snowflake?Solution
Step 1: Understand the function of row access policies
Row access policies are designed to filter rows based on conditions, controlling visibility.Step 2: Compare with other options
Encryption, login management, and query optimization are unrelated to row access policies.Final Answer:
To control which rows a user can see in a table -> Option BQuick Check:
Row access policy = row visibility control [OK]
- Confusing row access policies with encryption
- Thinking it manages user credentials
- Assuming it improves query speed automatically
Solution
Step 1: Recall Snowflake row access policy syntax
The correct syntax uses 'AS (parameters) RETURNS BOOLEAN -> condition;' with a single equals sign for comparison.Step 2: Analyze each option
CREATE ROW ACCESS POLICY policy_name AS (user_role STRING) RETURNS BOOLEAN -> user_role = 'ADMIN'; matches the correct syntax. CREATE ROW ACCESS POLICY policy_name AS (user_role STRING) RETURNS BOOLEAN -> user_role == 'ADMIN'; uses '==' which is invalid in Snowflake SQL. CREATE ROW ACCESS POLICY policy_name AS (user_role STRING) RETURNS BOOLEAN { user_role = 'ADMIN' }; uses braces which are not valid. CREATE ROW ACCESS POLICY policy_name AS (user_role STRING) RETURNS BOOLEAN BEGIN RETURN user_role = 'ADMIN'; END; uses BEGIN...END block which is not used here.Final Answer:
CREATE ROW ACCESS POLICY policy_name AS (user_role STRING) RETURNS BOOLEAN -> user_role = 'ADMIN'; -> Option CQuick Check:
Correct syntax uses single '=' and '->' arrow [OK]
- Using '==' instead of '=' for comparison
- Adding braces {} around condition
- Using BEGIN...END block in policy definition
CREATE ROW ACCESS POLICY filter_policy AS (region STRING) RETURNS BOOLEAN -> region = CURRENT_REGION();
and a table with rows having regions 'US', 'EU', 'APAC', what rows will a user in region 'EU' see?
Solution
Step 1: Understand the policy condition
The policy returns TRUE only if the row's region matches the user's current region.Step 2: Apply the policy for user in 'EU'
Only rows with region = 'EU' will satisfy the condition and be visible.Final Answer:
Only rows where region = 'EU' -> Option AQuick Check:
Policy filters rows by matching region = CURRENT_REGION() [OK]
- Assuming all rows are visible
- Confusing CURRENT_REGION() with a fixed value
- Thinking policy hides all rows for unmatched regions
CREATE ROW ACCESS POLICY sales_policy AS (sales_amount NUMBER) RETURNS BOOLEAN -> sales_amount > 1000;
But after applying it, users see all rows including those with sales_amount = 500. What is the likely issue?
Solution
Step 1: Check policy syntax and logic
The syntax and logic (sales_amount > 1000) are correct to filter rows.Step 2: Verify policy application
If users still see all rows, the policy was likely not applied to the table or column properly.Final Answer:
The policy was not applied to the table column -> Option AQuick Check:
Policy must be applied to enforce row filtering [OK]
- Assuming wrong comparison operator
- Ignoring policy application step
- Blaming NULL values without checking application
department matches their assigned department stored in a session variable USER_DEPT. Which row access policy correctly enforces this?Solution
Step 1: Understand the requirement
The policy must compare the row's department to the user's assigned department stored in a session variable USER_DEPT.Step 2: Analyze options
Options A, B, and D use CURRENT_ROLE(), CURRENT_SESSION(), and CURRENT_USER(), which do not directly access USER_DEPT variable. CREATE ROW ACCESS POLICY dept_policy AS (department STRING) RETURNS BOOLEAN -> department = $USER_DEPT; correctly accesses the session variable with $USER_DEPT.Step 3: Confirm correct syntax
CREATE ROW ACCESS POLICY dept_policy AS (department STRING) RETURNS BOOLEAN -> department = $USER_DEPT; shows the correct approach to access and compare the session variable USER_DEPT with the department column.Final Answer:
CREATE ROW ACCESS POLICY dept_policy AS (department STRING) RETURNS BOOLEAN -> department = $USER_DEPT; -> Option DQuick Check:
Use session variable USER_DEPT to filter department rows [OK]
- Using CURRENT_ROLE() or CURRENT_USER() instead of session variable
- Not casting session variable properly
- Confusing CURRENT_SESSION() with user attributes
Ready to prove it?
Pass the test and this pattern moves unknown
