I read the Laravel manual and found this symbol in the code below:
select * from users where name = 'John' or (votes > 100 and title <> 'Admin')
So what dose "<>" mean??
It means "not equal to" in SQL.
Both <> and != are OK I think!
Both != and <> has same Properties IN SQL:
!= :
Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.
(a != b) is true.
<>:
Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.
(a <> b) is true.
Related
In SQL where clause,
Is
.....
AND NOT (A OR B OR C)
.....
the same as
.....
AND ((NOT A) OR (NOT B) OR (NOT C))?
.....
Thank you!
No, it's not the same. First and second will return different result. First one will return the result of the or condition.
Query below will be like the first you wrote, it will filter and return result you want.
SELECT * FROM tablename
WHERE NOT(columnname= 'columnvalue' OR columnname= 'columnvalue')
For second one, you need to use AND instead of OR. This is because you apply 'NOT' to each one.
SELECT * FROM tablename
WHERE (NOT(columnname= 'columnvalue') AND NOT(columnname= 'columnvalue'))
You can refer to this example
SQL uses ternary logic with truth tables including true, false, and unknown to handle comparisons with null. In the OR truth table, (false or unknown) == unknown (which is falsy) but (true or unknown) == true
Consider:
SELECT 1
WHERE not (1=2 or 1=null)
Versus
SELECT 2
WHERE (not 1=2) or (1=null)
Example SqlFiddle
Redgate article with more about ternary logic in SQL
I am currently learning SQL utilizing Codecademy and am curious if there is a difference between using "IS" or "=".
In the current lesson, I wrote this code:
SELECT *
FROM nomnom
WHERE neighborhood IS 'Midtown'
OR neighborhood IS 'Downtown'
OR neighborhood IS 'Chinatown';
Which ran perfectly fine. I always like to look at the answer after to see if there was something I did wrong or could improve on. The answer had this code:
SELECT *
FROM nomnom
WHERE neighborhood = 'Midtown'
OR neighborhood = 'Downtown'
OR neighborhood = 'Chinatown';
Do IS and = function the same?
All that you want to know you can find it here:
The IS and IS NOT operators work like = and != except when one or both
of the operands are NULL. In this case, if both operands are NULL,
then the IS operator evaluates to 1 (true) and the IS NOT operator
evaluates to 0 (false). If one operand is NULL and the other is not,
then the IS operator evaluates to 0 (false) and the IS NOT operator is
1 (true). It is not possible for an IS or IS NOT expression to
evaluate to NULL. Operators IS and IS NOT have the same precedence as
=.
taken from: SQL As Understood By SQLite.
The important part is: ...except when one or both of the operands are NULL... because when using = or != (<>) and 1 (or both) of the operands is NULL then the result is also NULL and this is the difference to IS and IS NOT.
They work the same but "IS" is a keyword in MySQL and is generally used while comparing NULL values. While comparing NULL values "=" does not work.
SELECT * FROM nomnom WHERE neighborhood IS NULL
The above statement would run perfectly fine but
SELECT * FROM nomnom WHERE neighborhood = NULL
would result in an error.
They are the same for these cases, but further down the line you will discover one nifty little value called NULL.
NULL is a pain because... it doesn't exist.
0 = NULL returns FALSE;
Date <> [Column] will not return lines with NULL, only those with a value that is different.
Hell, even NULL = NULL returns false. And NULL <> NULL also returns false. That is why "IS" exists. Because NULL IS NULL will return true.
So as a general rule, use = for values.
Keep "IS" for null.
[Column] IS NULL
or
[Column] IS NOT NULL
And remember to always check if your column is nullable that you need to plan for null values in your WHERE or ON clauses.
I am confused and did not find in Google. Can anyone tell me What is Sql <> operator name?
<> is NOT Equal to, it's the same as !=
It's "not equal". Look in the list of operators for the database you're using, and find the appropriate section (usually "comparison operators"). For example:
SQL server
MySQL
Oracle
Postgres
It is the not equals operator.
Usage:
select *
from table
where foo <> 0
It is the Not Equal operator, but I am going to have to be verbose to get my answer posted because I haven't entered enough characters yet.
<> means not equal same as !=
Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (a <> b) or (a != b) is true.
Here is the answer – Technically there is no difference between != and <>. Both of them work the same way and there is absolutely no difference in terms of performance or result.
If != and <> both are the same, which one should be used in SQL queries?
You can use either != or <> both in your queries as both technically same but I prefer to use <> as that is SQL-92 standard.
In a table containing five records where the Toppings value is "Chocolate", two of them have the value "Yes" in the MaraschinoCherry column, the other three contain nothing in that column (not "No" - nothing/blank).
This query works fine:
select definition from desserts
where (Toppings = 'Chocolate') and
(MaraschinoCherry <> 'Yes')
order by id
...returning the expected three records; but this one returns nothing at all, rather than the two records I expect:
select definition from desserts
where (Toppings = 'Chocolate') and
(MaraschinoCherry = 'Yes')
order by id
???
The answer to your question is simple. Any comparison to a NULL value, with two exceptions, produces NULL as the result. So,
MaraschinoCherry = 'Yes'
and
MaraschinoCherry <> 'Yes'
Both return NULL when MaraschinoCherry has a NULL value. NULL comparisons are treated the same as FALSE.
The two exceptions are: IS NULL and IS NOT NULL. Note that "= NULL" always returns NULL, which is interpreted as FALSE.
The normal way to fix this is by using COALESCE:
COALESCE(MaraschinoCherry, 'NO') = 'Yes'
(The function ISNULL() is kind of equivalent, but COALESCE allows more arguments and is standard SQL.)
There are other ways you can fix this, such as by specifying a default value for the column when you define the table, by adding an explicit comparison to NULL, by declaring the column to be "NOT NULL", or in some databases by overriding the behavior of NULLs in comparisons to violate the SQL standards (HIGHLY NOT RECOMMENDED!).
Looking over code examples in my lecture slides, I notice this:
c = head();
s = null;
while (c <> null && s == null) {
if (c.value().matches("33812"))
s = c
c = next
}
Whilst I understand basically what the code does, I don't understand this particular part of the while loop condition "c <> null".
Thanks in advance!
It's almost certainly just a typo/braino for != ("not equal to"). <> is used in Basic and SQL, but not in any C-like language I'm familiar with.
c <> null is the same as c != null, which means that c is not equal to null. I can't think of any languages offhand that use it, but I know they exist. (Haskell uses <>, but it's for something completely different, and uses /= for null because it looks like the symbol ≠.)
As stated, <> means != or not equal to. The reasoning behind this is one operand cannot be equal if it is strictly greater/less than the other operand, thus <> really means all numeric possibilities EXCEPT ==, that is, !=.
<> and != mean "not equal to". SQL uses <> in general; depending on the language, this is either equivalent to != or not supported by its lexer.