Exclude column in redshift spectrum sql queries - sql

In my table having columns col1,col2.....coln
I want to
select all columns except col1
instead of writing select col2,col3.... coln from I can specify
select * from <table name> except col1
Select all column excluding one column

You can do something like this-
-- create a temporary copy of your table
SELECT * INTO temp_table FROM original_table;
-- drop the column you don't need
ALTER TABLE temp_table DROP COLUMN col1;
-- select all columns
SELECT * FROM temp_table;
-- drop the temporary table
DROP TABLE temp_table;

Related

TSQL How do I create a new table from two existing tables [duplicate]

This question already has answers here:
How to create table using select query in SQL Server?
(4 answers)
Closed 9 years ago.
I want to create a table from select query result in SQL Server, I tried
create table temp AS select.....
but I got an error
Incorrect syntax near the keyword 'AS'
Use following syntax to create new table from old table in SQL server 2008
Select * into new_table from old_table
use SELECT...INTO
The SELECT INTO statement creates a new table and populates it with
the result set of the SELECT statement. SELECT INTO can be used to
combine data from several tables or views into one table. It can also
be used to create a new table that contains data selected from a
linked server.
Example,
SELECT col1, col2 INTO #a -- <<== creates temporary table
FROM tablename
Inserting Rows by Using SELECT INTO
Standard Syntax,
SELECT col1, ....., col# -- <<== select as many columns as you want
INTO [New tableName]
FROM [Source Table Name]
Please be careful,
MSSQL: "SELECT * INTO NewTable FROM OldTable"
is not always the same as
MYSQL: "create table temp AS select.."
I think that there are occasions when this (in MSSQL)
does not guarantee that all the fields in the new table are of the same type as the old.
For example :
create table oldTable (field1 varchar(10), field2 integer, field3 float)
insert into oldTable (field1,field2,field3) values ('1', 1, 1)
select top 1 * into newTable from oldTable
does not always yield:
create table newTable (field1 varchar(10), field2 integer, field3 float)
but may be:
create table newTable (field1 varchar(10), field2 integer, field3 integer)
Please try:
SELECT * INTO NewTable FROM OldTable
Try using SELECT INTO....
SELECT ....
INTO TABLE_NAME(table you want to create)
FROM source_table
Select [Column Name] into [New Table] from [Source Table]

Multiple Drop table statement in a query fails

I have a temp table which needs to be recreated with different where conditions. Even though I have a drop statement for the temp table the query fails when executed, is there any way to overcome this issue. Please find the below example for better clarity. any help is much appreciated.
drop table if exists table1;
create table table1(id int)
insert into table1 values (2),(3)
drop table if exists #temp;
select * into #temp from(select * from table1 where id=2)a;
drop table if exists #temp;
select * into #temp from(select * from table1 where id=3)a;
Try using this. It is good practice to use GO to make batches of your query.
drop table if exists table1;
go
create table table1(id int)
insert into table1 values (2),(3)
go
drop table if exists #temp;
go
select * into #temp from table1 where id=2;
go
drop table if exists #temp;
go
select * into #temp from table1 where id=3;

How to create a table from select query result in SQL Server 2008 [duplicate]

