Add scalar functions lessons

feat/course
Kamran Ahmed 2 weeks ago
parent c176731aa7
commit a7e914024c
  1. 178
      src/data/courses/sql-mastery/chapters/scalar-functions/lessons/string-functions.md
  2. 66
      src/data/courses/sql-mastery/chapters/scalar-functions/lessons/what-are-they.md
  3. 5
      src/data/courses/sql-mastery/chapters/scalar-functions/scalar-functions.md

@ -0,0 +1,178 @@
---
title: String Functions
description: Learn how to manipulate text data using SQL string functions
order: 110
type: lesson-challenge
setup: |
```sql
CREATE TABLE customer (
id INT PRIMARY KEY,
name VARCHAR(255),
email VARCHAR(255)
);
INSERT INTO customer (id, name, email)
VALUES (1, 'john doe', 'john.DOE@example.com'),
(2, 'JANE SMITH', 'JANE.smith@example.com'),
(3, 'Alice Johnson', 'alice.johnson@example.com');
```
---
String functions are among the most commonly used scalar functions in SQL. They help you manipulate text data in various ways, from simple case changes to complex pattern matching.
Let's look at some of the most common string functions.
## Case Conversion Functions
SQL provides several functions to change the case of text data. These are useful for standardizing data or display formatting:
```sql
-- Convert to uppercase
SELECT UPPER('hello world'); -- Returns: HELLO WORLD
-- Convert to lowercase
SELECT LOWER('HELLO WORLD'); -- Returns: hello world
-- Convert first letter of each word to uppercase
SELECT INITCAP('hello world'); -- Returns: Hello World
```
> Note: MySQL does not support the `INITCAP` function.
## String Length and Extraction
If you need to know the length of a string or extract a portion of it, you can use the following functions:
```sql
-- Get string length
SELECT LENGTH('hello world'); -- Returns: 11
-- Extract substring
SELECT SUBSTRING('hello world', 1, 5); -- Returns: hello (from 1st character to the 5th character)
SELECT SUBSTRING('hello world', 7); -- Returns: world (from 7th character to the end)
-- Returns the first `n` characters of a string
SELECT LEFT('hello world', 2); -- Returns: he
-- Returns the last `n` characters of a string
SELECT RIGHT('hello world', 3); -- Returns: rld
```
## String Trimming
Trimming functions help remove unwanted spaces or characters from strings. This is particularly useful when cleaning up data:
```sql
-- Remove spaces from both ends
SELECT TRIM(both ' ' from ' hello world '); -- Returns: hello world
-- Remove spaces from left
SELECT LTRIM(' hello world '); -- Returns: hello world
-- Remove spaces from right
SELECT RTRIM(' hello world '); -- Returns: ' hello world'
```
## String Concatenation
SQL provides multiple ways to combine strings. The standard concatenation operator is `||`, though some databases use different operators or functions.
```sql
-- Using concatenation operator
SELECT 'hello' || ' ' || 'world'; -- Returns: hello world
-- Using concat function
SELECT CONCAT('hello', ' ', 'world'); -- Returns: hello world
```
## String Replacement
You can replace parts of strings using various functions. This is useful for data cleaning and formatting:
```sql
-- Replace characters
SELECT REPLACE('hello world', 'o', '0'); -- Returns: hell0 w0rld
-- Pad string with characters
SELECT LPAD('hello', 10, '*'); -- Returns: *****hello
SELECT RPAD('hello', 10, '*'); -- Returns: hello*****
```
## Practical Examples
Given the following `customer` table:
| id | name | email |
| --- | ------------- | ------------------------- |
| 1 | john doe | john.DOE@example.com |
| 2 | JANE SMITH | JANE.smith@example.com |
| 3 | Alice Johnson | alice.johnson@example.com |
Let's clean up the data in the `customer` table using string functions we covered in this lesson.
### Standardize Email Addresses
Notice how the email addresses are not in a consistent format in `customer` table. We can use the `LOWER` function to standardize the email addresses to lowercase.
```sql
SELECT
name,
LOWER(email) as standardized_email
FROM customer;
```
The output will be:
| name | standardized_email |
| ------------- | ------------------------- |
| john doe | john.doe@example.com |
| JANE SMITH | jane.smith@example.com |
| Alice Johnson | alice.johnson@example.com |
### Extract Domain from Email Address
Let's say that we want to extract the domain from the email address. We can use the `SPLIT_PART` function in PostgreSQL.
```sql
SELECT
email,
SPLIT_PART(email, '@', 2) as domain
FROM customer;
```
The output will be:
| email | domain |
| ------------------------- | ----------- |
| john.DOE@example.com | example.com |
| JANE.smith@example.com | example.com |
| alice.johnson@example.com | example.com |
> **Note**: MySQL does not support SPLIT_PART function, so we can use the `SUBSTRING_INDEX` function instead.
>
> ```sql
> -- Extract domain from email
> SELECT
> email,
> SUBSTRING_INDEX(email, '@', -1) as domain
> FROM customer;
> ```
### Format Names Consistently
Let's say that we want to format the names consistently. We can use the `UPPER` and `LOWER` functions to format the names. `LEFT` to get the first character and `SUBSTRING` to get the rest of the characters.
```sql
-- Format names consistently
SELECT
name,
CONCAT(
UPPER(LEFT(name, 1)),
LOWER(SUBSTRING(name, 2))
) as formatted_name
FROM customer;
```
String functions are particularly useful when you need to standardize data or extract specific parts of strings, clean up user input, or generate formatted output.
In the next lesson, let's explore some numeric functions for mathematical operations.

