SQL GRANT / REVOKE (Permissions)

Article Summary

The GRANT and REVOKE statements are used to manage user access and privileges in SQL. These ensure users have only the permissions they need β€” nothing more. πŸ” Why Use GRANT / REVOKE? To control who can do what in the database: πŸ”Ή GRANT – Give Permissions You can also grant privileges with WITH GRANT […]

The GRANT and REVOKE statements are used to manage user access and privileges in SQL. These ensure users have only the permissions they need β€” nothing more.

πŸ” Why Use GRANT / REVOKE?

To control who can do what in the database:

  • Read data (SELECT)
  • Modify data (INSERT, UPDATE, DELETE)
  • Execute procedures
  • Manage tables or users

πŸ”Ή GRANT – Give Permissions

-- Syntax
GRANT privilege [, ...] ON object TO user;

-- Example: Allow user to select and insert on employees table
GRANT SELECT, INSERT ON employees TO john;

You can also grant privileges with WITH GRANT OPTION to allow users to grant to others:

πŸ”Ή REVOKE – Remove Permissions

GRANT SELECT ON employees TO john WITH GRANT OPTION;
-- Syntax
REVOKE privilege [, ...] ON object FROM user;

-- Example: Remove insert permission
REVOKE INSERT ON employees FROM john;

This ensures the user can no longer perform those operations.

πŸ”Ή Common Privileges

PrivilegeAction Allowed
SELECTRead rows
INSERTAdd new rows
UPDATEModify existing rows
DELETERemove rows
EXECUTERun stored procedures/functions
ALLGrants all applicable rights

🧠 Quick Recap

Key PointDescription
GRANTAssigns privileges to a user
REVOKERemoves previously granted privileges
ObjectsTables, views, procedures, etc.
Best PracticeGrant minimum permissions needed

πŸ’‘ Always apply the principle of least privilege for better database security.

Was this helpful?