What is the purpose of the GRANT statement in MySQL?
The GRANT statement is used to give users specific permissions to perform actions on databases, tables, or other objects in MySQL.
Click to reveal answer
beginner
How do you grant SELECT privilege on a database named 'shop' to a user 'alice'?
Use: GRANT SELECT ON shop.* TO 'alice'@'localhost';
Click to reveal answer
intermediate
What does the ALL PRIVILEGES keyword mean in a GRANT statement?
ALL PRIVILEGES means the user gets all available permissions on the specified database or table, like SELECT, INSERT, UPDATE, DELETE, etc.
Click to reveal answer
intermediate
Why is it important to specify the host when granting privileges in MySQL?
Because MySQL users are identified by both username and host, specifying the host controls where the user can connect from, improving security.
Click to reveal answer
beginner
What command should you run after granting privileges to make sure they take effect immediately?
Run: FLUSH PRIVILEGES; to reload the privilege tables and apply changes immediately.
Click to reveal answer
Which command grants a user 'bob' permission to insert data into the 'employees' table?
AGRANT INSERT ON employees TO 'bob'@'localhost';
BGRANT INSERT ON database.* TO 'bob'@'localhost';
CGRANT INSERT ON employees.* TO 'bob'@'localhost';
DGRANT INSERT ON database.employees TO 'bob'@'localhost';
✗ Incorrect
Privileges are granted on databases or tables using the format database.table. To grant INSERT on the employees table, use database.employees. Option D correctly grants INSERT on the employees table.
What does this command do? GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY 'pass';
ARevokes all privileges from 'admin'.
BGives 'admin' all permissions on all databases from any host.
CCreates a new database named 'admin'.
DGives 'admin' only SELECT permission on all databases.
✗ Incorrect
The command grants all privileges on all databases and tables (*.*) to user 'admin' connecting from any host ('%').
Why might you use 'GRANT SELECT ON mydb.* TO 'user'@'localhost';' instead of 'GRANT SELECT ON mydb.* TO 'user'@'%';'?
ATo allow the user to connect only from the local machine for security.
BTo allow the user to connect from any machine.
CTo grant the user all privileges.
DTo revoke privileges from the user.
✗ Incorrect
Specifying 'localhost' restricts the user to connect only from the local machine, which is more secure than allowing connections from any host ('%').
What is the effect of running 'FLUSH PRIVILEGES;' after a GRANT statement?
AIt backs up the privilege tables.
BIt deletes all user privileges.
CIt reloads the privilege tables so changes take effect immediately.
DIt restarts the MySQL server.
✗ Incorrect
FLUSH PRIVILEGES reloads the grant tables in memory so that privilege changes apply without restarting the server.
Which of these is NOT a valid privilege you can grant in MySQL?
AFLY
BINSERT
CSELECT
DUPDATE
✗ Incorrect
'FLY' is not a valid MySQL privilege. SELECT, INSERT, and UPDATE are valid privileges.
Explain how to grant a user permission to read data from a specific database in MySQL.
Think about the syntax: GRANT SELECT ON database.* TO 'user'@'host';
You got /4 concepts.
Describe why specifying the host is important when granting privileges in MySQL.
MySQL treats 'user'@'localhost' and 'user'@'%' as different users.
You got /3 concepts.
Practice
(1/5)
1. What does the GRANT SELECT ON database.* TO 'user'@'host'; command do in MySQL?
easy
A. Allows the user to read data from all tables in the specified database.
B. Allows the user to modify data in all tables in the specified database.
C. Creates a new user with read access to the database.
D. Deletes all data from the specified database.
Solution
Step 1: Understand the GRANT command
The GRANT command is used to give specific permissions to a user on a database or table.
Step 2: Analyze the SELECT privilege
SELECT privilege allows reading data but not modifying it.
Final Answer:
Allows the user to read data from all tables in the specified database. -> Option A
Quick Check:
GRANT SELECT = read permission [OK]
Hint: SELECT privilege means read-only access [OK]
Common Mistakes:
Confusing SELECT with UPDATE or DELETE privileges
Thinking GRANT creates a user
Assuming it deletes data
2. Which of the following is the correct syntax to grant all privileges on the shop database to user 'alice' connecting from any host?
easy
A. GRANT ALL PRIVILEGES ON shop.* TO 'alice'@'%';
B. GRANT ALL PRIVILEGES ON shop TO 'alice'@'%';
C. GRANT ALL ON shop.* TO 'alice';
D. GRANT ALL PRIVILEGES TO 'alice'@'%' ON shop.*;
Solution
Step 1: Check the database and user format
The database must be followed by .* to specify all tables, and user must include host.
Step 2: Verify the correct order of clauses
The syntax is: GRANT privileges ON database.* TO 'user'@'host';
Final Answer:
GRANT ALL PRIVILEGES ON shop.* TO 'alice'@'%'; -> Option A
Quick Check:
Correct syntax = GRANT ALL PRIVILEGES ON shop.* TO 'alice'@'%'; [OK]
Hint: Use database.* to grant on all tables [OK]
Common Mistakes:
Omitting .* after database name
Placing TO clause incorrectly
Not specifying host with user
3. Given the commands:
GRANT SELECT, INSERT ON sales.* TO 'bob'@'localhost';
SHOW GRANTS FOR 'bob'@'localhost';
What will the SHOW GRANTS output include?
medium
A. GRANT ALL PRIVILEGES ON sales.* TO 'bob'@'localhost'
B. GRANT SELECT ON sales.* TO 'bob'@'localhost'
C. GRANT SELECT, INSERT ON sales.* TO 'bob'@'localhost'
D. No privileges granted
Solution
Step 1: Understand the GRANT command given
The user 'bob' is granted SELECT and INSERT privileges on all tables in the sales database.
Step 2: Interpret SHOW GRANTS output
SHOW GRANTS displays the exact privileges granted, so it will list SELECT and INSERT on sales.* for bob.
Final Answer:
GRANT SELECT, INSERT ON sales.* TO 'bob'@'localhost' -> Option C
Quick Check:
SHOW GRANTS matches granted privileges [OK]
Hint: SHOW GRANTS shows exact privileges granted [OK]
Common Mistakes:
Assuming ALL PRIVILEGES are granted
Ignoring INSERT privilege
Thinking SHOW GRANTS returns no rows
4. Identify the error in the following command:
GRANT UPDATE, DELETE ON * TO 'carol'@'localhost';
medium
A. Host '%' must be used instead of 'localhost'
B. User name must be in double quotes
C. Privileges UPDATE and DELETE cannot be granted together
D. Missing database or table name after ON
Solution
Step 1: Check the ON clause syntax
The ON clause requires a database or table name, e.g., database.* or database.table.
Step 2: Identify the missing part
The command uses * alone, which is invalid syntax in MySQL for ON.
Final Answer:
Missing database or table name after ON -> Option D
Quick Check:
ON must specify database or table [OK]
Hint: ON must specify database or table, not just * [OK]
Common Mistakes:
Using * alone without database name
Using wrong quotes for user
Assuming localhost is invalid host
5. You want to allow user 'dave' to create new tables only in the inventory database but not modify data. Which GRANT command is correct?
hard
A. GRANT ALL PRIVILEGES ON inventory.* TO 'dave'@'localhost';
B. GRANT CREATE ON inventory.* TO 'dave'@'localhost';
C. GRANT CREATE, INSERT ON inventory.* TO 'dave'@'localhost';
D. GRANT CREATE ON *.* TO 'dave'@'localhost';
Solution
Step 1: Identify the needed privilege
To create tables, the CREATE privilege is required; INSERT or ALL PRIVILEGES would allow data changes.
Step 2: Scope the privilege to the inventory database
Granting on inventory.* limits the permission to that database only.
Step 3: Check user and host format
User 'dave'@'localhost' is correctly specified.
Final Answer:
GRANT CREATE ON inventory.* TO 'dave'@'localhost'; -> Option B
Quick Check:
CREATE only on inventory = GRANT CREATE ON inventory.* TO 'dave'@'localhost'; [OK]
Hint: Grant only CREATE on specific database for table creation [OK]
Common Mistakes:
Granting ALL PRIVILEGES instead of just CREATE
Including INSERT which allows data changes
Granting on *.* instead of specific database
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.