cancel
Showing results for 
Search instead for 
Did you mean: 
Get Started Discussions
Start your journey with Databricks by joining discussions on getting started guides, tutorials, and introductory topics. Connect with beginners and experts alike to kickstart your Databricks experience.
cancel
Showing results for 
Search instead for 
Did you mean: 

How list all USERs FROM a especific GROUP USING SQL?

Sardenberg
New Contributor II

I want to list, using sql editor, all users name from a specific group.
Reading documentation, I only learned how to show the groups or the users, using simples filters, like:

SHOW GROUPS LIKE '*XPTO*';
SHOW GROUPS WITH USER `test@gmail.com`
SHOW USERS LIKE '*gus*'

I would like to create something like:
select users_name from USERS where group_name = "XPTO_GROUP";
or
SHOW USERS WHERE GROUP "xpto_group";

2 REPLIES 2

Kaniz
Community Manager
Community Manager

Hi @Sardenberg,  To retrieve a list of users from a specific group using SQL, you can follow these steps:

 

Assumptions:

  • Let’s assume you have three tables: USERS, GROUPS, and GROUP_USERS.
  • The USERS table contains user information.
  • The GROUPS table contains group information.
  • The GROUP_USERS table establishes the relationship between users and groups.

SQL Query: You can use the following query to get a list of users who belong to a specific group and include all the groups they are members of:

SELECT u.*, GROUP_CONCAT(g.name) AS groups FROM USERS u JOIN GROUP_USERS gu ON gu.USER_ID = u.ID JOIN GROUPS g ON g.ID = gu.GROUP_ID WHERE g.name = 'XPTO_GROUP' GROUP BY u.ID;

Explanation:

  • We join the USERS, GROUP_USERS, and GROUPS tables based on their respective IDs.
  • The GROUP_CONCAT(g.name) function aggregates the group names for each user.
  • The WHERE clause filters the results to include only users from the specified group.
  • The GROUP BY ensures that each user appears only once in the result set.

Customization:

  • Replace 'XPTO_GROUP' with the actual name of the group you want to query.

This query will provide you with a list of users from the specified group along with all the other groups they belong to. 📊👥

Sardenberg
New Contributor II

This solution seems to have been answered by an AI.
I need to know how create a SELECT for create this tables: USERS, GROUPS and GROUP_USERS.