Sql Between In Like Is Null Complete Guide

 Last Update:2025-06-23T00:00:00     .NET School AI Teacher - SELECT ANY TEXT TO EXPLANATION.    6 mins read      Difficulty-Level: beginner

Understanding the Core Concepts of SQL BETWEEN, IN, LIKE, IS NULL

SQL BETWEEN Operator

The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates. The BETWEEN operator is inclusive, meaning it includes the start and end values specified.

Syntax:

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Example:

SELECT *
FROM employees
WHERE salary BETWEEN 50000 AND 100000;

This query selects all employees whose salary is between 50,000 and 100,000, inclusive.

SQL IN Operator

The IN operator allows you to specify multiple values in a WHERE clause. It is used when you want to match any one of several values.

Syntax:

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

Example:

SELECT *
FROM employees
WHERE department IN ('Sales', 'Marketing', 'HR');

This query selects all employees who work in the Sales, Marketing, or HR departments.

SQL LIKE Operator

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. It is often used with wildcard characters to match various conditions.

Common Wildcards:

  • % represents zero or more characters.
  • _ represents a single character.

Syntax:

SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;

Examples:

  • Using %:

    SELECT *
    FROM employees
    WHERE last_name LIKE 'S%';
    

    This query selects all employees whose last name starts with 'S'.

  • Using _:

    SELECT *
    FROM employees
    WHERE first_name LIKE 'J_n';
    

    This query selects all employees whose first name starts with 'J' and ends with 'n', having exactly three letters.

SQL IS NULL Operator

The IS NULL operator is used to filter records where a column has no value, i.e., it is NULL.

Syntax:

SELECT column_name(s)
FROM table_name
WHERE column_name IS NULL;

Example:

SELECT *
FROM orders
WHERE shipping_date IS NULL;

This query selects all orders that do not have a shipping date specified.

Important Information

  • BETWEEN Range Inclusivity: Remember that the BETWEEN operator is inclusive; it includes both the start and end values.
  • IN Operator with Subqueries: The IN operator can also be used with subqueries to filter based on a dynamic set of values.
  • LIKE Operator Performance: Use LIKE with caution, especially with leading wildcard characters ('%something'), as it can lead to performance issues and prevent the use of indexes.
  • IS NULL vs = NULL: Always use IS NULL to check for NULL values. The expression column_name = NULL will not return any rows because NULL is not a value but a placeholder for unknown data.

Conclusion

Online Code run

🔔 Note: Select your programming language to check or run code at

💻 Run Code Compiler

Step-by-Step Guide: How to Implement SQL BETWEEN, IN, LIKE, IS NULL

1. Using BETWEEN

BETWEEN is used to filter the result set within a certain range. The values can be numbers, dates, or text.

Example Table:

CREATE TABLE Employees (
    EmployeeID INT,
    Name VARCHAR(50),
    Position VARCHAR(50),
    Salary DECIMAL(10, 2),
    HireDate DATE
);

INSERT INTO Employees (EmployeeID, Name, Position, Salary, HireDate) VALUES
(1, 'Alice', 'Manager', 75000.00, '2018-01-15'),
(2, 'Bob', 'Developer', 60000.00, '2019-06-01'),
(3, 'Charlie', 'Designer', 55000.00, '2020-03-20'),
(4, 'David', 'Developer', 62000.00, '2018-05-10'),
(5, 'Eve', 'Analyst', 58000.00, '2021-01-05');

Example Query: Find employees whose salary is between 56,000 and 62,000.

SELECT * FROM Employees
WHERE Salary BETWEEN 56000 AND 62000;

Result:

EmployeeID | Name    | Position    | Salary  | HireDate  
----------------------------------------------------------
3          | Charlie | Designer    | 55000.00| 2020-03-20
4          | David   | Developer   | 62000.00| 2018-05-10
5          | Eve     | Analyst     | 58000.00| 2021-01-05

2. Using IN

IN is used to specify multiple values in a WHERE clause.

Example Query: Find employees who are either 'Developer' or 'Analyst'.

SELECT * FROM Employees
WHERE Position IN ('Developer', 'Analyst');

Result:

EmployeeID | Name    | Position  | Salary  | HireDate   
------------------------------------------------------
2          | Bob     | Developer | 60000.00| 2019-06-01
4          | David   | Developer | 62000.00| 2018-05-10
5          | Eve     | Analyst   | 58000.00| 2021-01-05

3. Using LIKE

LIKE is used in a WHERE clause to search for a specified pattern in a column.

Example Query: Find employees whose names start with 'A'.

SELECT * FROM Employees
WHERE Name LIKE 'A%';

Result:

EmployeeID | Name  | Position  | Salary  | HireDate   
-----------------------------------------------------
1          | Alice | Manager   | 75000.00| 2018-01-15

Example Query: Find employees whose names contain 'e'.

SELECT * FROM Employees
WHERE Name LIKE '%e%';

Result:

