Who may connect to your SQL Server, and what may each person do? This guide shows how to create a login and a database user, give and remove permissions with GRANT, DENY and REVOKE, test them with EXECUTE AS, use fixed and custom roles, and apply the rule of least privilege.
Part of the SQL Server for Beginners series. This article is the written version of lesson 11 of the Bigland Network video course, with the same sample data. The results shown were worked out by hand from that data, so run every example on a test database, not on production data.
Watch lesson 11 as a video or open it on YouTube. The written lesson below follows the same steps and the same sample data.
Contents
- 1. The security model
- 2. Create a login
- 3. Create a database user
- 4. GRANT, and test with EXECUTE AS
- 5. DENY and REVOKE
- 6. Column level permissions
- 7. Fixed roles and your own roles
- 8. Procedures, server roles and authentication
- 9. Least privilege, mistakes and best practices
- 10. Practice
1. The security model
Think of a building. The login is the key to the front door. The user is the badge for one floor. The permission says which rooms you can open on that floor.
| Term | Meaning |
|---|---|
| Login | Who may connect to the SQL Server instance |
| User | Who may enter one database, linked to a login |
| Permission | What a user may do: SELECT, INSERT, EXECUTE and so on |
| Role | A group of users that share the same permissions |
2. Create a login
CREATE LOGIN ReportLogin
WITH PASSWORD = 'Str0ng#Pass2026',
CHECK_POLICY = ON;
A login opens the door to the server, but not to any database yet. CHECK_POLICY = ON applies the password rules of the company, such as length and complexity.
3. Create a database user
USE CompanyDB;
GO
CREATE USER ReportUser
FOR LOGIN ReportLogin;
The user lives inside one database and is linked to the login. One login can have a user in many databases, with different permissions in each one.
4. GRANT, and test with EXECUTE AS
A brand new user can do almost nothing. You give each permission:
GRANT SELECT ON Employees TO ReportUser;
GRANT SELECT ON Departments TO ReportUser;
The syntax is always the same: GRANT, the permission, ON, the object, TO, the user. To test, act as that user with EXECUTE AS, then come back with REVERT:
EXECUTE AS USER = 'ReportUser';
SELECT COUNT(*) AS Employees FROM Employees;
REVERT;
| Employees |
|---|
| 6 |
Now something the user is not allowed to do:
EXECUTE AS USER = 'ReportUser';
DELETE FROM Employees WHERE Id = 6;
REVERT;
SQL Server answers with error 229: the DELETE permission was denied on the object Employees. Nothing was removed, which is exactly what we want.
5. DENY and REVOKE
DENY SELECT ON Employees TO ReportUser;
-- the user is now blocked
REVOKE SELECT ON Employees FROM ReportUser;
-- the permission is simply removed
- DENY always wins, even if the user also has a GRANT through a role.
- REVOKE cancels an earlier GRANT or DENY, and the user goes back to having nothing.
6. Column level permissions
REVOKE SELECT ON Employees FROM ReportUser;
GRANT SELECT ON Employees (Id, FirstName, DeptId)
TO ReportUser;
The user can read three columns, and Salary stays private:
EXECUTE AS USER = 'ReportUser';
SELECT FirstName, DeptId FROM Employees ORDER BY Id;
-- SELECT Salary would fail with error 230
REVERT;
| FirstName | DeptId |
|---|---|
| Amina | 1 |
| Youssef | 2 |
| Salma | 1 |
| Karim | 3 |
| Nadia | 2 |
| Omar | NULL |
7. Fixed roles and your own roles
| Fixed database role | What it allows |
|---|---|
| db_datareader | Read every table and view |
| db_datawriter | Insert, update and delete in every table |
| db_owner | Everything in the database. Be careful! |
| db_denydatawriter | Can never change data |
Fixed roles are quick but broad. For precise control, create your own role, give the permissions to the role, then add people to it:
CREATE ROLE ReportReaders;
GRANT SELECT ON Employees TO ReportReaders;
GRANT SELECT ON Departments TO ReportReaders;
ALTER ROLE ReportReaders ADD MEMBER ReportUser;
Tomorrow, when a new colleague joins, you only add them to the role. To see who is in a role:
SELECT r.name AS RoleName, m.name AS MemberName
FROM sys.database_role_members rm
JOIN sys.database_principals r
ON rm.role_principal_id = r.principal_id
JOIN sys.database_principals m
ON rm.member_principal_id = m.principal_id
WHERE m.name = 'ReportUser';
| RoleName | MemberName |
|---|---|
| ReportReaders | ReportUser |
8. Procedures, server roles and authentication
GRANT EXECUTE ON usp_GetEmployees TO ReportUser;
-- or on a whole schema:
GRANT SELECT ON SCHEMA::dbo TO ReportReaders;
Users can run the procedure without touching the tables behind it.
| Item | Notes |
|---|---|
| sysadmin | Full control of the whole server. Very few people. |
| dbcreator | Can create databases, nothing more |
| Windows authentication | Login with the company account. The best choice. |
| SQL login | Name and password stored in SQL Server |
9. Least privilege, mistakes and best practices
Least privilege means giving each person only what the job needs. A mistake or a stolen password then does less damage. Use roles, avoid db_owner and sysadmin, and review who has access at least once a year.
- Using the
saaccount for every application. - Giving
db_ownerto people who only read data. - Weak or shared passwords, or no password policy.
- Granting permissions to single users instead of roles.
- Forgetting to remove access when someone leaves.
- Best practice: use Windows authentication when you can, grant permissions to roles and add users to roles, test with
EXECUTE AS, use strong passwords withCHECK_POLICY ON, and remove unused logins and users. More detail in SQL Server Logins, Users and Permissions.
10. Practice
- Create a login AppLogin and a user AppUser. Give the user SELECT on Accounts.
- Add AppUser to db_datawriter, then DENY DELETE on Accounts.
- Create a role HRTeam with SELECT on Employees, and add AppUser to it.
CREATE LOGIN AppLogin WITH PASSWORD = 'Str0ng#App2026';
GO
CREATE USER AppUser FOR LOGIN AppLogin;
GRANT SELECT ON Accounts TO AppUser;
ALTER ROLE db_datawriter ADD MEMBER AppUser;
DENY DELETE ON Accounts TO AppUser; -- deleting gives error 229
CREATE ROLE HRTeam;
GRANT SELECT ON Employees TO HRTeam;
ALTER ROLE HRTeam ADD MEMBER AppUser;
Frequently asked questions
What is the difference between a login and a user?
A login lets someone connect to the SQL Server instance. A user gives that login access to one database. You need both.
What is the difference between DENY and REVOKE?
DENY blocks a permission and always wins over GRANT. REVOKE removes an earlier GRANT or DENY and does not block anything by itself.
Why use roles instead of granting to single users?
With a role you set the permissions once, and a new colleague only needs to be added to the role. It is easier to manage and to review.
What to learn next
The next lessons in this series cover backup and restore, and performance tuning. Meanwhile, see How to Back Up and Restore a SQL Server Database with T-SQL. Previous lesson: Lesson 10: Triggers and Audit Tables.
The whole series
- Lesson 1: SELECT, WHERE and ORDER BY
- Lesson 2: JOINs
- Lesson 3: GROUP BY, HAVING and Subqueries
- Lesson 4: INSERT, UPDATE, DELETE and Transactions
- Lesson 5: CREATE TABLE, Data Types and Constraints
- Lesson 6: Indexes, Views and Stored Procedures
- Lesson 7: CTEs and Window Functions
- Lesson 8: Built-in Functions
- Lesson 9: Functions, Temp Tables and Table Variables
- Lesson 10: Triggers and Audit Tables
- Lesson 11: Security: Logins, Users and Roles (this article)
- Lesson 12: Backup, Restore and Recovery Models
- Lesson 13: Query Performance Tuning
- Lesson 14: SQL Server Agent Jobs and Maintenance
- Lesson 15: Encryption and Auditing Basics
- Lesson 16: Working with JSON
- Lesson 17: Temporal Tables and Row History
- Lesson 18: Partitioning and Large Tables
- lesson 19: Full Text Search
- lesson 20: Query Store and Slow Queries
Sources and further reading
- Microsoft Learn, Transact-SQL reference (Database Engine), on learn.microsoft.com: the official syntax for the statements used here.
- Microsoft Learn, SQL Server documentation, for SQL Server Management Studio (SSMS) and the sample concepts.
More tutorials like this
Msg 208 Invalid Object Name in SQL Server: Causes and Fix
Fix SQL Server Msg 208 Invalid object name: check the spelling, the database and the schema, with real SSMS examples and scripts to find…
SQL Server Query Store Tutorial: Find Slow Queries, Compare Plans and Force a Plan
Turn on Query Store, find the most expensive queries with the sys.query_store views and the SSMS reports, compare the plans of one query and…
SQL Server Full Text Search Tutorial: CONTAINS, FORMSOF and CONTAINSTABLE
Search words inside text columns fast: create a full text catalog and index, use CONTAINS, word forms, phrases and rank the best matches with…
