- SQL Basics
- SQL Introduction
- SQL Syntax
- SQL Sample Database
- SQL SELECT
- SQL WHERE
- SQL ORDER BY
- SQL DISTINCT
- SQL LIMIT
- SQL FETCH
- SQL AND
- SQL OR
- SQL BETWEEN
- SQL IN
- SQL LIKE
- SQL IS NULL
- SQL Comparison Operators
- SQL Logical Operators
- SQL Alias
- SQL CASE
- Joins and Subqueries
- SQL INNER JOIN
- SQL LEFT JOIN
- SQL RIGHT JOIN
- SQL FULL OUTER JOIN
- SQL SELF JOIN
- SQL CROSS JOIN
- SQL Subquery
- SQL Correlated Subquery
- SQL UNION
- SQL INTERSECT
- SQL EXCEPT
- Aggregate Functions
- SQL AVG
- SQL COUNT
- SQL MAX
- SQL MIN
- SQL SUM
- SQL GROUP BY
- SQL HAVING
- SQL ROLLUP
- SQL CUBE
- SQL GROUPING SETS
- Database Management
- SQL CREATE DATABASE
- SQL ALTER DATABASE
- SQL DROP DATABASE
- SQL BACKUP DATABASE
- SQL SHOW DATABASES
- SQL SELECT DATABASE
- Table Management
- SQL CREATE TABLE
- SQL ALTER TABLE
- SQL ADD COLUMN
- SQL DROP COLUMN
- SQL DROP TABLE
- SQL TRUNCATE TABLE
- SQL SHOW TABLES
- SQL RENAME TABLE
- SQL Constraints
- SQL Primary Key
- SQL Foreign Key
- SQL UNIQUE Constraint
- SQL CHECK Constraint
- SQL NOT NULL Constraint
SQL SELECT DATABASE
In a real-world production environment, a single database server often hosts dozens or even hundreds of different databases—such as one for your web application, another for analytics, and perhaps a third for legacy logs. Before you can start querying tables or inserting data, you need to tell the database engine which specific environment you want to interact with. The USE statement is the standard command used to select this active database in systems like MySQL and MariaDB. Once you execute this command, the server assumes all subsequent queries are directed at that database until you switch to another one or close your connection.
USE statement like opening a folder on your computer. Once the folder is open, you can work on any file inside it without having to type the full file path every single time.
Key Features of SQL SELECT DATABASE
- Context Setting: It defines the "current" or "active" database for your session, allowing you to use short table names (e.g.,
SELECT * FROM usersinstead ofSELECT * FROM my_app_db.users). - Session Based: The selection only lasts for the duration of your current connection. If you disconnect and reconnect, you'll need to run the
USEcommand again. - Engine Specifics: While it is the standard for MySQL and MariaDB, different database engines (like PostgreSQL) handle database switching through connection strings or specialized CLI commands.
USE statement does not mean you have permission to see the data. You must still have the appropriate user privileges granted by a database administrator to access the tables within that database.
SQL SELECT DATABASE Syntax
The syntax is straightforward. You simply provide the name of the database you wish to enter:
USE database_name;
Example:
Imagine you are managing an e-commerce platform and need to pull reports from your testing environment before pushing changes to production. You would switch to your test database like this:
USE test_db;
This sets test_db as the active database. From this point forward, if you run SELECT * FROM orders;, the system knows exactly where to look.
USE statement at the very top of the file. This prevents the script from accidentally running against the wrong database and causing data corruption.
Checking the Current Database
It is easy to lose track of which database you are currently using, especially when juggling multiple terminal windows. To verify your current context, you can call the built-in DATABASE() function:
SELECT DATABASE();
Output Example:
The result will return a single row containing the name of the database currently in use.
| DATABASE() |
|---|
| test_db |
SELECT DATABASE(); will return NULL.
Selecting a Database in SQL Server
In SQL Server (T-SQL), the USE command functions similarly but often requires the GO command to signal the end of a batch, especially in scripts handled by SQL Server Management Studio (SSMS).
USE test_db;
GO
To check your current database in SQL Server, the function name is slightly different:
SELECT DB_NAME();
USE command within the SQL language itself. Instead, you typically switch databases using the \c database_name meta-command in the psql terminal or by specifying the database name in your connection string.
Summary
- The
USE database_name;command is your primary way to set the active working environment in MySQL/MariaDB. - Use
SELECT DATABASE();(MySQL) orSELECT DB_NAME();(SQL Server) to confirm your current location and avoid running queries in the wrong place. - Always remember that PostgreSQL requires a different approach (connection-level switching) compared to the SQL-level
USEcommand.