In SQL, how do you write a GRANT that lets team_lead pass on SELECT on orders, and a REVOKE that removes only that ability?
answer
- one clause last, one clause first
- the option belongs to a privilege
- two words swap between the forms
- PUBLIC cannot hold it in PostgreSQL
basics
~20 sGRANT SELECT ON orders TO team_lead WITH GRANT OPTION; adds the right to pass the privilege on. REVOKE GRANT OPTION FOR SELECT ON orders FROM team_lead; removes that right and leaves team_lead's own SELECT in place.
solid answer
~40 sThe two clauses sit at opposite ends of their statements. `WITH GRANT OPTION` goes last in a grant: `GRANT SELECT ON orders TO team_lead WITH GRANT OPTION;`. `GRANT OPTION FOR` goes first in a revoke, before the privilege list: `REVOKE GRANT OPTION FOR SELECT ON orders FROM team_lead;`. That second form keeps the privilege and drops the option, whereas PostgreSQL's reference says a plain `REVOKE SELECT` removes both. The option attaches to the privilege named, so this statement says nothing about `INSERT`. PostgreSQL also refuses it for `PUBLIC`, though the SQL standard allows that. Engines differ on the revoke: Oracle has no `GRANT OPTION FOR`, so you revoke and grant again without the clause, and MySQL writes `REVOKE GRANT OPTION ON shop.orders FROM 'team_lead'@'%';`, where the option covers every privilege held at that level.
code
sql · 3 linesGRANT SELECT ON orders TO team_lead WITH GRANT OPTION;
REVOKE GRANT OPTION FOR SELECT ON orders FROM team_lead;go deeper
Recall the positions: WITH GRANT OPTION goes at the end of a GRANT, and GRANT OPTION FOR goes right after REVOKE. Write both out for one table and one user.
Explain what each statement leaves behind: the option form keeps the privilege, and the plain form removes both. Say that the option attaches to the privileges named, and that PUBLIC cannot hold it in PostgreSQL.
Show you know how a script run by a role without the option can end in a warning, not an error, with access missing. State how you would verify the result of a grant script.
Grant options spread the power to hand out access, so say whether your team would allow them outside a small owner group, and how you would spot WITH GRANT OPTION in reviewed scripts.
A **grant option** is the right to hand a privilege you hold on to someone else. Without it, you can use the privilege and nothing more. What the option then means for the people downstream is a matter of engine semantics, so this answer stays with the exact statements, which are the part a candidate is asked to write out. ## Adding the option `WITH GRANT OPTION` is a clause at the very end of the statement, after the grantee: ```sql GRANT SELECT ON orders TO team_lead WITH GRANT OPTION; ``` `team_lead` can now run the ordinary form, naming whichever grantee they choose: ```sql GRANT SELECT ON orders TO bob; ``` Some details of the clause are worth stating precisely: - **It attaches to the privileges in that statement.** The statement above says nothing about `INSERT` on `orders`, and in PostgreSQL the option is tracked per privilege. - **It covers the whole list.** `GRANT SELECT, INSERT ON orders TO team_lead WITH GRANT OPTION;` gives the option on both privileges. To give it on one and not the other, write two statements. - **It is additive.** If `team_lead` already holds plain `SELECT`, rerunning the grant with the clause adds the option. There is no need to revoke first. - **It does not pass itself on.** `team_lead` must write `WITH GRANT OPTION` again for `bob` to be able to pass it further. - **`PUBLIC` cannot hold it in PostgreSQL.** The reference says grant options cannot be granted to `PUBLIC`, and notes that the SQL standard does allow it. ## Removing only the option `GRANT OPTION FOR` goes at the start of the revoke, before the privilege list: ```sql REVOKE GRANT OPTION FOR SELECT ON orders FROM team_lead; ``` The pair of statements below looks similar and differs a lot. PostgreSQL's reference states the rule directly: with `GRANT OPTION FOR` only the option is revoked, and otherwise both the privilege and the option are revoked. | Statement | `team_lead` can read | `team_lead` can pass on | |---|---|---| | `REVOKE GRANT OPTION FOR SELECT ON orders FROM team_lead;` | Yes | No | | `REVOKE SELECT ON orders FROM team_lead;` | No, unless granted another way | No | A common slip is to mirror the grant and write `REVOKE SELECT WITH GRANT OPTION ON orders FROM team_lead;`. The two clauses are not mirror images. The grant has `WITH GRANT OPTION` at the end, and the revoke has `GRANT OPTION FOR` at the front. In PostgreSQL the revoke form also works per privilege, so `REVOKE GRANT OPTION FOR INSERT ON orders FROM team_lead;` leaves the option on `SELECT` in place. If `team_lead` has already passed the privilege on, either statement can fail in PostgreSQL until you add `CASCADE`. That keyword is a separate clause at the end of the statement. ## Running it without the option In PostgreSQL, a role that lacks the option does not always get an error. The reference describes the behaviour: 1. If the role holds no privileges at all on the object, the `GRANT` fails outright. 2. If it holds some privilege, the command proceeds and grants only what it has grant options for. 3. It issues a warning for what it could not grant. So a script run by the wrong role can finish without an error and leave access missing. Read the warnings, and check the result afterwards. An object's owner never meets this case, because the reference says the owner is always treated as holding all grant options. ## The role-membership counterpart Role membership has the same idea under a different name. PostgreSQL's reference says that when revoking membership, `GRANT OPTION` is instead called `ADMIN OPTION`: ```sql GRANT reporting TO team_lead WITH ADMIN OPTION; REVOKE ADMIN OPTION FOR reporting FROM team_lead; ``` ## Where the spelling moves - **PostgreSQL and SQL Server** have both `WITH GRANT OPTION` and `REVOKE GRANT OPTION FOR`. - **MySQL** has `WITH GRANT OPTION`, and treats `GRANT OPTION` as a privilege of its own: `REVOKE GRANT OPTION ON shop.orders FROM 'team_lead'@'%';`. There is no `FOR`, and the option covers every privilege the account holds at that level, not only the one you named. - **Oracle** has `WITH GRANT OPTION` on object privileges but no `GRANT OPTION FOR`. You revoke the privilege and grant it again without the clause. - **SQLite** has no `GRANT` at all.
- In PostgreSQL, what happens if team_lead, who holds SELECT on orders but no grant option, runs GRANT SELECT ON orders TO bob?It does not fail outright, because team_lead holds some privilege on the object. PostgreSQL's reference says the command proceeds, grants only privileges with grant options, and warns about the rest. So nothing reaches bob and the script carries on, unless you read the warning.
- team_lead already holds plain SELECT on orders. How do you add the option without interrupting their access?Rerun the grant with the clause: `GRANT SELECT ON orders TO team_lead WITH GRANT OPTION;`. A `GRANT` adds to what is held, so no revoke is needed and access never lapses.
- How do you remove only the grant option in Oracle, which has no GRANT OPTION FOR?Revoke the privilege, then grant it again without the clause: `REVOKE SELECT ON orders FROM team_lead;` followed by `GRANT SELECT ON orders TO team_lead;`. Plan for the brief gap, and check first whether team_lead has passed the privilege on.
- Writes REVOKE SELECT WITH GRANT OPTION, mirroring the grant's clause.
- Assumes a plain REVOKE SELECT leaves the privilege and removes only the option.
- Thinks team_lead's grantee automatically receives the grant option as well.
- Expects GRANT ... TO PUBLIC WITH GRANT OPTION to work in PostgreSQL.
- Uses WITH GRANT OPTION for role membership in PostgreSQL instead of WITH ADMIN OPTION.
- Assumes REVOKE GRANT OPTION FOR runs everywhere, though Oracle has no such form.