How to select row with name starting with 'A' until name starting with 'D'? And sort them alphabetically? Something like a combination of LIKE and =<> ?
Sample Table:
ID Name
4001 Spartakol
4002 Tabunjong
4003 Mabini
4004 Carlos
4005 Antonio
4006 Babsy
4007 Jose
4008 David
4009 Cruz
Sample Output:
4005 Antonio
4006 Babsy
4004 Carlos
4009 Cruz
4008 David
with name starting with 'Cr' until name starting with 'D'
Sample Output:
4009 Cruz
4008 David
Here's another simpler solution
SELECT * FROM table_name WHERE name BETWEEN "A" AND "E" ORDER BY name
Guffa's answer is probably the most efficient. To be complete, you could also use
LIKE '[a-d]%'
Depending on your database COLLATION
, LIKE
might be case sensitive or not.
Select the names from 'A'
up to, but not including 'E'
:
select ID, Name
from SampleTable
where Name >= 'A' and Name < 'E'
order by Name
As this is a plain comparison, it can use an index if you have one for that field.
select id, name from Your Table where LOWER(LEFT(name, 1)) between 'a' and 'd'; order by name;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With