Tutorials  /  Databases

A Guide to the SQL IN Operator

Ccentron Redaktion · January 2024 ·4 min read ·Databases, Tutorial

SQL IN operator is used along with WHERE clause for providing multiple values as part of the WHERE clause.

1. SQL IN

SQL IN operator is almost like having multiple OR operators for the same column. Let’s discuss in detail about the SQL IN operator. There are two ways to define IN operator. We will discuss both the ways in details below.

1.1) Multiple Values as Part of IN

Syntax:

SQL
SELECT Column(s) FROM table_name WHERE column IN (value1, value2, ... valueN);

Using the above-mentioned syntax, we can define multiple values as part of IN operator. We will understand the above-mentioned syntax in more detail through some examples. Let’s consider the following Student table for example purpose.

RollNo StudentName StudentGender StudentAge StudentPercent
1 George M 14 85
2 Monica F 12 88
3 Jessica F 13 84
4 Tom M 11 78

Scenario: Get the percentage of students whose age is 12 or 13. Query:

SQL
SELECT StudentPercent FROM Student WHERE StudentAge IN ('12', '13');

Output:

StudentPercent
88
84

1.2) Select Query as Part of IN

Syntax:

SQL
SELECT Column(s) FROM table_name WHERE column IN (SELECT Statement);

Using the above-mentioned syntax, we can use SQL SELECT statement for providing values as part of the IN operator. We will understand the above-mentioned syntax in more detail through some examples. Let’s consider the following Product and Supplier table for example purpose.

PRODUCT Table

ProductId ProductName ProductPrice
1 Cookie 10
2 Cheese 11
3 Chocolate 15
4 Jam 20

SUPPLIER Table

ProductId ProductName SupplierName
1 Cookie ABC
2 Cheese XYZ
3 Chocolate ABC
4 Jam XDE

Scenario: Get the price of the product where the supplier is ABC. Query:

SQL
SELECT ProductPrice FROM Product WHERE ProductName IN (SELECT ProductName FROM Supplier WHERE SupplierName = "ABC");

Output:

ProductPrice
10
15

1.3) SQL Nested IN

We can also use IN inside other IN operator. To understand it better, let’s consider the below-mentioned scenario.

Scenario: Get the price of the product where the supplier is ABC and XDE. Query:

SQL
SELECT ProductPrice FROM Product WHERE ProductName IN (SELECT ProductName FROM Supplier WHERE SupplierName IN ( "ABC", "XDE" ));

Output:

ProductPrice
10
15
20
DB

Matching infrastructure at centron

Databases need care in production: centron can take over updates, backups and monitoring – from a single instance to a full cluster. Explore managed clusters →

2. SQL NOT IN

SQL NOT IN operator is used to filter the result if the values that are mentioned as part of the IN operator is not satisfied. Let’s discuss in detail about SQL NOT IN operator.

Syntax:

SQL
SELECT Column(s) FROM table_name WHERE Column NOT IN (value1, value2... valueN);

In the syntax above the values that are not satisfied as part of the IN clause will be considered for the result. Let’s consider the earlier defined Student table for example purpose.

Scenario: Get the percentage of students whose age is not in 12 or 13. Query:

SQL
SELECT StudentPercent FROM Student WHERE StudentAge NOT IN ('12', '13');

Output:

StudentPercent
85
78

2.1) Select Query as Part of SQL NOT IN

Syntax:

SQL
SELECT Column(s) FROM table_name WHERE column NOT IN (SELECT Statement);

Using the above-mentioned syntax, we can use SELECT statement for providing values as part of the IN operator. We will understand the above-mentioned syntax in more detail through some examples. Let’s consider the earlier defined Product and Supplier table for example purpose.

Scenario: Get the price of the product where the supplier is not ABC. Query:

SQL
SELECT ProductPrice FROM Product WHERE ProductName NOT IN (SELECT ProductName FROM Supplier WHERE SupplierName = "ABC");

That’s all for SQL IN and SQL NOT IN operator examples. A Guide to the SQL IN Operator

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Databases
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren