Menu

How to exclude a column using select * except column?

How to exclude a column using select * except column in SQL? Let's also see some work arounds.

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

idfirst_namelast_nameemailsalary
1JohnDoejohn.doe@example.com50000
2JaneSmithjane.smith@example.com60000
3BobBrownbob.brown@example.com55000

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.machinelearningplus.com
Explore Course
Before you go...

Get Your Free AI/ML Engineer Roadmap

The step-by-step path used by 25,000+ learners to go from zero to career-ready in AI/ML.

šŸ”’ 100% Free ā˜• No spam, ever āœ“ Instant delivery
Your roadmap is on the way to your inbox

Want help choosing the right AI/ML path?

Book a free guidance call and our team will help you find right starting point for your AI/ML journey.

Get a free guidance call
šŸ‡®šŸ‡³ +91 ā–¾
Thank you for your submission!
Our team will call you shortly. You'll also receive a confirmation on your email.
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