🙄

SQLにおける演算子

2024/06/09に公開

このページでは、SQLにおける算術演算子と比較演算子について記述します。

算術演算子

加算(+)

SELECT column1 + column2 AS result
FROM table_name;

減算(-)

SELECT column1 - column2 AS result
FROM table_name;

乗算(*)

SELECT column1 * column2 AS result
FROM table_name;

除算(/)

SELECT column1 / column2 AS result
FROM table_name;

剰余(%)

SELECT column1 % column2 AS result
FROM table_name;

比較演算子

等しい(=)

SELECT column1, column2
FROM table_name
WHERE column1 = column2;

等しくない(<>, !=):

SELECT column1, column2
FROM table_name
WHERE column1 <> column2;

より大きい(>)

SELECT column1
FROM table_name
WHERE column1 > 10;

より小さい(<)

SELECT column1
FROM table_name
WHERE column1 < 10;

以上(>=)

SELECT column1
FROM table_name
WHERE column1 >= 10;

以下(<=)

SELECT column1
FROM table_name
WHERE column1 <= 10;

BETWEEN

SELECT column1
FROM table_name
WHERE column1 BETWEEN 10 AND 20;

LIKE

SELECT column1
FROM table_name
WHERE column1 LIKE 'A%';

IN

SELECT column1
FROM table_name
WHERE column1 IN ('value1', 'value2', 'value3');

比較演算子(NULL)

IS NULL

SELECT column1
FROM table_name
WHERE column1 IS NULL;

IS NOT NULL

SELECT column1
FROM table_name
WHERE column1 IS NOT NULL;

論理演算子

AND演算子

SELECT column1, column2
FROM table_name
WHERE condition1 > 10 AND condition2 < 20;

OR演算子

SELECT column1, column2
FROM table_name
WHERE condition1 > 10 OR condition2 < 20;

NOT演算子

SELECT column1, column2
FROM table_name
WHERE NOT condition = 10;

参考文献

SQL 第2版 ゼロからはじめるデータベース操作

Discussion