SQL GRANT / REVOKE (Permissions)

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.