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

Chapter 5 - Introduction To SQL: 5.1 SQL Basic Data Types

Uploaded by

Samyog Shrestha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Chapter 5 - Introduction To SQL: 5.1 SQL Basic Data Types

Uploaded by

Samyog Shrestha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

@coding.

clan

Chapter 5 – Introduction to SQL


5.1 SQL basic data types

Following are the SQL basic data types


1) Char: This data type is used to store character values like letters,
symbols,numbers etc. The maximum length allowed is 256
characters.
2) Varchar: It is more flexible form of char data type. This data type is
mainly used when length of the data is not fixed. The maximum
length allowed is 2000 characters.
Varchar2 is latest version of varchar introduced by oracle. The
maximum length for varchar2 allowed is 32767 characters.
3) Date: This data type is used to represent current date and time. The
default format of date is DD-MON-YY and The default format of time
is 24 hours.
4) Number(p,s): This data type is used to store integer or float values.
Here, P stands for precision which indicates maximum number of
digits in actual number and s stands for scalewhich indicates total
number of digits after decimal point.
e.g. For declaring 123.75, the data type should be declared as
number(5,2).
The maximum precision allowed is 38. @coding.clan
5) Long: This data type is used to store large data of the order of 2GB.
Long data type cannot be used in sub queries.
6) Raw: This data type is used to store binary data like digitized images.
The long raw data type can be used to store binary data upto 2GB.

roll_no name city marks grade


1 Sagar Sangli 92.64 A
5 Nilesh Pune 90.48 A
12 Mahesh Mumbai 84.12 A
28 Sandeep Satara 76.84 B
45 Harsh Nagpur 54.22 C
Fig. 5.1 The studentrelation
5.2 Data Definition Language
5.2.1The CREATE command:
1) This command is used to creates new database objects like tables, views,
indexes, synonyms etc.
2) The general syntax of create table command is
SQL> create table <table_name>(<attribute1_name><data_type>(<size>),
<attribute2_name><data_type>(<size>),
<attribute3_name><data_type>(<size>)….<attributen_name><data_
type>(<size>));
Output: table created. @coding.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.2 Output of create table command

Here, a new table namely student will be created having attributes


roll_no, name, city, marks and grade.

3) There are some rules while giving name to a table


a) The table name length should be at max 30 characters.
b) All alphabets A-Z and a-z, digits 0-9 and only one special symbol
_(underscore) can be used.
c) The table name must start with alphabets only.
d) reserved keywords in SQL like select, insert, delete are not
allowed.
5.2.2 The DESC command:
1) This command is used to describe basic structure of table already
created.
2) The general syntax of desc table command is
SQL> desc<table_name>; @coding.clan
e.g. Suppose we want to see basic structure of student table, then
following SQL query can be used.
SQL> desc student;
Output:

Fig. 5.3 Output of desc table command

5.2.3 The ALTER command:


1) This command is used to modify basic structure of table already created.
2) Using alter table command, we can do following:
a) Adding new attribute
b) Deleting existing attribute
c) Renaming existing attribute
d) Changing data type of existing attribute

Let us study one by one.

a) Adding new attribute


The general syntax of adding new attribute using alter table command is
SQL> alter table <table_name> add
<new_attribute_name><data_type>(<size>);
e.g. Suppose we want to add new attribute namely contact in above
table, then following SQL query can be used.
SQL> alter table student add contact number(10); @coding.clan
Output:

Fig. 5.4 Output of alter table command on adding new attribute

Fig. 5.5 Output of desc table command after adding new attribute

b) Deleting existing attribute


The general syntax of deleting existing attribute using alter table command
is
SQL> alter table <table_name> drop column <attribute_name>;
e.g. Suppose we want to delete grade attribute from above table, then
following SQL query can be used.
SQL> alter table student drop column grade;
Output:

@coding.clan

Fig. 5.6 Output of alter table command after deleting existing attribute

Fig. 5.7 Output of desc table commandafter deleting existing attribute

c) Renaming existing attribute


The general syntax of renaming existing attribute using alter table
command is
SQL> alter table <table_name> rename column
<old_attribute_name> to <new_attribute_name>;
e.g. Suppose we want to rename marks attribute as percentage in
above table, then following SQL query can be used.
SQL> alter table student rename column marks to percentage;
Output:
@coding.clan

Fig. 5.8 Output of alter table command after renaming existing attribute

