Lukas Lastarria
Lukas Lastarria

Reputation: 11

Query to retrieve all people with a last name starting with a specific letter

How to write a query to retrieve data, only with specific last name first letter. For example I need a query to retrieve last name starting with A, B, C and E.

Upvotes: 0

Views: 43199

Answers (3)

Joao Ricardo
Joao Ricardo

Reputation: 421

Below SQL works for most of the cases

SELECT * FROM people WHERE last_name LIKE '[A-E]%';

The brackets mean anything from alphabets A to E and the percentage (%) means followed by any number of any characters.

If your running SQLite, you can try the below SQL

SELECT * FROM people WHERE last_name >= 'A' and <= 'E';

Hope this helps.

Upvotes: 0

Lukas Lastarria
Lukas Lastarria

Reputation: 11

Thanks a lot. With the model you have provided me with I have develop the one below:

SELECT vendor_name,
  CONCAT(vendor_contact_last_name, ', ', vendor_contact_first_name) AS full_name
FROM vendors
WHERE vendor_contact_last_name LIKE 'A%' OR vendor_contact_last_name LIKE 'B%'
   OR vendor_contact_last_name LIKE 'C%' OR vendor_contact_last_name LIKE 'E%'
ORDER BY vendor_contact_last_name;

Upvotes: 0

Marcel Gwerder
Marcel Gwerder

Reputation: 8520

Use LIKE and %:

SELECT * FROM people WHERE last_name LIKE 'A%' OR last_name LIKE 'B%' OR last_name LIKE 'C%' OR last_name LIKE 'E%'

Upvotes: 4

Related Questions