Most commonly used SQL functions
SQL provides a wide range of built-in functions to manipulate and perform operations on data within your database. Here are some of the most commonly used SQL functions:
1. `SELECT` Functions:
- `COUNT()`: Counts the number of rows or non-null values in a column.
- `SUM()`: Calculates the sum of values in a column.
- `AVG()`: Computes the average of values in a column.
- `MIN()`: Returns the minimum value in a column.
- `MAX()`: Returns the maximum value in a column.
2. `WHERE` Functions:
- `LIKE`: Searches for a specified pattern in a column.
- `IN()`: Specifies multiple values for comparison.
- `BETWEEN`: Filters results within a specified range.
- `NULL`: Checks for NULL values.
- `IS NULL` / `IS NOT NULL`: Tests for NULL values.
3. String Functions:
- `CONCAT()`: Concatenates two or more strings.
- `SUBSTRING()`: Extracts a portion of a string.
- `LENGTH()` or `LEN()`: Returns the length of a string.
- `UPPER()`: Converts a string to uppercase.
- `LOWER()`: Converts a string to lowercase.
- `TRIM()`: Removes leading and trailing spaces.
4. Date and Time Functions:
- `GETDATE()` / `NOW()`: Retrieves the current date and time.
- `DATEPART()`: Extracts a specific part (year, month, day, etc.) from a date.
- `DATEDIFF()`: Calculates the difference between two dates.
- `DATEADD()`: Adds or subtracts a specific time interval from a date.
5. Aggregation Functions:
- `GROUP BY`: Groups rows based on a specified column.
- `HAVING`: Filters grouped rows.
- `SUM()`, `AVG()`, `MIN()`, `MAX()`: Used with `GROUP BY` to perform aggregate calculations.
6. Conditional Functions:
- `CASE`: Performs conditional logic within SQL queries.
- `COALESCE()`: Returns the first non-null value in a list of expressions.
- `NULLIF()`: Compares two expressions and returns null if they are equal.
7. Conversion Functions:
- `CAST()`: Converts one data type into another.
- `CONVERT()`: Converts one data type into another, with additional formatting options.
8. Mathematical Functions:
- `ABS()`: Returns the absolute value of a number.
- `ROUND()`: Rounds a number to a specified number of decimal places.
- `FLOOR()`: Returns the largest integer less than or equal to a number.
- `CEIL()` or `CEILING()`: Returns the smallest integer greater than or equal to a number.
These are some of the most commonly used SQL functions, and they play a crucial role in performing various operations, filtering data, aggregating results, and transforming data within a relational database.
Comments
Post a Comment