Fig. 5.9 Output of desc table commandafter renaming existing attribute

d) Changing data type of existing attribute


The general syntax of Changing data type of existing attributeusing alter
table command is
SQL> alter table <table_name>modify
<attribute_name><new_data_type>(<new_size>);
e.g. Suppose we want to change data type of name attribute from
varchar(10) to varchar(15) in above table, then following SQL query can be
used.
SQL> alter table student modify name varchar(15);
Output:
@coding.clan

Fig. 5.10 Output of alter table command after changing data type of
existing attribute

Fig. 5.11Output of desc table commandafter changing data type of existing


attribute

5.2.4 The DROP command:


1) This command is used to delete given table permanently along with its
structure.
2) When table is dropped, the database loses all its data and all the indexes
associated with it.
3) The general syntax of drop table command is
SQL>drop table<table_name>;
e.g. Suppose we want to drop student table, then following SQL
query can be used.
SQL>drop table student;
Output: table dropped.

@coding.clan

Fig. 5.12 Output of drop table command

5.2.5 The RENAME command:


1) This command is used to rename a given table.
3) The general syntax of rename table command is
SQL>rename <old_table_name> to <new_table_name>;
e.g. Suppose we want to rename student table to candidate table,
then following SQL query can be used.
SQL>rename student to candidate;
Output: table renamed.

Fig. 5.13 Output of rename table command

5.2.6 The TRUNCATE command:


1) This command is used to remove all the data from the given table
keeping the table structure intact.
2) The general syntax of truncate table command is
SQL>truncate table <table_name>;
e.g. Suppose we want to truncate student table, then following SQL
query can be used.
SQL>truncate table student;
Output: table truncated.

@coding.clan

Fig. 5.14 Output of truncate table command

5.3 Data Manipulation Language


5.3.1 The INSERT command:
1) This command is used to insert new tuples(records) in the existing table.
2) The general syntax of insert command is
SQL>insert into<table_name> values(<attribute1_value>,
<attribute2_value>, <attribute3_value>,…… <attributen_value>);
Output: 1 row inserted.
Here, if the values are of number data type, then they are written as
they are and if the values are of char or varchar data type, then they
are written in single quotations.
e.g. Suppose we want to insert single tuple, then following SQL query can
be used.
SQL>insert into student values(1, „sagar‟, „sangli‟, 92.64, „A‟);
Output: 1 row inserted.
Fig. 5.15 Output of insert command

5.3.2 The DELETE command:


1) This command is used to delete existing tuples(records) from the table.
2) The general syntax of delete table command is
SQL> delete from <table_name>;
Output: 5 rows deleted.
Here, all the tuples will get deleted, because have not provided any
condition.
2) The general syntax of delete command to delete only specific tuple is
SQL> delete from <table_name> where <condition>;
Output: 1 row deleted.
Here, tuples which satisfy condition specified in where clause will
get deleted. @coding.clan

Fig. 5.16 Output of deletecommand


5.3.3 The UPDATE command:
1) This command is used to update or change value of particular attribute
of a table.
2) The general syntax of update command is
SQL>update <table_name> set <attribute_name> = <new_value>;
Output: 5 rows updated.
Here, all the tuples will get updated, because have not provided any
condition.
2) The general syntax of update command to update only specific tuple is
SQL>update <table_name> set <attribute_name> = <new_value>where
<condition>;
Output: 1 row updated.
Here, tuples which satisfy condition specified in where clause will
get updated.

@coding.clan

Fig. 5.17 Output of update command

5.4 Data Transaction Language


Transaction is a logical unit of work to do particular task. In SQL, there are
3 Transaction control statements as below
1. Commit
2. Savepoint
3. Rollback

5.4.1 The COMMIT command:


1) This command is used to end the transaction and to make its effects
permanent to the database.
2) Commit command can remove savepoints and transaction locks if any.
3) The general syntax of commit command is
SQL>commit work;
OR
SQL> commit;

5.4.2 The SAVEPOINT command:


1) This command is used to mark for the operations of ongoing transaction
which is very lengthy in size.
2) When savepoint is used along with rollback, then we have the facility to
cancel the transaction steps upto that savepoint.
3) The general syntax of commit command is
SQL>savepoint <savepoint _id>;

5.4.3 The ROLLBACK command:


