How to do a Select in a Select - sql

I have a table containing a unique ID field. Another field (REF) contains a reference to another dataset's ID field.
Now I have to select all datasets where REF points to a dataset that doesn't exist.
SELECT * FROM table WHERE ("no dataset with ID=REF exists")
How can I do this?

3 ways
SELECT * FROM YourTable y WHERE NOT EXISTS
(SELECT * FROM OtherTable o WHERE y.Ref = o.Ref)
SELECT * FROM YourTable WHERE Ref NOT IN
(SELECT Ref FROM OtherTable WHERE Ref IS NOT NULL)
SELECT y.* FROM YourTable y
LEFT OUTER JOIN OtherTable o ON y.Ref = o.Ref
WHERE o.Ref IS NULL
See also Five ways to return all rows from one table which are not in another table

Try this:
SELECT * FROM TABLE WHERE NOT EXISTS
(SELECT * FROM OtherTable WHERE TABLE.Ref = OtherTable.ID)

I think this should work
SELECT * FROM table WHERE id NOT IN (SELECT ref_id FROM ref_table)
or with JOIN
SELECT table.*
FROM table LEFT JOIN ref_table ON table.id = ref_table.ref_id
WHERE ref_table.ref_id IS NULL

SELECT
table1.*
FROM
table1
LEFT JOIN table2 ON table1.id = table2.ref
WHERE
table2.ref IS NULL

You can do a subquery like:
select * from table where somefield not in (select otherfield from sometable where ID=REF)

SELECT *
FROM table
WHERE ((SELECT COUNT(*) FROM table2 WHERE table2.id = table.ref) = 0)

Something like that :
SELECT * FROM table WHERE ID NOT IN(SELECT REF FROM Table2 )

Yes you can use
select * from x where not exist ( select * from y )

Related

SQL Select where condition : value 1 <> value 2

Need your help to know if possible to select values from a table with the below condition :
Table content : matching between 2 objects
(Id_obj_A; name_obj_A; country_obj_A; Id_obj_B; name_obj_B; country_obj_B)
Select *
from table
Where (only if country_obj_A <> country_obj_B)
Many thanks for your help
Yes. There are a few ways, one is to use NOT EXISTS like this:
select
*
from tableA
where NOT EXISTS (
select NULL
from tableB
where tableB.country_obj_B = tableA.country_obj_A
)
or, using NOT IN
select
*
from tableA
where country_obj_A NOT IN (
select country_obj_B
from tableB
)
or, using a LEFT JOIN then exclude the joined rows:
select
*
from tableA
left join tableB on tableA.country_obj_A = tableB.country_obj_B
where tableB.country_obj_B IS NULL

SQL Statement not exists in

I want to select entries from first table that have no entries in second table
I tried this:
SELECT
first.clientid
FROM
table1 AS first,
table2 AS second
WHERE
first.clientid NOT IN second.clientid
but i have realize problem
any tips?
Wrong Syntax.
Either use IN:
SELECT clientid FROM table1
WHERE clientid NOT IN (SELECT clientid FROM table2);
Or use EXISTS:
SELECT clientid FROM table1
WHERE NOT EXISTS (SELECT * FROM table2 where table2.clientid = table1.clientid);
Or use MINUS (not avaliable in every DBMS):
SELECT clientid FROM table1
MINUS
SELECT clientid FROM table2;
You can use NOT EXISTS to do that:
SELECT first.clientid
FROM table1 AS first
WHERE NOT EXISTS
(SELECT * FROM table2 AS second WHERE first.clientid = second.clientid)

Select statement to select item in table 1 that does not exist in table 2

I am writing a simple select statement to compare two different tables.
table 1 table 2
a a
b b
c c
H d
e
f
I need to select any item in table 1 that does not exist in table 2.
You have a few options, one of which is
select table1.col from table1 where
not exists (select col from table2 where table2.col = table1.col)
SELECT table_1.name
FROM table_1
LEFT JOIN table_2 ON table_1.name = table_2.name
WHERE table_2.name IS NULL
Subquery should do it:
Select * from table1
where Id not in
(select distinct col from table2)
Since it looks like there is only one column.
Try this.
select * from table a -- select all of the things in a
minus
select * from table b -- remove from it the things in b

Simple SQL Query problem: how to only "select" items from table_A which are also in table_B

This is the query I have been using in sqlite / python:
Select wbCode from CountriesList_A
but I would like to have some sort of IF statement to only return me the items which are ALSO in CountriesListB.
Suggestions? Thank you very much.
You can just join to the other table using an inner join, which only returns rows that are present in both tables.
SELECT a.wbCode
FROM CountriesList_A a
INNER JOIN CountriesList_B b ON a.wbCode = b.wbCode
You can do this:
select wbCode
from CountriesList_A
where wbCode in (select wbCode from CountriesListB)
TRY:
Select * From tableA A
Where Exists
(Select * From Table B
Where wbCode = A.wbCode )
Select a.wbCode from CountriesList_A a
WHERE EXISTS (
select b.wbCode
FROM CountriesListB b
WHERE a.wbCode = b.wbCode)
Or
Select a.wbCode from CountriesList_A a
WHERE a.wbCode IN (
select b.wbCode
FROM CountriesListB b)
Or
Select a.wbCode
from CountriesList_A a inner join CountriesListB b
on a.wbCode = b.wbCode

Self-join of a subquery

I was wondering, is it possible to join the result of a query with itself, using PostgreSQL?
You can do so with WITH:
WITH subquery AS(
SELECT * FROM TheTable
)
SELECT *
FROM subquery q1
JOIN subquery q2 on ...
Or by creating a VIEW that contains the query, and joining on that:
SELECT *
FROM TheView v1
JOIN TheView v2 on ...
Or the brute force approach: type the subquery twice:
SELECT *
FROM (
SELECT * FROM TheTable
) sub1
LEFT JOIN (
SELECT * FROM TheTable
) sub2 ON ...
Do you mean, the result of a query on a table, to that same table. If so, then Yes, it's possible... e.g.
--Bit of a contrived example but...
SELECT *
FROM Table
INNER JOIN
(
SELECT
UserID, Max(Login) as LastLogin
FROM
Table
WHERE
UserGroup = 'SomeGroup'
GROUP BY
UserID
) foo
ON Table.UserID = Foo.UserID AND Table.Login = Foo.LastLogin
Yes, just alias the queries:
SELECT *
FROM (
SELECT *
FROM table
) t1
JOIN (
SELECT *
FROM table
) t2
ON t1.column < t2.other_column