Exclude a column using SELECT * [except columnA] FROM tableA?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
When working with SQL, the SELECT * syntax is often used to retrieve all columns from a given table. However, there might be scenarios where you want to exclude one or more columns from the result set without listing all the remaining columns explicitly. Unfortunately, SQL does not directly support a syntax like SELECT * EXCEPT [columnA] FROM tableA. Instead, you need to use alternative methods to achieve this result.
Understanding the Limitations of SELECT *
Using SELECT * is simple and allows you to quickly fetch all columns from a table. Yet, this convenience comes at the cost of performance, clarity, and control over the returned data. When you know exactly which columns are needed—or which are not—specifying them can greatly enhance the performance of your queries, especially with large datasets.
Workarounds to Exclude Columns
1. Specifying All Other Columns
The most straightforward approach is to manually specify every column you want to include in your results, omitting those you do not. This method is reliable but can be cumbersome if your table has many columns.
Example:
Suppose tableA has columns id, name, email, and age. To select all but the email column, you would write:
2. Using a View or a Stored Procedure
If the requirement to exclude certain columns is recurring, it might be worth the effort to create a view or a stored procedure that encapsulates the logic.
Creating a View:
Then, simply use:
Creating a Stored Procedure:
3. Dynamic SQL
Dynamic SQL can build SQL commands dynamically and execute them. This is powerful as it can adapt to changes in the table's schema. However, caution should be used with dynamic SQL due to potential security risks like SQL injection.
Example using Dynamic SQL:
Best Practices and Considerations
- Code Readability: Always aim for clarity and simplicity. If the table only has a few columns, explicitly list them instead of using complex SQL scripts.
- Performance:
SELECT *can lead to unnecessarily large amounts of data being transferred and can negatively impact performance. Always select only the necessary columns. - Security: Be cautious with Dynamic SQL to prevent SQL injection by validating and sanitizing inputs.
Summary Table
| Method | Use Case | Pros | Cons |
| Listing Columns | Small tables or few needed columns | Simple and safe | Cumbersome with many columns |
| Using Views/Stored Procs | Recurring complex queries | Clean and encapsulated code | Overhead of additional objects |
| Dynamic SQL | Flexible column manipulation in large tables | Adapts to schema changes | Complex, potential security risks |
Conclusion
While SQL does not directly support excluding a column with shorthand syntax, the methods outlined provide effective alternatives. Whether you choose to manually specify columns, utilize views or stored procedures, or adopt dynamic SQL, each approach has its own advantages and limitations that can be leveraged depending on specific use cases and requirements.
Related reading
- Executing an update/delete query in the JQPL query
- Existing tools to find unused tables in cassandra cluster
- Explicitly select items from a list or tuple
- Export and Import all MySQL databases at once
- Execute web service method and return immediately
- Executing tasks in parallel
- Export data from DynamoDB
- Export MySQL dump from command line

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.