📖 Scenario: You are working as a junior database administrator for a small company. Your manager wants you to understand why SQL security is important to protect sensitive company data from unauthorized access and misuse.
🎯 Goal: Build a simple SQL example that shows how to create a user table, add a user with a password, and then demonstrate how to restrict access to this sensitive data using SQL commands.
📋 What You'll Learn
Create a table called users with columns id, username, and password
Insert one user with username 'admin' and password 'secure123'
Create a role called read_only that can only select data from the users table
Grant the read_only role SELECT permission on the users table
💡 Why This Matters
🌍 Real World
Companies use SQL security to protect sensitive data like user passwords and personal information from unauthorized access.
💼 Career
Database administrators and developers must understand SQL security to keep data safe and comply with privacy laws.
Progress0 / 4 steps
1
Create the users table
Write a SQL statement to create a table called users with three columns: id as an integer primary key, username as a text field, and password as a text field.
SQL
Hint
Use CREATE TABLE followed by the table name and define each column with its data type. Mark id as the primary key.
2
Insert a user into the users table
Write a SQL statement to insert a user with id 1, username 'admin', and password 'secure123' into the users table.
SQL
Hint
Use INSERT INTO with the column names and VALUES to add the user data.
3
Create a read_only role
Write a SQL statement to create a role called read_only that will be used to restrict access to the users table.
SQL
Hint
Use CREATE ROLE followed by the role name to create a new role.
4
Grant SELECT permission to the read_only role
Write a SQL statement to grant SELECT permission on the users table to the read_only role.
SQL
Hint
Use GRANT SELECT ON followed by the table name and TO the role name.
Practice
(1/5)
1. Why is it important to be aware of SQL security when managing a database?
easy
A. To protect sensitive data from unauthorized access
B. To make queries run faster
C. To reduce the size of the database
D. To increase the number of users who can access the database
Solution
Step 1: Understand the purpose of SQL security
SQL security is designed to protect data from unauthorized users who might misuse or steal it.
Step 2: Identify the correct reason for security awareness
Protecting sensitive data is the main goal, not improving speed or increasing users.
Final Answer:
To protect sensitive data from unauthorized access -> Option A
Quick Check:
SQL security = protect data [OK]
Hint: Security means protecting data from wrong users [OK]
Common Mistakes:
Confusing security with performance
Thinking security increases user count
Believing security reduces database size
2. Which SQL command is used to give a user permission to access a database object?
easy
A. REVOKE
B. GRANT
C. DELETE
D. SELECT
Solution
Step 1: Recall SQL commands for permissions
GRANT is used to give permissions, REVOKE removes them.
Step 2: Identify the command that gives access
GRANT allows users to access or modify database objects.
Final Answer:
GRANT -> Option B
Quick Check:
GRANT = give permission [OK]
Hint: GRANT gives permission, REVOKE takes it away [OK]
Common Mistakes:
Confusing GRANT with REVOKE
Using DELETE or SELECT as permission commands
Thinking SELECT grants permissions
3. Consider this SQL command: REVOKE UPDATE ON employees FROM user_john; What is the effect of this command?
medium
A. User John is granted update permission on employees
B. User John can update the employees table
C. User John is deleted from the database
D. User John can no longer update the employees table
Solution
Step 1: Understand the REVOKE command
REVOKE removes permissions previously given to a user.
Step 2: Apply to the given command
This command removes the UPDATE permission on the employees table from user_john.
Final Answer:
User John can no longer update the employees table -> Option D
Quick Check:
REVOKE UPDATE = remove update rights [OK]
Hint: REVOKE removes permission, so user loses update rights [OK]
Common Mistakes:
Thinking REVOKE grants permission
Confusing REVOKE with DELETE user
Assuming REVOKE affects all permissions
4. You run this SQL command: GRANT SELECT, INSERT employees TO user_anna; But it returns an error. What is the likely problem?
medium
A. The keyword TO is missing
B. The table name is incorrect
C. The keyword ON is missing before the table name
D. User_anna does not exist
Solution
Step 1: Check correct GRANT syntax
The correct syntax is: GRANT privileges ON table TO user;
Step 2: Identify missing keyword
The command misses ON before the table name employees.
Final Answer:
The keyword ON is missing before the table name -> Option C
Quick Check:
GRANT syntax requires ON before table [OK]
Hint: GRANT needs ON before table name [OK]
Common Mistakes:
Omitting ON keyword
Confusing TO placement
Assuming user existence causes syntax error
5. A database admin wants to ensure that a user can only read data but not change it. Which SQL command should they use?
hard
A. GRANT SELECT ON database TO user_name;
B. GRANT UPDATE ON database TO user_name;
C. REVOKE SELECT ON database FROM user_name;
D. GRANT DELETE ON database TO user_name;
Solution
Step 1: Identify read-only permission
SELECT permission allows reading data without changing it.
Step 2: Choose the command granting only read access
GRANT SELECT ON database TO user_name; gives read-only access.
Final Answer:
GRANT SELECT ON database TO user_name; -> Option A
Quick Check:
SELECT = read-only permission [OK]
Hint: Use GRANT SELECT for read-only access [OK]
Common Mistakes:
Granting UPDATE or DELETE instead of SELECT
Revoking SELECT instead of granting
Granting all permissions accidentally
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.