Can i save the output of a joined table into another table? - sqlite

I created a database and having two tables like x and y. I cross joined them both and got an output.Now my question is can i save that output to a new table in the same database or different?

First you can try running the query SELECT * FROM table1 CROSS JOIN table2. Observe what columns are returned from the query, then create a new table new_table with columns of the appropriate type. Then you can try using INSERT INTO ... SELECT:
INSERT INTO new_table
SELECT * FROM table1 CROSS JOIN table2;

The CREATE TABLE statement directly accepts a query:
CREATE TABLE new_table AS
SELECT * FROM table1 CROSS JOIN table2;
(This will not keep the column types.)
The output can be saved in a different database if that database has been ATTACHed.

Related

Delete duplicate rows in sqlite database

I want to delete duplicate rows in a sqlite database. I found this code online to do it:
DELETE FROM table1
WHERE ROWID NOT IN (
SELECT min(rowid)
FROM table1
GROUP BY col1, col2
)
But the issue is that my database contains 50+ columns and I don't want to write all the column names in group by. Is there a workaround to group by all the columns?
I am new to this so I may be missing something simple.
In a GROUP BY clause you must enumerate all the columns.
For your case, I think it's better to create another table table2 with all the distinct rows of table1, then delete all rows of table1 and finally insert all rows from table2 to table1:
CREATE TABLE table2 AS SELECT DISTINCT * FROM table1;
DELETE FROM table1;
INSERT INTO table1 SELECT * FROM table2;
DROP TABLE table2;
See a simplified demo.

update (overwrite) an entire record (all columns) in sqlite

When I am copying rows from one table to another, there is a convenient syntax:
INSERT INTO table1 SELECT * FROM table2 WHERE <table 2 rows have some property>
But what if I want to overwrite entire existing rows in table1 with entire rows from table2? So I want something like:
UPDATE table1 SET * FROM table2 WHERE <table 1 and 2 rows match on some key field>
But from what I can tell, the only way to do this is to enumerate the columns being set one by one (set table1.columnA = table2.columnA, table1.columnB = table2.columnB, and so on). Is there some way to say "do it for all the columns" when using UPDATE like there is when using INSERT? If not, why not?
(I guess I could delete all rows from table1 with the given property, and then use the INSERT INTO table1 SELECT * syntax to bring in the replacement rows from table2. But that seems like it leaves a bunch of unwanted deleted rows in the database needing to be vacuumed at some point, as opposed to a clean UPDATE where there are no deleted records? Or maybe I'm not understanding the efficiency of a bunch of deletes followed by a bunch of inserts?)
There is no such syntax for exactly what you have in mind, and I think you will need to SET each column separately. Also, SQLite does not support a direct update join syntax, but we may use correlated subqueries:
UPDATE table1
SET table1.columnA = (SELECT columnA FROM table2 WHERE table1.col = table2.col),
SET table1.columnB = (SELECT columnB FROM table2 WHERE table1.col = table2.col),
SET table1.columnC = (SELECT columnC FROM table2 WHERE table1.col = table2.col);

Return a column once time

I execute a query in my DB:
SELECT table1.*, tabl2.* FROM table1 JOIN table2 USING(id);
In these 2 tables i have a common column "id". What I have to ask, in order to get the column 'id' once time in the results and not twice?
I thought one solution is to write down in the query which columns I want. But If I want to avoid this (as there are many) ?
Will it work for you to name specific columns you need from both tables? something like:
SELECT table1.id, table2.other_column1, table2.other_column2 FROM table1 JOIN table2 USING(id);
You are selecting all fields from both tables by using (*)

create table with constraints from another table in sqlite?

I want create table from another table with constraint?
I used this query "create table destination as select * from source;" fro table creation.
But its copy only the column name in table without column constraint.
There is a special table named sqlite_master, holding the full CREATE TABLE statement for each table (it's modified as appropriate during ALTER TABLE).
I would make my application retrieve that CREATE TABLE statement:
SELECT sql FROM sqlite_master WHERE type='table' AND name='source';
Then I would replace the table name right after CREATE TABLE tokens, and execute the result as a new sqlite query.
I don't think that it's possible to do in sqlite's pure SQL without extensions.

SQLite: JOIN back INTO left table?

If I have this statement,
SELECT table1.*, table2_1.`values` AS val_1
FROM table1 JOIN table2_1
ON table1.row_names=table2_1.row_names
I would actually like this the result joined back into table1. Any inclusion of a join statement after SELECT and before FROM gives me an error.
Why can I not save the results to a table, and is it possible to save it back to one of the original tables?
Because you do it the wrong way. SELECT statement will not modify you data no matter how much you want it. If you want to modify data, you need to use UPDATE statement.

Resources