select id from table = 260 595 records
select id from table
left join table2 on table2.id = table.parent = 260 595 records
select id from table
inner join table2 on table2.id = table.parent = 260 192 records
What is the easiest way to find out what records in table have wrong (nonexistent) join so I can correct them?
Thanks.
select id from table left join table2 on table2.id = table.parent
EXCEPT
select id from table inner join table2 on table2.id = table.parent
Use this(it will give you id's in table that doesn't have corresponding records in table2):
select id
from table left join table2
on table2.id = table.parent
where table.parent is null
select id from table inner join table2 on table2.id <> table.parent
Related
I am learning sql (postgres) and id like to find values that do not exist.
I have a table, table 1 with ids and i want to find those ids that are not in table 4.
I have to join between 3 tables as table 1 holds id and table 4 contact_id (not the same number)
The tables 2,3 need to be joined as that connects the ids.
So how do i do that with “not exists”?
Select t1.id, table4.contact_id
From table1 t1
Join table2 using(id)
Join table3 using(id)
Join table4 using(contact_id)
Where not exists (
Select 1
From table4
Where table4.contact_id=t1.id
);
It returns no values, but should
No error msg…
I have thinking error i assume
Your query probably returns no values because you join table4 on contact_id and then you exclude in the WHERE clause the rows which come from this join.
To find values that don't exist, you can usually use LEFT JOIN or RIGHT JOIN or FULL OUTER JOIN and then filter the rows with NULL values in the WHERE clause.
Try this :
SELECT t1.id
FROM table1 t1
LEFT JOIN table2 t2 using(id)
LEFT JOIN table3 t3 using(id)
LEFT JOIN table4 t4 using(contact_id)
WHERE t4.contact_id IS NULL
I am faced with an issue returning data records. I first wanted to find records where a certain value existed '0000' in a column doing a join for 2 tables. Below is my T-SQL;
SELECT ColumnA, ColumnB, ColumnC
FROM Table1, Table2
WHERE Table1.ColumnB. = Table2.ColumnB
and ColumnC='0000'
This returns the desired data records where '0000' exists at least once in all returned records.
The question I have is, how do I do the same, only returning Distinct records where '0000' is the only value that exists (one or many times) and no other value exists for the returned data records
Many thanks!
SELECT distinct ColumnA, ColumnB
FROM Table1, Table2
WHERE Table1.ColumnB = Table2.ColumnB
and ColumnC = '0000'
except
SELECT distinct ColumnA, ColumnB
FROM Table1, Table2
WHERE Table1.ColumnB = Table2.ColumnB
and ColumnC <> '0000'
if you want to use a join
guessing ColumnC is in Table2
SELECT distinct Table1.ColumnA, Table1.ColumnB, Table2.ColumnC
FROM Table1
JOIN Table2
on Table1.ColumnB = Table2.ColumnB
and Table2.ColumnC = '0000'
left join Table2 exclude
on Table1.ColumnB = exclude.ColumnB
and exclude.ColumnC <> '0000'
where exclude.ColumnB is null
this may be the best performer
SELECT distinct Table1.ColumnA, Table1.ColumnB, Table2.ColumnC
FROM Table1
JOIN Table2
on Table1.ColumnB = Table2.ColumnB
and Table2.ColumnC = '0000'
and not exists (select * from table2 exclude
where exclude.ColumnB = Table1.ColumnB
and exclude.ColumnC <> '0000')
Here's a solution using the ALL keyword
SELECT DISTINCT
columna,
columnb,
columnc
FROM table1 t1
INNER JOIN table2 t2
ON table1.columnb = table2.columnb
WHERE t2.columnc = '0000'
AND t2.columnc = ALL (SELECT columnc
FROM table2 t2Check
WHERE t2.columnb = t2Check.columb)
Here's a Example where I'm using only one table since the joins in your problem aren't actually important.
OK so I have a query I am trying to build.. I have 2 tables, table1 has a bunch of regular records as normal with a unique ID (auto increment) and table2 has records that include some of those ids from table1. I am trying to order by the highest records with that same ID in table1.. Heres what I've got:
SELECT * FROM table1
WHERE table1.status = 1
AND (SELECT COUNT(*) FROM table2 WHERE table2.tbl1_id = table1.id)
ORDER BY table1.id DESC
Thanks :)
SELECT table1.id
FROM table1
LEFT JOIN table2 ON table2.tbl1_id = table1.id
WHERE table1.status = 1
GROUP BY table1.id
ORDER BY COUNT(table2.tbl1_id) DESC
Try this:
SELECT a.*, b.cnt
FROM table1 a LEFT JOIN
(
SELECT tbl1_id, COUNT(*) cnt
FROM table2
GROUP BY tbl1_id
) b
ON a.id = b.tbl1_id
WHERE table1.status = 1
ORDER BY cnt DESC
I wrote this, and it is wrong syntax, help me fix it, I want 'T' to be an alias of the result of the two inner joins.
select T.id
from table1
inner join table2 on table1.x = table2.y
inner join table3 on table3.z = table1.w as T;
You cannot use aliases to name the "entire" join, you can, however, put aliases on individual tables of the join:
select t1.id
from table1 t1
inner join table2 t2 on t1.x = t2.y
inner join table3 t3 on t3.z = t1.w
In the projection, you will have to use the alias of the table, which defines the id column you are going to select.
You can't directly name the result of a join. One option is to use a subquery:
select T.id
from (
select *
from table1
inner join table2 on table1.x = table2.y
inner join table3 on table3.z = table1.w
) T
Another option is subquery factoring:
with T as (
select *
from table1
inner join table2 on table1.x = table2.y
inner join table3 on table3.z = table1.w
)
select T.id
from T
I have a SQL like this:
Select tbl.id, tbl.name
From
(select table1.id, table1.name
from table1
inner join table2 on table1.id = table2.id
order by table2.priority
) tbl
group by table1.id
order by table1.name
What I'm trying to achieve is to first sort (order by table2.priority), and then get the record with table1.id, name with highest priority.
Note, MAX(table2.priority) doesn't work here, because table1 to table2 is one to many, and for one table1 record, table2 can have N records with the highest priority = 1, where another table1 record with highest priority = 3.
If you only need one record from the result, and they are in order such that the record you need is at the end (or beginning) of the sort, simply limit the results to one. i.e:
SELECT tbl.id, tbl.name
FROM (
SELECT table1.id, table1.name
FROM table1
INNER JOIN table2 ON table1.id = table2.id
ORDER BY table2.priority
) tbl
GROUP BY table1.id
ORDER BY table1.name
LIMIT 1;
Note that depending on the order, you could specify ASC or DESC to ensure the correct record is the one you retrieve.