SQL - to evaluate multiple conditions, is OR faster than AND? - sql

Just wonder how conditional statements work behind the scene.
I am checking if multiple fields are all NULL.
-- OR: if there is any NOT NULL value
CASE
WHEN field_1 IS NOT NULL OR field_2 IS NOT NULL
THEN 1 ELSE 0
END
-- AND: if all values are null
CASE
WHEN field_1 IS NULL AND field_2 IS NULL
THEN 1 ELSE 1
END
Just wonder if it will be faster if I use OR rather than AND, because if condition 1 returns True then other conditions will not be evaluated?
Or it will be the same because all conditions are evaluated independently?
Do all SQL engines work in the same way on this matter? I'm using Teradata

Related

Anonymize data in select statement

We have a requirement to anonymize user columns, so they show NULL instead of the username.
Though of doing this
case when 1 = 1 then NULL else NULL end as USERNAME
But of course this doesn't work: At least one of the result expressions in a CASE specification must be an expression other than the NULL constant.
Is there a cleaner way to anonymize data in a select statement?

<> and != avoiding NULL in WHERE condition

I have a table PATIENT with Column STATUS. When I queried to get STATUS not equal to 1, I was expecting the result as NULL and 2.
But I am only getting 2 as the result. Can someone help me with this?
CREATE TABLE #PATIENT
(STATUS INT)
INSERT INTO #PATIENT (STATUS)
SELECT 1
UNION
SELECT 2
UNION
SELECT NULL
SELECT * FROM #PATIENT WHERE STATUS <> 1
When I queried with
SELECT * FROM #PATIENT WHERE ISNULL(STATUS, 0) != 1
I am able to get NULL and 2 as the result.
This is SQL SERVER 2012.
You can use OR in WHERE with Condition STATUS IS NULL .
SELECT * FROM #PATIENT WHERE STATUS <> 1 OR STATUS IS NULL
This will do it.
EDIT:
Conceptually, NULL means “a missing unknown value” and it is
treated somewhat differently from other values.
You cannot use arithmetic comparison operators such as =, <, or <>
to test for NULL
Because the result of any arithmetic comparison with NULL is also
NULL, you cannot obtain any meaningful results from such comparisons
we can not equate or not equate anything with null, thats why IS NULL
SELECT NULL <> 1 ===> NULL
Even though it is supposed to be true, it will return `NULL`
Hope this helps.
When you compare NULL value to any value then the result is always NULL.
So if you wan to select the NULL value as well then try this:
SELECT * FROM #PATIENT WHERE STATUS <> 1 OR STATUS IS NULL
DEMO
Conceptually "NULL" means a missing value. To test for NULL IS NULL or IS NOT NULL condition is used. Arithmetic operators cannot be used for comparing NULL values.
NULL<>1 :: returns false because NULL can be 1 or any other value (an unknown value).
Null is a special marker used in Structured Query Language (SQL) to indicate that a data value does not exist in the database.
And reason because you got only 2 and NOT NULL is because in SQL nothing is equal to NULL but also nothing is NOT equal to NULL.
In Simple words you cannot equate and not equate any value with NULL.

Checking tables for null returns 0 regardless

I don't understand why this query doesn't work.
I have a table which is full of rows where 3 of the column values are set to NULL.
But when I run the following query, it returns 0 (it should return 96)
SELECT COUNT(*) FROM SEAT WHERE BOOKED=null;
Do you know what I am doing wrong?
It depends on your database settings and the specific RDBMS you are using, but if you are using ANSI NULL syntax you cannot directly compare a value to NULL with an equality test (=) -- it will always fail.
Use WHERE BOOKED IS NULL instead.
You have to use IS NULL instead of = null
Since null technically isn't a value, you can't compare null using the = operator.
Use IS NULL
SELECT COUNT(*)
FROM SEAT
WHERE BOOKED IS NULL

Ordering by Nullness of field

Hello all :) I'm strugglind to come up with the right SQL syntax in Oracle 10g. I would like to come up with something like this:
SELECT
LAST_VALUE FIELD_INFO OVER(ORDER BY FIELD_1 IS NULL, FIELD_2, FIELD_1)
FROM TABLE_1
FIELD_1 IS NULL raising a syntax error.
How would you do it?
NULLs First
This expression is a compact Oracle syntax to return 0 for Nulls and 1 for non-Nulls
Order by NVL2(FIELD_1,1,0), ...
Or you could use a case statement:
Order by Case when FIELD_1 is null then 0 else 1 end, ...
NULLs Last
Order by NVL2(FIELD_1,0,1)
Order by Case when FIELD_1 is null then 1 else 0 end, ...
There's possibly a fractional optimisation in this method:
Order by Case when FIELD_1 is null then null else 0 end nulls last, ...
... through requiring slightly less sort area.
I think you should make a compound field and sort by it
COALESCE(FIELD_1,'[lots of spaces to ensure they go first]')||FIELD_2
But it would really help if you post 10-20 example records to show exactly, what you want to achieve.

Trouble comparing smallint in where clause

I have a table with a column called status of type smallint and accepts null. Sql server 2000.
My data contains mostly 2 in that field, but also 0 and null.
When I do
select *
from table
where status <> 2
I don't get all the proper records (where status is null or 0). Any idea why this is happening and how to correct? Shouldn't <> give me everything other than 2?
The NULL test doesn't match any arithmetic comparison.
use
where status <> 2 OR status is null
select *
from table
where ISNULL(status, 1) <> 2
NULL doesn't compare: so remove it.
Your example is the "common mistake" on Wikipedia too...
What you're expecting can be accomplished by setting ANSI_NULLS to off.
For example, try running these queries:
set ansi_nulls off
select case when 1 != null then 'true' else 'false' end
set ansi_nulls on
select case when 1 != null then 'true' else 'false' end
That being said, this is very non-standard SQL behaviour that you're expecting to see. NULL comparisons should always be considered false whether equals to or not equals to comparisons, as every developer will expect that type of SQL query behaviour.
Your WHERE clause should then look like:
where status <> 2 or status is null
Another option would be just to compare to status = 1, if that is the only status that you're expecting to be included in your query.