SQLite - sorting question - sqlite

I am using the following query to display database rows in an alphabetical order:
SELECT word_id FROM table1 ORDER BY word ASC
But I want to get values from table2, where I don't have column "word" and to sort it by column "word" which is in table1.
I want something like this:
SELECT word_id FROM table2 ORDER BY table1.word ASC
Thank you in advance.

You must connect the two tables with a join:
SELECT t2.word_id
FROM table2 t2
, table1 t1
WHERE t2.word_id = t1.word_id -- this is the join
ORDER BY t1.word ASC

Related

Sqlite3 GROUPBY with HAVING and ORDER BY not working

I have a SQL query that is calculating the correct output, and is even doing the order by correctly, however it is not executing the having clause. am i doing something wrong? It is not filtering the percentages at all.
select table1.name as theme,
printf("%.2f", cast(count(table2.name)as float)/(select count(table2.name)
from table1
join table2
where table1.id = table2.theme_id)*100) as percentage from table1
join table2
where table1.id = table2.theme_id
group by table1.id
having percentage >=5.00
order by percentage desc;
The problem is that since printf() returns a string, the calculated column percentage is a string and you compare it to 5.00 which is a number.
This comparison can't give you what you expect because it is not a comparison between numbers.
One way to solve this is to drop printf() and use round() which returns a number instead:
select table1.name as theme,
round(cast(count(table2.name)as float)/(select count(table2.name)
from table1
join table2
where table1.id = table2.theme_id)*100, 2) as percentage from table1
join table2
where table1.id = table2.theme_id
group by table1.id
having percentage >=5.00
order by percentage desc;
or cast percentage to float:
having cast(percentage as float) >= 5.00

SQL Query - adding output to SELECT statement

I have written an SQL query which amalgamates data from two separate tables with the following query:
SELECT * FROM table 1
UNION ALL
SELECT * FROM table 2
ORDER BY column 1
What I'd like to be able to do is to add a column or 'stamp' in a newly created column which details the table which each text entry originally came from. So my output would have a column which detailed the table which each row was originally from.
Essentially, the tables I have are made up of large quantities of numeric data and are hard to distinguish upon completing the Union command.
Thanks for any help.
Regards,
CJW.
You can select a scalar value from your selects, but you need to specify columns instead of *:
SELECT col1, col2, 'TABLE1' FROM table 1
UNION ALL
SELECT col1, col2, 'TABLE2' FROM table 2 ORDER BY column 1
You can simply add any expression(s) anywhere in the SELECT clause:
SELECT *, 1 AS SourceTable FROM Table1
UNION ALL
SELECT *, 2 AS SourceTable FROM Table2
ORDER BY Column1;

Join 2 different tables in sqlite

There are two tables TableA & TableB which has to be merged and shown.
for e.g.,
Table A has columnA, columnB
Table B has column1, column2, column3
I want to merge / join these two table and resultant should be
columnA, columnB, column1, column2, column3
Please help me how can i form the query? Is it possible to use join or union queries?
Your query should be like,
select * from TableA join TableB
It will merge both table like you want and resultant table have n x m records. n is number of records in TableA and m is number of records in TableB

Union columns from different tables

Assuming I have 2 tables:
Persons: ID, Name
Pets: PName
I want to select all the IDs from Persons and assign each one with a different PName or NULL if there are more IDs.
Basically it looks like a simple select query from Persons and then somehow append a select query from Pets but I'm not sure how to do that. I tried to use JOIN and UNION but none of them gave me exactly what I wanted.
Create temporary tables for IDs and names:
CREATE TEMP TABLE t1(ID) AS SELECT ID FROM Persons;
CREATE TEMP TABLE t2(PName) AS SELECT PName FROM Pets;
Then use the rowid to join them:
SELECT t1.ID, t2.PName
FROM t1 LEFT JOIN t2 ON t1.rowid = t2.rowid;

How to know if a row doesn't exist?

I have the following query:
SELECT rowid FROM table1 ORDER BY RANDOM() LIMIT 1
And as well I have another table (table3). In that table I have columns table1_id and table2_id. table1_id is a link to a row in table1 and table2_id is a link to a row in another table.
I want in my query to get only those results that are defined in table3. Only those that have table1 rowid in their table1_id column. There may not be any columns at all referring to a certain table1 rowid so in this case I don't want to receive them.
How can I achieve this goal?
Update: I tried the following query, which doesn't work:
SELECT rowid FROM table1
WHERE rowid IN (SELECT table1_id FROM table3 WHERE table1_id = table1.rowid)
ORDER BY RANDOM() LIMIT 1
SELECT rowid FROM table1
WHERE rowid IN ( SELECT DISTINCT table1_id FROM table3 )
ORDER BY RANDOM() LIMIT 1;
This query means "choose at random a row from table1 which has an entry in table3".
Every row in table1 equal likelihood of being selected (DISTINCT) as long as it is referenced at least once in table3.
If you are trying to get more than one result, then you should remove the "ORDER BY RANDOM() LIMIT 1" clause.
Assuming you want to select more than just a rowid, you need to SELECT from a JOIN between the tables you're interested in. SQLite doesn't have the full set of standard JOIN functionality, so you'll need to re-work your query so it can use a LEFT OUTER JOIN.
SELECT table1.rowid, table1.other_field
FROM table3
LEFT OUTER JOIN table1 ON table3.table1_id = table1.rowid
ORDER BY RANDOM()
LIMIT 1;

Resources