Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Operator Aggregate Function Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

SQL Operators

Operator is a reserved word or a character used primarily in an SQL statement WHERE clause to perform
operations, such as comparisons and arithmetic operations.
Arithmetic Operators: Arithmetic operators can perform arithmetical operation on numeric operands
involved

Relational Operators or Comparison Operators


Relational operators are used in condition to compare one expression with other. The Relation operators
are =, <, >, >=, <=, !=.

Logical operators: The Logical operators are those that are true or false. They return true or false values
to combine one or more true or false values. Logical Operators are And , Or and Not. The
SQL AND & OR operators are also used to combine multiple conditions. These two operators can be
combined to test for multiple conditions.
AND Operator:
This operator displays only those records where both the conditions condition1 and condition2 evaluates
to True.
Example- SELECT * FROM Student WHERE Age = 18 AND ADDRESS = 'Delhi';
OR Operator:
This operator displays the records where either one of the conditions condition1 and condition2
evaluates to True. That is, either condition1 is True or condition2 is True.
SELECT * FROM Student WHERE NAME = 'Ram' OR NAME = 'SUJIT';
To fetch all the records from the Student table where Age is 18 NAME is Ram or RAMESH.
SELECT * FROM Student WHERE Age = 18 AND (NAME = 'Ram' OR NAME = 'RAMESH');
NOT Operator
Not takes a single Boolean as an argument and changes its value from false to true or from true to false.
SELECT column1, colomn2, … FROM table_name WHERE NOT condition;
SELECT * FROM Customers WHERE NOT Country=’UK’;
Special operators:
Between operator is used to check between two values or specific range.
Syntax:
... ColumnName BETWEEN lower limit AND upper limit;
Selects the rows that contain values within a specified lower and upper limit.
The lower and upper limit values must be linked with the keyword AND.
The lower and upper limits are inclusive in range.

IN operator
The IN operator can be used to select rows that match one of the values in a list.
Select rows that contain any value given in a set.
This is similar to '='. But, '=' compares single value to another single value, while IN compares single value
to a list (set) of values provided with IN predicate.
Can be used when there is a need to use multiple OR conditions.
Can be used with numerical, Character as well as date data type.

Like operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in
a column.
There are two wildcards often used in conjunction with the LIKE operator:
The percent sign (%) represents zero, one, or multiple characters
The underscore sign (_) represents one, single character
LIKE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
Example SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';

NULL value
A field with a NULL value is a field with no value.
If a field in a table is optional, it is possible to insert a new record or update a record without adding a
value to this field. Then, the field will be saved with a NULL value.
Note: A NULL value is different from a zero value or a field that contains spaces. A field with a NULL value
is one that has been left blank during record creation!
IS NULL & IS NOT NULL
The IS NULL operator is used to test for empty values (NULL values). The IS NOT NULL operator is used to
test for non-empty values (NOT NULL values).
IS NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
IS NOT NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;

DISTINCT Statement- is used to return only distinct (different)or unique values. If you put only one
expression in the DISTINCT clause, the query will return the unique values for that expression.In MySQL,
the DISTINCT clause doesn't ignore NULL values. So if you are using the DISTINCT clause in your SQL
statement, your result set will include NULL as a distinct value.
The SQL ORDER BY Keyword

The ORDER BY keyword is used to sort the result-set in ascending or descending order.The ORDER
BY keyword sorts the records in ascending order by default. To sort the records in descending order, use
the DESC keyword.

ORDER BY Syntax

SELECT column1, column2, ...


FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

Example

SELECT * FROM Customers


ORDER BY Country;

Aggregate Function in SQL


MIN()
It returns the smallest SELECT MIN(Price) AS SmallestPrice
value of the selected FROM Products;
column.

MAX() This function returns the SELECT MAX(Price) AS SmallestP


largest value of the rice
selected column. FROM Products;

COUNT( The COUNT() functi SELECT COUNT(ProductID)


) on returns the number FROM Products;
of rows that matches a
specified
criterion. NULL values
are not counted.

AVG() SELECT AVG(Price)


It returns the average FROM Products;
value of a numeric
column. NULL values
are ignored.

SUM() SELECT SUM(Quantity)


It returns the total sum FROM OrderDetails;
of a numeric
column. NULL values
are ignored.

You might also like