This question already has answers here:
How to create table using select query in SQL Server?
(4 answers)
Closed 9 years ago.
I want to create a table from select query result in SQL Server, I tried
create table temp AS select.....
but I got an error
Incorrect syntax near the keyword 'AS'
Use following syntax to create new table from old table in SQL server 2008
Select * into new_table from old_table
use SELECT...INTO
The SELECT INTO statement creates a new table and populates it with
the result set of the SELECT statement. SELECT INTO can be used to
combine data from several tables or views into one table. It can also
be used to create a new table that contains data selected from a
linked server.
Example,
SELECT col1, col2 INTO #a -- <<== creates temporary table
FROM tablename
Inserting Rows by Using SELECT INTO
Standard Syntax,
SELECT col1, ....., col# -- <<== select as many columns as you want
INTO [New tableName]
FROM [Source Table Name]
Please be careful,
MSSQL: "SELECT * INTO NewTable FROM OldTable"
is not always the same as
MYSQL: "create table temp AS select.."
I think that there are occasions when this (in MSSQL)
does not guarantee that all the fields in the new table are of the same type as the old.
For example :
create table oldTable (field1 varchar(10), field2 integer, field3 float)
insert into oldTable (field1,field2,field3) values ('1', 1, 1)
select top 1 * into newTable from oldTable
does not always yield:
create table newTable (field1 varchar(10), field2 integer, field3 float)
but may be:
create table newTable (field1 varchar(10), field2 integer, field3 integer)
Please try:
SELECT * INTO NewTable FROM OldTable
Try using SELECT INTO....
SELECT ....
INTO TABLE_NAME(table you want to create)
FROM source_table
Select [Column Name] into [New Table] from [Source Table]

Exchange column order of table after it is created

I have created a table with 4 columns. I need to change the structure of the table. I need to interchange the position of the 4th and 2nd columns permanently. Is this possible in Oracle?
Not possible. See this.
Oracle only allows columns to be added to the end of an existing
table.
So you must drop and recreate the tables.
You can run a script like this:
CREATE TABLE TMP_TBL as SELECT * FROM TBL_ORIG;
ALTER TABLE TBL_ORIG ADD COLUMN COL3;
DROP TABLE TBL_ORIG;
CREATE TABLE TBL_ORIG AS SELECT COL1, COL3, COL2 FROM TMP_TBL;
DROP TABLE TMP_TBL
You would need to consider indexes as well as storage concerns.
Why in the world is this necessary? Column order means nothing in SQL.
Swap of columns col1 and col2
It is assumed that col1 is int and col2 is varchar2(20)
-- drop all indexes and constraints concerning col1 and col2
alter table your_table add temp_col int; -- type of col1
update your_table set col1 = null, temp_col = col1;
alter table your_table modify col1 varchar2(20); -- type of col2
update your_table set col2 = null, col1 = col2;
alter table your_table modify col2 int; -- type of col1
update your_table set col2 = temp_col;
alter table your_table drop column temp_col;
alter table your_table rename column col1 to temp_col;
alter table your_table rename column col2 to col1;
alter table your_table rename column temp_col to col1;
-- recreate indexes and constraints
Simply rename table columns if they are the same datatype. If not then Alter - see Sean and Egor examples.
Rename:
http://docs.oracle.com/cd/E11882_01/server.112/e25494/tables006.htm#ADMIN11662
And on the interview they are looking for Sean's answer. Just FYI...

Couldn't create temp table from select query if result empty

I want to crate a temp table from select query (My table has many columns, therefore I don't want to create the temp table manually)
I use the following query:
SELECT * INTO #TempTable
FROM MyTable
WHERE ...
If this query return empty rows, it won't create #TempTable. Hence, I cannot use this #TempTable for the next queries.
Is there a way to resolve this?
If the query SELECT * FROM MyTable WHERE ... in your code you posted:
SELECT *
INTO TempTable
FROM MyTable WHERE ...
returned no rows, it will create an empty TempTable, but it won't fill any data in it if there is no rows matched the WHERE clause. But it should create the table TempTable at least with the same structure as the MyTable and it will be empty.
For example this:
SELECT * INTO TempTable FROM MyTable WHERE 1 <> 1;
Will always create an empty table TempTable with the same structure as MyTable since the predicate 1 <> 1 is always false.
However you can declare it like so:
DECLARE #Temp TABLE(Field1 int, ...);
This is because you are dynamically creating and populating temporary table and not creating it explicitly.In such scenario, you must check the existence of the temp table in the beginning before you create one.
Try this:
IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL
BEGIN
DROP TABLE #TempTable
END
SELECT * INTO #TempTable FROM MyTable
Select * From #TempTable
your query
SELECT * INTO #TempTable
FROM MyTable
WHERE ...
will create an empty table if the select returns no rows