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
What are SQL Functions
Functions perform operations on data and return a result.
Common Types of Functions
- String Functions
UPPER (convert to uppercase)
sqlSELECT UPPER(name) FROM employees;
LOWER (convert to lowercase)
sqlSELECT LOWER(name) FROM employees;
LENGTH (count characters)
sqlSELECT name, LENGTH(name) FROM employees;
- Numeric Functions
Basic Arithmetic
sqlSELECT name, salary + 5000 AS new_salaryFROM employees;
- Aggregate Functions (Preview)
sqlSELECT COUNT(*) FROM employees;
Counts total rows.
What are Expressions
Expressions combine:
- columns
- values
- operators
- functions
to produce new results.
Examples:
Increase Salary
sqlSELECT name, salary * 1.10 AS increased_salaryFROM employees;
Concatenate Values
sqlSELECT name || ' works in ' || department AS infoFROM employees;
Conditional Expression (CASE)
sqlSELECT name,CASEWHEN salary > 60000 THEN 'High'WHEN salary >= 50000 THEN 'Medium'ELSE 'Low'END AS salary_levelFROM employees;
SQL LIKE vs Expression Example
sqlSELECT name, salary * 2 AS double_salaryFROM employeesWHERE 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.
Name length
Show name and length of each name.
Add 5000 to salary
Increase each employee's salary by 5000.
10 percent increase
Create a column showing salary after 10 percent increase.
Name and department
Create a column combining name and department.
Salary category
Categorize employees as High, Medium, or Low based on salary.
Double salary
Show double salary for all employees.
Starts with A and salary + 1000
Retrieve employees whose name starts with 'A' and show salary + 1000.
Lowercase departments
Convert department names to lowercase.
Senior vs Junior
Show employee name and label Senior if salary > 65000, Junior otherwise.