Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

We all know that to select all columns from a table, we can use

SELECT * FROM tableA

Is there a way to exclude column(s) from a table without specifying all the columns?

SELECT * [except columnA] FROM tableA

The only way that I know is to manually specify all the columns and exclude the unwanted column. This is really time consuming so I'm looking for ways to save time and effort on this, as well as future maintenance should the table has more/less columns.

Thanks!

3 Answers

0 votes
by (40.7k points)

It’s recommendable to do it in this way:

SELECT * INTO #Temp_Table

/* Get the data into a temp1 table(Here temp1 is a temporary table which is used to store the temporary data)  */

FROM Table  /* Drop those columns which are not needed */

ALTER TABLE #Temp_Table

DROP COLUMN ColumnToDrop /* Get the results and drop temporary table */

SELECT * FROM #Temp_Table

DROP TABLE #Temp_Table

0 votes
by (1.5k points)
SQL does not have an instruction to exclude a column such as SELECT * EXCEPT columnA FROM tableA but rather requires columns you will include in your SELECT clause and exclude all those unwanted columns.
Example
SELECT col1, col2, col3
FROM tableX;
0 votes
by (1.5k points)

SQL does not have an instruction to exclude a column such as SELECT * EXCEPT columnA FROM tableA but rather requires columns you will include in your SELECT clause and exclude all those unwanted columns.

Example

SELECT col1, col2, col3 

FROM tableX;

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...