select id as sales_id
from sl_sales
where sales_id not in (select sales_id from sl_sales_dtls )
Why above sql is wrong. I need to check if there is any sales without a details. Can any one guide me
select id as sales_id
from sl_sales
where sales_id not in (select sales_id from sl_sales_dtls )
Why above sql is wrong. I need to check if there is any sales without a details. Can any one guide me
You can not use aliases you have created in the same scope you have created them in. You need this:
select id as sales_id
from sl_sales
where id not in (select sales_id from sl_sales_dtls)
Here is another way to write this query, without using inline statement.
select S.id as sales_id
from sl_sales S
Left join sl_sales_dtls D on S.id=D.sales_id
Where D.sales_id is null
I would use NOT EXISTS
instead :
select sl.id as sales_id
from sl_sales sl
where not exists (select 1 from sl_sales_dtls dt where dt.sales_id = sl.id);
NOT IN
will return no row if sub-query
have null
value.