When we are working with the SQL (Structured Query Language) Server database, understanding its structure is one of the fundamental tasks which is includes knowing which tables are available. Whether you are the database administrator, a developer or an analyst being able to list the tables within the database is a crucial skill.
In this article, we will develop the various methods to accomplish this task using the SQL queries in the SQL server. In SQL Server, there are different ways to list tables within the database such as using INFORMATION_SCHEMA.TABLES View, query system catalog views, dynamic management views (DMVs).
The syntax for the querying system views to list the tables in SQL Server:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE';
Explanation:
- We use a SELECT statement to retrieve the table names from INFORMATION_SCHEMA.TABLES View.
- The WHERE clause is used to filter the results to include only those rows where the TABLE_TYPE column equals BASE TABLE, ensuring that only user-defined tables are included in the result set.
Example 1: Listing Tables in the Current Database
Step 1: Write the below code in SQL database to list tables in the current database
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE';
Output:
Serial Number
| TABLE_NAME
|
---|
1
| spt_fallback_db
|
2
| spt_fallback_dev
|
3
| spt_fallback_usg
|
4
| Empt
|
5
| spt_monitor
|
6
| MSreplication_options
|
The above output shows the list of the table names in the current database without including the system tables.
Explanation of the Code:
- SELECT statement is used to SELECT the statement to query data from INFORMATION_SCHEMA.TABLES system view.
- FROM clause is used to specify the INFORMATION_SCHEMA.TABLES view from the which you want to retrieve the data.
- WHERE clause is used to filter to include only the rows where the TABLE_TYPE column equals BASE TABLE. This will be only user-defined tables are included in the result set.
Example 2: Listing Tables in a Specific Schema
Step 1: Write the below code in SQL database to list tables in the specific schema
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo'
AND TABLE_TYPE = 'BASE TABLE';
Output:
Serial Number
| TABLE_NAME
|
---|
1
| spt_fallback_db
|
2
| spt_fallback_dev
|
3
| spt_fallback_usg
|
4
| Empt
|
5
| spt_monitor
|
6
| MSreplication_options
|
The above output shows the list of table names in the specified schema i.e. dbo without including the system tables.
Explanation of the Code:
- SELECT statement is used to SELECT the statement to query data from INFORMATION_SCHEMA.TABLES system view.
- FROM clause is used to specify the INFORMATION_SCHEMA.TABLES view from the which you want to retrieve the data.
- WHERE clause is used to filter to include only the rows where the TABLE_SCHEMA column equal to the dbo (dbo is the default schema in the SQL server ) and TABLE_TYPE column equals BASE TABLE.
Conclusion:
In conclusion, understanding the how to list tables in the SQL Server is the essential for the effectively managing the databases and performing the various data-related tasks. By the querying system views are like INFORMATION_SCHEMA.TABLES or catalog view, users can be retrieve the valuable metadata about the tables within the database including its names, types and schemas. We are using different methods in this article to retrieve the table names in the schema with detailed explanation and examples for the each approach. Whether the you are database administrator, or a developer, or an analyst mastering the above techniques you can navigate the database schemas analyse data structures and the process of streamline database management.
Similar Reads
SQL Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.In this S
11 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
ACID Properties in DBMS In the world of DBMS, transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliability. This is where the ACID prop
8 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read