SQL IN and BETWEEN – WHERE Variations

sql-in-between
Spread the love

SQL IN

SQL IN helps you specify multiple values in a WHERE clause.

SYNTAX:
SELECT *
FROM DEMO_TAB
WHERE COL_NAME IN (VALUE_1, VALUE_2, . . . )
EXAMPLE:

For example we will use the following table:-

Suppose you want to find only the rows where Subject is either ‘English’ or ‘Maths’ , here’s how you can do it.

SELECT *
FROM MARKS_DATA
WHERE SUBJECT IN ('English', 'Maths');

You can also combine IN keyword with NOT in the WHERE Clause

SELECT *
FROM MARKS_DATA
WHERE SUBJECT NOT IN ('English','Maths');

SQL BETWEEN

SQL BETWEEN is used to specify range in WHERE clause.

SYNTAX:
SELECT *
FROM DEMO_TAB
WHERE COL BETWEEN VALUE_1 AND VALUE_2

We will be using the same example table!

Suppose you want to find marks BETWEEN 70 and 84, here is how you write the query:-

SELECT *
FROM MARKS_DATA
WHERE MARKS BETWEEN 70 AND 84

OUTPUT:

You can also use BETWEEN keyword with the NOT keyword in the WHERE clause.

SELECT *
FROM MARKS_DATA
WHERE MARKS NOT BETWEEN 70 AND 85

OUTPUT:

Thanks For Reading!

Sources : T-SQL Documentation

READ MORE: SQL JOINS