![SQL Logo](https://www.tutorialspoint.com/sql/images/sql-mini-logo.jpg)
- SQL Tutorial
- SQL - Home
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Database
- 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 - Comments
- 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 - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
SQL - Show Database
A database is a collection of data stored and organized in a way that the data can be retrieved, inserted, and deleted. Nowadays, databases are used by most organizations to store data such as financial transactions, customer records, employee records, etc.
Database in SQL
In an SQL server, the database is a collection of tables, views, stored procedures, and other objects that are used to store and manage data.
When we install the SQL server, some databases are automatically created; hence, they are called “System Databases”. The databases which are created by the user are called “User Databases”.
System Databases − The system databases such as Master, Model, MSDB, Tempdb, etc. are created automatically when we install the MS SQL Server on our system. These system databases are crucial for the functioning of the MS SQL Server and these cannot be deleted or dropped.
User Databases − The user databases in the SQL server are created and managed by the database users.
Listing databases in SQL server
There is no particular command in SQL to show or list the databases that exist in an SQL server. So, we use the SELECT…FROM command.
The “SELECT…FROM” command is used to retrieve records from the sys.databases table. This table contains all the information about the databases in the SQL server such as name of the database, database_id, owner_sid, create_date, compatibility_level, etc.
SELECT command is one of the T-SQL commands. The Transact-SQL or T-SQL Commands in SQL are used to interact with the SQL server databases.
Syntax
Following is the syntax of the SELECT…FROM command to list the databases −
SELECT column_name, column_name1,…… FROM sys.databses;
Here, the “column_name” is the name of the columns from the sys.databases table.
Example
First of all, let us try to list the databases that are present in the SQL server using the query below −
SQL> SELECT * FROM sys.databases; +----------+-------------+--------------------+-------------+ | name | database_id | source_database_id | owner_sid | +----------+-------------+--------------------+-------------+ | master | 1 | NULL | 0×01 | | tempdb | 2 | NULL | 0×01 | | model | 3 | NULL | 0×01 | | msdb | 4 | NULL | 0×01 | +----------+-------------+--------------------+-------------+
Now, let us create some databases in the SQL server using the query below −
SQL> CREATE DATABASE testDB1; CREATE DATABASE testDB2; CREATE DATABASE testDB3;
Note − Since the above databases are created by the user, they are called "user databases".
Once the databases are created, you can list all the databases that are present in the SQL server using the query below −
SQL> SELECT * FROM sys.databases;
Output
If we execute the above query, it returns a table that lists all the databases and also the information about the databases.
+----------+-------------+--------------------+-------------+ | name | database_id | source_database_id | owner_sid | +----------+-------------+--------------------+-------------+ | master | 1 | NULL | 0×01 | | tempdb | 2 | NULL | 0×01 | | model | 3 | NULL | 0×01 | | msdb | 4 | NULL | 0×01 | | testDB1 | 5 | NULL | 0x010500…… | | testDB2 | 6 | NULL | 0x010500…… | | testDB3 | 7 | NULL | 0x010500…… | +----------+-------------+--------------------+-------------+
Stored procedure to show all the Databases
Following is the query to show all the databases that are present in the SQL server using the stored procedure −
SQL> EXEC sp_databases;
Output
After executing the above query, it returns a table that contains all the databases and also the information about the databases.
+---------------+---------------+--------+ | DATABASE_NAME | DATABASE_SIZE | REMARKS| +---------------+---------------+--------+ | master | 5376 | NULL | | model | 16384 | NULL | | masdb | 16960 | NULL | | tempdb | 16384 | NULL | | testDB1 | 16384 | NULL | | testDB2 | 16384 | NULL | | testDB3 | 16384 | NULL | +---------------+---------------+--------+
Showing selected information about the databases
The sys.databases table consists of several columns such as name, database_id, source_database_id, owner_sid, create_date, compatibility_level, etc.
Using the below query, we are trying to show only some particular columns from the sys.databases table like name and compatibility_level.
SQL> SELECT name, compatibility_level FROM sys.databases;
Output
If we execute the above query, the result is produced as follows −
+-----------+--------------------+ | name | compatibility_level| +---------------+----------------+ | master | 160 | | model | 160 | | masdb | 160 | | tempdb | 160 | | testDB1 | 160 | | testDB2 | 160 | | testDB3 | 160 | +---------------+----------------+
Showing only User-Created Databases
The databases that are created by the user on the SQL server are known as "User-created databases".
There is no particular command in SQL to retrieve only the user-created databases from the SQL server instead, we use the query below −
SQL> SELECT name FROM sys.databases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb');
Here, the master, tempdb, model, msdb are the system-created databases. So, we are eliminating them to show the user-created databases.
Output
On executing the given query, the output is displayed as follows −
+-------+ | name | +-------+ |testDB1| |testDB2| |testDB3| +-------+
Showing only System-Created Databases
The system databases are automatically created at the time of the SQL server installation.
To show only the system-created databases in the SQL server, we use the following query −
SQL> SELECT name FROM sys.databases WHERE name NOT IN ('testDB');
Output
When we execute the above query, the output is obtained as follows −
+--------+ | name | +--------+ | master | | model | | msdb | | tempdb | +--------+