EmployeeID | Name  | Position  | Salary  | HireDate   
-----------------------------------------------------
2          | Bob   | Developer | 60000.00| 2019-06-01
4          | David | Developer | 62000.00| 2018-05-10
5          | Eve   | Analyst   | 58000.00| 2021-01-05

4. Using IS NULL

IS NULL is used to filter rows where a column has a NULL value.

Example Table:

CREATE TABLE Orders (
    OrderID INT,
    CustomerID INT,
    OrderDate DATE,
    Total DECIMAL(10, 2),
    PaymentType VARCHAR(50) NULL
);

INSERT INTO Orders (OrderID, CustomerID, OrderDate, Total, PaymentType) VALUES
(1, 101, '2022-02-15', 150.75, 'Credit Card'),
(2, 102, '2022-03-20', 200.00, NULL),
(3, 103, '2022-04-25', 125.50, 'PayPal'),
(4, 104, '2022-06-01', 99.99, NULL);

Example Query: Find orders where the payment type is not specified (NULL).

SELECT * FROM Orders
WHERE PaymentType IS NULL;

Result:

Top 10 Interview Questions & Answers on SQL BETWEEN, IN, LIKE, IS NULL

1. How does the BETWEEN operator work in SQL?

Answer: The BETWEEN operator is used to filter the result set within a certain range. The values can be numbers, text, or dates. It includes the boundary values unless excluded with NOT BETWEEN. Syntax:

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Example:

SELECT *
FROM employees
WHERE salary BETWEEN 30000 AND 50000;

This query retrieves all employees whose salaries are between $30,000 and $50,000, inclusive.

2. Can the BETWEEN operator be used with dates?

Answer: Yes, the BETWEEN operator can be used with dates. It is commonly used to filter results within a date range. Ensure the date format matches the database requirements. Example:

SELECT *
FROM orders
WHERE order_date BETWEEN '2023-01-01' AND '2023-12-31';

This query will fetch all orders placed in the year 2023.

3. What is the difference between BETWEEN and <=, >=?

Answer: The BETWEEN operator includes the boundary values, whereas using <= and >= does the same thing but requires manually specifying both conditions. BETWEEN is more concise and easier to write. Example:

SELECT *
FROM employees
WHERE salary BETWEEN 30000 AND 50000;

-- Equivalent query using <=, >=
SELECT *
FROM employees
WHERE salary >= 30000 AND salary <= 50000;

Both queries return the same result set.

4. How does the IN operator function in SQL?

Answer: The IN operator allows you to specify multiple values in a WHERE clause. It is used to match any of the values in a subquery or a list of values. Example:

SELECT *
FROM employees
WHERE department IN ('HR', 'IT', 'Finance');

This query fetches all employees from the departments HR, IT, and Finance.

5. Can the IN operator be used with subqueries?

Answer: Yes, the IN operator can be used with subqueries, which can return values from another table or calculation. Example:

SELECT *
FROM orders
WHERE customer_id IN (SELECT customer_id FROM customers WHERE country = 'USA');

This query retrieves all orders placed by customers from the USA.

6. How is the LIKE operator used in SQL?

Answer: The LIKE operator is used for pattern matching, and is very useful for searching within strings. It is often used with the wildcard % and _ characters.

  • % represents zero or more characters.
  • _ represents a single character. Example:
SELECT *
FROM customers
WHERE last_name LIKE 'S%';  -- All last names starting with 'S'
SELECT *
FROM customers
WHERE first_name LIKE 'J_n';  -- All first names starting with 'J', followed by any character, and ending with 'n'

7. What is the difference between '%' and '_' in the LIKE operator?

Answer: The % wildcard matches zero or more characters, while the _ wildcard matches exactly one character. Example:

SELECT *
FROM products
WHERE product_name LIKE 'W_'    -- Matches any product name starting with 'W' followed by exactly one character (e.g., Wax)
SELECT *
FROM products
WHERE product_name LIKE 'W%'    -- Matches any product name starting with 'W' (e.g., Wax, Widget, Widgetry)

8. How does the IS NULL operator work in SQL?

Answer: The IS NULL operator is used to filter records with a NULL value in a specified column. It is important because NULL represents the absence of any value. Example:

SELECT *
FROM employees
WHERE commission_pct IS NULL;

This query retrieves all employees who do not receive a commission.

9. Can IS NULL be combined with other conditions?

Answer: Yes, the IS NULL operator can be combined with other conditions using the AND or OR logical operators. Example:

SELECT *
FROM employees
WHERE department_id IS NULL AND salary > 30000;

This query fetches employees who are not assigned to any department and earn more than $30,000.

10. What is the difference between IS NULL and = NULL in SQL?

Answer: In SQL, you cannot use the equality operator = to check for NULL values. The correct operator for checking NULL values is IS NULL. The expression column = NULL will always evaluate to FALSE because NULL does not equal anything, not even another NULL. Example:

SELECT *
FROM employees
WHERE commission_pct IS NULL;  -- Correct way
SELECT *
FROM employees
WHERE commission_pct = NULL;  -- Incorrect way

The first query will return the desired results, while the second will return zero rows.

You May Like This Related .NET Topic

Login to post a comment.