Write a query to fetch records where the name starts with ‘A’.
Task:
Fetch employee records where the name starts with the letter 'A' from the employees table.
Step 1: Use the LIKE Operator
The SQL LIKE operator is used for pattern matching in strings.
-
LIKE 'A%'means:- Starts with
A - Followed by zero or more characters
%is a wildcard for any number of characters
- Starts with
SQL Query
SELECT * FROM employees
WHERE name LIKE 'A%';
Sample Data in employees Table (as added earlier)
| id | name | department | salary | hire_date |
|---|---|---|---|---|
| 1 | John Smith | HR | 50000.00 | 2020-01-15 |
| 2 | Alice Johnson | Engineering | 75000.00 | 2019-03-22 |
| 3 | Bob Lee | Sales | 62000.00 | 2021-07-10 |
| 4 | Mary Jane | Engineering | 80000.00 | 2018-11-05 |
| 5 | Tom Hardy | Marketing | 55000.00 | 2022-06-01 |
Output After Running Query
| id | name | department | salary | hire_date |
|---|---|---|---|---|
| 2 | Alice Johnson | Engineering | 75000.00 | 2019-03-22 |
Explanation:
- Only one record matches the condition: Alice Johnson
- Because her name starts with ‘A’
Additional Tips:
-
Case-sensitivity depends on the database:
- MySQL: Not case-sensitive by default (so
a%works likeA%) - PostgreSQL: Case-sensitive (
A%≠a%)
- MySQL: Not case-sensitive by default (so
-
To match case-insensitive in all databases, you can use:
WHERE LOWER(name) LIKE 'a%'
0
likes
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
