Can we write conditions using both AND and OR?


#1

Question

In a SQL query, can we write conditions that utilize both AND and OR?

Answer

Yes, queries can combine multiple conditions using AND and OR without a real limit to how many conditions you can combine. However, the more conditions you combine, the more specific the results will be and the more complex it can get.

Example

/* 
This will select movies with id values 
from 10 to 20 inclusive, OR with id 
values from 50 to 60 inclusive.
*/
SELECT *
FROM movies
WHERE
(id > 10 AND id < 20)
OR
(id > 50 AND id < 60);