SQL Query for Matching Multiple Values in the Same Column



To query data in SQL where you need to match multiple values within the same column, you can use operators like IN and OR. For example, how would you find employees in the "Sales" or "Marketing" departments, or list those with job titles like "Manager" or "Salesperson"?

These are common scenarios when filtering data based on multiple conditions in SQL: The IN operator allows you to match values from a list, while OR helps combine multiple conditions efficiently.

In this article, we'll show you how to write SQL queries to match multiple values within the same column, making it easier to filter and retrieve the exact data you need.

SQL Query for Matching Multiple Values in the Same Column

When we want to match multiple values in the same column, we can use the IN and OR operators. Let's go through the steps together and see how we can do this.

Step 1: Creating the Database

The first step is to create a database to store our employee data. We will create a new database called tutorialspoint.

CREATE DATABASE tutorialspoint;

Output

Output Image

Step 2: Switching to the Database

Next, we need to switch to the tutorialspoint database to ensure that we work within this database.

USE tutorialspoint;

Output

Output Image

Step 3: Creating the Employees Table

Now, we create a table named employees with columns for employee ID, name, department, job title, and salary.

CREATE TABLE employees (
    emp_id INT PRIMARY KEY,           -- Employee ID
    emp_name VARCHAR(100),            -- Employee Name
    dept_name VARCHAR(50),            -- Department Name
    job_title VARCHAR(50),            -- Job Title
    salary DECIMAL(10, 2)             -- Employee Salary
);

Step 4: Inserting Sample Data

Next, we will add sample employee data to the table, including information such as department, job title, and salary, to make it easier to query the data later.

INSERT INTO employees (emp_id, emp_name, dept_name, job_title, salary)
VALUES
    (1, 'John Doe', 'Sales', 'Manager', 75000),
    (2, 'Jane Smith', 'Marketing', 'Salesperson', 55000),
    (3, 'Michael Brown', 'Sales', 'Salesperson', 48000),
    (4, 'Sarah Johnson', 'HR', 'Manager', 80000),
    (5, 'David Clark', 'Marketing', 'Manager', 95000),
    (6, 'Emily White', 'HR', 'Salesperson', 42000);

The table is created. You can check it by running:

SELECT * FROM employees;
output order

Step 5: Using the IN Operator

We will now match multiple values in the same column. Using the IN operator, we can retrieve employees from the "Sales" or "Marketing" department. We will run the query below to retrieve employees from those departments.

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

Output

output order

Step 6: Using the OR Operator

Next, let's match employees by their job title. We'll use the OR operator to match employees with the titles "Manager" or "Salesperson." The query below will show all employees with either of these titles.

SELECT * FROM employees
WHERE job_title = 'Manager' OR job_title = 'Salesperson';

Output

output order

Step 7: Combining IN and OR Operators

We can also combine the IN and OR operators. For example, to find employees in the "Sales" or "Marketing" department with the job title "Manager" or "Salesperson," we can use both operators together. This query will return employees who match both conditions: they must be in one of these departments and have one of these job titles.

SELECT * FROM employees
WHERE dept_name IN ('Sales', 'Marketing')
AND (job_title = 'Manager' OR job_title = 'Salesperson');

Output

Output Image

Conclusion

In this article, we learned how to match multiple values in the same column using SQL's IN and OR operators. These methods simplify filtering data based on different conditions, making it faster and easier to retrieve the specific information you need from your database.

Updated on: 2025-01-17T12:07:30+05:30

58 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements