Skip to content
Home » SQL Exercise – 4

SQL Exercise – 4

SQL Exercise:

Write Query to:

  • Find all customers with the last name “Singh”.
  • Retrieve customers whose email address contains the word “kumar”.
  • Retrieve customers living in cities that end with “pur”.




Table Creation Script:

CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
Name VARCHAR(50),
City VARCHAR(50),
Email VARCHAR(100)
);

Insert Data into Customers Table:

INSERT INTO Customers (CustomerID, Name, City, Email)
VALUES
(1, 'Rajesh Kumar', 'New Delhi', 'rajesh.kumar@gmail.com'),
(2, 'Priya Sharma', 'Mumbai', 'priya_sharma@yahoo.com'),
(3, 'Anil Mehta', 'Jaipur', 'anil.mehta@outlook.com'),
(4, 'Sneha Singh', 'Kolkata', 'sneha.singh@gmail.com'),
(5, 'Suresh Iyer', 'Bangalore', 'suresh.iyer@gmail.com'),
(6, 'Kavita Rajan', 'Chennai', 'kavita.rajan@gmail.com'),
(7, 'Ajay Kapoor', 'Pune', 'ajay.kapoor@gmail.com');

Solution:

1. Customers with the last name ‘Singh’

SELECT * FROM customers 
WHERE name LIKE '%singh'

Explanation:

Name LIKE ‘%Singh’:

  • %Singh matches any string ending with “Singh”.
  • This will match “Sneha Singh”.

 

2. Customers whose email contains the word ‘kumar’

SELECT * FROM Customers
WHERE Email LIKE '%kumar%';

Explanation:

Email LIKE ‘%kumar%’:

  • %kumar% matches any string containing “kumar”.
  • This will match rajesh.kumar@gmail.com.

 

3. Customers living in cities ending with ‘pur’

SELECT * FROM Customers 
WHERE City LIKE '%pur';
Explanation:

City LIKE ‘%pur’:

  • %pur matches any string ending with “pur”.
  • This will match cities like “Jaipur”.




Loading

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.