1) As the name suggests, this command is used to undo the work done in
current transaction.
2) Rollback can be used to undo the complete transaction or upto certain
savepoint.
3) The general syntax of commit command is
SQL>rollback to <savepoint _id>;
OR
SQL> rollback work;
OR
SQL> rollback; @coding.clan

5.5 Data Control Language


Data Control statements are mainly used for granting or revoking various
types of privileges to various users.In SQL, there are 2 Data Control
statements as below
1. Grant
2. Revoke

5.5.1 The GRANT command:


1) This command is used to grant or give different types of privileges to
various users.
2) The objects on which privileges are to be granted can be either table or
view or index or synonym etc.
3) The general syntax of grant command is
SQL>grant <privilege_list> on <object_name> to <user_name>;
e.g. Suppose we want to allow user1 to insert and update the data in
student table,but not to delete data in it, then following SQL query
can be used.
SQL>grant insert, update on student to user1;
3) If DBA wants to grant select privilege to all the users, then following
SQL query can be used.
SQL>grant select on student to all;
@coding.clan

5.5.2 The REVOKE command:


1) This command is used to revoke or remove different types of privileges
from various users.
2) The objects from which privileges are to be revoked can be either table or
view or index or synonym etc.
3) The general syntax of revoke command is
SQL>revoke<privilege_list> on <object_name>from<user_name>;
e.g. Suppose we want to disallow user1 to insert the data in student table,
then following SQL query can be used.
SQL>revoke insert on student from user1;

5.6 Data Query Language


Data query Language is mainly used to access or retrieve necessary data
from the database. In SQL, DQL includes Select command.

5.6.1 The SELECT command:


1) This command is used to access or retrieve necessary data from the
database.
2) The objects from which privileges are to be accessed can be either table
or view etc.
3) The general syntax of select command is
SQL> select * from <table_name>;
Here, * means complete tuple or record.
e.g. Suppose we want to access complete data in student table, then
following SQL query can be used.
SQL> select * from student;
Output: 5 rows selected.

roll_no name city marks grade


1 Sagar Sangli 92.64 A
5 Nilesh Pune 90.48 A
12 Mahesh Mumbai 84.12 A
28 Sandeep Satara 76.84 B
45 Harsh Nagpur 54.22 C
Fig. 5.1 The student relation
@coding.clan

4) If we want to access only particular attributes, then the general syntax of


select command is
SQL> select <attribute_list> from <table_name>;
e.g. Suppose we want to access only data about roll_no and marks from
student table, then following SQL query can be used.
SQL> select roll_no, marks from student;
Output: 5 rows selected.
5 rows selected.

roll_no marks
1 92.64
5 90.48
12 84.12
28 76.84
45 54.22
Fig. 5.1 The student relation

5.6.1.1 The where clause:


1) This clause is used to access only particular data from the table
depending on given condition.
2) The general syntax of where clause is
SQL>select * from <table_name> where <condition>;
e.g. Suppose we want to access student data with more than 80% marks,
then following SQL query can be used.
SQL> select * from student where marks > 80.00;
Output: 3 rows selected.

roll_no name city marks grade


1 Sagar Sangli 92.64 A
5 Nilesh Pune 90.48 A
12 Mahesh Mumbai 84.12 A
Fig. 5.1 The student relation
@coding.clan

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.

roll_no name city marks grade


5 Nilesh Pune 90.48 A

5.6.1.2 The distinct clause:


1) This clause is used to display unique values for a given attribute.
2)The general syntax of distinct clause is
SQL> select distinct <attribute_name> from <table_name>;
e.g. Suppose we want to display unique values of grade, then following
SQL query can be used.
SQL>select distinct grade from student;
Output: 3 rows selected.

grade
A
B
C
Fig. 5.1 The student relation

5.6.1.3 The order-by clause:


1) This clause is used to sort data from the table in specific order either
ascending or descending.
2) This clause is always used along with select query.
3) The general syntax of order-by clause is
SQL> select * from <table_name>order by <attribute_name><sort_order>;
e.g. Suppose we want to arrange student data in increasing order of marks,
then following SQL query can be used.
SQL> select * from student order by marks;
Output: 5 rows selected.

roll_no name city marks grade


