Search⌘ K
AI Features

Answer: String Comparison

Explore effective SQL string comparison methods like LIKE, NOT LIKE, REGEXP, INSTR, and XOR to filter data based on complex name patterns. Understand how to use aliases, WHERE clauses, and logical operators to handle string queries in MySQL with practical examples and alternative solutions.

Solution

The solution is given below:

MySQL
-- The query to find employees with either "J" or "H," but not both, in their names
SELECT EName AS "Employee Name" FROM Employees
WHERE (EName LIKE '%J%' AND EName NOT LIKE '%H%')
OR (EName LIKE '%H%' AND EName NOT LIKE '%J%');

Code explanation

The explanation of the solution code is given below:

  • Line 2: The SELECT statement selects the column EName from the table Employees. We use AS to set an alias for the column.

  • Lines 3–4: The WHERE clause specifies the condition on which we want to retrieve the data from the table. Here, we use the LIKE and NOT LIKE operator to filter the names that contain J but not H, or H but not J.

Recalling relevant concepts

We have covered the following concepts in this question:

  • Selective columns

  • Aliases

  • Filtering ...