
- SQL Tutorial
- SQL - Home
- SQL - Roadmap
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL - Comments
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Cheatsheet
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL - Show Databases
Many a times you face a situation where you need to list down all the available databases. MySQL provides a convenient way to list down all the databases using SHOW DATABASES command where as there is no particular command in MS SQL Server to show or list the databases but, you can use the SELECT...FROM command as a work around list down available databases.
List Databases using SQL
The SQL SHOW DATABASES statement is used to list down all the available databases in MySQL database. You can use SHOW SCHEMAS as an alternate command for SHOW DATABASES.
Syntax
Following is the syntax of SQL SHOW DATABASES to list down all the available databases in MySQL −
SHOW DATABASES [LIKE 'pattern' | WHERE expr] ;
We can use LIKE or WHERE clause along with SHOW DATABASES to filter out a list of databases.
Example
Following is an example to list down all the available databases.
SHOW DATABASES;
The output will be displayed as follows. This output depends on the number of databases available in the system −
Database |
---|
performance_schema |
information_schema |
mysql |
testDB |
Following is an example to list down all the databases whose name starts with test.
SHOW DATABASES LIKE 'test%';
The output will be displayed as follows −
Database (test%) |
---|
testDB |
The SHOW SCHEMAS Statement
You can use the SHOW SCHEMAS statement as an alternate for the SHOW DATABASES statement.
Syntax
Following is the syntax of the SQL SHOW SCHEMAS statement to list down all the available databases in MySQL −
SHOW SCHEMAS [LIKE 'pattern' | WHERE expr] ;
We can use LIKE or WHERE clause along with SHOW SCHEMAS to filter out a list of databases.
Example
Following is an example to list down all the available databases.
SHOW SCHEMAS;
The output will be displayed as follows. This output depends on the number of databases available in the system −
Database |
---|
performance_schema |
information_schema |
mysql |
testDB |
Following is an example to list down all the databases whose name starts with test.
SHOW SCHEMAS LIKE 'test%';
The output will be displayed as follows −
Database (test%) |
---|
testDB |
The SELECT...FROM Statement
If you are working with MS SQL Server then you can use the SELECT...FROM statement to list down all the available databases as shown below.
SQL> SELECT * FROM sys.databases;
Output
If we execute the above query, it returns a table that lists down all the databases and associated information about the databases.
name | database_id | source_database_id | owner_sid |
---|---|---|---|
master | 1 | NULL | 001 |
tempdb | 2 | NULL | 001 |
model | 3 | NULL | 001 |
msdb | 4 | NULL | 001 |
testDB | 5 | NULL | 001000 |
The EXEC sp_databases Statement
If you are using MS SQL Server then you can use the following EXEC sp_databases statement to list down all the databases −
SQL> EXEC sp_databases;
Output
This will display the same result as we got from SELECT...FROM statement.
name | database_id | source_database_id | owner_sid |
---|---|---|---|
master | 1 | NULL | 001 |
tempdb | 2 | NULL | 001 |
model | 3 | NULL | 001 |
msdb | 4 | NULL | 001 |
testDB | 5 | NULL | 001000 |