Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In SQLite, how do I select elements in one table that are not in other?

Tags:

sql

sqlite

I've two tables in SQLite:

Table1:
-------
id
name

Table2:
-------
id
temp_name

My question is, how do I write an SQL query that returns names in Table2 that are not in Table1?

For example:

Table1:
-------
1, 'john'
2, 'boda',
3, 'cydo',
4, 'linus'

Table2:
-------
1123, 'boda'
2992, 'andy',
9331, 'sille',
2,    'cydo'

In this example the SQL query should return elements andy, and sille from Table2, because they're not in Table1.

like image 689
bodacydo Avatar asked Feb 11 '13 18:02

bodacydo


People also ask

How can I get data that is not present in another table?

How to Select All Records from One Table That Do Not Exist in Another Table in SQL? We can get the records in one table that doesn't exist in another table by using NOT IN or NOT EXISTS with the subqueries including the other table in the subqueries.

How do you SELECT all records from one table that do not exist in another table Excel?

select [ selecting columns] From table1 Right OUTER JOIN table2 ON(table1. SQL> select e. select [ selecting columns] From table1 Right OUTER JOIN table2 ON(table1. select column_name from table 1 full outer join table 2 on(connection); here all the data from table 1 and table 2 will get retrieved.

How do I SELECT specific data in SQLite?

To select data from an SQLite database, use the SELECT statement. When you use this statement, you specify which table/s to select data from, as well as the columns to return from the query. You can also provide extra criteria to further narrow down the data that is returned.

How do I query two tables in SQLite?

To query data from multiple tables, you use INNER JOIN clause. The INNER JOIN clause combines columns from correlated tables.


1 Answers

This is how to do it in "obvious" standard SQL:

select *
from table2
where temp_name not in (select name from table1)

There are other methods, such as using left outer join, exists in the where clause, and the except operation.

like image 64
Gordon Linoff Avatar answered Oct 18 '22 20:10

Gordon Linoff