SQL: “IN“ Condition

The SQL "IN" condition helps reduce the need to use multiple SQL "OR" conditions. The SQL "IN" condition can be used in any valid SQL statement - SQL SELECT statement,SQL INSERT statementSQL UPDATE statement, or SQL DELETE statement.

The syntax for the "IN" condition is:

expression in (value1, value2, .... value_n);

The SQL "IN" condition will return the records where expression is value1, value2..., or value_n.

SQL "IN" Condition - Character example

The following is an SQL SELECT statement that uses the IN condition to compare character values:

SELECT *
FROM suppliers
WHERE supplier_name in ('IBM', 'Hewlett Packard', 'Microsoft');

This SQL "IN" condition example would return all rows where the supplier_name is either IBM, Hewlett Packard, or Microsoft. Because the * is used in the select, all fields from the suppliers table would appear in the result set.

It is equivalent to the following statement:

SELECT *
FROM suppliers
WHERE supplier_name = 'IBM'
OR supplier_name = 'Hewlett Packard'
OR supplier_name = 'Microsoft';

As you can see, using the SQL "IN" condition makes the statement easier to read and more efficient.

SQL "IN" Condition - Numeric example

You can also use the SQL "IN" condition with numeric values.

For example:

SELECT *
FROM orders
WHERE order_id in (10000, 10001, 10003, 10005);

This SQL "IN" condition example would return all orders where the order_id is either 10000, 10001, 10003, or 10005.

It is equivalent to the following statement:

SELECT *
FROM orders
WHERE order_id = 10000
OR order_id = 10001
OR order_id = 10003
OR order_id = 10005;

SQL "IN" Condition - Using the NOT operator

The SQL "IN" condition can also be combined with the SQL NOT operator.

For example,

SELECT *
FROM suppliers
WHERE supplier_name not in ( 'IBM', 'Hewlett Packard', 'Microsoft');

This SQL example would return all rows where the supplier_name is neither IBM, Hewlett Packard, or Microsoft. Sometimes, it is more efficient to list the values that you do notwant, as opposed to the values that you do want.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值