0% found this document useful (0 votes)
11 views

SQL Functions

Uploaded by

urvashi gohil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

SQL Functions

Uploaded by

urvashi gohil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

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;

SELECT AVG(salary) AS average_salary


FROM employees;

2. Character Functions:
Character functions manipulate character data, such as strings. Examples of character
functions include CONCAT, LENGTH, UPPER, LOWER, and SUBSTRING.
Example Query:

SELECT CONCAT(first_name, ' ', last_name) AS full_name


FROM employees;
3. Arithmetic Functions:
Arithmetic functions perform mathematical operations on numeric data. Examples of
arithmetic functions include ADDITION (+), SUBTRACTION (-), MULTIPLICATION (*), and
DIVISION (/).
SELECT (quantity * price) AS total_cost
FROM order_details;

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';

SELECT * FROM products


WHERE category = 'Electronics' OR price < 500;

2.Relational Operators:
SELECT * FROM employees
WHERE salary >= 50000;

SELECT * FROM orders


WHERE order_date < '2023-01-01’;

3. BETWEEN Predicate:
SELECT * FROM products
WHERE price BETWEEN 10 AND 50;

SELECT * FROM orders


WHERE order_date BETWEEN '2023-01-01' AND '2023-06-30’;
4.IN and NOT IN Predicates:
SELECT * FROM customers
WHERE country IN ('USA', 'Canada');

SELECT * FROM products


WHERE category NOT IN ('Clothing', 'Shoes’);

5. LIKE Predicate:
SELECT * FROM employees
WHERE first_name LIKE 'J%';

SELECT * FROM customers


WHERE email LIKE '%gmail.com';
IS NULL
The IS NULL command is used to test for empty values (NULL values).
The following SQL lists all customers with a NULL value in the "Address" field:

SELECT CustomerName, ContactName, Address


FROM Customers
WHERE Address IS NULL;

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

SELECT CustomerName, ContactName, Address


FROM Customers
WHERE Address IS NOT NULL;
The following SQL creates a CHECK constraint on the "Age" column when the "Persons"
table is created. The CHECK constraint ensures that you can not have any person below
18 year

CREATE TABLE Persons (


Age number,
CHECK (Age>=18)
);

You might also like