Rename column

2 snippets in SQL

SQLSQL

Column and Table Aliases

SQL · Basic Queries
Syntax
SELECT column AS alias_name FROM table AS t;
Example
SELECT
  u.first_name AS name,
  o.total_amount AS order_total
FROM users AS u
JOIN orders AS o ON o.user_id = u.id;
Output
-- name  | order_total
-- Alice | 149.99

Note AS is optional in most databases but improves clarity. You cannot reference a column alias in the WHERE clause of the same query - use a subquery or CTE instead.

ALTER TABLE

SQL · Table Operations
Syntax
ALTER TABLE table_name
  ADD COLUMN col type constraints,
  DROP COLUMN col,
  ALTER COLUMN col SET DATA TYPE new_type,
  ADD CONSTRAINT name constraint_definition;
Example
ALTER TABLE users
  ADD COLUMN phone VARCHAR(20),
  ADD COLUMN updated_at TIMESTAMP;

ALTER TABLE users
  ALTER COLUMN email SET NOT NULL;

ALTER TABLE users
  ADD CONSTRAINT chk_email CHECK (email LIKE '%@%');
Output
-- Adds columns, modifies existing, adds constraint

Note ALTER TABLE syntax varies across databases. PostgreSQL uses ALTER COLUMN ... TYPE. MySQL uses MODIFY COLUMN. Adding NOT NULL to a column with existing NULLs will fail - update the data first.

Frequently asked questions

How do you rename column?
SQL covers this with 2 copy-ready snippets on this page. The "Column and Table Aliases" snippet in SQL uses `SELECT column AS alias_name FROM table AS t;`.
Which statement does the SQL example use?
The "Column and Table Aliases" snippet uses `SELECT column AS alias_name FROM table AS t;`, from the Basic Queries section of the SQL cheat sheet.
What other SQL snippets are shown for "rename column"?
Besides "Column and Table Aliases", this page also shows "ALTER TABLE".
Is there anything to watch out for?
Yes. For "Column and Table Aliases": AS is optional in most databases but improves clarity. You cannot reference a column alias in the WHERE clause of the same query - use a subquery or CTE instead.