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

i want to display the employee names which having names start with a and b ,it should be like list will display employees with 'a' as a first letter and then the 'b' as a first letter...

so any body tell me what is the command to display these...

This question is related to sql

The answer is


If you're asking about alphabetical order the syntax is:

SELECT * FROM table ORDER BY column

the best example I can give without knowing your table and field names:

SELECT * FROM employees ORDER BY name

select name_last, name_first
from employees
where name_last like 'A%' or name_last like 'B%'
order by name_last, name_first asc

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)

Oracle: Just felt to do it in different way. Disadvantage: It doesn't perform full index scan. But still gives the result and can use this in substring.

select employee_name 
from employees
where lpad(employee_name,1) ='A'
 OR lpad(employee_name,1) = 'B'
order by employee_name

We can use LEFT in SQL Server instead of lpad . Still suggest not a good idea to use this method.


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

select * 
from stores 
where name like 'a%' or 
name like 'b%' 
order by name

We can also use REGEXP

select employee_name 
from employees
where employee_name REGEXP '[ab].*'
order by employee_name

Here what I understood from the question is starting with "a " and then "b" ex:

  • abhay
  • abhishek
  • abhinav

So there should be two conditions and both should be true means you cant use "OR" operator Ordered by is not not compulsory but its good if you use.

Select e_name from emp 
where  e_name like 'a%' AND e_name like '_b%'
Ordered by e_name

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

From A to Z:

select employee_name from employees ORDER BY employee_name ;

From Z to A:

select employee_name from employees ORDER BY employee_name desc ;

What cfengineers said, except it sounds like you will want to sort it as well.

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

Perhaps it would be a good idea for you to check out some tutorials on SQL, it's pretty interesting.