Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle SQL how to find out which table has the following two columns?

Tags:

sql

oracle

I know of a statement that I can issue to find out the table with the column I am looking for:

SELECT DISTINCT(table_name)
  FROM all_tab_cols 
 WHERE column_name = 'EMP_ID';

However, how should I modify this so that I may find the table that has both say EMP_ID and EMP_NAME?

like image 831
Oh Chin Boon Avatar asked Jul 12 '11 01:07

Oh Chin Boon


People also ask

How can I compare two columns in a table in Oracle?

Compare columns in two tables and list out column names which are different. for ex:- create table t1(c1 number(2), c2 varchar2(10)); create table t2(c1 number(2), c2 varchar2(10)); insert into t1 values(1,'a'); insert into t2 values(1,'b'); result should be column c2 is different.

How do I compare two columns in the same table in SQL?

In SQL, problems require us to compare two columns for equality to achieve certain desired results. This can be achieved through the use of the =(equal to) operator between 2 columns names to be compared. For this article, we will be using the Microsoft SQL Server as our database.

How do I find a specific table in Oracle?

You can narrow down your search by providing the owner of the table. The owner is the schema in which the table resides. select owner, table_name from all_tables where table_name='table%' and owner='MYOWNER'; Alternatively you can log into that particular schema and query user_tables.

How do I view two columns in a table in SQL?

To select multiple columns from a table, simply separate the column names with commas! For example, this query selects two columns, name and birthdate , from the people table: SELECT name, birthdate FROM people; Sometimes, you may want to select all columns from a table.


1 Answers

Use:

  SELECT table_name
    FROM all_tab_cols 
   WHERE column_name IN ('EMP_ID', 'EMP_NAME')
GROUP BY table_name
  HAVING COUNT(DISTINCT column_name) = 2

The count comparison must equal the number of parameters defined in the IN clause.

...but this is a safer means of determining the table in case their are duplicates:

  SELECT table_name
    FROM all_tab_cols 
   WHERE column_name IN ('EMP_ID', 'EMP_NAME')
GROUP BY table_name, owner
  HAVING COUNT(column_name) = 2
like image 126
OMG Ponies Avatar answered Nov 15 '22 03:11

OMG Ponies