DataBolt

All Lessons

Introduction to SQL
SQL Lesson 1: Your Sample Database
SQL Lesson 2: SELECT — Reading data
SQL Lesson 3: WHERE — Filtering rows
SQL Lesson 4: AND, OR, NOT
SQL Lesson 5: BETWEEN, IN, LIKE
SQL Lesson 6: NULL — The Mystery Value
SQL Lesson 7: ORDER BY
SQL Lesson 8: LIMIT & OFFSET
SQL Lesson 9: Aggregate Functions
SQL Lesson 10: GROUP BY
SQL Lesson 11: HAVING
SQL Lesson 12: INNER JOINs
SQL Lesson 13: LEFT JOINs
SQL Lesson 14: RIGHT JOINs
SQL Lesson 15: SELF JOINs
SQL Lesson 16: UNION JOINs
SQL Lesson 17: Joining Multiple Tables
SQL Lesson 18: Subqueries
SQL Lesson 19: CTEs (WITH)
SQL Lesson 20: CASE Statements
SQL Lesson 21: Window Functions
SQL Lesson 22: String Functions
SQL Lesson 23: Date & Time Functions
SQL Lesson 24: INSERT, UPDATE, DELETE
SQL Lesson 25: CREATE TABLE & DDL
SQL Lesson 26: Indexes & Performance
SQL Lesson 27: Transactions & ACID
SQL Lesson 28: SQL Execution Order
SQL Lesson 29: 50 Practice Problems

CHAPTER 23

String Functions

Manipulating and transforming text data.

IN THIS CHAPTER

  • UPPER, LOWER, TRIM — basic cleanup
  • CONCAT — combining strings
  • SUBSTRING, LEFT, RIGHT — extracting parts
  • REPLACE, INSTR — search and replace

FunctionWhat It DoesExample → Output
UPPER(str)Convert to uppercaseUPPER('sql book') → 'SQL BOOK'
LOWER(str)Convert to lowercaseLOWER('SQL') → 'sql'
TRIM(str)Remove leading + trailing spacesTRIM(' hi ') → 'hi'
LENGTH(str)Number of charactersLENGTH('Arjun') → 5
CONCAT(s1,s2,...)Join stringsCONCAT('Hello','SQL') → 'Hello SQL'
LEFT(str,n)First n charactersLEFT('Bangalore',4) → 'Bang'
RIGHT(str,n)Last n charactersRIGHT('Bangalore',4) → 'lore'
SUBSTRING(s,pos,n)Extract n chars from positionSUBSTRING('Hyderabad',2,5) → 'yderi'
REPLACE(s,old,new)Replace all occurrencesREPLACE('SQL is hard','hard','easy')
INSTR(str,sub)Position of substringINSTR('Hyderabad','bad') → 7

Real Examples

Full display string with city

SELECT CONCAT(name, ' — ', city, ', ', country) AS profile
FROM customers;

Included Tables in Exercise

Table: Employees

emp_idnamedepartmentsalarymanager_idhire_date
1ArjunIT85000NULL2020-06-15
2PriyaIT7200012021-03-10
3RahulSales5500052022-01-20
4SnehaHR6000052021-07-08
5VikramSales90000NULL2019-11-01
6DivyaIT6800012023-02-14
7KiranHR5800042022-08-30
8MeeraSales6200052023-05-12

Table: Customers

customer_idnamecitycountry
1AnanyaHyderabadIndia
2RohanBangaloreIndia
3SamMumbaiIndia
4LisaLondonUK
5RaviDelhiIndia

Exercise 👇

Exercise:

Tasks

1.👉Convert all employee names to uppercase.
2.Convert all customer names to lowercase.
3.Find the length of each employee's name.
4.Combine employee name and department into a single column.
5.Extract first 3 characters of each customer's city.
Stuck? Read this task's