Explain Codes LogoExplain Codes Logo

How to find the users list in oracle 11g db?

sql
database-security
sql-queries
oracle-11g
Anton ShumikhinbyAnton Shumikhin·Dec 28, 2024
TLDR

Retrieve all accessible Oracle 11g database users with:

/* "WELCOME! Enjoy our alphabetical order list of users. Brought to you by the letters S, Q, and L." */ SELECT username FROM all_users ORDER BY username;

Here, all_users gives a list of usernames that the current user can access, sorted alphabetically. For the full list (given you have admin rights), switch all_users with dba_users.

Working with User Views in Oracle

Oracle allows different views for you to access user information efficiently:

  • DBA_USERS is ideally suited for DBAs, providing extensive user information, but be warned, it requires privileged access.
  • ALL_USERS delivers the list of all users, that is the current user has privileges to view.
  • USER_USERS offers information about the person currently logged in only.

Keep in mind there's a subtle yet crucial difference between username and user_id. Username is what we humans use, while user_id is for the internal workings of the database.

Right Privileges, Right Results

To query DBA_USERS, you need the SELECT ANY DICTIONARY privilege or have been granted SELECT rights on the DBA_USERS view directly by the system administrator.

/* "The SQL time machine, taking you back to the day each user was created. Duck! Low timestamp ahead!" */ SELECT username FROM dba_users ORDER BY created;

This grants you the full list of users along their creation dates, making user account auditing a walk in the park.

Top Tips for SQL Security

When dealing with user data, always position security as your top priority. Refrain from using SELECT * that exposes all user data and specify only the necessary data columns to request. This offers less exposure, which aligns better with data privacy practices.

Beware! Database Systems Vary

Remember, what Oracle calls a user, MySQL refers to as a schema. Knowing the differences can prevent misunderstandings when switching between different database systems.

A Sorted List is a Sorted Life

Sorting your returned results can bring clarity. To sort by account status or expiry date:

/* "The Sorting Hat has moved on to bigger challenges. 'Locked' or 'open' accounts! Let's find out." */ SELECT username, account_status, expiry_date FROM dba_users ORDER BY account_status, expiry_date;

This query brings to light accounts that are locked or about to expire — highly useful for routine database maintenance.