SQL Tutorial SQL Advanced SQL Database SQL References

SQL - SELECT DATABASE



When there are multiple databases in the SQL Schema, then before starting any operation on a given database, that database need to be selected. The SQL USE clause is used to select any existing database in the SQL schema.

Syntax

The syntax of selecting database is given below:

USE DatabaseName;

To see the list of available databases in MySQL and MariaDB, the following command can be used:

SHOW DATABASES;

Similarly in SQL Server, the following statement can be used to see the list of available databases.

SELECT name FROM sys.databases;

Example: Select a database

To select a database with name testDB, the following statement can be used:

USE testDB;

The SHOW DATABASES; command can be used to see the list of databases available in MySQL & MariaDB:

SHOW DATABASES;
Result:
+-------------+
| Database    |
+-------------+
| SQLExample1 |
| SQLExample2 |
| SQLExample3 |
| SQLExample4 |
| SQLExample5 |
| SQLExample6 |
| SQLExample7 |
| SQLExample8 |
| testDB      |
+-------------+
9 rows in set (0.00 sec)

Similarly in SQL Server, the following statement can be used to see the list of available databases.

SELECT name FROM sys.databases;
Result:

name        
--------------
SQLExample1 
SQLExample2 
SQLExample3 
SQLExample4 
SQLExample5 
SQLExample6 
SQLExample7 
SQLExample8 
testDB