45 Harsh Nagpur 54.22 C
28 Sandeep Satara 76.84 B
12 Mahesh Mumbai 84.12 A
5 Nilesh Pune 90.48 A
1 Sagar Sangli 92.64 A
@coding.clan
4) The default sort order is ascending order and if we want to arrange data
in descending order, then „desc‟ keyword is used.
e.g. Suppose we want to arrange student data in reverse alphabetical order
of city, then following SQL query can be used.
SQL> select * from student order by city desc;
Output: 5 rows selected.

roll_no name city marks grade


28 Sandeep Satara 76.84 B
1 Sagar Sangli 92.64 A
5 Nilesh Pune 90.48 A
45 Harsh Nagpur 54.22 C
12 Mahesh Mumbai 84.12 A
@coding.clan

5.6.1.4 Creating new table from given table:


1) Using select command, we can create new table from underlying table.
2) The general syntax for this purpose
SQL>create table <new_table><attribute 1, attribute 2.. attributen> as select
<attribute 1, attribute 2.. attributen> from <given_table_name>;
e.g. Suppose we want to create new table with name candidate which
consists of roll_no and name attribute from original table student,
then following SQL query can be used.
SQL> create table candidate(roll_no,name) as select roll_no,name from
student;
Output: table created.

5.6.1.5 Inserting data into new table from given table:


1) Using insert command, we can insert data into new table with the data
from underlying table.
2) The general syntax for this purpose
SQL> insert into <new_table> select <attribute 1, attribute 2.. attributen>
from <given_table_name>;
e.g. Suppose we want to insert data into new table with name candidate
which consists of roll_no and name attribute with the data from
original table student, then following SQL query can be used.
SQL> insert into candidate select roll_no,name from student;
Output: 5 rows inserted.

5.7 Operators in SQL


In SQL, there are 3 types of operators in SQL as below
1. Arithmetic operators
2. Comparison operators
3. Logical operators
empid ename city salary comm deptid
101 Sagar Sangli 10000 500 10
105 Nilesh Pune 20000 1000 11
102 Mahesh Mumbai 15000 750 11
112 Sandeep Satara 25000 1250 12
115 Harsh Nagpur 30000 1500 12
Fig. 5.1 The emprelation

@coding.clan

5.7.1 Arithmetic operators

Sr. no. Operator Meaning


1 + Addition
2 - Subtraction
3 * Multiplication
4 / Divison

These operators can be used in select query as per our requirement as


shown in following SQL query

SQL> select empid,ename, salary+comm from emp;


Output: 5 rows selected.

empid ename salary+comm


101 Sagar 10500
105 Nilesh 21000
102 Mahesh 15750
112 Sandeep 26250
115 Harsh 31500

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.

empid ename gross_salary


101 Sagar 10500
105 Nilesh 21000
102 Mahesh 15750
112 Sandeep 26250
115 Harsh 31500
5.7.2 Comparison operators

Sr. no. Operator Meaning


1 = is equal to
2 != not equal to
3 < less than
4 <= less than equal to
5 > greater than
6 >= greater than equal
to
7 <> not equal to
8 between between two
values
9 in in the list
10 like value like
11 null is null

The first seven operators can be used in select query as per our
requirement as shown in following SQL query

SQL> select * from emp where dept_id != 11;


Output: 3 rows selected.

empid ename city salary comm deptid


101 Sagar Sangli 10000 500 10
112 Sandeep Satara 25000 1250 12
115 Harsh Nagpur 30000 1500 12
SQL> select * from emp where salary > 20000;
Output: 2 rows selected.

empid ename city salary comm deptid


112 Sandeep Satara 25000 1250 12
115 Harsh Nagpur 30000 1500 12

5.7.2.1 The between operator


1) The between operator is used to select the tuples satisfying values
between two specified values.
2) The general syntax of between operator is
SQL> select * from <table_name> where <attribute_name> between
<min_value> and <max_value>;
e.g. Suppose we want to select tuples fromemp table with salary greater
than or equal to 10000 and less than or equal to 20000, then following
SQL query can be used.
SQL>select* from emp where salarybetween 10000 and 20000;
Output: 3 rows selected.

empid ename city salary comm deptid


101 Sagar Sangli 10000 500 10
105 Nilesh Pune 20000 1000 11
102 Mahesh Mumbai 15000 750 11

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

5.7.2.2 The in operator


