Database / Snowflake Interview Questions
What is Row Access Policy in Snowflake and how does it implement row-level security?
A Row Access Policy (RAP) is a Snowflake security object that transparently injects an additional filter predicate into every query that touches the protected table. From the querying user's perspective, their SQL is unchanged — they simply receive fewer rows. The policy's logic determines which rows are visible based on the current user's identity or role.
The policy body is a SQL Boolean expression referencing CURRENT_USER(), CURRENT_ROLE(), or IS_ROLE_IN_SESSION(). A common pattern uses a mapping table: the policy joins the mapping table to determine which regions or tenants the current role may see, then returns only those rows.
Multiple policies can be applied to a single table; all must return TRUE for a row to be visible (AND logic). Policies can also be applied to views. Row Access Policies are evaluated after column-level masking policies.
-- Mapping table: role -> allowed region
CREATE TABLE region_access_map (
role_name VARCHAR,
region VARCHAR
);
INSERT INTO region_access_map VALUES
('EMEA_ANALYST', 'EMEA'),
('APAC_ANALYST', 'APAC'),
('GLOBAL_ANALYST', 'GLOBAL');
-- Row Access Policy: show all rows to GLOBAL_ANALYST,
-- otherwise filter by the mapped region
CREATE ROW ACCESS POLICY region_rap AS (row_region VARCHAR) RETURNS BOOLEAN ->
EXISTS (
SELECT 1 FROM region_access_map
WHERE role_name = CURRENT_ROLE()
AND (region = 'GLOBAL' OR region = row_region)
);
-- Attach the policy to the orders table on the region column
ALTER TABLE orders
ADD ROW ACCESS POLICY region_rap ON (region);
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
