selecting entire table when selecting columns - How - sql

Currently i have joined together several tables. now i want to select some columns out of this joined mesh.
my question is: is it possible to select all columns of a specific table?
for example:
select col1,col4,col7,all_columns.Table1,col9 from (joined tables including Table1)
if it is possible, how to implement it

this is what you want?
select col1,
col4,
col7,
Table1.*,
col9
from (joined tables including Table1)

How about this?
SELECT ... Table1.*, ... FROM ...

Related

combining data from two tables when no data overlaps

I have two tables:
This is table1:
customer_name|customer_id|customer_phone|customer_birthday|(10 more columns that don't match with table2)
This is table2:
customer_name|customer_id|customer_phone|customer_status|(141 columns)
table1.customer_id is numeric
table2.customer_id is alphanumeric
table2 was imported from an old database and contains legacy customer data
How do I get data from both tables while only specifying the columns I want from each table.
The two tables have no overlapping data.
Please help me with this. I am not sure how to resolve it
Using SQL Server 2019
You could simply combine data from the both tables using UNION:
select customer_name, cast(customer_id as varchar), customer_phone, customer_birthday, null customer_status from table1
union all -- no need to do a useless distinct
select customer_name, customer_id, customer_phone, null as customer_birthday, customer_status from table2
As long as you have same column names, you can make a list of columns and use UNION like below:
SELECT col1, col2, col3, ...
FROM t1
UNION
SELECT col1, col2, col3
FROM t2;

Find differences in both tables without multiple joins

After comparing two data sets I'd like to extract information such as:
Rows that are only present in table A
Rows that are only present in table B
Non-key value differences after join
What's the preferred way to go about this? Is there a way to do this without having to do LEFT and RIGHT joins separately?
It sounds you want a FULL OUTER JOIN. That gives you all rows from both tables, joining ones that match keys. Then you can see which rows are present in only one table, and compare values for rows that are in both.
I would typically use group by for this, so I'm not sure what the reference to multiple joins is.
select col1, col2, col3, sum(in_a) as a_cnt, sum(in_b) as b_cnt
from ((select col1, col2, col3, 1 as in_a, 0 as in_b
from a
) union all
(select col1, col2, col3, 0 as in_a, 1 as in_b
from b
)
) ab;

db2 select distinct rows, but select all columns

Experts, I have a single table with multiple columns. col1, col2, col3, col4, col5, col6
I need to select distinct (col4), but I need all other columns also on my output.
If I run, this ( select distinct(col4 ) from table1 ), then I get only col4 on my output.
May I know, how to do it on db2?.
Thank you
You simply do this...
Select * From Table1 Where col4 In (Select Distinct(col4) From Table1)
I'm not sure if you will be able to do this.
You might try to run group by on this column. You will be able to run some aggregate functions on other columns.
select count(col1), col4 from table1 group by (col4);
none of the answers worked for me so here is one that i got working. use group by on col4 while taking max values of other columns
select max(col1) as col1,max(col2) as col2,max(col3) as col3
, col4
from
table1
group by col4
At least in DB2, you can execute
SELECT
DISTINCT *
FROM
<YOUR TABLE>
Which will give you every distinct combination of your (in this case) 6 columns.
Otherwise, you'll have to specify what columns you want to include. If you do that, you can either use select distinct or group by.

How do I concatenate two similar tables on a result

I have two tables with similar columns. I would simply like to select both tables, one after another, so that if I have 'x' rows on table1 and 'y' rows on table2, I'd get 'x + y' rows.
You would use UNION [ALL] for this. The tables don't need to have the same column names but you do need to select the same number of columns from each and the corresponding columns need to be of compatible datatypes
SELECT col1,col2,col3 FROM table1
UNION ALL
SELECT col1,col2,col3 FROM table2
UNION ALL is preferrable to UNION where there is a choice as it can avoid a sort operation to get rid of duplicates.
Just to add to what they were saying, you might want to add an Order By. Depends on the version of SQL you're using.
SELECT Col1, Col2, Col3
FROM Table1
UNION
SELECT Col1, Col2, Col3
FROM Table2
ORDER BY Col1
Note that ORDER and GROUP BYs have to go after the last table in the UNION.
select col1,col2,col3 from table1
union
select col1,col2,col3 from table2
Look at the Union operator.

join two tables into one big table

I have two tables with the same columns, and I need to copy one table's rows to the other table's rows to create one big table with all the values from both tables. Right now I am doing this query to return the same thing:
SELECT col1, col2, col3 from Table1
union
SELECT col1, col2, col3 from Table2
However, it seems horribly inefficient, and on my system is very slow (returns 1210189 records).
May it work to just do:
SELECT col1, col2, col3
INTO Table1
FROM Table2
Start with union all:
select col1, col2, col3 from Table1
union all
select col1, col2, col3 from Table2
Your query is trying to deduplicate things, which would slow it down considerably.
I think the best option is to create a view in sql server, this will optimize the performance of the query:
SELECT col1, col2, col3 from Table1
union all
SELECT col1, col2, col3 from Table2
(As other users said: "union" is used to select distinct values from two tables
where as "union all" is used to select all values including
duplicates from the tables.)
At the same time I would restrict the number of rows I get from the database if i am writing them for a web and if this is giving me problems, with the new functions of Sql Server 2005 row_number(), with this I would page results.
You could use this to fill the second table:
Insert into table2 select * from table1;
Or if you want to be more specific:
Insert into table2(col1, col2, col3) select col1, col2, col3 from table1;
(Note: some DBMSs might require putting parenthesis around the SELECT clause.)
select * into new table(your new table name)
from table1.col1,table1.col2,table2.col1;
here columns can be your required columns .
select * into newtable from table1
union all
select * from table2
Worked well. Guidelines, both tables have exact same column names :)