![]() |
In SQL Server, the In this article, We will learn about the How to Combine SQL SQL Combine
|
employee_id | employee_name | department | salary |
---|---|---|---|
1 | Alice Johnson | Marketing | 75000.00 |
2 | Bob Smith | Sales | 68000.00 |
3 | Charlie Brown | IT | 95000.00 |
4 | Diana Prince | Marketing | 72000.00 |
5 | Evan Roberts | HR | 60000.00 |
6 | Fiona Clark | IT | 88000.00 |
Query:
SELECT *
FROM employees
WHERE (employee_name LIKE 'A%' OR employee_name LIKE 'F%')
AND department IN ('Marketing', 'IT');
Output:
employee_id | employee_name | department | salary |
---|---|---|---|
1 | Alice Johnson | Marketing | 75000.00 |
6 | Fiona Clark | IT | 88000.00 |
Explanation: This query retrieves all employees whose names start with ‘A’ or ‘F’ and belong to the departments ‘Marketing’ or ‘IT’. The parentheses around the LIKE
conditions ensure that the OR
condition is evaluated before the AND
condition with IN
.
Query:
SELECT *
FROM employees
WHERE (employee_name LIKE '%Charlie%' OR employee_name LIKE '%Bob%')
AND department IN ('IT', 'Sales');
Output:
employee_id | employee_name | department | salary |
---|---|---|---|
2 | Bob Smith | Sales | 68000.00 |
3 | Charlie Brown | IT | 95000.00 |
Explanation: This query retrieves all employees whose names contain the word ‘Charlie’ or ‘Bob’ and belong to the departments ‘IT‘ or ‘Sales‘. The use of parentheses ensures that the OR
conditions for the LIKE
pattern matching are properly grouped before applying the AND
condition with IN
.
Combining the LIKE
and IN
operators in SQL Server provides a powerful way to filter data based on both specific patterns and predefined categories. This versatile approach allows for complex and precise queries, enhancing the capability to retrieve relevant information efficiently.
Reffered: https://www.geeksforgeeks.org
Databases |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 23 |