SQL FOUNDATIONS:SQL Functions & Expressions

Use functions and expressions to transform and calculate data.

SQL Functions & Expressions Explained with Examples + Practice Questions

When to Use Functions & Expressions

  • Use functions to transform or calculate data
  • Use expressions to create new values from existing columns

These are commonly used in:

  • Data analysis
  • Reporting
  • SQL interviews

Example Dataset

Table: employees

idnamedepartmentsalary
1JohnHR40000
2AliceIT60000
3BobIT55000
4EmmaFinance70000
5DavidHR45000

What are SQL Functions

Functions perform operations on data and return a result.

Common Types of Functions

  1. String Functions

UPPER (convert to uppercase)

sql
SELECT UPPER(name) FROM employees;

LOWER (convert to lowercase)

sql
SELECT LOWER(name) FROM employees;

LENGTH (count characters)

sql
SELECT name, LENGTH(name) FROM employees;
  1. Numeric Functions

Basic Arithmetic

sql
SELECT name, salary + 5000 AS new_salary
FROM employees;
  1. Aggregate Functions (Preview)
sql
SELECT COUNT(*) FROM employees;

Counts total rows.

What are Expressions

Expressions combine:

  • columns
  • values
  • operators
  • functions

to produce new results.

Examples:

Increase Salary

sql
SELECT name, salary * 1.10 AS increased_salary
FROM employees;

Concatenate Values

sql
SELECT name || ' works in ' || department AS info
FROM employees;

Conditional Expression (CASE)

sql
SELECT name,
CASE
WHEN salary > 60000 THEN 'High'
WHEN salary >= 50000 THEN 'Medium'
ELSE 'Low'
END AS salary_level
FROM employees;

SQL LIKE vs Expression Example

sql
SELECT name, salary * 2 AS double_salary
FROM employees
WHERE name LIKE 'A%';

Combines:

  • filtering
  • expressions

Key Concepts

  • Functions modify or calculate values
  • Expressions create new computed columns
  • Use AS to rename output columns
  • Expressions are heavily used in real-world queries

Common Questions (FAQ)

What are SQL functions

SQL functions are built-in operations that perform calculations or transformations on data.

What is an expression in SQL

An expression is a combination of columns, values, and operators used to compute a result.

What is CASE in SQL

CASE is used for conditional logic, similar to if-else statements.

Can we use functions in WHERE

Yes, but it may impact performance in large datasets.

Internal Learning Links

  • Learn SELECT basics
  • Learn WHERE filtering
  • Learn Aggregations

Practice CTA

Practice SQL function-based questions on Schoolabe to strengthen your query-building skills.

SQL Functions & Expressions Missions

Solve exercises in sequence to unlock the next mission.

1

Uppercase names

Convert all employee names to uppercase.

Solve Mission
2

Name length

Show name and length of each name.

Locked
3

Add 5000 to salary

Increase each employee's salary by 5000.

Locked
4

10 percent increase

Create a column showing salary after 10 percent increase.

Locked
5

Name and department

Create a column combining name and department.

Locked
6

Salary category

Categorize employees as High, Medium, or Low based on salary.

Locked
7

Double salary

Show double salary for all employees.

Locked
8

Starts with A and salary + 1000

Retrieve employees whose name starts with 'A' and show salary + 1000.

Locked
9

Lowercase departments

Convert department names to lowercase.

Locked
10

Senior vs Junior

Show employee name and label Senior if salary > 65000, Junior otherwise.

Locked