Reorder Columns in SQL: Query Order, Physical Order and When to Change Either

How to reorder columns in SQL: change output order with SELECT or a view, or physically rearrange table columns in MySQL, PostgreSQL, SQL Server, SQLite and Oracle, with the risks of each.

People who want to reorder columns in SQL usually mean one of two different things. They may want query results, reports or exports to show columns in a different sequence. Or they may want to change the order in which columns are stored in the table definition. The first is simple and safe in every major database. The second ranges from a single statement to a full table rebuild, depending on the engine. This review explains what each approach changes, when it is worth doing, and what to check before relying on the result.

Is reordering columns the right move for your use?

In most cases, change the order in the query, not in the table. Listing columns explicitly in a SELECT statement, or defining a view that does so, gives the exact output order needed without touching stored data, locking the table or risking dependent code. The main benefit of this approach is that it is fully reversible and costs nothing at the storage level. Its tradeoff is that the preferred order lives in queries or views rather than in the table itself, so anyone running SELECT * against the base table still sees the original order.

Physically rearranging columns is reasonable mainly for cosmetic consistency in schema tools, or when a new table is being designed and no data or code depends on it yet. On a large production table, the decisive tradeoff is cost and risk: several engines can only do it by recreating the table, which takes time, needs extra storage and can block writes.

If the goal is simply a file with columns in a different order, such as a CSV for a spreadsheet or another system, a plain-text approach can be preferable: export with an explicit column list, or rearrange the columns of the exported delimited file with a column-reordering tool. Either leaves the database untouched.

What reordering actually changes

In the relational model, column order carries no meaning. A table is defined by its column names and types, and the database does not treat one column as coming logically before another. The order you see is either the order specified in a query or the order recorded in the table's definition, which is typically the order in which columns were created.

Reordering in a query changes only presentation: the result set is produced with columns in the listed sequence, while the stored table and its data are unchanged. Reordering the table definition changes the stored metadata and, in some engines, rewrites every row. This distinction matters because only the second affects statements that depend on positional order, such as SELECT * or INSERT without a column list.

Approach | What changes | Effect on stored data | Reversibility
Explicit SELECT column list | Output order of one query | None | Edit the query
View with chosen order | Output order for anyone querying the view | None | Drop or redefine the view
ALTER TABLE reorder (where supported) | Table definition | May rewrite the table | Run another ALTER
Recreate and copy table | Table definition and storage | Full rewrite | Requires another rebuild

Changing column order in query results

Name the columns in the order you want. This works the same way in every SQL database.

SELECT last_name, first_name, email, customer_id
FROM customers;

To make that order reusable, wrap it in a view. Applications and reports can then query the view as if it were a table with the preferred column order.

CREATE VIEW customers_ordered AS
SELECT last_name, first_name, email, customer_id
FROM customers;

Rearranging columns in the table definition by database

Support for physically moving a column differs considerably between engines. The descriptions below reflect documented general behavior; details can change between versions, so confirm against the documentation for the exact version in use before running anything on real data.

MySQL and MariaDB

These are the only widely used engines with direct syntax for moving a column. MODIFY COLUMN (or CHANGE COLUMN) accepts FIRST or AFTER. The full column definition must be restated, including type, nullability, default and any other attributes; omitting one silently drops it. Moving a column generally requires the table to be rebuilt, so on large tables it should be planned like any other heavy schema change.

ALTER TABLE customers
  MODIFY COLUMN email VARCHAR(255) NOT NULL AFTER last_name;

ALTER TABLE customers
  MODIFY COLUMN customer_id INT NOT NULL FIRST;

PostgreSQL

PostgreSQL has no statement for changing column position. The options are a view, or creating a new table with the desired order, copying the data, and then recreating indexes, constraints, defaults, triggers, permissions and any dependent views before swapping names.

BEGIN;
CREATE TABLE customers_new (
  customer_id integer PRIMARY KEY,
  last_name   text NOT NULL,
  first_name  text,
  email       text NOT NULL
);
INSERT INTO customers_new (customer_id, last_name, first_name, email)
SELECT customer_id, last_name, first_name, email FROM customers;
ALTER TABLE customers RENAME TO customers_old;
ALTER TABLE customers_new RENAME TO customers;
COMMIT;

This sketch omits foreign keys, sequences, grants, triggers and dependent objects, all of which must be handled for a real table.

SQL Server

T-SQL has no ALTER TABLE option to reposition a column. The table designer in SQL Server Management Studio lets users drag columns into a new order, but it does so by generating a script that creates a new table, copies the data and drops the original. By default the designer blocks saving changes that require table re-creation, and that safeguard exists for good reason on populated tables. Reviewing the generated script before applying it is the safer path.

SQLite

SQLite's ALTER TABLE support is limited and does not include moving columns. The documented approach for such changes is to create a new table with the desired schema, copy the data, drop the old table, rename the new one, and recreate indexes, triggers and views.

Oracle

Oracle has no direct reorder statement. Rebuilding the table is the conventional route. In versions that support invisible columns, making a column invisible and then visible again moves it to the end of the column order, which can be used to shuffle columns without copying data. This is a side effect rather than a dedicated feature, so test it on a non-production copy first.

Will results stay usable after copying, exporting or sharing?

Output produced by an explicit column list is stable: exports, query results pasted into spreadsheets and CSV files follow the order in the SELECT. Output from SELECT * follows the table definition, so it changes whenever the table is physically reordered. Anything consuming that output by position, such as a spreadsheet formula, an import mapping or a script reading the third field, can break or silently read the wrong data.

The same risk applies inside the database. Before physically reordering, search for code that depends on column position:

  • INSERT statements without a column list, which assign values by position
  • SELECT * in application code, stored procedures or views
  • Code that reads result columns by index rather than by name
  • Export jobs and import mappings configured by column position
  • Views created with SELECT *, which in some engines capture the column list at creation time and may not reflect later changes

For people reading the output, column order affects how a table is read. Screen readers move through table cells in source order, so placing the identifying column first, such as a name or ID, usually makes shared tables easier to follow. This is a general principle of table structure; no testing with specific assistive technologies or destinations was supplied for this review.

Search and editing are unaffected by column order inside the database, since queries refer to columns by name. Where order genuinely matters is in delimited text files that leave the database. If a recipient expects a specific layout, deliver a file generated with an explicit column list, or a plain-text CSV whose header row names every column, rather than depending on table order.

Evidence and gaps

This review relies on general, documented behavior of the database engines named. No performance measurements, rebuild timings, lock durations or version-specific test results were available, so none are given. The time and locking impact of a table rebuild depends on table size, indexes, engine version and configuration, and should be measured on a copy of the actual data before scheduling a production change.

Recommendation

For reports, exports and day-to-day querying, set column order in the SELECT list or a view and leave the table alone. Physically rearrange columns only when the cosmetic benefit justifies the rebuild, and only after removing positional dependencies such as SELECT * and column-less INSERT statements. When the real need is a differently ordered file, generate it with an explicit column list or reorder the exported delimited text; the database does not need to change at all.