skip to content

In SQL, what statements create a reporting role, give it read access to the orders table, and add the user alice?

level: juniorimportance: should knowfreq 40%

answer

  1. three statements, in dependency order
  2. two different kinds of GRANT
  3. one form has no ON clause
  4. the member goes after TO

basics

~10 s

CREATE ROLE reporting; GRANT SELECT ON orders TO reporting; GRANT reporting TO alice; The last form has no ON clause, because it grants membership in a role instead of a privilege on an object.

solid answer

~50 s

Three statements, in order: `CREATE ROLE reporting;`, then `GRANT SELECT ON orders TO reporting;`, then `GRANT reporting TO alice;`. The second and third are different forms of `GRANT`. The object form has an `ON` clause and names a privilege; the membership form has no `ON` and names a role. The direction matters: the role comes first and the member goes after `TO`, so `GRANT alice TO reporting;` makes the role a member of alice. Taking alice out is `REVOKE reporting FROM alice;`, which leaves the role's own privileges alone, and running `REVOKE SELECT ON orders FROM alice;` instead removes only a direct grant she may not hold. To let her add members, use `WITH ADMIN OPTION`, not `WITH GRANT OPTION`. SQL Server spells membership differently, `ALTER ROLE reporting ADD MEMBER alice;`, and MySQL names her `'alice'@'%'`.

code

sql · 5 lines
sql
CREATE ROLE reporting;
GRANT SELECT ON orders TO reporting;
GRANT reporting TO alice;

REVOKE reporting FROM alice;

go deeper

for a junior

Recall the three statements and their order: create the role, grant it the privilege, grant the role to the user. Notice that the last statement has no ON clause.

for a middle

Explain the two forms of GRANT and how to tell them apart. Say why REVOKE SELECT from the member can leave her access intact, and which option lets a member manage the role.

for a senior

Move a role script between engines without surprises: the membership line is what changes, and MySQL needs a default-role step. Mention the swapped-direction mistake, because it parses and silently does the wrong thing.

for a principal

A team standard that privileges go only on roles makes membership statements the thing to review, so say how you would keep those statements in version control and detect direct grants that drift outside the standard.

A **role** is a named holder of privileges. Instead of granting access to each person, you grant it to the role and then make people members. The reasons for working that way belong to access-control design. An interviewer asking this question wants the statements themselves, and wants to see that you know `GRANT` has two different forms. ## The three statements ```sql CREATE ROLE reporting; GRANT SELECT ON orders TO reporting; GRANT reporting TO alice; ``` They run in this order because each depends on the one before it: 1. `CREATE ROLE` makes the empty role, which must exist before anything can be granted to it. 2. The object form of `GRANT` gives the role its privilege. 3. The membership form of `GRANT` makes alice a member of the role. ## Two forms of one keyword PostgreSQL's reference says `GRANT` has two basic variants, one for privileges on a database object and one for membership in a role. They read alike and do different things: | | Object form | Membership form | |---|---|---| | Example | `GRANT SELECT ON orders TO reporting;` | `GRANT reporting TO alice;` | | First slot | A privilege | A role name | | `ON` clause | Required | Absent | | Passing it on | `WITH GRANT OPTION` | `WITH ADMIN OPTION` | | To `PUBLIC` | Allowed | Not allowed in PostgreSQL | The missing `ON` is how you tell them apart at a glance. `GRANT reporting ON orders TO alice;` mixes the two and is not valid, because a role is not a privilege on a table. The membership form takes lists on both sides, so `GRANT reporting, billing TO alice, bob;` gives both people both memberships. Writing one role per line is usually easier to review, because each line can then be removed on its own. ## Direction and reversal The first slot holds what is being handed over, and the slot after `TO` holds who receives it. Swapping them still parses and does the wrong thing: - `GRANT reporting TO alice;` makes alice a member of `reporting`. - `GRANT alice TO reporting;` makes `reporting` a member of alice, so the role and its members can gain what alice holds. The reverse uses `FROM`, in the same way as every other `REVOKE`: ```sql REVOKE reporting FROM alice; ``` This changes one person's membership and leaves the role and its privileges untouched, so everyone else keeps working. Two near misses are worth knowing: - `REVOKE SELECT ON orders FROM alice;` targets a direct grant to alice. If her access comes through the role, there is no such grant to remove, and she keeps it. - `DROP ROLE reporting;` tries to remove the role for everyone. PostgreSQL refuses while the role still holds privileges, so it is not a way to remove one member. ## Letting a member manage the role To let alice add other members, use `WITH ADMIN OPTION`: ```sql GRANT reporting TO alice WITH ADMIN OPTION; ``` PostgreSQL's `GRANT` synopsis lists `ADMIN`, `INHERIT` and `SET` as the membership options, and its `REVOKE` reference says that when revoking membership, `GRANT OPTION` is instead called `ADMIN OPTION`. To change an option on a membership that already exists, repeat the statement with the new value: `GRANT reporting TO alice WITH ADMIN FALSE;`. The role form also accepts `WITH INHERIT FALSE` and `WITH SET FALSE` in PostgreSQL, which shape how the membership behaves. How privileges then reach a member's session is a question for the role model, not this statement. ## Where the spelling moves - **PostgreSQL and Oracle** use the three statements as written. In PostgreSQL, `CREATE ROLE reporting` makes a role that cannot log in, and `CREATE USER alice` is the same as `CREATE ROLE alice LOGIN`. - **MySQL** uses the same three statements, with accounts written as `'alice'@'%'`. It also has a further step, since a granted role is not active by default: `SET DEFAULT ROLE ALL TO 'alice'@'%';`. - **SQL Server** has `CREATE ROLE` and the object form, but membership is `ALTER ROLE reporting ADD MEMBER alice;`, reversed with `DROP MEMBER`. - **SQLite** has no roles and no `GRANT`. The object form travels between these engines far better than the membership form does, so check that third statement first when a script moves between them.

  • How is the membership statement written in SQL Server?
    With `ALTER ROLE reporting ADD MEMBER alice;`, and `ALTER ROLE reporting DROP MEMBER alice;` reverses it. SQL Server shares the `CREATE ROLE` statement and the object form, `GRANT SELECT ON orders TO reporting;`, so the membership line is what a migrated script has to rewrite.
  • In PostgreSQL, can you write GRANT reporting TO PUBLIC to give every role that membership?
    No. PostgreSQL's reference says that, unlike privileges, membership in a role cannot be granted to `PUBLIC`. If everyone should have the access, grant the privilege itself: `GRANT SELECT ON orders TO PUBLIC;`.
  • As statements in PostgreSQL, how do CREATE ROLE and CREATE USER differ?
    Only in the default for logging in. `CREATE ROLE alice` gives a role without login, and `CREATE USER alice` is the same as `CREATE ROLE alice LOGIN`. Both can then appear on either side of `GRANT reporting TO alice;`.

A role is a key ring. GRANT SELECT ON orders TO reporting puts a key on the ring, and GRANT reporting TO alice hands her the ring. Taking the ring back does not touch the keys, and she never held a key of her own to take away.

  • Writes GRANT alice TO reporting and expects alice to join the role.
  • Adds an ON clause to the membership form, as in GRANT reporting ON orders TO alice.
  • Removes a member with REVOKE SELECT ON orders FROM alice when her access comes through the role.
  • Uses WITH GRANT OPTION on a role grant in PostgreSQL, where it is ADMIN OPTION.
  • Expects GRANT reporting TO alice to run unchanged on SQL Server, which uses ALTER ROLE.
  • Drops the role to remove one person from it.