Languages
[Edit]
EN

PostgreSQL - find all tables with specific column names

0 points
Created by:
Dirask Admin - Marvin
1820

In this article, we would like to show you how to find all tables with specific column names in PostgreSQL

Quick solution:

SELECT table_name 
FROM information_schema.columns 
WHERE column_name IN ('name','surname');

Practical example

Let's say we have a database named dirask that contains users table and admins table, both with the following columns:

  • id
  • name
  • surname
  • email

and locations table with columns:

  • id
  • address

Example 1

In this example, we will display all the tables from dirask database that contains name OR surname columns.

Query:

SELECT DISTINCT TABLE_NAME 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('name','surname')
AND TABLE_SCHEMA='dirask';

Output:

PostgreSQL - find all tables with specific column names
Results - HeidiSQL

Example 2

In this example, we will display all the tables from dirask database that contain id column.

Query:

SELECT table_name 
FROM information_schema.columns 
WHERE COLUMN_NAME = 'id';

Output:

PostgreSQL - find all tables with specific column name - result
Results - HeidiSQL
Native Advertising
ūüöÄ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

‚̧ԳŹūüíĽ ūüôā

Join