Skip to content
Related Articles

Related Articles

Improve Article
SQL Query to Find the Name of a Person Whose Name Starts with Specific Letter
  • Difficulty Level : Basic
  • Last Updated : 07 Apr, 2021

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 DATBASE 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:

My Personal Notes arrow_drop_up
Recommended Articles
Page :