SQL AND & OR
AND & OR operators are used to filter records based on more than one condition.
SQL AND & OR Operator
The AND operator displays a record if both the first and second conditions are true.
If only one of the first condition and the second condition is true, the OR operator displays a record.
Demo Database
In this tutorial, we will use the RUNOOB sample database.
The following is the data selected from the "Websites" table:
| id | name | url --------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | Taobao | https://www.taobao.com/ | 13 | CN |
| 3 | php Chinese website | http://www.php.cn/ | 4689 | CN |
| 4 | Weibo | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
+--- -+-------------+--------------------------+----- --+---------+
AND Operator Example
The following SQL statement selects all websites whose country is "CN" and whose alexa ranking is greater than "50" from the "Websites" table:
Example
Execution output result:
OR operator example
The following SQL statement is from Select all customers whose country is "USA" or "CN" in the "Websites" table:
Example
Execution output result:
Combining AND & OR
You can also combine AND and OR (using parentheses to form complex expressions).
The following SQL statement selects all websites with alexa ranking greater than "15" and country "CN" or "USA" from the "Websites" table:
Example
Execution output result: