Aggregate Functions
Aggregate Functions
SQL> select * from product where (price not between 20 and 100) or (quan not in('100','200'));
Guidelines to Use:
• DISTINCT keyword is used to ignore duplicate values and function
including duplicates.
• CHAR, VARCHAR2, NUMBER or DATE data types can be used for
argument.
• All group function except COUNT (*) function ignore NULLs. The NVL
6 rows selected.
AVG (DISTINCT / ALL ColumnName) Function.
SUM(AGE) SUM(DISTINCTAGE)
---------- ----------------
90 70
MAX (DISTINCT / ALL ColumnName) Function
MIN (DISTINCT / ALL ColumnName) Function.
MAX:
• Output will be the maximum value of column.
• It doesn’t consider NULL values.
SQL> select max(marks) from student;
MAX(MARKS)
----------
80
MIN:
• Output will be the minimum value of column.
• It doesn’t consider NULL values.
MIN(MARKS)
----------
45
STDDEV (DISTINCT / ALL ColumnName) Function
STDDEV(DISTINCTMARKS)
---------------------
14.9303941
STDDEV(MARKS)
-------------
VARIANCE (DISTINCT / ALL ColumnName) Function
VARIANCE(MARKS)
---------------
227.5
COUNT (*/ DISTINCT / ALL ColumnName) Function
Example: Query to sort the employee table by salary of the employee, the sql query would be.
NOTE:
• The columns specified in ORDER BY clause should be one of the columns selected in the SELECT
column list.
• Oracle sorts query results in ascending order by default.
Can also represent the columns in the ORDER BY clause by
specifying the position of a column in the SELECT list, instead of
writing the column name.
The above query sorts only the column 'salary' in descending order
and the column 'name' by ascending order.
SQL> select * from student; vishwa
vishwa
STUD_ID SNAME AGE vishnu
MARKS mahima
---------- ------------------------------ ---------- ---------- mahanya
3 vishwa 10 45
1 mahima 20 70 6 rows selected.
2 mahanya 21 80
3 vishwa 10 45 SQL> select sname from student order by age desc;
4 vishnu 19 60
3 vishwa 10 45 SNAME
------------------------------
6 rows selected. mahanya
mahima
SQL> select sname from student order by age; vishnu
vishwa
SNAME vishwa
------------------------------ vishwa
vishwa
SQL> select sname,age from student order by age desc; vishwa 10
vishnu 19
SNAME AGE mahima 20
------------------------------ ---------- mahanya 21
mahanya 21
mahima 20 6 rows selected.
vishnu 19 SQL> select sname,age from student order by 2;
vishwa 10
vishwa 10 SNAME AGE
vishwa 10 ------------------------------ ----------
vishwa 10
6 rows selected. vishwa 10
vishwa 10
SQL> select sname,age from student order by age; vishnu 19
mahima 20
mahanya 21
SNAME AGE
------------------------------ ---------- 6 rows selected.
vishwa 10
vishwa 10
The Rename Operation
select name, course id from instructor, teaches where instructor.ID= teaches.ID;
• The result of this query is a relation with the following attributes: name, course id
• The names of the attributes in the result are derived from the names of the attributes
in the relations in the from clause.
• Cannot always derive names in this way, for several reasons:
o First, two relations in the from clause may have attributes with the same name, in
which case an attribute name is duplicated in the result.
o Second, if we use an arithmetic expression in the select clause, the resultant attribute
does not have a name.
o Third, even if an attribute name can be derived from the base relations as in the preceding
example, we may want to change the attribute name in the result.
o Hence,
• SQL provides a way of renaming the attributes of a result relation.
It uses the as clause
• old-name as new-name is also commonly referred to as a table alias,
• The as clause can appear in both the select or a correlation variable, or a tuple
instructor.ID= teaches.ID;
SQL> select T.name, S.course id from SQL> select max(marks) as maximum_mark
S.ID; MAXIMUM_MARK
• A single quote character that is part of a string can be specified by using two single quote
characters; for example, the string “It’s right” can be specified by 'It''s right'.
• The SQL standard specifies that the equality operation on strings is case sensitive; as a
result, the expression “'comp. sci.' = 'Comp. Sci.'” evaluates to false. (but mysql or
• SQL also permits a variety of functions on character strings, such as concatenating (using
“∥”), extracting substrings, finding the length of strings, converting strings to uppercase
(using the function upper(s) where s is a string) and lowercase (using the function lower(s)),
removing spaces at the end of the string (using trim(s)), and so on.
Pattern matching
Pattern matching can be performed on strings using the operator like.
Patterns are case sensitive; 4 that is, uppercase characters do not match lowercase characters, or
vice versa.
examples:
• '%Comp%' matches any string containing “Comp” as a substring, for example, 'Intro. to
UNION:
(select course id from section where semester = 'Fall' and year= 2017) union
(select course id from section where semester = 'Spring' and year= 2018);
• The union operation automatically eliminates duplicates and sort them
• , unlike the select clause.
(select course id from section where semester = 'Fall' and year= 2017) union all
(select course id from section where semester = 'Spring' and year= 2018);
SQL> select * from student;
SQL> select * from course;
STUD_ID SNAME GE MARKS
---------- ------------------------------ ---------- ---------- CID CNAME
3 vishwa 10 45 ---------- --------------------
1 mahima 20 70 1 mca
2 mahanya 21 80 2 mba
3 vishwa 10 45 3 bsc
4 vishnu 19 60 4 msc
3 vishwa 10 45 5 mca
• or:
– The result of true or unknown is true,
– false or unknown is unknown,
– while unknown or unknown is unknown.
• not:
– The result of not unknown is unknown
• SQL uses the special keyword null in a predicate
to test for a null value.
• Thus, to find all instructors who appear in the
instructor relation with null values for salary, we
write:
• select name from instructor where salary is null;
SQL> select * from subject; SQL> select sid from subject where sname is null;
SID SN SID
---------- --
----------
1 c
2 cp 8
3 ja 9
4 da 4
8
9
6 = While inserting number value -> add null value
4 as null
ww SQL> insert into subject values(&sid,'&sname');
9 rows selected. Enter value for sid: null
Enter value for sname: ww
SQL> select sname from subject where sid is null; old 1: insert into subject values(&sid,'&sname')
new 1: insert into subject values(null,'ww')
SN
--
ww 1 row created.