Queries using Substring Comparison
Assume we have the following Employees Table:
EmployeeID Name Department Salary
101 Alice Johnson HR 50000.00
102 Bob Smith IT 45000.00
103 Charlie Brown IT 47000.00
104 Dave Wilson HR 52000.00
105 Eve White Marketing 48000.00
1. Substring with LIKE
Find all employees whose names start with "A".
Sql> SELECT * FROM Employees WHERE Name LIKE 'A%';
Output:
EmployeeID Name Department Salary
101 Alice Johnson HR 50000.00
2. Substring Using SUBSTRING Function
Find employees whose last name (characters after the space in their name) starts with "W".
Sql> SELECT * FROM Employees WHERE SUBSTRING(Name, CHARINDEX(' ', Name) + 1,
LEN(Name)) LIKE 'W%';
Output:
EmployeeID Name Department Salary
104 Dave Wilson HR 52000.00
105 Eve White Marketing 48000.00
3. Extract Substring Using SUBSTRING
Get the first three characters of each employee's name.
Sql> SELECT EmployeeID, SUBSTRING(Name, 1, 3) AS Initials FROM Employees;
Output:
EmployeeID Initials
101 Ali
102 Bob
103 Cha
104 Dav
105 Eve
4. Substring with Case Conversion
Find employees whose names contain "smith" (case-insensitive).
Sql> SELECT * FROM Employees WHERE LOWER(Name) LIKE '%smith%';
Output:
EmployeeID Name Department Salary
102 Bob Smith IT 45000.00
5. Find Employees with Specific Patterns in Substrings
Find employees whose department contains "IT" within the string.
Sql> SELECT * FROM Employees WHERE Department LIKE '%IT%';
Output:
EmployeeID Name Department Salary
102 Bob Smith IT 45000.00
103 Charlie Brown IT 47000.00
6. Substring and LEFT/RIGHT Functions
Retrieve employees whose names end with "son".
Sql> SELECT * FROM Employees WHERE RIGHT(Name, 3) = 'son';
Output:
EmployeeID Name Department Salary
101 Alice Johnson HR 50000.00
Finding Values with a Certain Range
Here the examples of queries that find values within a specific range using the BETWEEN operator
and WHERE clause. Assume we have the following Employees Table:
EmployeeID Name Department Salary
101 Alice Johnson HR 50000.00
102 Bob Smith IT 45000.00
103 Charlie Brown IT 47000.00
104 Dave Wilson HR 52000.00
105 Eve White Marketing 48000.00
1. Finding Employees with Salaries in a Certain Range
Find employees with a salary between 45,000 and 50,000.
Sql> SELECT * FROM Employees WHERE Salary BETWEEN 45000 AND 50000;
Output:
EmployeeID Name Department Salary
102 Bob Smith IT 45000.00
103 Charlie Brown IT 47000.00
105 Eve White Marketing 48000.00
2. Finding Employees with EmployeeID in a Certain Range
Find employees with EmployeeID between 102 and 104.
Sql> SELECT * FROM Employees WHERE EmployeeID BETWEEN 102 AND 104;
Output:
EmployeeID Name Department Salary
102 Bob Smith IT 45000.00
103 Charlie Brown IT 47000.00
104 Dave Wilson HR 52000.00
3. Finding Employees Joined After a Certain Salary Range
Find employees whose salary is NOT between 46,000 and 50,000.
Sql> SELECT * FROM Employees WHERE Salary NOT BETWEEN 46000 AND 50000;
Output:
EmployeeID Name Department Salary
102 Bob Smith IT 45000.00
104 Dave Wilson HR 52000.00
4. Filter by Range Using Date
Assume we have an extra column JoinDate in the Employees table:
EmployeeID Name JoinDate
101 Alice Johnson 2023-01-10
102 Bob Smith 2023-03-15
103 Charlie Brown 2023-05-20
104 Dave Wilson 2023-08-25
105 Eve White 2023-10-05
Find employees who joined between March 1, 2023, and August 31, 2023.
Sql> SELECT * FROM Employees WHERE JoinDate BETWEEN '2023-03-01' AND '2023-08-31';
Output:
EmployeeID Name JoinDate
102 Bob Smith 2023-03-15
103 Charlie Brown 2023-05-20
104 Dave Wilson 2023-08-25
5. Find Employees with Salary in a Narrower Range
Find employees with a salary between 47,000 and 52,000.
Sql> SELECT * FROM Employees WHERE Salary BETWEEN 47000 AND 52000;
Output:
EmployeeID Name Department Salary
103 Charlie Brown IT 47000.00
104 Dave Wilson HR 52000.00
105 Eve White Marketing 48000.00
6. Find Employees with EmployeeIDs Outside a Certain Range
Find employees whose EmployeeID is not between 102 and 104.
Sql> SELECT * FROM Employees WHERE EmployeeID NOT BETWEEN 102 AND 104;
Output:
EmployeeID Name Department Salary
101 Alice Johnson HR 50000.00
105 Eve White Marketing 48000.00
Apart from the BETWEEN keyword, there are other ways to check for values within a range or
set.
7. Using >= and <= Operators
We can replace BETWEEN with a combination of >= and <= for range checking.
Find employees with a salary between 45,000 and 50,000.
Sql> SELECT * FROM Employees WHERE Salary >= 45000 AND Salary <= 50000;
Output:
EmployeeID Name Department Salary
102 Bob Smith IT 45000.00
103 Charlie Brown IT 47000.00
105 Eve White Marketing 48000.00
8. Using IN for Specific Values
If checking for specific values instead of a range, IN can be used.
Find employees with salaries of 45000, 47000, or 48000.
Sql> SELECT * FROM Employees WHERE Salary IN (45000, 47000, 48000);
Output:
EmployeeID Name Department Salary
102 Bob Smith IT 45000.00
103 Charlie Brown IT 47000.00
105 Eve White Marketing 48000.00
9. Using OR for Range Checking
We can also use OR for range comparisons, although this is less efficient for larger datasets.
Find employees with a salary less than 45,000 or greater than 50,000.
Sql> SELECT * FROM Employees WHERE Salary < 45000 OR Salary > 50000;
Output:
EmployeeID Name Department Salary
104 Dave Wilson HR 52000.00
10. Using NOT for Negating Conditions
We can combine NOT with BETWEEN, IN, or other conditions.
Find employees whose salary is not between 45,000 and 50,000.
Sql> SELECT * FROM Employees WHERE Salary NOT BETWEEN 45000 AND 50000;
Output:
EmployeeID Name Department Salary
104 Dave Wilson HR 52000.00
11. Using LIKE for Range-Like Comparisons
Although not for numerical ranges, LIKE can be used for pattern-based range checks.
Find employees whose names start with letters between "A" and "C".
Sql> SELECT * FROM Employees WHERE Name LIKE '[A-C]%' ;
Output:
EmployeeID Name Department Salary
101 Alice Johnson HR 50000.00
103 Charlie Brown IT 47000.00
Queries with string Comparison and Ordering
queries using string comparison and ordering based on the Employees table:
Employees Table
EmployeeID Name Department Salary
101 Alice Johnson HR 50000.00
102 Bob Smith IT 45000.00
103 Charlie Brown IT 47000.00
104 Dave Wilson HR 52000.00
105 Eve White Marketing 48000.00
1. String Comparison Using LIKE
Q: Find employees whose names start with the letter 'A'.
Sql> SELECT * FROM Employees WHERE Name LIKE 'A%' ;
Output:
EmployeeID Name Department Salary
101 Alice Johnson HR 50000.00
Q: Find employees whose names contain the word 'Smith'.
Sql> SELECT * FROM Employees WHERE Name LIKE '%Smith%' ;
Output:
EmployeeID Name Department Salary
102 Bob Smith IT 45000.00
2. String Comparison Using =
Query: Find employees from the department 'HR'.
Sql> SELECT * FROM Employees WHERE Department = 'HR';
Output:
EmployeeID Name Department Salary
101 Alice Johnson HR 50000.00
104 Dave Wilson HR 52000.00
3. Ordering Data
Query: Display all employees ordered by their names in ascending order.
Sql> SELECT * FROM Employees ORDER BY Name ASC;
Output:
EmployeeID Name Department Salary
101 Alice Johnson HR 50000.00
102 Bob Smith IT 45000.00
103 Charlie Brown IT 47000.00
104 Dave Wilson HR 52000.00
105 Eve White Marketing 48000.00
Query: Display all employees ordered by their salaries in descending order.
Sql> SELECT * FROM Employees ORDER BY Salary DESC;
Output:
EmployeeID Name Department Salary
104 Dave Wilson HR 52000.00
101 Alice Johnson HR 50000.00
105 Eve White Marketing 48000.00
103 Charlie Brown IT 47000.00
102 Bob Smith IT 45000.00
4. Combining String Comparison and Ordering
Query: Find employees whose names contain 'o' and order them by salary in ascending order.
Sql> SELECT * FROM Employees WHERE Name LIKE '%o%' ORDER BY Salary ASC;
Output:
EmployeeID Name Department Salary
102 Bob Smith IT 45000.00
103 Charlie Brown IT 47000.00
101 Alice Johnson HR 50000.00
5. Using NOT LIKE for Exclusion
Query: Find employees whose names do not start with 'D'.
Sql> SELECT * FROM Employees WHERE Name NOT LIKE 'D%';
Output:
EmployeeID Name Department Salary
101 Alice Johnson HR 50000.00
102 Bob Smith IT 45000.00
103 Charlie Brown IT 47000.00
105 Eve White Marketing 48000.00
6. Ordering by Multiple Columns
Query: Order employees by Department (ascending) and then by Salary (descending).
Sql> SELECT * FROM Employees ORDER BY Department ASC, Salary DESC;
Output:
EmployeeID Name Department Salary
104 Dave Wilson HR 52000.00
101 Alice Johnson HR 50000.00
103 Charlie Brown IT 47000.00
102 Bob Smith IT 45000.00
105 Eve White Marketing 48000.00
7. Using String Functions (LENGTH() or CHAR_LENGTH())
Query: Find employees whose names have more than 12 characters.
Sql> SELECT * FROM Employees WHERE LENGTH(Name) > 12;
Output:
EmployeeID Name Department Salary
101 Alice Johnson HR 50000.00
103 Charlie Brown IT 47000.00
8. Combining Conditions with AND and OR
Query: Find employees whose names start with 'A' or 'E' and work in the HR department.
Sql> SELECT * FROM Employees WHERE (Name LIKE 'A%' OR Name LIKE 'E%') AND
Department = 'HR';
Output:
EmployeeID Name Department Salary
101 Alice Johnson HR 50000.00
9. Find Names Using SUBSTR()
Query: Find employees whose names have 'li' starting at the 2nd position.
Sql> SELECT * FROM Employees WHERE SUBSTR(Name, 2, 2) = 'li';
Output:
EmployeeID Name Department Salary
101 Alice Johnson HR 50000.00
10. Ordering and Filtering Combined
Query: Find employees from the IT department, order their salaries in ascending order, and
display only the top result.
Sql> SELECT * FROM Employees WHERE Department = 'IT' ORDER BY Salary ASC
LIMIT 1;
Output:
EmployeeID Name Department Salary
102 Bob Smith IT 45000.00
Advanced Commands
1.String Concatenation Using CONCAT()
Query: Concatenate first and last names of employees.
Sql> SELECT EmployeeID, CONCAT(Name, ' - ', Department) AS FullInfo FROM Employees;
Output:
EmployeeID FullInfo
101 Alice Johnson - HR
102 Bob Smith - IT
103 Charlie Brown - IT
104 Dave Wilson - HR
105 Eve White - Marketing
2. Removing Leading and Trailing Spaces Using TRIM()
Query: Remove any leading or trailing spaces from employee names.
Sql> SELECT EmployeeID, TRIM(Name) AS TrimmedName FROM Employees;
Output:
EmployeeID TrimmedName
101 Alice Johnson
102 Bob Smith
103 Charlie Brown
104 Dave Wilson
105 Eve White
3. Removing Leading Spaces Using LTRIM()
Query: Remove leading spaces (spaces before the name) from employee names.
Sql>SELECT EmployeeID, LTRIM(Name) AS NameWithoutLeadingSpaces FROM Employees;
Output:
EmployeeID NameWithoutLeadingSpaces
101 Alice Johnson
102 Bob Smith
103 Charlie Brown
104 Dave Wilson
105 Eve White
4. String Padding Using LPAD() and RPAD()
Query: Pad employee names with '_' from the left to make the total length 15.
Sql> SELECT EmployeeID, LPAD(Name, 15, '_') AS PaddedLeft FROM Employees;
Output:
EmployeeID PaddedLeft
101 ______Alice Johnson
102 ______Bob Smith
103 ___Charlie Brown
104 ____Dave Wilson
105 _____Eve White
5. Case Conversion Using UPPER() and LOWER()
Query: Convert employee names to uppercase.
sql> SELECT EmployeeID, UPPER(Name) AS UpperName FROM Employees;
Output:
EmployeeID UpperName
101 ALICE JOHNSON
102 BOB SMITH
103 CHARLIE BROWN
104 DAVE WILSON
105 EVE WHITE
Query: Convert employee names to lowercase.
Sql> SELECT EmployeeID, LOWER(Name) AS LowerName FROM Employees;
Output:
EmployeeID LowerName
101 alice johnson
102 bob smith
103 charlie brown
104 dave wilson
105 eve white
6. Checking String Length Using LENGTH()
Query: Find employees whose name has more than 10 characters.
Sql>SELECT EmployeeID, NameFROM EmployeesWHERE LENGTH(Name) > 10;
Output:
EmployeeID Name
101 Alice Johnson
103 Charlie Brown
7. Replacing Substring Using REPLACE()
Query: Replace 'HR' with 'Human Resources' in department names.
Sql> Copy code SELECT EmployeeID, REPLACE(Department, 'HR', 'Human Resources')
AS UpdatedDepartment FROM Employees;
Output:
EmployeeID UpdatedDepartment
101 Human Resources
102 IT
103 IT
104 Human Resources
105 Marketing
8. Finding Position of Substring Using INSTR()
Query: Find the position of the substring 'son' in employee names.
Sql> SELECT EmployeeID, INSTR(Name, 'son') AS Position FROM Employees;
Output:
EmployeeID Position
101 7
102 5
103 9
104 5
105 4
9. Substring Extraction Using SUBSTRING()
Query: Extract the first 4 characters of employee names.
Sql> SELECT EmployeeID, SUBSTRING(Name, 1, 4) AS SubName FROM Employees;
Output:
EmployeeID SubName
101 Alic
102 Bob
103 Char
104 Dave
105 Eve