how to display employee names starting with a and then b in sql

select employee_name 
from employees
where employee_name LIKE 'A%' OR employee_name LIKE 'B%'
order by employee_name

To get employee names starting with A or B listed in order...

select employee_name 
from employees
where employee_name LIKE 'A%' OR employee_name LIKE 'B%'
order by employee_name

If you are using Microsoft SQL Server you could use

....
where employee_name  LIKE '[A-B]%'
order by employee_name

This is not standard SQL though it just gets translated to the following which is.

WHERE  employee_name >= 'A'
       AND employee_name < 'C' 

For all variants you would need to consider whether you want to include accented variants such as Á and test whether the queries above do what you want with these on your RDBMS and collation options.


Regular expressions work well if needing to find a range of starting characters. The following finds all employee names starting with A, B, C or D and adds the “UPPER” call in case a name is in the database with a starting lowercase letter. My query works in Oracle (I did not test other DB's). The following would return for example:
Adams
adams
Dean
dean

This query also ignores case in the ORDER BY via the "lower" call:

SELECT employee_name 
FROM employees
WHERE REGEXP_LIKE(UPPER(TRIM(employee_name)), '^[A-D]')
ORDER BY lower(employee_name)

select columns
  from table
 where (
         column like 'a%' 
      or column like 'b%' )
 order by column asc

Tags:

Sql