Oracle SQL Query for listing all Schemas in a DB

OraclePlsql

Oracle Problem Overview


I wanted to delete some unused schemas on our oracle DB.

How can I query for all schema names ?

Oracle Solutions


Solution 1 - Oracle

Using sqlplus

sqlplus / as sysdba

run:

SELECT *
FROM dba_users

Should you only want the usernames do the following:

SELECT username
FROM dba_users

Solution 2 - Oracle

Most likely, you want

SELECT username
  FROM dba_users

That will show you all the users in the system (and thus all the potential schemas). If your definition of "schema" allows for a schema to be empty, that's what you want. However, there can be a semantic distinction where people only want to call something a schema if it actually owns at least one object so that the hundreds of user accounts that will never own any objects are excluded. In that case

SELECT username
  FROM dba_users u
 WHERE EXISTS (
    SELECT 1
      FROM dba_objects o
     WHERE o.owner = u.username )

Assuming that whoever created the schemas was sensible about assigning default tablespaces and assuming that you are not interested in schemas that Oracle has delivered, you can filter out those schemas by adding predicates on the default_tablespace, i.e.

SELECT username
  FROM dba_users
 WHERE default_tablespace not in ('SYSTEM','SYSAUX')

or

SELECT username
  FROM dba_users u
 WHERE EXISTS (
    SELECT 1
      FROM dba_objects o
     WHERE o.owner = u.username )
   AND default_tablespace not in ('SYSTEM','SYSAUX')

It is not terribly uncommon to come across a system where someone has incorrectly given a non-system user a default_tablespace of SYSTEM, though, so be certain that the assumptions hold before trying to filter out the Oracle-delivered schemas this way.

Solution 3 - Oracle

SELECT username FROM all_users ORDER BY username;

Solution 4 - Oracle

select distinct owner 
from dba_segments
where owner in (select username from dba_users where default_tablespace not in ('SYSTEM','SYSAUX'));

Solution 5 - Oracle

How about :

SQL> select * from all_users;

it will return list of all users/schemas, their ID's and date created in DB :

USERNAME                          USER_ID CREATED
------------------------------ ---------- ---------
SCHEMA1                         120 09-SEP-15
SCHEMA2                         119 09-SEP-15
SCHEMA3                         118 09-SEP-15

Solution 6 - Oracle

Below sql lists all the schema in oracle that are created after installation ORACLE_MAINTAINED='N' is the filter. This column is new in 12c.

select distinct username,ORACLE_MAINTAINED from dba_users where ORACLE_MAINTAINED='N';

Solution 7 - Oracle

Either of the following SQL will return all schema in Oracle DB.

  1. select owner FROM all_tables group by owner;
  2. select distinct owner FROM all_tables;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvicszView Question on Stackoverflow
Solution 1 - Oraclea_horse_with_no_nameView Answer on Stackoverflow
Solution 2 - OracleJustin CaveView Answer on Stackoverflow
Solution 3 - OraclesuhpranoView Answer on Stackoverflow
Solution 4 - OracleFeRtollView Answer on Stackoverflow
Solution 5 - Oraclemauek unakView Answer on Stackoverflow
Solution 6 - OraclesidnakoppaView Answer on Stackoverflow
Solution 7 - OracleSreejuView Answer on Stackoverflow