SQLite query select all records that does not exist in another table - sql

I am having some problem when trying to perform a SQLite query to get the records which does not exist from another table. Basically I have two database tables:
My exercise table stored all the exercises available whereas the bookedExercise table store the exercises booked by each users. What I am trying to do is, for example if the exercise does exist in the bookedExercise, it should be filtered out.
Here is the SQLite query which I used:
SELECT exercise.exerciseID, exercise.exerciseType, exercise.amout FROM exercise LEFT JOIN bookedExercise WHERE exercise.exerciseID = bookedExercise.exerciseID AND bookedExercise.exerciseID IS NULL
However, it returned me empty records. Any ideas?
Thanks in advance.

If you're fine with not using joins you could use
SELECT * FROM exercise WHERE exerciseID not in (SELECT exerciseID FROM bookedExercise)

When you are using LEFT JOIN, you must put the join condition into the ON clause:
SELECT exercise.exerciseID,
exercise.exerciseType,
exercise.amout
FROM exercise /* !! */
LEFT JOIN bookedExercise ON exercise.exerciseID = bookedExercise.exerciseID
WHERE bookedExercise.exerciseID IS NULL

Your SQL looked okay... I think the problem might be you have a datatype mismatch. You have exercise ID as an integer in one table and text in another.
Also, if you have huge data volumes, you may want to consider an anti-join:
select
e.*
from
exercise e
where not exists (
select 1
from bookedExercise be
where
e.excerciseId = be.exerciseID
)
-- edit --
On second glance, your SQL was not okay. You had your join condition in the where clause. This little change would fix your existing SQL:
SELECT exercise.excerciseId , exercise.exerciseType , exercise.amout
FROM exercise LEFT JOIN bookedExercise on
exercise.excerciseId = bookedExercise.exerciseID
WHERE bookedExercise.exerciseID IS NULL

Related

SQL query on Maria DB

i tried to figure out how to do the number 7 query on Helpdesk DB on SQLZOO (medium questions) but I can't. Not even using subquery or not exist statement. Here is the link (n. 7).
http://sqlzoo.net/wiki/Helpdesk_Medium_Questions
select first_name, last_name
from Caller a
left join Issue b
on a.Caller_id = b.Caller_id
where Call_date is null
This query says -
select ALL rows from Caller table regardless of them having a join
select ONLY rows from Issue where they have a join
Therefore, any row that returns from the Caller table that has not made a call will have a null value for Call_date. The where clause says only give me those rows.
You need to use LEFT JOIN on the Issue table and then add a filter where a field in the Issue table is null
SELECT ca.First_name, ca.Last_name
FROM Caller ca
LEFT JOIN Issue i ON ca.Caller_id = i.Caller_id
WHERE i.Caller_id is null

SQL ORACLE Joining two queries together as one?

thanks in advance for your help.
I have a set of tables like this.
Also, here is a semi-accurate ERD to see what's going on.
I need to list all donations made by individual Alumni, and businesses. I need the first/last name and IDs present.
The following code returns all I need about the Alumni donors:
SELECT DonationID, Donation.AlumniID, BusinessID_FK, DONATIONDATE, Value, Alumnus.FirstName, Alumnus.LastName
FROM DONATION
JOIN Alumnus
on Donation.AlumniID=Alumnus.alumniid;
And the following code returns all I need about the Business donors:
SELECT * FROM DONATION
JOIN BusinessSponser
on Donation.businessid_fk=businesssponser.businessid;
Furthermore, the following query returns all the IDs, however no First/Last names:
SELECT * FROM DONATION
LEFT JOIN BusinessSponser
on Donation.businessid_fk=businesssponser.businessid;
So my question is, how can I merge these queries together as one? My intention is to create a view that would display the final result.
Thanks in advance for your help.
The outer join is a SQL technique which allows us to join rows from one table with some rows from another table. In your case you have two optional tables so you need two left outer joins:
select donationid
, donation.donationdate
, donation.value
, donation.alumniid
, alumnus.firstname
, alumnus.lastname
, donation.businessid
, businesssponser.businessname
from donation
left outer join alumnus
on donation.alumniid=alumnus.alumniid;
left outer join businesssponser
on donation.businessid_fk=businesssponser.businessid;

Getting way more results than expected in SQL left join query

