Functions in SQL
All SQL topics∙ Topic
Functions in SQL
SQL functions are built-in operations that perform calculations, transformations, and data processing tasks on database values.
Syntax
SELECT function_name(column)
FROM table_name;
📝 Edit Code
👁 Preview
💡 This preview does not execute SQL; itβs for reading/editing the query.
What are SQL Functions?
- 1Predefined operations in SQL.
- 2Perform calculations and transformations.
- 3Used in SELECT queries.
- 4Simplify data handling.
Types of SQL Functions
- 1String Functions (UPPER, LOWER).
- 2Numeric Functions (ABS, ROUND).
- 3Date Functions (NOW, CURDATE).
- 4Aggregate Functions (COUNT, SUM).
String Functions
- 1Used to manipulate text data.
- 2Example: UPPER(name).
- 3LOWER() converts to lowercase.
- 4Useful in formatting output.
Numeric Functions
- 1Used for mathematical operations.
- 2ABS() returns absolute value.
- 3ROUND() rounds numbers.
- 4Useful in calculations.
Date Functions
- 1Work with date and time values.
- 2NOW() returns current timestamp.
- 3CURDATE() returns current date.
- 4Used in scheduling systems.
Aggregate Functions
- 1Perform calculations on multiple rows.
- 2COUNT(), SUM(), AVG().
- 3Used in reporting.
- 4Return single result.
Real-world
- 1Formatting names in reports.
- 2Calculating totals and averages.
- 3Processing date and time values.
- 4Generating summary reports.
- 5Data transformation in queries.
Common Mistakes
- 1Using wrong function type.
- 2Applying functions on large datasets unnecessarily.
- 3Ignoring performance impact.
- 4Using functions in WHERE clause incorrectly.
Best Practices
- 1Use appropriate function for task.
- 2Avoid unnecessary function usage.
- 3Be careful using functions in indexed columns.
- 4Combine functions with optimized queries.
Quick Summary
- SQL functions process and transform data.
- Include string, numeric, date, and aggregate functions.
- Used in SELECT queries.
- Simplify data manipulation.
- Improve query efficiency.
Interview Questions
Q1. What are SQL functions?
Answer: Built-in operations to process and manipulate data.
Q2. What are types of SQL functions?
Answer: String, numeric, date, and aggregate functions.
Q3. What does UPPER() function do?
Answer: Converts text to uppercase.
Q4. What is COUNT() used for?
Answer: It counts the number of rows.
Q5. Where are SQL functions used?
Answer: In SELECT queries for data processing.
Quiz
Which function is used to count rows in SQL?