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.

</>
Copy
USE database_name;

For example, the following statement selects the database named school.

</>
Copy
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:

</>
Copy
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.

</>
Copy
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:

</>
Copy
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.

Connect to the MySQL Server

Step 2: List Databases Available to Your MySQL User

To see a list of all databases on the server, use the following command:

</>
Copy
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.

View Available Databases

Step 3: Select the MySQL Database with USE

Use the USE statement to select the desired database:

</>
Copy
USE database_name;

Replace database_name with the name of the database you want to select. For example, let us select the database school.

</>
Copy
USE school;

Upon successful execution, you will see a message confirming that the database has been changed:

Select a Database in MySQL

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:

</>
Copy
SELECT DATABASE();

The output will display the name of the currently selected database, which is school.

Verify the Selected Database

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.

</>
Copy
mysql -u username -p database_name

For example, the following command logs in and selects the school database immediately.

</>
Copy
mysql -u root -p school

After logging in, you can confirm the selected database with SELECT DATABASE();.

</>
Copy
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.

</>
Copy
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.

</>
Copy
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.

</>
Copy
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.

Select the Database in MySQL Workbench

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:

</>
Copy
SELECT DATABASE();
Verify the Selected Database in MySQL Workbench

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:

</>
Copy
USE school;
SELECT * FROM students;

Or qualify the table name directly:

</>
Copy
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

TaskCommand or action
List databases visible to the current userSHOW DATABASES;
Select a database after loginUSE database_name;
Select a database while logging inmysql -u username -p database_name
Check the current databaseSELECT DATABASE();
Query a table without changing databaseSELECT * FROM database_name.table_name;
Select database in MySQL WorkbenchRight-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 -p and 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.