select文、update 文、delete 文には where 句で検索条件を指定できます。ここでは where 句の詳細な書き方について学習します。
where句ではin、between、is、likeといった特別な演算子も用意されています。これらの使い方については後述します。
比較演算子
SQLの where 句では比較演算子(<, <=, =, !=など)を使って検索条件を定義できます。次のSQLでは courses テーブルから learning_time が 50 と等しいレコードを検索します。

MariaDB [eldb]> select * from courses where learning_time = 50;
+----+--------------+---------------+-------------+
| id | title | learning_time | category_id |
+----+--------------+---------------+-------------+
| 2 | PHP Database | 50 | 1 |
| 4 | Web Design | 50 | 2 |
+----+--------------+---------------+-------------+
2 rows in set (0.00 sec)
MariaDB [eldb]>
SQLにおける
=は代入演算子ではなく、等価演算子(等しいかどうか)として機能します。
論理演算子
論理演算子によって複数の条件式を組み合わせて検索できます。論理積は and 、論理和は or、否定は not 演算子を使います。論理積は日本語の "かつ” 、論理和は日本語の "あるいは" のように機能します。
論理積 and による条件指定
次のSQLは courses テーブルから learning_time が 50 と等しい、かつ category_id が 1 と等しいレコードを検索します。

MariaDB [eldb]> select * from courses
-> where learning_time = 50 and category_id = 1;
+----+--------------+---------------+-------------+
| id | title | learning_time | category_id |
+----+--------------+---------------+-------------+
| 2 | PHP Database | 50 | 1 |
+----+--------------+---------------+-------------+
1 row in set (0.01 sec)
MariaDB [eldb]>
論理和 or による条件指定
次のSQLは courses テーブルから learning_time が 50 と等しい、あるいは category_id が 1 と等しいレコードを検索します。

MariaDB [eldb]> select * from courses
-> where learning_time = 50 or category_id = 1;
+----+--------------+---------------+-------------+
| id | title | learning_time | category_id |
+----+--------------+---------------+-------------+
| 1 | PHP Basic | 30 | 1 |
| 2 | PHP Database | 50 | 1 |
| 3 | Python Basic | 40 | 1 |
| 4 | Web Design | 50 | 2 |
+----+--------------+---------------+-------------+
4 rows in set (0.00 sec)
MariaDB [eldb]>
否定 not による条件指定
次のSQLは courses テーブルから learning_time が 50 と等しくないレコードを検索します。

MariaDB [eldb]> select * from courses
-> where not learning_time = 50;
+----+-----------------+---------------+-------------+
| id | title | learning_time | category_id |
+----+-----------------+---------------+-------------+
| 1 | PHP Basic | 30 | 1 |
| 3 | Python Basic | 40 | 1 |
| 5 | Japan's History | 100 | NULL |
+----+-----------------+---------------+-------------+
3 rows in set (0.00 sec)
MariaDB [eldb]>
この場合、
select * from courses where learning_time != 50;としても結果は同じになります。