1) The in operator is used to select the tuples satisfying values specified in
the given list.
2) The general syntax of in operatoris
SQL> select * from <table_name> where <attribute_name> in
(<list_of_values>);
e.g. Suppose we want to select tuples from emp table with name Sagar and
Nilesh, then following SQL query can be used.
SQL>select * from emp where ename in(„Sagar‟, „Atul‟, „Nilesh‟);
Output: 2 rows selected.

empid ename city salary comm deptid


101 Sagar Sangli 10000 500 10
105 Nilesh Pune 20000 1000 11

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‟);

empid ename city salary comm deptid


102 Mahesh Mumbai 15000 750 11
112 Sandeep Satara 25000 1250 12
115 Harsh Nagpur 30000 1500 12
@coding.clan

5.7.2.3 The like operator


1) The likeoperator is used to select the tuples which match the given string
pattern.
2) Like operator is always used with character data type values only
3) The general syntax of likeclause is
SQL> select * from <table_name> where <attribute_name>like
<string_pattern>;
e.g. Suppose we want to select tuples from emp table with names staring
with the letter „S‟, then following SQL query can be used.
SQL> select * from emp where ename like „S%‟;
Output: 2 rows selected.

empid ename city salary comm deptid


101 Sagar Sangli 10000 500 10
112 Sandeep Satara 25000 1250 12

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.

empid ename city salary comm deptid


101 Sagar Sangli 10000 500 10
102 Mahesh Mumbai 15000 750 11

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.

empid ename city salary comm deptid


101 Sagar Sangli 10000 500 10
102 Mahesh Mumbai 15000 750 11
112 Sandeep Satara 25000 1250 12
115 Harsh Nagpur 30000 1500 12
e.g. Suppose we want to select tuples from emp table with city containing
„ata‟as its substring, then following SQL query can be used.
SQL>select * from emp where city like „%ata%‟;
Output: 1 row selected.

empid ename city salary comm deptid


112 Sandeep Satara 25000 1250 12

5.7.2.4 The null operator @coding.clan

1) null means absence of value for a particular attribute.


2) In other words, null values means value is unkown for that attribute.
3) null doesn‟t mean value is zero or empty.
4) Every null value is independent or different from another null value
5) Any value added to null value results in null value.
6) null operator can be used to update particular attribute value to null or
to select the tuples based on null values.
7) The general syntax of for updating attribute value is
SQL>update <table_name>set<attribute_name>=null;
e.g. Suppose we want to set null values to comm values of the tuples from
emp table with salary less than or equal to 15000, then following
SQL query can be used..
SQL> update emp set comm=null where salary <=15000;
Output: 2 rows updated.

SQL> select * from emp;


Output: 5 rows selected.

empid ename city salary comm deptid


101 Sagar Sangli 10000 null 10
105 Nilesh Pune 20000 1000 11
102 Mahesh Mumbai 15000 null 11
112 Sandeep Satara 25000 1250 12
115 Harsh Nagpur 30000 1500 12
7) The general syntax of for selecting the tuples based on null values is
SQL> select * from <table_name> where <attribute_name> is null;
e.g. Suppose we want to select the tuples from emp table with comm.
value to be null, then following SQL query can be used.
SQL> select * from emp where comm is null;
Output: 2 rows selected.

empid ename city salary comm deptid


101 Sagar Sangli 10000 null 10
102 Mahesh Mumbai 15000 null 11

@coding.clan

5.7.3 Logical operators

Sr. no. Operator Meaning


1 AND Logical AND
2 OR Logical OR
3 NOT Logical NOT

These operators are used to select the tuples depending on combination of


conditions.

5.7.3.1 The AND operator


1) The AND operator is used to select the tuples which satisfy all the
conditions specified in the query.
2) The general syntax of AND operator is
SQL> select * from <table_name> where <condition 1>AND<condition 2>;
e.g. Suppose we want to select the tuples from emp table with deptid 12
and salary greater than 28000, then following SQL query can be
used.
SQL> select * from emp where deptid=12 and salary>28000;
Output: 1 row selected.
empid ename city salary comm deptid
115 Harsh Nagpur 30000 1500 12

@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.

empid ename city salary comm deptid


105 Nilesh Pune 20000 1000 11
112 Sandeep Satara 25000 1250 12
115 Harsh Nagpur 30000 1500 12

5.7.3.3 The NOT operator


1) The NOT operator is used in combination with between, in or like
operator.
2) NOT operator is used to negate the given condition.
2) As an example, consider following SQL query.
SQL> select * from emp where ename not like „S%‟

