Chapter 5 - Introduction To SQL: 5.1 SQL Basic Data Types
Chapter 5 - Introduction To SQL: 5.1 SQL Basic Data Types
clan
e.g. Suppose we want to create above table, then following SQL query can
be used.
SQL>create table student(roll_no number(2), name varchar(10), city
varchar(10), marks number(4,2), grade char(1));
Output: table created.
Fig. 5.5 Output of desc table command after adding new attribute
@coding.clan
Fig. 5.6 Output of alter table command after deleting existing attribute
Fig. 5.8 Output of alter table command after renaming existing attribute
Fig. 5.10 Output of alter table command after changing data type of
existing attribute
@coding.clan
@coding.clan
@coding.clan
roll_no marks
1 92.64
5 90.48
12 84.12
28 76.84
45 54.22
Fig. 5.1 The student relation
4) This clause can be used along with logical operators like AND, OR etc.
For this purpose, the general syntax is
SQL>select * from <table_name>where <condition1><
logical_operators><condition2>;
e.g. Suppose we want to access only data about students who live in pune
and marks greater than 90%, then following SQL query can be used.
SQL> select * from student where city = ‟Pune‟ AND marks > 90.00;
Output: 1 row selected.
grade
A
B
C
Fig. 5.1 The student relation
@coding.clan
If we want assign new name to the resultant attributes, then following SQL
query can be used.
SQL> select empid,name, salary+comm as gross_salary from emp;
Output: 5 rows selected.
The first seven operators can be used in select query as per our
requirement as shown in following SQL query
Here, the two specified values are also included in the result.
3) Within between operator, „not‟ operator can be used to negate the
condition. For this the general syntax of between operator is
SQL> select * from <table_name> where <attribute_name> not between
<min_value> and <max_value>;
e.g. Suppose we want to select tuples from emp table with comm value
excluding the range 1000 and 1250, then following SQL query can be
used.
SQL> select * from emp where comm not between 1000 and 1250;
Output: 3 rows selected.
@coding.clan
empid ename city salary comm deptid
101 Sagar Sangli 10000 500 10
102 Mahesh Mumbai 15000 750 11
115 Harsh Nagpur 30000 1500 12
@coding.clan
3) Within in clause, „not‟ operator can be used to negate the condition. For
this the general syntax of in clause is
SQL> select * from <table_name> where <attribute_name> not in
<list_of_values>;
e.g Suppose we want to select tuples from emp table excluding the name
Sagar and Nilesh, then following SQL query can be used.
SQL> select * from emp where ename not in(„Sagar‟, „Atul‟, „Nilesh‟);
e.g. Suppose we want to select tuples from emp table with city ending with
the letter „i‟, then following SQL query can be used.
SQL> select * from emp where city like „%i‟;
Output: 2 rows selected.
e.g. Suppose we want to select tuples from emp table with names in which
second letter is „a‟, then following SQL query can be used.
SQL> select * from emp where ename like „_a%‟;
Output: 4 rows selected.
@coding.clan
@coding.clan
5.7.3.2 The OR operator
1) The OR operator is used to select the tuples which satisfy one or any
number conditions specified in the query.
2) The general syntax of AND operator is
SQL> select * from <table_name> where <condition 1> OR <condition 2>;
e.g. Suppose we want to select the tuples from emp table with deptid 12 or
salary greater than 15000, then following SQL query can be used.
SQL> select * from emp where deptid=12 OR salary>15000;
Output: 3 rows selected.
roll_no contact
1 8657468542
5 9875412346
15 8087564123
28 9623574125
45 8804612745
Fig. 2.5 The stuconrelation
roll_no
1
5
15
23
28
31
45
5.8.2 The union all operator
1) Union operator is used to select all the tuples with repetition present in
the output of two SQL queries.
5
15
15
23
28
28
31
45
2) concat:
@coding.clan
It is used to concat two strings.
3) replace:
It is used to replace old group of letters with new group of letters.
4) substring:
It is used to extract specific portion of the string.
5) chr:
It is used to find letter for a specified ASCII value.
@coding.clan
6) translate:
It is used to replace old group of letters with new group of letters.
7) ltrim:
It is used to truncate partial part of the string from left hand side.
8) rtrim:
It is used to truncate partial part of the string from right hand side.
9) length:
It is used to find length of the given word.
@coding.clan
10) lpad:
It is used to pad or attach specific symbol specified number of times
to left hand side of given string.
11) rpad:
It is used to pad or attach specific symbol specified number of times
to right hand side of given string.
@coding.clan
12) lower:
It is used to make the given string in small case completely.
13) upper:
It is used to make the given string in upper case completely.
1) abs(n):
It is used convert given number into its absolute value.
2) ceil(n):
It is used find smallest number greater than or equal to specified
number.
@coding.clan
3) floor(n):
It is used find greatest number less than or equal to specified number.
4) log(a,b):
It is used find logarithmic value of the number with respect to given
base.
5) mod(a,b):
It is used find remainder when we divide first number with the
second.
@coding.clan
6) power(a,b):
It is used find raise to value of the number.
7) sqrt(n):
It is used find square root of the number.
8) sign(n):
It is used find sign(positive or negative) of the number.
9) round(n):
It is used find round-up value upto specified decimal places of the
given number.
10) trunc(n):
It is used find complete number by eliminating fractional part in the
given number.
@coding.clan
1) sysdate:
It is used display current date and time of the system.
2) add_months:
It is used add specified number of months in the given date.
3) last_day:
It is used find the last date of the month of the specified date.
@coding.clan
4) months_between:
It is used find number of months between the two specified dates.
5) next_day:
It is used find the date on the day specified in the query after the
given date.
6) greatest:
It is used find the latest date from the list of dates.
Syntax @coding.clan
2) to_date
The functionconverts character values of dateto date in the specified
format.
Syntax:
TO_DATE( string1, [ format_mask ], [ nls_language ] )
3) to_number
The to_number function converts a character value to a numeric
datatype.
Syntax
TO_NUMBER (string1, [format], [nls_parameter])
2) user:
It is used display underlying user name.
@coding.clan
3) vsize:
It is used find number of bytes required for the string under
consideration..
4) NVL:
It is used replace null values of the specific attributes with zero.
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
@coding.clan
5.9.6Aggregate Functions
sum(salary)
100000
5.9.6.2avg() function
1) This function is used to find the average of values of particular
attribute of given relation.
2)The general syntax of avg() function is
SQL> select avg(attribute_name) from <table_ name>;
e.g. Suppose we want to find average of salaries of employees, then
following SQL query can be used.
SQL> select avg(salary) from emp;
Output:
avg(salary)
@coding.clan 20000
5.9.6.3min() function
1) This function is used to find minimum value of particular attribute of
given relation.
2)The general syntax of min() function is
SQL> select min(attribute_name) from <table_ name>;
e.g. Suppose we want to find minimum salary of employees, then
following SQL query can be used.
SQL> select min(salary) from emp;
Output:
min(salary)
10000
5.9.6.4max() function
1) This function is used to find maximum value of particular attribute of
given relation.
2)The general syntax of max() function is
SQL> select max(attribute_name) from <table_ name>;
e.g. Suppose we want to find maximum salary of employees, then
following SQL query can be used.
SQL> select max(salary) from emp;
Output:
max(salary)
30000
5.9.6.5count() function
1) This function is used to find the count of values of particular attribute of
given relation.
2) The count() function will not consider null value in the calculation.
3)The general syntax of count() function is
SQL> select count (attribute_name) from <table_ name>;
e.g. Suppose we want to find count of commissions of employees, then
following SQL query can be used.
SQL> select count(comm) from emp;
Output:
count(comm)
4
5.9.6.6count(*) function
1) This function is used to find count of tuples(records) of given relation.
2)The general syntax of count(*) function is
SQL> select count(*) from <table_ name>;
e.g. Suppose we want to find count of tuples of employee table, then
following SQL query can be used.
SQL> select count(*) from emp;
Output:
count(*)
5
@coding.clan
SQL> select aggregate_function(<attribute_name>) from <table_ name>
group by <attribute_name>;
e.g. Suppose we want to find sum of salaries of employees department-
wise, then following SQL query can be used.
SQL> select dept_id,sum(salary) from emp group by dept_id ;
Output:
deptid sum(salary)
10 10000
11 35000
12 55000
deptid sum(salary)
11 35000
12 55000
5.12.1.2Check constraint
1) This constraint is used to avoid insertion of invalid values for a given
attribute.
2) In other words, it is used to check value being inserted must satisfy
specified condition or predicate.
3) Check constraint can be specified by create table or alter table command.
4) Consider following query for applying check constraint to salary
attribute of emp table using create table command.
@coding.clan