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%'
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.