Search with system tables


Your database doesn’t just contain your data.

It also contains data about your data in system tables.

In SQL Server these are often referred to as system tables and views. They can be found in the master database, which holds data about the database. And in the system views within each database for specific information about each database.


Examples of system views

sys.objects – shows each object, type and created date

sys.indexes – shows each index and type

information_schema.columns – shows each column, it’s position and datatype


In PostgreSQL, a similar collection of tables can be found in the information_schema and PostgreSQL catalogue.

Examples of catalog objects

information_schema.tables – each object, type and created date

pg_index – shows each index and type

information_schema.columns – shows each column, it’s position and datatype


Useful scripts

To illustrate how useful these can be, and which views and tables you need, here are six scripts in SQL Server and PostgreSQL.

Count columns
Count rows
Show data types
Search for a column name
Show all tables in a schema
Show number of tables in each schema

Tested using SQL Server Standard and Aurora (Postgres)


Count columns

This query returns a list of tables, in alphabetical order, with a count of the columns. Add your schema or database name to the code and run the query.



Count rows

This query returns a list of tables, in alphabetical order, with a count of the rows. In the case of SQL Server, this column will contain the schema and table name. Add your schema or database name to the code and run the query.



Show data types

This query returns a list of tables, in alphabetical order, with their column names, data types and lengths. In the case of SQL Server, this also has a column for the schema name. Add your schema or database name to the code and run the query.



Search for a column name

This query returns a list of column names that match the search criteria in the WHERE clause. Add your schema or database name to the code and run the query.




Show all tables in a schema

This query returns a list of tables, in alphabetical order, from the schema or database requested. Add your schema or database name to the code and run the query.



Show the number of tables in each schema

This query returns a list of tables, in alphabetical order, with their last modified and created date. Add your schema or database name to the code and run the query.



Photo by Clement Nivesse from Pexels

Comments are closed, but trackbacks and pingbacks are open.