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_id | first_name | last_name |
|---|---|---|
| 1 | Amit | Kumar |
| 2 | Neha | Singh |
| 3 | Ravi | Verma |
products
| product_id | product_name |
|---|---|
| 1 | Laptop |
| 2 | MobilePhone |
| 3 | Headphones |
customers
| customer_id | full_name |
|---|---|
| 1 | Amit Kumar␠ |
| 2 | Neha Singh␠ |
orders
| order_id | order_number |
|---|---|
| 1 | ORD2024001 |
| 2 | ORD2024123 |
product_descriptions
| id | description |
|---|---|
| 1 | old_string model |
| 2 | new old_string version |
๐งพ Summary Table (Questions)
| No. | Function | Question |
|---|---|---|
| 1 | CONCAT | Join first & last name |
| 2 | SUBSTRING | Extract part of string |
| 3 | CHAR_LENGTH | Find string length |
| 4 | UPPER | Convert to uppercase |
| 5 | LOWER | Convert to lowercase |
| 6 | TRIM | Remove extra spaces |
| 7 | LEFT | Get left characters |
| 8 | RIGHT | Get right characters |
| 9 | REPLACE | Replace 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_name | length |
|---|---|
| Laptop | 6 |
| MobilePhone | 11 |
| Headphones | 10 |
✔ 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)
| Function | Purpose |
|---|---|
| CONCAT | Join strings |
| SUBSTRING | Extract part |
| LENGTH | Count characters |
| UPPER | Uppercase |
| LOWER | Lowercase |
| TRIM | Remove spaces |
| LEFT | Left characters |
| RIGHT | Right characters |
| REPLACE | Replace 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.