Menu

How to exclude a column using select * except column?

Written by Selva Prabhakaran | 2 min read

Problem

You have a database table, how to select all columns except specific columns mentioned by the user?

Example Syntax:

sql
SELECT * [except columnA] 
FROM tabA;

Input

id first_name last_name email salary
1 John Doe john.doe@example.com 50000
2 Jane Smith jane.smith@example.com 60000
3 Bob Brown bob.brown@example.com 55000

This is not possbile in native SQL databases.

MySQL doesn’t natively support the SELECT * EXCEPT(column_name) syntax. To achieve the desired result, you need to manually specify the columns you want to include in your SELECT statement.
However, it is possible to do certain inefficient workarounds like this:e:5969d8a384b6b52f4ccf6acae27d8b7e)

Workaround Solution:

This solution is inefficient because it creates copies of data in new table and involves multiple steps. However addresses the problem of not having to specify all the column names in the table.

Using TempTables

sql
/* Get the data into a temp table */
SELECT * INTO #TempTable
FROM YourTab
    le
/* Drop the columns that are not needed */
ALTER TABLE #TempTable
DROP COLUMN ColumnTo
Drop
/* Get results and drop temp table */
SELECT * FROM #TempTable
DROP TABLE #TempTable1;

Modern Databases solution:

Modern databases such as Databricks and BigQuery supports the following syntax:

sql
SELECT * EXCEPT(ColumnNameX, [ColumnNameY, ...])
FROM TableA;

DuckDB supports the following: Use EXCLUDE instead of EXCEPT

sql
SELECT * EXCLUDE(ColumnNameX, [ColumnNameY, ...])
FROM TableA
  1. SQL for Data Science – Level 1
  2. SQL for Data Science – Level 2
  3. SQL for Data Science – Level 3
  1. Introduction to SQL
  2. SQL Window Functons – Made Simple and Easy
  3. SQL Subquery

More SQL Questions

  1. How to concatenate multiple rows into one field in MySQL?
  2. What is the difference between UNION and UNION ALL in SQL?
  3. How to find duplicate values in SQL?
  4. How to select first row in each GROUP BY group?
Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Related Course
Master SQL — Hands-On
Join 5,000+ students at edu.darkorange-mallard-189514.hostingersite.com
Explore Course
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science (AI/ML) professionals use and kick start your Data Science (AI/ML) Career.

🐍
Core Python — from first line of code to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗀
SQL Levels I–III — from basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students
★★★★★ Rated 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
Or start your first course right now:
Start the free courses →
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science