String Functions in SQL

Perfect ๐Ÿ‘
Now STRING FUNCTIONS in SQL in the same crystal-clear pattern:

  • One base table

  • Question

  • SQL

  • Exact OUTPUT shown

  • ✔ short explanation


๐Ÿ“˜ Base Tables (Used for ALL Examples)

employees

emp_idfirst_namelast_name
1AmitKumar
2NehaSingh
3RaviVerma

products

product_idproduct_name
1Laptop
2MobilePhone
3Headphones

customers

customer_idfull_name
1Amit Kumar␠
2Neha Singh␠

orders

order_idorder_number
1ORD2024001
2ORD2024123

product_descriptions

iddescription
1old_string model
2new old_string version

๐Ÿงพ Summary Table (Questions)

No.FunctionQuestion
1CONCATJoin first & last name
2SUBSTRINGExtract part of string
3CHAR_LENGTHFind string length
4UPPERConvert to uppercase
5LOWERConvert to lowercase
6TRIMRemove extra spaces
7LEFTGet left characters
8RIGHTGet right characters
9REPLACEReplace text

1️⃣ CONCAT()

Question: Show full name of employees.

SELECT CONCAT(first_name, ' ', last_name) AS full_name
FROM employees;

✅ Output

full_name
Amit Kumar
Neha Singh
Ravi Verma

✔ Joins multiple strings into one


2️⃣ SUBSTRING() / SUBSTR()

Question: Extract first 5 characters of product name.

SELECT SUBSTRING(product_name FROM 1 FOR 5) AS substring
FROM products;

✅ Output

substring
Lapto
Mobil
Headp

✔ Extracts part of a string


3️⃣ CHAR_LENGTH() / LENGTH()

Question: Find length of each product name.

SELECT product_name, CHAR_LENGTH(product_name) AS length
FROM products;

✅ Output

product_namelength
Laptop6
MobilePhone11
Headphones10

✔ Counts number of characters


4️⃣ UPPER()

Question: Convert employee first names to uppercase.

SELECT UPPER(first_name) AS uppercase_first_name
FROM employees;

✅ Output

uppercase_first_name
AMIT
NEHA
RAVI

✔ Converts text to uppercase


5️⃣ LOWER()

Question: Convert employee last names to lowercase.

SELECT LOWER(last_name) AS lowercase_last_name
FROM employees;

✅ Output

lowercase_last_name
kumar
singh
verma

✔ Converts text to lowercase


6️⃣ TRIM()

Question: Remove trailing spaces from customer names.

SELECT TRIM(TRAILING ' ' FROM full_name) AS trimmed_full_name
FROM customers;

✅ Output

trimmed_full_name
Amit Kumar
Neha Singh

✔ Removes unwanted spaces


7️⃣ LEFT()

Question: Get first 5 characters of product name.

SELECT LEFT(product_name, 5) AS left_product_name
FROM products;

✅ Output

left_product_name
Lapto
Mobil
Headp

✔ Extracts characters from left


8️⃣ RIGHT()

Question: Get last 4 characters of order number.

SELECT RIGHT(order_number, 4) AS right_order_number
FROM orders;

✅ Output

right_order_number
0001
4123

✔ Extracts characters from right


9️⃣ REPLACE()

Question: Replace old_string with new_string.

SELECT REPLACE(description, 'old_string', 'new_string')
AS replaced_description
FROM product_descriptions;

✅ Output

replaced_description
new_string model
new new_string version

✔ Replaces text inside a string


๐Ÿง  One-Line Memory Table (String Functions)

FunctionPurpose
CONCATJoin strings
SUBSTRINGExtract part
LENGTHCount characters
UPPERUppercase
LOWERLowercase
TRIMRemove spaces
LEFTLeft characters
RIGHTRight characters
REPLACEReplace text

If you want next:

  • Numeric functions

  • Date & Time functions

  • MCQs (output-based)

  • All SQL functions cheat-sheet

  • Hindi explanation for exams

Just say ๐Ÿ‘

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.