Answer: String Comparison

Find a detailed explanation about string comparison in SQL.

Solution

The solution is given below:

Press + to interact
-- 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 ...

Access this course and 1400+ top-rated courses and projects.