My code is such:
SELECT COUNT(*)
FROM earned_dollars a
LEFT JOIN product_reference b ON a.product_code = b.product_code
WHERE a.activity_year = '2015'
I'm trying to match two tables based on their product codes. I would expect the same number of results back from this as total records in table a (with a year of 2015). But for some reason I'm getting close to 3 million.
Table a has about 40,000,000 records and table b has 2000. When I run this statement without the join I get 2,500,000 results, so I would expect this even with the left join, but somehow I'm getting 300,000,000. Any ideas? I even refered to the diagram in this post.
it means either your left join is using only part of foreign key, which causes row multiplication, or there are simply duplicate rows in the joined table.
use COUNT(DISTINCT a.product_code)
What is the question are are trying to answer with the tsql?
instead of select count(*) try select a.product_code, b.product_code. That will show you which records match and which don't.
Should also add a where b.product_code is not null. That should exclude the records that don't match.
b is the parent table and a is the child table? try a right join instead.
Or use the table's unique identifier, i.e.
SELECT COUNT(a.earned_dollars_id)
Not sure what your datamodel looks like and how it is structured, but i'm guessing you only care about earned_dollars?
SELECT COUNT(*)
FROM earned_dollars a
WHERE a.activity_year = '2015'
and exists (select 1 from product_reference b ON a.product_code = b.product_code)

Replacing Not In clause in sql query

I have a temporary table #allocations
which has the following fields
DAllocationId DAllocationName FundCode DSplitTotal DDisabled DistAlloc AAllocationId AAllocationName ASplitTotal ADisabled
I have another table TRAN_POST_PTN which also has these columns along with other columns.
So for asplit id and dsplit id we just have the same column name which is "posting number" in TRAN_POST_PTN.
What i need to do is insert into my allocations table all the rows from TRAN_POST_PTN where the
posting_number is not in (select DAllocationId from #allocations)
and posting_number not in (select AAllocationId from #allocations)
I do not want to use Not in here.
Can some one please suggest me a better way of writing this query.
I tried writing it using union,but that did not work.
There are a couple alternatives to NOT IN. Here's one using NOT EXISTS:
SELECT fields
FROM TRAN_POST_PTN tpp
WHERE NOT EXISTS (
SELECT 1
FROM #allocations a
WHERE tpp.posting_number IN (a.DAllocationId, a.AAllocationId))
Another common way is to use LEFT JOIN with NULL checks, but I believe you'll see a better performance with NOT EXISTS.
SELECT fields
FROM TRAN_POST_PTN tpp
LEFT JOIN #allocations a ON tpp.posting_number = a.DAllocationId
OR tpp.posting_number = a.AAllocationId
WHERE a.DAllocationId IS NULL

Sql join shows null value

I have 2 sql tables mtblSite_Lavel_Budget and mtblExpenditure_Site_Lavel. I want to join them, so I wrote the query below, but one column always give null result although there are values in that field.
Query is as below
select mtblExpenditure_Site_Lavel.SFTI_Id,
mtblExpenditure_Site_Lavel.Site_Lavel_Interventions,
mtblExpenditure_Site_Lavel.Sub_Site_Lavel_Interventions,
mtblExpenditure_Site_Lavel.Bill_No, mtblExpenditure_Site_Lavel.Date_Of_Bill,
mtblExpenditure_Site_Lavel.Eligible_Exp,
mtblExpenditure_Site_Lavel.Non_Eligible_Exp,
mtblExpenditure_Site_Lavel.Total,
mtblExpenditure_Site_Lavel.Physical_Progress,
mtblSite_Lavel_Budget.Sanction_Amount_DPR
from mtblExpenditure_Site_Lavel
left join mtblSite_Lavel_Budget
on mtblExpenditure_Site_Lavel.Sub_Site_Lavel_Interventions= mtblSite_Lavel_Budget.Sub_Site_Lavel_Interventions
WHERE mtblExpenditure_Site_Lavel.SFTI_Id =13
ORDER BY Date_Of_Bill desc
and the result is as below
Please advise regarding the issue.
Try this
from mtblExpenditure_Site_Lavel left join mtblSite_Lavel_Budget on
mtblExpenditure_Site_Lavel.sfti_id = mtblSite_Lavel_Budget.sfti_id
I think its confused becouse you are joining on a string column its always best to join on the id where posible.