0
0
PostgreSQLquery~20 mins

Login vs group roles in PostgreSQL - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Role Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Difference between Login and Group Roles in PostgreSQL
Which statement correctly describes the difference between a login role and a group role in PostgreSQL?
AA login role can connect to the database, while a group role cannot connect but can own privileges and be a member of other roles.
BA group role can connect to the database, while a login role cannot connect but can own privileges.
CBoth login and group roles can connect to the database, but only login roles can own tables.
DLogin roles are used only for authentication, and group roles are used only for schema ownership.
Attempts:
2 left
💡 Hint
Think about which roles have the ability to log in and which are used for grouping privileges.
query_result
intermediate
2:00remaining
Query to List All Login Roles
Given the following query, what will be the output?
PostgreSQL
SELECT rolname FROM pg_roles WHERE rolcanlogin = true ORDER BY rolname;
AA list of all role names that have login privileges, sorted alphabetically.
BA list of all group roles that cannot login, sorted alphabetically.
CAn error because rolcanlogin is not a valid column.
DA list of all roles regardless of login capability.
Attempts:
2 left
💡 Hint
Check the meaning of rolcanlogin column in pg_roles.
📝 Syntax
advanced
2:00remaining
Create a Group Role and Assign Login Roles
Which SQL command sequence correctly creates a group role named 'dev_team' and assigns two existing login roles 'alice' and 'bob' as members?
ACREATE ROLE dev_team NOLOGIN; GRANT dev_team TO alice, bob;
BCREATE ROLE dev_team; GRANT alice, bob TO dev_team;
CCREATE ROLE dev_team NOLOGIN; GRANT alice, bob TO dev_team;
DCREATE ROLE dev_team LOGIN; GRANT dev_team TO alice, bob;
Attempts:
2 left
💡 Hint
Remember group roles should not have LOGIN and members are granted to the group role.
optimization
advanced
2:00remaining
Efficient Role Membership Check
You want to efficiently check if a login role 'charlie' is a member of the group role 'managers'. Which query is the most efficient and correct?
ASELECT * FROM pg_roles WHERE rolname = 'charlie' AND rolname IN (SELECT rolname FROM pg_roles WHERE rolname = 'managers');
BSELECT 1 FROM pg_auth_members m JOIN pg_roles r ON m.roleid = r.oid JOIN pg_roles u ON m.member = u.oid WHERE r.rolname = 'managers' AND u.rolname = 'charlie';
CSELECT * FROM pg_auth_members WHERE roleid = (SELECT oid FROM pg_roles WHERE rolname = 'charlie') AND member = (SELECT oid FROM pg_roles WHERE rolname = 'managers');
DSELECT * FROM pg_roles WHERE rolname = 'managers' AND rolcanlogin = true;
Attempts:
2 left
💡 Hint
Check the join conditions and role/member columns in pg_auth_members.
🔧 Debug
expert
3:00remaining
Why Does This Role Membership Query Fail?
Consider this query to check if 'dave' is a member of 'admins': SELECT * FROM pg_auth_members WHERE roleid = (SELECT oid FROM pg_roles WHERE rolname = 'dave') AND member = (SELECT oid FROM pg_roles WHERE rolname = 'admins'); Why does this query return no rows even if 'dave' is a member of 'admins'?
PostgreSQL
SELECT * FROM pg_auth_members WHERE roleid = (SELECT oid FROM pg_roles WHERE rolname = 'dave') AND member = (SELECT oid FROM pg_roles WHERE rolname = 'admins');
ABecause the query syntax is invalid and causes a runtime error.
BBecause 'dave' is not a login role and cannot be a member of any group role.
CBecause pg_auth_members only stores direct memberships, and 'dave' is an indirect member.
DBecause roleid should be the group role 'admins' and member should be the login role 'dave', but the query reverses them.
Attempts:
2 left
💡 Hint
Check the meaning of roleid and member columns in pg_auth_members.