SQL Functions

Sql tutorial · PySpark.in

5.2 SQL Functions

SQL provides many built-in functions that help us perform operations on data stored in tables.

Functions make SQL powerful because they allow us to:

Categories of SQL Functions

SQL functions are commonly divided into the following categories:

  1. Date Functions
  2. CAST Functions
  3. Arithmetic Functions
  4. String Functions

1. Date Functions

Date functions are used to work with:

These functions help us extract:

from a date or datetime field.

SQLite strftime() Function

In SQLite, the strftime() function is one of the most important date functions.

It extracts parts of a date based on a format.

Syntax

```

hide

strftime(format, field_name)

```

Common Format Codes

Format

Description

Example

%Y

Year

2024

%m

Month

01 to 12

%d

Day

01 to 31

%H

Hour

00 to 23

%M

Minute

00 to 59

Example Table — movie

id

name

release_date

1

Leo

2023-10-19

2

Jailer

2023-08-10

3

Avengers Endgame

2019-04-26

4

Sherlock Holmes

2010-01-15

Example: Extract Year from Release Date

Query

```

sql

CREATE TABLE movie (

id INTEGER PRIMARY KEY,

name TEXT,

genre TEXT,

release_date TEXT,

rating REAL

);

INSERT INTO movie (

id,

name,

genre,

release_date,

rating

)

VALUES

(1, 'Leo', 'Action', '2023-10-19', 9.0),

(2, 'Jailer', 'Action', '2023-08-10', 8.5),

(3, 'Avengers Endgame', 'Superhero', '2019-04-26', 9.3),

(4, 'Sherlock Holmes', 'Mystery', '2010-01-15', 8.1);

SELECT name,

strftime('%Y', release_date) AS release_year

FROM movie;

```

Output

name

release_year

Leo

2023

Jailer

2023

Avengers Endgame

2019

Sherlock Holmes

2010

How strftime() Works

For the date:

2023-10-19

Examples

Expression

Result

strftime('%Y', '2023-10-19')

2023

strftime('%m', '2023-10-19')

10

strftime('%d', '2023-10-19')

19

2. CAST Function

Different columns can store different data types such as:

Sometimes we need to convert one data type into another.

The CAST() function is used for this purpose.

Syntax

```

hide

CAST(value AS data_type)

```

Common Data Types Used in CAST

Data Type

Description

INTEGER

Whole numbers

REAL

Decimal numbers

TEXT

Strings

BLOB

Binary data

Example: Movies Released in Each Month of Year 2010

Query

```

sql

SELECT strftime('%m', release_date) AS month,
COUNT(*) AS total_movies
FROM movie
WHERE CAST(strftime('%Y', release_date) AS INTEGER) = 2010
GROUP BY month;

```

Step-by-Step Explanation

Step 1 — Extract Year

```

hide

strftime('%Y', release_date)

```

Output:

release_date

Year

2010-01-15

"2010"

Notice that strftime() returns a string.

Step 2 — Convert String to Integer

```

hide

CAST(strftime('%Y', release_date) AS INTEGER)

```

Now:

"2010" → 2010

Step 3 — Filter Movies Released in 2010

```

hide

WHERE CAST(strftime('%Y', release_date) AS INTEGER) = 2010

```

Step 4 — Group Movies by Month

```

hide

GROUP BY month

```

Example Output

month

total_movies

01

2

05

1

11

3

3. Arithmetic Functions

Arithmetic functions perform mathematical operations on numeric values.

Common Arithmetic Functions

Function

Purpose

FLOOR()

Rounds down

CEIL()

Rounds up

ROUND()

Rounds to specified decimals

FLOOR Function

FLOOR() returns the nearest integer less than or equal to the number.

Syntax

```

hide

FLOOR(number)

```

Example

```

sql

SELECT FLOOR(2.3);

```

Output

FLOOR(2.3)

2

More Examples

Expression

Output

FLOOR(7.9)

