Null value in column resulting into no output in sql query - sql

I have 2 tables (stud and stud1). Both having 2 columns but stud1 contains 1 record which is null.
I have created following 2 queries.First one is returning the accurate result but other one which is using not in returning nothing. I guess that is because of the null value. But I don't understand the reason for it. Can someone help me with this?

See NOT IN clause and NULL values.
That's because your 2nd query equals:
SELECT * FROM #stud
WHERE ID <> NULL
When ansi_nulls is on, ID <> NULL is unknown, so you won't get any rows.

Related

Oracle SQL: Where 2 columns are not null

Should be simple enough I think but I'm not getting it and I'm not sure how to explain it without an example to look it up.
Table:
1. A B
2. null 1
3. null null
4. 1 null
5. 1 1
I want my query to omit anything where both column A and B are null (in example, row 3 would be omitted).
select * from Table where (A is not null) and (B is not null)
Is just giving me columns where both are present.
Any help would be great.
*edit: changed "return" to "omit"
You can filter like this:
select * from Table where nvl(A,B) is not null

Select from table with 2 condition and 2 operations

I have a Table BoxTrans
the table Contain Rows (ID,Date,FromBox,ToBox,Value)
I want to make a View like (ID,Date,Box,ValueIn,ValueOut)
select when frombox Give Value to ValueOut
and when tobox Give Value to ValueIN
You can use a CASE statement to check the value of a different column when populating a column. The below query will return your output as long as either ToBox or FromBox is NULL, if they are both not null you may get unexpected results.
SELECT ID,
Date,
COALESCE(ToBox,FromBox) as Box,
CASE WHEN ToBox IS NOT NULL THEN value ELSE NULL as ValueIn,
CASE WHEN FromBox IS NOT NULL THEN value ELSE NULL as ValueOut
FROM BoxTrans

Is a subquery, which is returning no row, equal to NULL?

In SQL Server, if my SELECT statement in a subquery returns no row, is then the result of the subquery equal to NULL? I made some research, but I am not sure about it.
Example:
IF (SELECT TOP 1 CLMN1 FROM SOMETABLE) IS NOT NULL THEN
....
I am asking to understand the behaviour of the if-statement above.
Looks like the answer is yes:
DECLARE #Test TABLE (Id INT)
INSERT INTO #Test VALUES (1)
SELECT * FROM #Test WHERE Id = 2
SELECT CASE WHEN (SELECT * FROM #Test WHERE Id = 2) IS NULL THEN 1 ELSE 0 END
EDIT: after you updated your question I think I should add that instead of checking if there are rows with IS NULL you should use the following that can be better optimised by the server:
IF EXISTS(SELECT * FROM #Test WHERE Id = 2)
BEGIN
-- Whatever
END
NULL means no value, for example that the "box" for a certain column in a certain row is empty. NO ROW means that there are no rows.
No, NULL is a column value that indicates that the value of that column for a given row has no valid value. There would have to be a row returned by your query for that row to contain NULL column values.
A query that returns no rows just means that no rows matched the predicate you used in the query and therefore no data was returned at all.
Edit: After the question was edited, my answer doesn't address the specific case called out in the question. Juan's answer above does.

SQL query to get null count from a column

I want to generate a SQL query to get null count in a particular column like
SELECT COUNT (column) AS count
FROM table
WHERE column = null ;
This is returning 0, but I want how many null values are present in that column like
SELECT COUNT (column) AS count
FROM table
WHERE column = 'some value';
which returns the count of the matched records
NULL value is special in that you cannot use = with it; you must use IS NULL instead:
SELECT COUNT (*) AS count FROM table where column IS null ;
This is because NULL in SQL does not evaluate as equal to anything, including other NULL values. Also note the use of * as the argument of COUNT.
You can use a conditional sum()
SELECT sum(case when column is null
then 1
else 0
end) AS count
FROM table
A different query but exact answer check it out
select count(*)-count(column) from table
please vote check this as answer if it helps you
To get exact output you can use below command as well -
SELECT COUNT (*) AS count FROM table where column IS null OR column='';
because some times only '' doesn't counted as NULL.

SQL 'select' Query with 3 conditions

SELECT *
FROM REVIEW
WHERE REVIEWERID =5 AND APPRAISEECONFIRMYN='Y' AND HRCONFIRMYN = NULL
Are 2 'AND' conditions allowed like this? I'm not getting the correct output. There are 2 records in the database that fulfil the above conditions. When I remove the last condition 'HRCONFIRMYN = NULL' and execute, I get the correct output.
How can this be solved? I need to check all 3 conditions whilst searchng the records.
To compare the NULL values, you have to use the IS NULL predicate instead of = NULL like so:
SELECT *
FROM REVIEW
WHERE REVIEWERID = 5
AND APPRAISEECONFIRMYN = 'Y'
AND HRCONFIRMYN IS NULL
use OR instead
group your condition
use IS NULL when comparing with NULLs
query,
SELECT *
FROM REVIEW
WHERE (REVIEWERID =5 AND APPRAISEECONFIRMYN='Y') OR
HRCONFIRMYN IS NULL