empid ename city salary comm deptid


105 Nilesh Pune 20000 1000 11
102 Mahesh Mumbai 15000 750 11
115 Harsh Nagpur 30000 1500 12
Here, string pattern specified is all names staring with S. But because of
NOT operator before like operator, this condition will be negated and all
names which does not start with S will be selected in the output.

5.8 Set operators in SQL @coding.clan

Sr. no. Operator Meaning


1 union Union of outputs of 2 queries without
repetition
2 Union all Union of outputs of 2 queries with
repetition
3 intersect Intersection of outputs of 2 queries
4 Minus Difference of outputs of 2 queries

1) Set operator are used to joins outputs of SQL queries.


2) The general syntax of set operator query is
SQL>select <common_attributes> from <table1_name>
<set_operator>
select <common_attributes> from <table2_name>;

roll_no name marks grade


1 Sumit 92.84 A
15 Akshay 84.63 A
23 Vikas 72.86 B
28 Amol 68.65 B
31 Vinayak 58.69 C
Fig. 2.4 The student relation

roll_no contact
1 8657468542
5 9875412346
15 8087564123
28 9623574125
45 8804612745
Fig. 2.5 The stuconrelation

5.8.1 The union operator


1) Union operator is used to select all distincttuples present in the output of
two SQL queries.

Fig. 2.5 Theunion operator

2) The general syntax of union operator query is @coding.clan

SQL> select <common_attributes> from <table1_name>


union
select <common_attributes> from <table2_name>;
e.g. Suppose we want to select distinct roll numbers from both the tables
student and stucon, then following SQL query can be used.
SQL> select roll_no from student
union
select roll_no from stucon;
Output: 7 rows selected.

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.

Fig. 2.5 Theunion all operator

2) The general syntax of union operator query is


SQL> select <common_attributes> from <table1_name>
union all
select <common_attributes> from <table2_name>;
e.g. Suppose we want to select all roll numberswith repetition from both
the tables student and stucon, then following SQL query can be
used.
SQL> select roll_no from student
union all
select roll_no from stucon;
Output: 10 rows selected.
roll_no
1
1 @coding.clan

5
15
15
23
28
28
31
45

5.8.3 The intersect operator


1) Intersect operator is used to select common tuples without repetition
present in the output of two SQL queries.

Fig. 2.5 Theintersect operator

2) The general syntax of union operator query is


SQL> select <common_attributes> from <table1_name>
intersect
select <common_attributes> from <table2_name>;
e.g. Suppose we want to select common roll numbers without repetition
from both the tables student and stucon, then following SQL query
can be used.
SQL> select roll_no from student
@coding.clan
intersect
select roll_no from stucon;
Output: 3 rows selected.
roll_no
1
15
28

5.8.4 The minus operator


1) Minus operator is used to select tuples present in output of first SQL
queries but not in second one.
Fig. 2.5 Theminus operator

2) In other words,minus operator is used to find the difference between


outputs of two queries.
2) The general syntax of minus operator query is @coding.clan
SQL> select <common_attributes> from <table1_name>
minus
select <common_attributes> from <table2_name>;
e.g. Suppose we want to select roll numbers from student table which are
not present in stucon table, then following SQL query can be used.
SQL> select roll_no from student
minus
select roll_no from stucon;
Output: 3 rows selected.
roll_no
23
31

5.9 SQL Functions


Following are the types of SQL functions
a) String functions
b) Arithmetic functions
c) Date and time functions
d) Miscellaneous Functions
e) Conversion Functions
f) Aggregate Functions
5.9.1 String Functions
1) initcap:
It is used to make first alphabet of the word to be uppercase.

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.

5.9.2 Arithmetic Functions

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

5.9.3 Date and Time Functions

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.

5.9.4 Conversion Functions


1) to_char:
to_char function is used to convertgiven date value to character type
with a specified date format.

Syntax @coding.clan

TO_CHAR(number1, [format], [nls_parameter])

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])

5.9.5 Miscellaneous Functions


1) uid:
It is used display underlying user id.

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

empid ename city salary comm deptid


101 Sagar Sangli 10000 null 10
105 Nilesh Pune 20000 1000 11
102 Mahesh Mumbai 15000 750 11
112 Sandeep Satara 25000 1250 12
115 Harsh Nagpur 30000 1500 12