7

FLOOR(-2.7)

-3

CEIL Function

CEIL() returns the nearest integer greater than or equal to the number.

Syntax

```

hide

CEIL(number)

```

Example

```

sql

SELECT CEIL(-2.7);

```

Output

CEIL(-2.7)

-2

More Examples

Expression

Output

CEIL(2.3)

3

CEIL(7.1)

8

ROUND Function

ROUND() rounds a number to a specified number of decimal places.

Syntax

```

hide

ROUND(number, decimal_places)

```

Examples

Round to 2 Decimal Places

```

sql

SELECT ROUND(2.345, 2);

```

Output

ROUND(2.345, 2)

2.35

Round to 1 Decimal Place

```

sql

SELECT ROUND(2.345, 1);

```

Output

ROUND(2.345, 1)

2.3

4. String Functions

String functions are used to manipulate text data.

These functions help us:

Common String Functions

Function

Purpose

UPPER()

Converts text to uppercase

LOWER()

Converts text to lowercase

UPPER Function

Converts all characters into uppercase.

Example

```

sql

SELECT UPPER('avengers');

```

Output

UPPER('avengers')

AVENGERS

LOWER Function

Converts all characters into lowercase.

Example

```

sql

SELECT LOWER('IRON MAN');

```

Output

LOWER('IRON MAN')

iron man

Case-Insensitive Search Using UPPER()

Example

```

sql

SELECT name
FROM movie
WHERE UPPER(name) LIKE UPPER('%avengers%');

```

How This Works

Both values are converted to uppercase before comparison.

Example:

Avengers
→ AVENGERS

This helps perform case-insensitive searches.

SQL Set Operations

Set operations are used to combine results from multiple SQL queries.

Common Set Operators

Operator

Description

UNION

Combines unique rows

UNION ALL

Combines all rows including duplicates

INTERSECT

Returns common rows

EXCEPT / MINUS

Returns rows from first query not present in second

SQLite uses EXCEPT instead of MINUS.

Syntax of Set Operations

```

hide

SELECT column1, column2
FROM table1

SET_OPERATOR

SELECT column1, column2
FROM table2;

```

Rules for Set Operations

When combining queries:

  1. Number of columns must match
  2. Data types should be compatible
  3. Column order should be same

Example: Actors Who Acted in Both Movies

Get actor IDs who acted in:

Query

```

sql

CREATE TABLE movie_cast (

movie_id INTEGER,

actor_id INTEGER

);

INSERT INTO movie_cast (

movie_id,

actor_id

)

VALUES

(6, 101),

(6, 102),

(6, 103),

(6, 104),

(15, 103),

(15, 104),

(15, 105),

(15, 106);

SELECT actor_id

FROM movie_cast

WHERE movie_id = 6

INTERSECT

SELECT actor_id

FROM movie_cast

WHERE movie_id = 15;

```

Example Output

actor_id

101

108

UNION Example

```

sql

SELECT actor_id
FROM movie_cast
WHERE movie_id = 6

UNION

SELECT actor_id
FROM movie_cast
WHERE movie_id = 15;

```

Difference Between UNION and UNION ALL

Operator

Removes Duplicates?

UNION

Yes

UNION ALL

No

ORDER BY with Set Operations

ORDER BY can appear only once at the end.

Example

```

sql

SELECT actor_id
FROM movie_cast
WHERE movie_id = 6

UNION

SELECT actor_id
FROM movie_cast
WHERE movie_id = 15

ORDER BY actor_id;

```

LIMIT and OFFSET with Set Operations

Similar to ORDER BY, LIMIT and OFFSET are written at the end.

Example

```

sql

SELECT actor_id
FROM movie_cast
WHERE movie_id = 6

UNION

SELECT actor_id
FROM movie_cast
WHERE movie_id = 15

LIMIT 5 OFFSET 2;

```

More Sql tutorials

All tutorials · Try the free PySpark compiler · Practice challenges