Learn SQL
-- security.grant_revoke_rbac

GRANT, REVOKE & Roles in SQL

Least privilege per role — tables, columns, actions

--concept

DCL gates access: GRANT SELECT/INSERT/UPDATE/DELETE ON tables (or columns) TO roles; REVOKE removes. Roles collect privileges; users inherit via membership. Least privilege contains injection blast radius and buggy jobs. Views + RLS refine to row/column granularity where table grants are too coarse.

--how it works
1

Grant minimal verbs

SELECT/INSERT/UPDATE scoped per table/column to each role.

2

Revoke danger

DELETE/DDL removed from support/analyst paths by default.

3

Roles carry grants

Users inherit via membership — auditable, revocable in one place.

4

Refine past tables

Views + RLS for row/column granularity table grants cannot express.

--visualization
rolegrants
analystSELECT (id,name)
app_writerorders CRUD
adminDDL + grants
Stage 1 / 1
Least privilege per role; membership for users. Default-deny new tables.
--example
query.sql
1GRANT SELECT ON employees TO analyst;
2REVOKE DELETE ON employees FROM analyst;
Result
rolegrants
analystSELECT only
app_writerINSERT/UPDATE scoped
adminDDL + grants

Analyst reads but cannot delete or alter. App writer mutates its tables only. Admin holds DDL separately — breach of one role cannot drop schemas.

--variants

Analyst column scope

Reads names, never salaries or deletes.

query.sql
1GRANT SELECT (id, name) ON employees TO analyst;
2REVOKE DELETE ON employees FROM analyst;
Result
rolegrants
analystSELECT (id,name) only
supportno DELETE/DDL
1Column list narrows even SELECT.
2Dangerous verbs explicitly absent.
3New tables default-deny until granted.

Service writer role

App mutates its tables, nothing else.

query.sql
1CREATE ROLE app_writer;
2GRANT SELECT, INSERT, UPDATE ON orders TO app_writer;
3GRANT app_writer TO app_svc;
Result
rolescope
app_writerorders CRUD scoped
membershipapp_svc inherits
1Grants attach to role, not user.
2Rotation = membership change, not re-grant.
3Quarterly grant audits catch drift.
--key facts
Least privilegeeach role minimal verbs + objects
Rolesgrant to roles, membership to users
Views+RLSrow/column refinement past tables
--use cases
Analyst/support sandboxing per least privilege
Service-account scoping for blast-radius control
--under the hood
Grants enforced at parse/plan — zero per-row cost; RLS adds per-row predicates where used.
Default privileges (ALTER DEFAULT PRIVILEGES) auto-grant future tables per schema.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.