Interview's MateSQL Learning
Interview's Mate

SQL Learning

Foundations → production SQL

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
SQL topics
Lesson 152/ 160
08. Transactions, Security & Production SQL Administration

Users, Roles & Privilege Management (GRANT, REVOKE)

Manage least-privilege access with roles, GRANT, and REVOKE.

IntermediateRoles · GRANT3 quiz questions
Practice SQL
In plain wordsDiagramExamples & theoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Roles are groups of permissions. Give a group only what it needs, then put people or apps in the group.

Think of it like…

Office key cards: rather than programming every door for every employee, you create groups (finance, engineering, visitors), give each group access to certain doors, and put people in groups.

Where you’ve seen it

A startup's database has three roles: app_rw (read and write data, no DDL) for the API, migrator (owns the schema, runs migrations), and analyst_ro (SELECT only, for the BI tool). When an analyst's password leaks, the attacker can read data but can't change or delete anything.

Words you’ll hear

Role
A named set of privileges; with LOGIN it acts as a user.
GRANT / REVOKE
Give or remove privileges on objects, or membership in roles.
Default privileges
ALTER DEFAULT PRIVILEGES: privileges automatically applied to tables created in the future.
PUBLIC
A pseudo-role meaning everyone; revoke what it doesn't need.

Visual diagram

Loading interactive diagram

Theory & example queries

Roles, membership and grants

Create group roles without LOGIN for each kind of access, grant them privileges, and grant the group to login roles: CREATE ROLE analyst_ro; GRANT analyst_ro TO asha;. Members inherit the group's privileges. Revoking membership removes all of them at once, which keeps access reviews manageable.

Access needs every level: CONNECT on the database, USAGE on the schema, then the table privilege. GRANT SELECT ON ALL TABLES IN SCHEMA app TO analyst_ro covers existing tables only; add ALTER DEFAULT PRIVILEGES IN SCHEMA app GRANT SELECT ON TABLES TO analyst_ro (run as the role that creates tables) so new tables are covered too. You can also grant on specific columns: GRANT SELECT (id, name, city) ON customers TO support.

A read-only analyst role (PostgreSQL)Query example
1Input dataStart from the source rows or state that the statement sees.
This statement works with the current schema, configuration, or database state rather than a row dataset. The command still follows the same input → query → result flow.
2QuerySwitch dialects, then select a line or execution step to trace it.
query.sql
1CREATE ROLE analyst_ro;23GRANT CONNECT ON DATABASE shop TO analyst_ro;45GRANT USAGE ON SCHEMA app TO analyst_ro;67GRANT8SELECT9  ON ALL TABLES IN SCHEMA app TO analyst_ro;1011ALTER DEFAULT PRIVILEGES FOR ROLE migrator IN SCHEMA app12GRANT13SELECT14  ON TABLES TO analyst_ro;1516CREATE ROLE asha LOGIN PASSWORD '…' IN ROLE analyst_ro;
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Choose the target

Identify the table or database object the statement will modify.

1/16
3OutputInspect the representative rows, affected-row count, plan, or command status.
Representative role setup status
CREATE ROLE GRANT GRANT GRANT ALTER DEFAULT PRIVILEGES CREATE ROLE

Note: PostgreSQL-style command tags. The password value is intentionally omitted, and existing database/schema names must be available.

4ExplanationConnect the result back to the SQL rule and the edge case it demonstrates.

Key takeaway: Grant to group roles at every level, and set default privileges for future tables.

Least privilege in practice

Separate duties: a migration role owns the schema and runs DDL; the application role gets only the DML it needs on its tables; read-only roles serve analytics and support. The application should never connect as a superuser or as the table owner, because owners can drop their tables regardless of grants.

Clean up defaults: in PostgreSQL before version 15, every role could create objects in the public schema; REVOKE CREATE ON SCHEMA public FROM PUBLIC closes that. For per-row restrictions, such as a tenant seeing only its own rows, add row-level security policies on top of grants. In MySQL, users are 'name'@'host' accounts, and roles exist from version 8.0.

