How to Select a Database in MySQL
To select a database in MySQL, use the USE statement followed by the database name. After a database is selected, MySQL runs your table queries in that database context unless you explicitly refer to another database name.
USE database_name;
For example, the following statement selects the database named school.
USE school;
In this tutorial, you will learn how to select a MySQL database from the command line, verify the current database, select a database while logging in, switch to another database, and set a default schema in MySQL Workbench.
Prerequisites Before Selecting a MySQL Database
Before using USE database_name;, make sure MySQL Server is running, you can log in with a valid MySQL user account, and the database already exists. Your user must also have the required privileges on that database. If the database does not exist or you do not have access to it, MySQL cannot make it the current database.
It is also useful to know that the words database and schema are often used together in MySQL tools. In MySQL Workbench, the left panel is named Schemas, but selecting a schema there is the same practical step as selecting a database for your queries.
MySQL USE Statement Syntax to Select a Database
The basic syntax to select a database in MySQL is:
USE database_name;
Replace database_name with the actual database name. The semicolon is required when you run the statement in the MySQL command-line client or in a SQL editor that expects statement termination.
If the database name contains spaces, hyphens, reserved words, or special characters, wrap it in backticks. For normal database names such as school, company_db, or inventory, backticks are usually not required.
USE `database-name`;
Select a Database Using the MySQL Command-Line Interface
The MySQL command-line interface is the most direct way to select a database. The steps below show the complete flow: log in, list available databases, select one database, and confirm the selected database.
Step 1: Connect to the MySQL Server from Terminal
Open your terminal or command prompt and log in to the MySQL server using the following command:
mysql -u username -p
Replace username with your MySQL username. Enter your password when prompted. For security, the password is not displayed while you type it.
In this guide, we will login to the server as root (username is root) as shown in the following screenshot.

Step 2: List Databases Available to Your MySQL User
To see a list of all databases on the server, use the following command:
SHOW DATABASES;
The output displays the databases that your current MySQL user has permission to see. Identify the database you want to select from this list.

Step 3: Select the MySQL Database with USE
Use the USE statement to select the desired database:
USE database_name;
Replace database_name with the name of the database you want to select. For example, let us select the database school.
USE school;
Upon successful execution, you will see a message confirming that the database has been changed:

All subsequent queries will now apply to the selected database, unless a query explicitly uses a different database name.
Step 4: Verify the Current MySQL Database
To verify the currently selected database, run the following command:
SELECT DATABASE();
The output will display the name of the currently selected database, which is school.

If no database has been selected, SELECT DATABASE(); returns NULL. In that case, run a valid USE statement before querying tables without a database prefix.
+------------+
| DATABASE() |
+------------+
| NULL |
+------------+
Select a MySQL Database While Logging In
You can also select a database at the time of connecting to MySQL. Add the database name at the end of the mysql command.
mysql -u username -p database_name
For example, the following command logs in and selects the school database immediately.
mysql -u root -p school
After logging in, you can confirm the selected database with SELECT DATABASE();.
SELECT DATABASE();
Switch from One MySQL Database to Another Database
To select another database in the same MySQL session, run another USE statement. You do not need to disconnect and log in again.
USE school;
SELECT DATABASE();
USE company;
SELECT DATABASE();
After USE company;, unqualified table names such as employees refer to tables in the company database, not the previously selected school database.
Query a MySQL Table Without Changing the Selected Database
You do not always have to change the current database before running a query. MySQL allows you to qualify a table name with the database name using the format database_name.table_name.
SELECT * FROM school.students;
This is useful when you need to read from multiple databases in the same session. For example, the following query uses tables from two databases by qualifying each table name.
SELECT s.student_name, e.exam_name
FROM school.students AS s
JOIN exams.exam_results AS e
ON s.student_id = e.student_id;
When you use fully qualified table names, the selected database still remains unchanged. This approach is different from USE database_name;, which changes the default database for the session.
Select a Database Using MySQL Workbench Default Schema
MySQL Workbench provides a graphical interface for selecting a database. In Workbench, selecting a database is commonly done by setting it as the default schema.
Step 1: Launch MySQL Workbench and Open a Connection
Open MySQL Workbench on your system and connect to the MySQL server by selecting the appropriate connection from the home screen.
Step 2: Open the Schemas Panel in MySQL Workbench
In the left-hand navigation panel, locate the “Schemas” section. This panel lists the databases available on the connected server.
Step 3: Set the MySQL Database as Default Schema
Right-click on the database you want to select and choose Set as Default Schema. This action sets the chosen database as the active schema for subsequent queries in that editor session.

Once the schema/database is set as default, then it appears in bold, as highlighted in the following screenshot.

Step 4: Verify the Selected Database in MySQL Workbench
To confirm the selected database, use the Query Editor in MySQL Workbench. Run the following SQL command:
SELECT DATABASE();

The output will display the currently selected database. If the result is NULL, set a default schema again or run USE database_name; in the query editor.
Common MySQL Database Selection Errors and Fixes
The following errors are common when selecting or using a MySQL database.
ERROR 1049 Unknown Database When Using USE
This error means the database name in the USE statement does not exist on the current MySQL server, or the name has been typed incorrectly.
ERROR 1049 (42000): Unknown database 'school'
Run SHOW DATABASES; to check the available database names. If the database name contains special characters, use backticks around the name.
ERROR 1046 No Database Selected Before Querying a Table
This error appears when you run a table query without selecting a database and without qualifying the table name with a database name.
ERROR 1046 (3D000): No database selected
Fix it by selecting the database first:
USE school;
SELECT * FROM students;
Or qualify the table name directly:
SELECT * FROM school.students;
Access Denied for a MySQL Database
If your user does not have permission to access a database, the database may not appear in SHOW DATABASES;, or MySQL may return an access-related error. Log in with the correct user or ask the database administrator to grant the required privileges.
MySQL Database Selection Quick Reference
| Task | Command or action |
|---|---|
| List databases visible to the current user | SHOW DATABASES; |
| Select a database after login | USE database_name; |
| Select a database while logging in | mysql -u username -p database_name |
| Check the current database | SELECT DATABASE(); |
| Query a table without changing database | SELECT * FROM database_name.table_name; |
| Select database in MySQL Workbench | Right-click schema and choose Set as Default Schema |
FAQs on Selecting a Database in MySQL
How do you select a specific database in MySQL?
Use the USE statement with the database name. For example, USE school; selects the school database for the current MySQL session.
How do I check the current selected database in MySQL?
Run SELECT DATABASE();. It returns the name of the current database. If no database is selected, it returns NULL.
How do I select another database in MySQL?
Run another USE statement with the new database name. For example, USE company; changes the current database to company in the same session.
Can I query a MySQL table without selecting a database first?
Yes. Use a fully qualified table name such as school.students. For example, SELECT * FROM school.students; reads from the students table in the school database without changing the current database.
Why does MySQL show No database selected?
MySQL shows this error when you run a table query without selecting a database and without writing the database name before the table name. Use USE database_name; first, or write the table as database_name.table_name.
Editorial QA Checklist for MySQL Select Database Tutorial
- Confirm that the tutorial uses
USE database_name;as the main method to select a MySQL database. - Confirm that
SELECT DATABASE();is included to verify the currently selected database. - Confirm that the command-line login example explains both
mysql -u username -pand selecting the database during login. - Confirm that MySQL Workbench instructions use the term
Set as Default Schema, because Workbench displays databases under the Schemas panel. - Confirm that error explanations for unknown database and no database selected are specific to MySQL database selection.
TutorialKart.com