Aggregate Functions
Aggregate functions are statistical functions such as count, min, max etc. They are used to
compute a single value from a set of attribute values of a column:
count Counting Rows
Example: How many tuples are stored in the relation EMP?
select count(*) from EMP;
Example: How many different job titles are stored in the relation EMP?
select count(distinct JOB) from EMP;
max Maximum value for a column
min Minimum value for a column
Example: List the minimum and maximum salary.
select min(SAL), max(SAL) from EMP;
Example: Compute the difference between the minimum and maximum salary.
select max(SAL) - min(SAL) from EMP;
sum Computes the sum of values (only applicable to the data type number)
Example: Sum of all salaries of employees working in the department 30.
select sum(SAL) from EMP
where DEPTNO = 30;
avg Computes average value for a column (only applicable to the data type number)
Note: avg, min and max ignore tuples that have a null value for the specified
attribute, but count considers null values.
Thursday, April 30, 2009
SQL BASICS-II FOR JNTU BTECH STUDENTS
Selection of TuplesUp to now we have only focused on selecting (some) attributes of all tuples from a table. If one is
interested in tuples that satisfy certain conditions, the where clause is used. In a where clause
simple conditions based on comparison operators can be combined using the logical connectives
and, or, and not to form complex conditions. Conditions may also include pattern matching
operations and even subqueries (Section 1.5).
4
Example: List the job title and the salary of those employees whose manager has the
number 7698 or 7566 and who earn more than 1500:
select JOB, SAL
from EMP
where (MGR = 7698 or MGR = 7566) and SAL > 1500;
For all data types, the comparison operators =, != or <>,<, >,<=, => are allowed in the
conditions of a where clause.
Further comparison operators are:
• Set Conditions:[not] in (
interested in tuples that satisfy certain conditions, the where clause is used. In a where clause
simple conditions based on comparison operators can be combined using the logical connectives
and, or, and not to form complex conditions. Conditions may also include pattern matching
operations and even subqueries (Section 1.5).
4
Example: List the job title and the salary of those employees whose manager has the
number 7698 or 7566 and who earn more than 1500:
select JOB, SAL
from EMP
where (MGR = 7698 or MGR = 7566) and SAL > 1500;
For all data types, the comparison operators =, != or <>,<, >,<=, => are allowed in the
conditions of a where clause.
Further comparison operators are:
• Set Conditions:
- )
Example: select * from DEPT where DEPTNO in (20,30);
• Null value:
i.e., for a tuple to be selected there must (not) exist a defined value for this column.
Example: select * from EMP where MGR is not null;
Note: the operations = null and ! = null are not defined!
• Domain conditions:
where SAL between 1500 and 2500;
• select ENAME from EMP
where HIREDATE between ’02-APR-81’ and ’08-SEP-81’;
1.2.3 String Operations
In order to compare an attribute with a string, it is required to surround the string by apostrophes,
e.g., where LOCATION = ’DALLAS’. A powerful operator for pattern matching is the
like operator. Together with this operator, two special characters are used: the percent sign
% (also called wild card), and the underline , also called position marker. For example, if
one is interested in all tuples of the table DEPT that contain two C in the name of the department,
the condition would be where DNAME like ’%C%C%’. The percent sign means that any
(sub)string is allowed there, even the empty string. In contrast, the underline stands for exactly
one character. Thus the condition where DNAME like ’%C C%’ would require that exactly one
character appears between the two Cs. To test for inequality, the not clause is used.
Further string operations are:
• upper(
= upper(DNAME) (The name of a department must consist only of upper case letters.)
• lower(
• initcap(
• length(
• substr(
n. If m is not specified, the end of the string is assumed.
substr(’DATABASE SYSTEMS’, 10, 7) returns the string ’SYSTEMS’.
SQL for JNTU BTECH STUDENTS
In order to retrieve the information stored in the database, the SQL query language is used. In
the following we restrict our attention to simple SQL queries and defer the discussion of more
complex queries to Section 1.5
In SQL a query has the following (simplified) form (components in brackets [ ] are optional):
select [distinct]
from
the following we restrict our attention to simple SQL queries and defer the discussion of more
complex queries to Section 1.5
In SQL a query has the following (simplified) form (components in brackets [ ] are optional):
select [distinct]
from