I've got a database with about 100 tables and I need to build a join query to get specific data from two of them. I know one but not the other. Basically I need something like:

select <tables> from <database> where exists table.column name;

How can I do this?

  • 6
    My answer assumed SQL Server. Is that the RDBMS you're working with? – Thomas Stringer Jun 18 '13 at 19:05
up vote 19 down vote accepted

Using information_schema

This is the standards-compliant cross-RDBMS way to do it.

SELECT table_catalog, table_schema, table_name, column_name
FROM INFORMATION_SCHEMA.columns
WHERE column_name = '<your column name>';

You can see this documented

  • 5
    Why not to use INFORMATION_SCHEMA, by our very own @AaronBertrand (sqlblog.com/blogs/aaron_bertrand/archive/2011/11/03/…) – Thomas Stringer Jun 18 '13 at 19:55
  • 2
    @ThomasStringer I agree but the OP has not tagged the question with any dbms tag. – ypercubeᵀᴹ Jun 19 '13 at 9:05
  • @ThomasStringer Use a real database, right off the bat PostgreSQL has supported Identity Columns in the information_schema.columns since 2006. A full decade before it even implemented them. If Microsoft SQL doesn't support the standard, perhaps we should be looking at nagging them. Others do. – Evan Carroll Oct 23 '17 at 16:51

For IBM DB2 you would use the following:

select tabschema,tabname from syscat.columns where colname = 'COLUMN_NAME'

Note that in DB2, column names will be in upper case unless they were defined inside of double quotes with something other than upper case. Then you have to supply the exact casing of the column name as well.

  • 1
    If the column where defined using quotes (which should be avoided), you can use UPPER or UCASE function to transform the colname to upper case: WHERE UPPER(colname) = 'COLUMN_NAME'. – Lennart Oct 23 '17 at 16:44

The below query should give you what you're looking for:

use YourDatabase;
go

select
    object_schema_name(t.object_id) + '.' + t.name as table_name,
    c.name as column_name
from sys.tables t
inner join sys.columns c
on t.object_id = c.object_id
where c.name like '%ColumnSearchText%';

If you're looking for columns of an exact name, just replace the WHERE clause with:

where c.name = 'ColumnSearchText';

in Teradata 15:

SELECT DATABASENAME||'.'||TABLENAME AS FULL_TABLENAME,
COUNT(1) AS NUMBER_OF_COLUMNS
FROM DBC.COLUMNSV
WHERE 1 = 1
AND COLUMNNAME LIKE '%<YOUR COLUMNNAME HERE>%'
GROUP BY 1

Oracle sql / plsql:

select table_name from all_tab_columns where column_name='yourcolumnname';

//Select the Particular table:

SYNTAX:
       SELECT COLUMN_NAME FROM TABLE_NAME WHERE COLUMN_NAME='VALUE';
EXAMPLE:
       SELECT PERSON_NAME FROM PERSON WHERE PERSON_ID=1;
  • You probably misread the OP's question, your answer does not answer the question that was asked. – Lennart May 26 '16 at 16:25

For SQL Server:

SELECT name 
FROM sysobjects 
WHERE id IN 
( 
    SELECT id 
    FROM syscolumns 
    WHERE name = 'EXACT_COLUMN_NAME_TO_SEARCH'
)

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.