Forum Discussion
Feature Proposal: Ability to Exclude a column/subset of Columns in Select.
Summary
I would like to propose a new T-SQL feature that allows developers to select all columns from a table while explicitly excluding a small subset of columns.
Currently, when a table contains many columns and only one or two need to be omitted, developers are forced to mention every remaining column manually in the "Select" SQL. This leads to verbose queries, reduced maintainability, and a higher chance of mistakes when the schema evolves.
Motivation
Consider a table with 20 or more columns.
Current approach,
SELECT EmployeeId, FirstName, LastName, Department, Designation, Email, PhoneNumber, DateOfBirth, Address, City, State, Country, PostalCode, ManagerId, JoiningDate, LastModifiedDate, Status, IsActive, CreatedDate FROM Employees;
If the intention is simply to exclude a single sensitive column such as Salary, the query becomes unnecessarily long.
A more concise alternative could be:
SELECT * FROM Employees EXCLUDE (Salary);
The engine would expand * internally and remove the specified columns before execution.
Benefits
1. Reduces boilerplate code.
2. Improves readability for wide tables.
3. Makes queries easier to maintain as schemas evolve.
4. Reduces the likelihood of accidentally omitting newly added columns.
5. Makes it simpler to exclude sensitive or internal-use columns from result sets.
Expected Behavior
Single column exclusion
SELECT * FROM Employees EXCLUDE (Salary);
Returns all columns except Salary.
Multiple column exclusion
SELECT * FROM Employees EXCLUDE (Salary, PasswordHash);
Returns all columns except Salary and PasswordHash.
Suggested Validation Rules
1. Every excluded column must exist in the projected result set.
If an excluded column does not exist, compilation should fail with an appropriate error.
2. Duplicate column names in the exclusion list should either:
be ignored, or
produce a validation error.
3. If the exclusion list removes every projected column, the statement should fail.
Example:
SELECT * FROM Employees EXCLUDE (Employee, Name, Salary);
If these are the only columns in the table, an error could be raised such as:
The EXCLUDE clause cannot eliminate all columns from the SELECT list.
Returning a zero-column result set would likely be confusing and less useful.
Additional Considerations
This syntax could also be valuable when selecting from joins, views, or derived tables, where developers frequently want "everything except a few fields."
Closing Thoughts
I believe this would be a practical quality-of-life enhancement for T-SQL that addresses a common developer pain point while remaining simple to understand and implement. It would reduce repetitive code and improve maintainability without affecting existing queries.