SQL Keywords

SQL TO

What is the SQL TO keyword used for?

TO specifies the destination, recipient, or new name in many SQL statements such as GRANT, RENAME, COPY, and EXPORT.
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 TO:

SQL TO Full Explanation

TO is not a standalone command but a directional keyword that follows a preceding verb (GRANT, ALTER, COPY, EXPORT, etc.). It tells the database where data, privileges, or objects should go. In GRANT, it identifies the role or user that receives privileges. In RENAME statements it declares the new identifier. In data export commands it marks the output file or external location. Because TO is context-sensitive, its exact semantics depend on the parent statement and the objects being referenced. It appears in every major SQL dialect and adheres to SQL standard wording introduced in early revisions (SQL-92). Caveats: TO never works alone, the target must exist (or be creatable) and must follow identifier or literal rules of the dialect. Incorrect quoting, missing privileges, or ambiguous object names often trigger errors.

SQL TO Syntax

<statement> ... TO <target>;

SQL TO Parameters

  • target (identifier or string) - the object, role, or file receiving the action

Example Queries Using SQL TO

-- Granting privileges
GRANT SELECT, INSERT ON sales.orders TO analyst_role;

-- Renaming a table
ALTER TABLE temp_users RENAME TO users_archive;

-- Copying query results to a CSV (PostgreSQL)
COPY (SELECT * FROM users) TO '/tmp/users.csv' WITH CSV HEADER;

-- Exporting data to cloud storage (BigQuery)
EXPORT TABLE dataset.sales TO 'gs://bucket/sales/*.parquet';

Expected Output Using SQL TO

  • Each statement succeeds silently or with a success message
  • GRANT stores new privileges, RENAME changes the object's identifier, and COPY/EXPORT create the target file

Use Cases with SQL TO

  • Assign privileges to roles or users
  • Rename tables, columns, databases, or sequences
  • Export query results or tables to files or cloud storage
  • Redirect output within procedural scripts

Common Mistakes with SQL TO

  • Using TO without a preceding command
  • Misspelling or misquoting the target identifier
  • Forgetting required privileges (e.g., need ALTER on table to RENAME TO)
  • Confusing TO with INTO in INSERT or SELECT INTO

Related Topics

First Introduced In

SQL-92 standard

Frequently Asked Questions

What is the primary purpose of TO in SQL?

TO tells the database where an action should be directed: the user receiving privileges, the new object name, or the output location.

Can TO be used by itself?

No. It must follow a commanding verb such as GRANT, ALTER, COPY, or EXPORT.

Does TO work in all SQL dialects?

Yes. The keyword appears in all mainstream relational databases, though specific commands can differ.

How is TO different from INTO?

TO points to an existing destination, while INTO usually creates or populates a new table or dataset.

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!