- 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
The USE
statement is used to select a specific database to work with in MySQL and MariaDB. Once selected, all subsequent queries will run within that database.
Key Features of SQL SELECT DATABASE
- Sets the active database for executing queries.
- Works in MySQL and MariaDB.
- Does not work in SQL Server or PostgreSQL (alternative methods required).
SQL SELECT DATABASE Syntax
USE database_name;
Example:
USE test_db;
This sets test_db
as the active database.
Checking the Current Database
To check which database is currently selected, use:
SELECT DATABASE();
Output Example:
DATABASE() |
---|
test_db |
Selecting a Database in SQL Server
In SQL Server, use:
USE test_db;
GO
Or check the current database with:
SELECT DB_NAME();
Summary
USE database_name;
selects a database in MySQL/MariaDB.- Use
SELECT DATABASE();
to check the current database. - SQL Server and PostgreSQL require different commands (
USE
,\c
).