An application role without DDLQuery example
1Input dataStart from the source rows or state that the statement sees.
This statement works with the current schema, configuration, or database state rather than a row dataset. The command still follows the same input → query → result flow.
2QuerySwitch dialects, then select a line or execution step to trace it.
query.sql
1CREATE ROLE app_rw;23GRANT USAGE ON SCHEMA app TO app_rw;45GRANT6SELECT7,8  INSERT,9UPDATE,10DELETE ON ALL TABLES IN SCHEMA app TO app_rw;1112GRANT USAGE ON ALL SEQUENCES IN SCHEMA app TO app_rw;
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Choose the target

Identify the table or database object the statement will modify.

1/12
3OutputInspect the representative rows, affected-row count, plan, or command status.
Representative application grants
CREATE ROLE GRANT GRANT GRANT

Note: Assumes the app schema and its referenced tables and sequences exist; command wording can differ by PostgreSQL client.

4ExplanationConnect the result back to the SQL rule and the edge case it demonstrates.

Key takeaway: Separate owner, application and read-only roles; never run the app as owner or superuser.

Interactive walkthrough

How it works, step by step

Follow one practical SQL workflow from start to finish. Select any step or play through the flow.

Progress1/4
Step 1Design

List who needs what

Identify each kind of access: application read-write, migrations, analysts, support, backups.

Focus at this step

app_rw · migrator · analyst_ro

What to watch at this step
One role
per kind of access
Tip: use ← → to move between steps

When to use it & trade-offs

What it is

Databases control access with roles (in PostgreSQL, a user is simply a role that can log in) and privileges granted on objects: SELECT, INSERT, UPDATE and DELETE on tables, USAGE on schemas, EXECUTE on functions. GRANT gives a privilege, REVOKE removes it, and roles can be members of other roles, so you grant to a group role once and add people to it.

Why it matters: Least privilege limits the damage from a leaked password, a buggy script or an SQL injection: an application role that can't DROP tables can't drop them. Setting up read-only analyst access and separate application and migration roles is everyday work, and a common security interview question.

When to reach for it

  • Read-only access for analysts and BI tools
  • Separate application and migration roles
  • Limiting damage from leaked credentials or SQL injection

When to avoid or be careful

  • Applications connecting as superuser or as the table owner
  • Granting privileges to individual users instead of groups
  • Forgetting default privileges, so new tables are invisible to read-only roles

Group roles vs per-user grants vs row-level security

Group roles with membership
Best for: Managing access for teams and services

Strengths: Grant once, add or remove members; easy audits.

Watch out: Needs a little upfront design.

Grants to individual users
Best for: One-off exceptions

Strengths: Quick.

Watch out: Becomes unmanageable and hard to review.

Row-level security policies
Best for: Restricting which rows a role can see (multi-tenant)

Strengths: Enforced by the database on every query.

Watch out: More complex; must be tested carefully.

Interview signals & numbers to know

When this topic comes up

  • “Give the analytics team read-only access”
  • “permission denied for table …”
  • “How would you limit what the app can do?”
  • Least privilege
  • Security review

Benchmarks & rules of thumb

3 -
levels to grant: database, schema, table
PostgreSQL 15 -
PUBLIC no longer gets CREATE on the public schema
MySQL 8.0 -
added roles

Common mistakes & follow-ups

Common interview mistakes

  • Granting SELECT on all tables but forgetting USAGE on the schema, so every query fails with permission denied.
  • Granting on existing tables only, so tables created next week are invisible to the analyst role.
  • Running the application as the owner of its tables, which can drop them whatever the grants say.

Likely interviewer follow-ups

  • How would you set up read-only access that also covers tables created later?
  • Why shouldn't the application connect as the table owner?
  • How would you restrict each tenant to its own rows?
Interview tip: Describe the three-role setup (owner/migrator, app read-write, analyst read-only) and mention default privileges. It's a complete, practical answer in under a minute.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Users, Roles & Privilege Management (GRANT, REVOKE) with hands-on SQL practice and interview Q&A.

Open SQL Practice →SQL interview Q&A
Previous topic
Triggers (BEFORE / AFTER, FOR EACH ROW / STATEMENT) & Audit Logging
Next topic
Row-Level Security (RLS) Policies for Multi-Tenant Databases