@ -0,0 +1,66 @@
---
title: What are they?
description: Learn what scalar functions are and how they can transform individual values
order: 100
type: lesson-challenge
setup: |
```sql
CREATE TABLE book (
id INT PRIMARY KEY,
title VARCHAR(255),
price DECIMAL(10, 2)
);
INSERT INTO book (id, title, price)
VALUES (1, 'The Great Gatsby', 10.99),
(2, 'To Kill a Mockingbird', 12.99),
(3, '1984', 14.99),
(4, 'Pride and Prejudice', 16.99),
(5, 'The Catcher in the Rye', 18.99),
(6, 'The Hobbit', 20.99),
(7, 'The Lord of the Rings', 22.99),
(8, 'The Silmarillion', 24.99),
(9, 'The Children of Hurin', 26.99),
(10, 'The Fall of Gondolin', 28.99);
```
---
In the previous chapter, we learned about aggregate functions that operate across multiple rows to produce a single result (like `SUM`, `COUNT`, `AVG`). Now, let's explore another powerful type of SQL functions - scalar functions.
## What are Scalar Functions?
Unlike aggregate functions that work across multiple rows, scalar functions operate on a single value at a time and return exactly one value per row. They're like individual data transformers that work row by row rather than summarizing data.
To understand the difference, let's look at an example:
```sql
-- Aggregate function (works across rows)
SELECT AVG(price)
FROM book; -- Returns one value for all rows
-- Scalar function (works row by row)
SELECT title, UPPER(title)
FROM book; -- Returns transformed value for each row
```
Scalar functions can be used in various parts of your SQL queries:
- `SELECT` statements
- `WHERE` clauses
- `ORDER BY` clauses
- `HAVING` clauses
- Computed columns
In this lesson we will look at several useful scalar functions. We will be covering some of the common ones in the following categories:
| Category | Description |
| ----------------- | ------------------------------------ |
| String Functions | Used to manipulate textual data |
| Numeric Functions | Help with mathematical operations |
| Date Functions | Handle date and time operations |
| Type Conversion | Convert between different data types |
| Logical | Handle conditional logic |
> Note: Since we are using PostgreSQL for the editor, we will be covering the ones that are supported by PostgreSQL. Other databases mostly support the same functions but always check the documentation for your specific database.
By combining your knowledge of aggregate functions with scalar functions, you'll be able to perform both powerful data summarization and row-level transformations in your queries.

@ -0,0 +1,5 @@
---
title: Scalar Functions
description: Learn how to use scalar functions to perform calculations on a set of values.
order: 7
---
Loading…
Cancel
Save