SQL REVOKE Statement
REVOKE is the opposite of GRANT: it takes privileges away from an account or role. You need it when someone changes team, when an application no longer needs write access, or after an audit finds an account with too much power. The privileges are removed immediately.
- Revoke individual privileges and all privileges
- Revoke roles and the grant option
- Verify with
SHOW GRANTS - Avoid the “revoke at a different level” trap
REVOKE syntax
REVOKE privilege [, privilege ...]ON levelFROM 'user'@'host';The level must match how the privilege was granted (company.*, company.orders, …).
Revoke one privilege
The shop application should no longer delete rows (it uses soft deletes now):
CREATE USER 'shop_app'@'localhost' IDENTIFIED BY 'Sh0p#App2026';GRANT SELECT, INSERT, UPDATE, DELETE ON company.* TO 'shop_app'@'localhost';REVOKE DELETE ON company.* FROM 'shop_app'@'localhost';SHOW GRANTS FOR 'shop_app'@'localhost';| Grants for shop_app@localhost |
|---|
| GRANT USAGE ON *.* TO `shop_app`@`localhost` |
| GRANT SELECT, INSERT, UPDATE ON `company`.* TO `shop_app`@`localhost` |
- The account starts with four privileges on
company.*. REVOKE DELETEremoves one;SHOW GRANTSnow listsSELECT, INSERT, UPDATE.- The change applies to new statements immediately, even in sessions that are already connected.
Revoke everything
CREATE USER 'contractor'@'localhost' IDENTIFIED BY 'C0ntract#2026';GRANT SELECT, INSERT ON company.* TO 'contractor'@'localhost';GRANT SELECT ON company.employees TO 'contractor'@'localhost';REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'contractor'@'localhost';SHOW GRANTS FOR 'contractor'@'localhost';| Grants for contractor@localhost |
|---|
| GRANT USAGE ON *.* TO `contractor`@`localhost` |
- The contractor had privileges at two levels: database and table.
REVOKE ALL PRIVILEGES, GRANT OPTION FROM ...(withoutON) removes them at every level.- Only
USAGEremains: the account can log in but do nothing. To remove the account completely,DROP USER.
The level trap
A privilege granted at database level cannot be revoked for one table:
CREATE USER 'analyst1'@'localhost' IDENTIFIED BY 'An@lyst2026';GRANT SELECT ON company.* TO 'analyst1'@'localhost';REVOKE SELECT ON company.employees FROM 'analyst1'@'localhost';ERROR 1147 (42000): There is no such grant defined for user 'analyst1' on host 'localhost' on table 'employees'
SELECTwas granted oncompany.*, not oncompany.employees.- There is no table-level grant to remove, so MySQL reports error 1147.
- To give “all tables except employees”, revoke the database grant and grant
SELECTtable by table (or through views).
Revoke a role
CREATE ROLE 'analyst';GRANT SELECT ON company.* TO 'analyst';CREATE USER 'priya'@'localhost' IDENTIFIED BY 'Pr1ya#2026';GRANT 'analyst' TO 'priya'@'localhost';REVOKE 'analyst' FROM 'priya'@'localhost';SHOW GRANTS FOR 'priya'@'localhost';| Grants for priya@localhost |
|---|
| GRANT USAGE ON *.* TO `priya`@`localhost` |
Changing the role itself (REVOKE INSERT ON company.* FROM 'developer';) changes the access of every account that has the role.
Revoke the grant option
CREATE USER 'team_lead'@'localhost' IDENTIFIED BY 'L3ad#2026';GRANT SELECT ON company.* TO 'team_lead'@'localhost' WITH GRANT OPTION;REVOKE GRANT OPTION ON company.* FROM 'team_lead'@'localhost';SHOW GRANTS FOR 'team_lead'@'localhost';| Grants for team_lead@localhost |
|---|
| GRANT USAGE ON *.* TO `team_lead`@`localhost` |
| GRANT SELECT ON `company`.* TO `team_lead`@`localhost` |
The team lead keeps SELECT but can no longer give it to others.
Quick quiz
1. How do you remove all privileges of an account at every level?
The form without ON covers all levels.
2. SELECT was granted ON company.*. What happens with REVOKE SELECT ON company.orders?
Revoke at the same level the privilege was granted.
3. Does a REVOKE affect sessions that are already connected?
Table and database privileges are checked on every statement.
REVOKE FAQ
What is the difference between GRANT and REVOKE?
GRANT gives privileges to an account or role; REVOKE removes them.
How do I remove all privileges from a MySQL user?
REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'user'@'host'; The account keeps only USAGE. Use DROP USER to delete it.
Why does REVOKE give error 1141 or 1147?
The privilege was not granted at the level you named. Check with SHOW GRANTS and revoke at the same level (db.*, db.table or global).