1) The functions which operate on group of data are called as aggregate


functions.
2) There are 5 types of aggregate functions in SQL as below:
a) Sum(sum)
b) Average(avg)
c) Minimum(min)
d) Maximum(max)
@coding.clan
e) Count(count)
f) Count(*)
3)The general syntax of aggregate function is
SQL> select aggregate_function(attribute_name) from <table_ name>;

5.9.6.1 sum() function


1) This function is used to find the sum of values of particular attribute of
given relation.
2)The general syntax of sum() function is
SQL> select sum(attribute_name) from <table_ name>;
e.g. Suppose we want to find sum of salaries of employees, then
following SQL query can be used.
SQL> select sum(salary) from emp;
Output:

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

5.10 The group by clause


1) As theword suggests, group by clause always works on group or set of
data items.
2)The group by clause is alwaysapplied on the attribute whose data values
are repeating .
3)The general syntax of group by clause is

@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

Here, there are 3 distinct departments. Hence, salaries of respective


departments will get added and displayed in the output.

5.11 The having clause


1) The having clause is always used in association with group b clause.
2)The group by clause does not directly act on original table, rather it acts
on the output of group by clause.
3)The general syntax of having clause is
SQL> select aggregate_function(<attribute_name>) from <table_ name>
group by <attribute_name> having <condition>;
e.g. Suppose we want to display only those departments havingsum of
salaries of employees department-wiseto be greater than 20000, then
following SQL query can be used.
SQL> select dept_id,sum(salary) from emp group by dept_id having
sum(salary) >20000 ;
Output:

deptid sum(salary)
11 35000
12 55000

Here, there are only 2 distinct departments having sum of salaries


greater than 20000 and displayed in the output.
5.12Constraints in SQL
1) Constraints are nothing but the conditions which must be satisfied by all
the relations..
2) Integrity constraints are used to maintain integrity of the database.
e.g. The minimum balance of savings account must be 500 rupees.
3) There are 3 types of integrity constraints in SQL
a) Domain integrity constraints
b) Entityintegrity constraints
c) Referential integrity constraints

5.12.1Domain integrity constraints


1) Domain is the set of values permissible for a given attribute.
2) Domain integrity constraints are used to check the values for a given
attribute are from domain of that attribute or not.
3) They are tested easily by the system whenever a new data item is
entered into the database.
4) There are 2 types of domain integrity constraints in SQL
a) Not null constraint
b) Check constraint

5.12.1.1 Not null constraint


1) This constraintis used to avoid presence of null values for a given
attribute. In other words, it prohibits insertion of null values for a given
attribute.
2) Not null constraint can be specified by create table or alter table
command.
3) Consider following query for applying not null constraint to empid
attribute of emp table using create table command.
SQL> create table emp(empid number(4) not null, empnm varchar(10),
salary number(7,2), comm number(5,2), deptid number(2));
Output: table created.

SQL> desc emp;


Output:
Name Null? Type
empid NOT NULL number(4)
empnm varchar(10)
salary number(7,2)
comm number(5,2)
deptid number(2)

SQL> insert into emp values(null, „Bhushan‟, 15000, 300, 10);


Output: Error at line 1: ORA 01400 Cannot insert null value
(“SYSTEM”.”EMP”.”EMPID”)

4) Consider following query for applying not null constraint to empnm


attribute of emp table using alter table command.
SQL> alter table emp modify(empnm varchar(10) not null);
Output: table altered.

SQL> desc emp;


Output:
Name Null? Type
empid NOT NULL number(4)
empnm NOT NULL varchar(10)
salary number(7,2)
comm number(5,2)
deptid number(2)

5) If particular attribute already contains null values, then not null


constraint cannot be applies using alter table command.
e.g. Suppose comm attribute of emp table already contains null values
and if we write following query
SQL> alter table emp modify(comm number(5,2) not null);
Output: Error at line 1: ORA-02296 Cannot enable
(SCOTT)_ null value found.
@coding.clan

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.

SQL> create table emp(empid number(4) not null, empnm varchar(10),


salary number(7,2),constraint check_sal check(salry>5000), comm
number(5,2), deptid number(2));
Output: table created.

SQL> insert into emp values(7255, „Bhushan‟, 4000, 100, 10);


Output: Error at line 1: ORA- 02290Check constraint
(SYSTEM.CHECK_SAL) violated.

@coding.clan

You might also like