MYSQL AND & OR priority - mysql

I know its recomended to use parenthesis to separate and ,or statements.but I'm wondering how mysql engine does render statements without parenthesis.lets say we have this statement:
select * from users where A and B or C and D;
how would it be with parenthesis?

AND has higher priority than OR.
select * from users where (A and B) or (C and D);
Refer to: http://dev.mysql.com/doc/refman/5.7/en/operator-precedence.html

It is like in math expressions:
AND is like a PRODUCT
OR is like a SUM
so AND are executed before OR, unless differently indicated through parenthesis, like in math again.

AND has higher precedence than OR.
Priority of AND and OR operator in Mysql select query
Operator Precedence

Related

The BETWEEN operator cannot be used with a subquery; however, the BETWEEN operator can be used within the subquery

I am unable to understand following rule of subquery..can anyone please help me in understanding this with example if possible...
"The BETWEEN operator cannot be used with a subquery; however, the BETWEEN operator can be used within the subquery"
These are two different statements :
A) The BETWEEN operator cannot be used with a subquery
This example is not allowed:
select * from MY_TABLE where CODE between (select INI_COD from CONFIG) and 999
A subquery cannot be used as an operand of the BETWEEN operator
B) The BETWEEN operator can be used within a subquery
This example is allowed :
select CODE, (select top 1 NAME from NAMES where ID between CODE and 999)
from MY_TABLE
The BETWEEN operator can be used within your subqueries

Along with which clauses in MySQL 'NOT' keyword can be used? Indeed specifically in MySQL

There is a keyword 'NOT' in MySQL.
For example, consider the below SQL query in MySQL :
SELECT * FROM Customers WHERE Country NOT LIKE '%land%';
See in above query 'NOT' keyword is used along with the LIKE keyword.
I want to know along with which clauses and keywords the 'NOT' keyword is used specifically in MySQL database queries.
Can someone please provide me help in this regard with example queries?
Thank you.
The NOT keyword can be used with any boolean expression (and hence actually with any numeric expression but let's not go there). For example:
where not (a = b)
where not (a is null)
where not (a in ('x', 'y', 'z'))
where not (a = 1 or b = 2 or c = 3)
That is the keyword not. There are at least three other infix operators that contain not: not in, not like, and is not null. For these, the not is part of the operator name, not a separate keyword.

having issue using and/or in mysql query

I'm having an issue thinking how to write the query i need.
I'll give you an example of what i'm trying to do..
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND clause3=30||40
I need to select entries where clause1 must be exactly 10 and clause2 must be exactly 20, but clause3 can be 30 or 40 but have to be exactly 30 or exactly 40.
so it would select the entry if it were 10,20,30 or 10,20,40
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND clause3=30 OR clause3=40 is incorrect though isn't it.
The numbers and clauses are only for example.
Thanks.
You've got to use parentheses 'round your last condition and use two times clause3=:
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND (clause3=30 || clause3=40)
or
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND clause3 IN (30, 40)
Note
I would recommend to use the operator OR instead of ||, because in other SQL dialects (i.e. Oracle, Postgres) || is the concatenation operator. OR is standard SQL. So the first statement would better be written:
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND (clause3=30 OR clause3=40)
Explanation
The operator AND has got higher precedence than OR. Without parentheses the expression the following two statements would be evaluated the same
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND clause3=30 OR clause3=40
SELECT * FROM table WHERE (clause1=10 AND clause2=20 AND clause3=30) OR clause3=40
and that is not what you want. So either put parentheses around your OR expressions or use the simpler IN().
Use IN.
SELECT *
FROM table
WHERE clause1=10 AND clause2=20 AND
clause3 in (30,40)

MySQL AND & OR operator

What is an operator in MySQL queries that covers AND and OR. So if I wanted to select a row where value1=lol OR value2=loly or both do.
this will look for one of the values or both.
SELECT * FROM my_table WHERE your_column IN ( 'lol','loly' )
With a query with the OR it'll work:
SELECT * FROM my_table WHERE my_field = 'lol' OR my_field = 'loly'
it'll give you all the results with the first or the second
As an extra, I think you could be looking for WHERE with regular expressions: Check this manual page: https://dev.mysql.com/doc/refman/5.1/en/regexp.html
Sorry, this wasn't a very good question.
I now understand that the OR operator will work with either condition met, or both.
AND only works with both.
XOR works with either one but not both.
Credit goes to the comments replying to my question.

Can not Access Aliased Columns in Select Statement (MySQL)

I have a problem with Aliased Columns in MySQL!
My Query:
SELECT Price AS Pr, (Pr*10/100) FROM MyTable;
MySQL WorkBench Error: UnKnown Column 'Pr' in Field List !!!
I tested my query in W3Schools with no error !
I tested my query in W3Schools with no error!
This doesn't prove that your query is valid.
You can only use aliases in GROUP BY, ORDER BY or HAVING clauses. Your usage variant is not allowed, because the value of alias is not known when MySQL is selecting the 2-nd column.
I've got a suspicion that W3Schools uses MS Access to run user queries, and MS Access does allow such atrocity as referencing column aliases in a SELECT clause that are defined in the same SELECT clause.
The standard doesn't allow this and MySQL does follow standard in this particular case.
As for solution to your problem, I can see two options.
The more generic solution, which would run in probably any SQL product, would be to use a derived table:
SELECT
Pr,
(Pr * 10 / 100) AS SomethingElse
FROM
(
SELECT
SomeComplexExpression AS Pr
FROM MyTable
) AS sub
;
The other option would be to use a variable, which is MySQL-specific:
SELECT
#Pr := SomeComplexExpression AS Pr,
(#Pr * 10 / 100) AS SomethingElse
FROM MyTable
;
Finally, if you need to test/demonstrate if something can/cannot work in MySQL, I'd recommend using SQL Fiddle.