Open In App

SQL Query to Find the Name of a Person Whose Name Starts with Specific Letter

Improve
Improve
Like Article
Like
Save
Share
Report

Here, we are going to see how to find the name of a person whose name starts with a specified letter in SQL. In this article, we will be making use of the Microsoft SQL Server as our database.

 For example, finding the name of the person whose name starts with the letter “H”. We will use the % symbol after the specified letter in the SQL query. Here, we will first create a database named “geeks” then we will create a table “department” in that database. After, that we will execute our query on that table.

Creating Database:

CREATE DATABASE geeks;

To use this database:

USE geeks;

This is our table in the geeks database

CREATE TABLE department(
    ID int,
    SALARY int,
    NAME Varchar(20),
    DEPT_ID Varchar(255));

To see the table:

EXEC sp_column department;

Add value into the table:

INSERT INTO department VALUES (1, 34000, 'ANURAG', 'UI DEVELOPERS');
INSERT INTO department VALUES (2, 33000, 'harsh', 'BACKEND DEVELOPERS');
INSERT INTO department VALUES (3, 36000, 'SUMIT', 'BACKEND DEVELOPERS');
INSERT INTO department VALUES (4, 36000, 'RUHI', 'UI DEVELOPERS');
INSERT INTO department VALUES (5, 37000, 'KAE', 'UI DEVELOPERS');

This is our data inside the table:

SELECT * FROM department;

Now let’s find the name of a person whose name starts with a specified letter:

Syntax:

SELECT “column_name”

FROM “table_name”

WHERE “column_name” LIKE {PATTERN};

Example 1: In this example, we will find the name starting with H.

 SELECT * FROM department WHERE NAME LIKE 'H%';

Output:

Example 2: In this example, we will find the name starting with R.

 SELECT * FROM department WHERE NAME LIKE 'R%';

Output:

Example 3 : In this example we will find the name ending with character ‘G’.

SELECT * FROM department WHERE NAME LIKE '%G';

Output :-

 


Last Updated : 24 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads