SQL Functions
SQL Functions
1.Aggregate Functions:
Aggregate functions operate on a set of values and return a single value as the result.
Examples of aggregate functions include SUM, COUNT, AVG, MIN, and MAX.
Example Query:
SELECT SUM(sales_amount) AS total_sales
FROM sales_table;
2. Character Functions:
Character functions manipulate character data, such as strings. Examples of character
functions include CONCAT, LENGTH, UPPER, LOWER, and SUBSTRING.
Example Query:
4.Date Functions:
Date functions manipulate and perform operations on date and time values.
Examples of date functions include DATE, EXTRACT, DATEADD, and DATEDIFF.
SELECT DATEADD(MONTH, 3, order_date) AS future_date
FROM orders;
5.Conversion Functions:
Conversion functions convert data from one type to another. Examples of conversion
functions include CAST, CONVERT, and TO_CHAR.
SELECT CAST(order_id AS VARCHAR) AS order_id_str
FROM orders;
Table-1: Order
| order_id | customer_id | order_date |
|----------|-------------|------------|
|1 | 101 | 2022-05-15 |
|2 | 102 | 2022-06-20 |
|3 | 103 | 2022-07-05 |
Table-2: Customer
| customer_id | customer_name | city |
|-------------|---------------|------------|
| 101 | John Doe | New York |
| 102 | Jane Smith | Los Angeles|
| 103 | David Johnson | Chicago |
1.Logical Operators (AND / OR):
SELECT * FROM customers
WHERE age > 25 AND city = 'New York';
2.Relational Operators:
SELECT * FROM employees
WHERE salary >= 50000;
3. BETWEEN Predicate:
SELECT * FROM products
WHERE price BETWEEN 10 AND 50;
5. LIKE Predicate:
SELECT * FROM employees
WHERE first_name LIKE 'J%';
IS NOT NULL
The IS NOT NULL command is used to test for non-empty values (NOT NULL values).
The following SQL lists all customers with a value in the "Address" field