SQL Keywords

SQL USE

What is the SQL USE statement?

Sets the default database (or schema) for the current session so subsequent statements run against that database by default.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL USE:

SQL USE Full Explanation

USE changes the execution context of a session to a specified database or schema. After the statement runs, any unqualified table, view, or procedure reference is resolved in the chosen database until another USE is issued or the session ends. The statement does not create a new connection; it only switches context on the existing one.USE is not part of the ANSI SQL standard. It is available in MySQL, MariaDB, SQL Server, Snowflake, SAP HANA, Teradata, and other systems, but it is absent in PostgreSQL, SQLite, and Oracle. Dialects that lack USE offer alternatives such as setting the search_path (PostgreSQL) or attaching additional databases (SQLite).Because USE affects every subsequent statement in the session, scripts that mix multiple databases should issue USE immediately before the block of statements it governs and document the change clearly. Many teams prefer to open separate connections instead of switching context inside a script to reduce human error.USE cannot be executed inside an explicit transaction in some systems (for example, SQL Server); attempting to do so will throw an error. In others (MySQL) it is allowed but still discouraged because it can make rollback logic confusing.

SQL USE Syntax

-- MySQL, MariaDB, Snowflake
USE database_name;

-- SQL Server
USE [database_name];
GO

SQL USE Parameters

  • database_name (identifier) - Name of the database (or, in some dialects, schema) to become the default

Example Queries Using SQL USE

-- Switch to the sales database, then fetch customers
USE sales_db;
SELECT *
FROM customers
WHERE created_at >= '2024-01-01';

-- SQL Server example switching back and forth
USE master;
GO
SELECT name, database_id
FROM sys.databases;

USE reporting;
GO
EXEC dbo.refresh_daily_metrics;

Expected Output Using SQL USE

  • The database context changes
  • No result set is returned by USE itself, though most clients print a confirmation such as "Database changed"
  • Subsequent queries run against the chosen database

Use Cases with SQL USE

  • Running ad hoc queries against multiple databases without opening new connections
  • Writing maintenance scripts that cycle through several databases
  • Interactive troubleshooting in a shared development environment
  • Teaching or demoing SQL when you need to move between sample databases quickly

Common Mistakes with SQL USE

  • Assuming USE works in PostgreSQL or SQLite
  • Typo in database name leading to "Unknown database" or "Cannot open database" errors
  • Omitting GO after USE in SQL Server scripts, causing the next statement to be parsed in the wrong batch
  • Issuing USE inside an active transaction in SQL Server, which raises an error

Related Topics

First Introduced In

MySQL 3.x and SQL Server 6.0 (1995)

Frequently Asked Questions

What does SQL USE do?

USE changes the default database or schema for your current session so that subsequent queries run against the selected database.

Why is my USE statement failing in PostgreSQL?

PostgreSQL does not implement USE. Open a new connection to the target database or use SET search_path to change schemas.

Do I need a semicolon after USE in MySQL?

Yes. Terminate USE with a semicolon so the client knows the statement is complete.

Can I revert a USE operation?

Simply issue another USE pointing to the original database, or close and reopen your connection which resets the context.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!