28
Ex No: 4.1 PERFORMING INSERTION, DELETION, MODIFYING, ALTERING, UPDATING AND VIEWING RECORDS BASED ON CONDITIONS Date: AIM: To study the various categories of DML commands such as logical operations, aggregate functions, string functions, numeric functions, date functions, conversion functions, group functions and set operations. Facilities required to do the xperiment: SQL DML commands allows the user to manipulate the data in database. Different DML commands are explained in following sections. BASIC STRUCTURE: The basic structure of an SQL expression consists of three clauses: select, from, and where. Sl.No. Facilities required Quantity 1 System 1 2 Operating System Windows XP 3 Back end Oracle11g

Web viewSQL>select title from book where unit_price>=300 and unit_price

Embed Size (px)

Citation preview

Page 1: Web viewSQL>select title from book where unit_price>=300 and unit_price

Ex No: 4.1 PERFORMING INSERTION, DELETION, MODIFYING,

ALTERING, UPDATING AND VIEWING RECORDS BASED ON

CONDITIONSDate:

AIM:

To study the various categories of DML commands such as logical operations,

aggregate functions, string functions, numeric functions, date functions, conversion

functions, group functions and set operations.

Facilities required to do the xperiment:

SQL DML commands allows the user to manipulate the data in database.

Different DML commands are explained in following sections.

BASIC STRUCTURE:

The basic structure of an SQL expression consists of three clauses: select, from, and

where.

• The select clause corresponds to projection operation of the relational algebra. It

is used to list the attributes desired in the result of a query.

• The from clause corresponds to the Cartesian product operation of the relational

algebra. It lists the relations to be scanned in the evaluation of the expression.

Sl.No. Facilities required Quantity

1 System 1

2 Operating System Windows XP

3 Back end Oracle11g

Page 2: Web viewSQL>select title from book where unit_price>=300 and unit_price

• The where clause corresponds to the selection predicate of the relational algebra.

It consists of a predicate involving attributes of the relations that appear in the

from clause.

THE SELECT CLAUSE:

This command is used to display all the fields/or set of selected fields for

all/selected records from a relation.

Different forms of select clause are given below:

• Form 1: Use of select clause for displaying selected fields

Example: Find the names of all publishers in the book relation.

SQL>Select publisher_name from book;

Output:

Above query displays all publisher_names, from book relation. Therefore, some

publishers name will get displayed repeatedly.SQL allows duplicates in relations as well

as in the results of SQL expressions.

• Form 2: Use of select for displaying distinct values

For elimination of duplicates the keyword distinct is used. The above query

is rewritten as,

SQL >select distinct publisher_name from book;

Page 3: Web viewSQL>select title from book where unit_price>=300 and unit_price

Output:

SQL allows us to use the keyword all to specify explicitly that duplicates are not

removed.

SQL>select all publisher_name from book;

OUTPUT:

• Form 3: Use of select for displaying all fields

The asterisk symbol "*” can be used to denote "all attributes”. A select

clause of the form select * indicates that all attributes of all relations appearing in the

from clause are selected.

Example:

SQL>select * from book;

Page 4: Web viewSQL>select title from book where unit_price>=300 and unit_price

SQL >select * from author;

OUTPUT:

SQL>select * from publisher;

OUTPUT:

• Form 4: Select clause with arithmetic expression

The select clause may also contain arithmetic expressions involving the

operators +,-,*, and / operating on constants or attributes of tuples.

Example:

SQL>select title,unit_price * 10 from book;

OUTPUT:

Page 5: Web viewSQL>select title from book where unit_price>=300 and unit_price

The above query returns a relation that is the same as the book relation with

attributes title as it is and unit_price,will get multiplied by 10.

THE WHERE CLAUSE:

The where clause is used to select specific rows satisfying given predicate.

Example: "Find the titles of books published in year 2004".

This query can be written in SQL as:

SQL>select title from book where pub_year='2004';

OUTPUT:

SQL uses the logical connectives and, or, and not in the where clause. The

operands of logical connectives can be expressions involving the comparison operators

<,<=,>,>=,=,and <>.SQL allows us to use the comparison operators to compare strings

and arithmetic expressions as well as special types, such as date types.

Between:

SQL includes a between comparison operator to specify that a value be less than or

equal to some value and greater than or equal to some other value.

Example: " Find the titles of book having price between 300 to 400".

SQL>select title from book where unit_price between 300 and 400;

Page 6: Web viewSQL>select title from book where unit_price>=300 and unit_price

OUTPUT:

Or

SQL>select title from book where unit_price>=300 and unit_price<=400;

OUTPUT:

Similarly, we can use the not between comparison operator.

THE FROM CLAUSE:

The from clause

The from clause defines a Cartesian product of the relations in the clause.

Example: Find the titles of the books with author name and country published in

the year 2004

SQL>select Title,Book.author_name,Country from Book,Author where

Book.authorname=Author.author_name and Pub_year=’2004’;

Output:

Page 7: Web viewSQL>select title from book where unit_price>=300 and unit_price

THE RENAME OPERATION:

SQL provides a mechanism for renaming both relations and attributes. It uses the

as clause, taking the form:

Old_name as new_name

The as clause can appear in both the select and from clause.

Example:

SQL>select title,unit_price * 1.15 as new_price from book;

OUTPUT:

TUPLE VARIABLES:

Tuple variables are defined in the from clause by the way of as clause.

Example:"Find the titles of books with author name and author country".

SQL>select title,b.author_name,country from book b,author a where

b.author_name=a.author name;

Page 8: Web viewSQL>select title from book where unit_price>=300 and unit_price

OUTPUT:

Tuple variables are most useful for comparing two tuples in the same relation.

SQL>select distinct b1.title from book b1,book b2 where b1.unit_price>b2.unit_price and

b2.pub_year='2004';

OUTPUT:

SQL>select distinct B1.Title from Book B1,Book B2 where B1.Unit_price>B2 Unitprice

and B2.Pub_year=’2004’;

Output:

STRING OPERATION:

Page 9: Web viewSQL>select title from book where unit_price>=300 and unit_price

SQL specifies string by enclosing them in single quotes for ex: DBMS a single

quote character that is part of a string can be specified by using two single quotes

characters; for ex: the string: “It’s right” can be specified by “It’s right”.

The most commonly used operation on strings is pattern matching using the

operator like. We describe patterns by using two special characters.

Percent(%): The % character matches any substring.

Underscore(_): The _ character matches any character.

Patterns are case sensitive that is upper case characters do not match lowercase characters

or vice-versa.

Example:

‘computer%’ – matches any string beginning with ‘computer’.

‘%Engg’-matches any string containing “Engg” as a substring for ex: “Computer

Engg Department”,”Mechanical engg”.

‘_s%’ – matches any string with second character ‘s’.

‘_ _ _’ – matches any string of exactly three characters.

‘_ _ _%’ – matches any string of at least three characters.

Example of SQL queries:

1.) Find the names of author’s from author table where the first two characters

of name are ‘Ba’;

SQL>Select author_name from author where author_name like ‘Ba%’;

Output:

1.) Select Author_name from author where the second character of name is

‘r’or ‘a’;

Page 10: Web viewSQL>select title from book where unit_price>=300 and unit_price

SQL>Select Author_name from author where Author_name like ‘_r%’ or Author_name

like ‘_a%’;

OUTPUT:

2.) Display the name of all publishers whose address includes the substring

‘Main’;

SQL> select publisher_name from publisher where pub_add like ‘%Main’;

OUTPUT:

ORDERING THE DISPLAY OF TUPLES:

SQL uses order by clause to display all the tuples in the result of the query to

appear in sorted order.

Examples:

1.) Display all titles of books with price in ascending order of titles.

SQL>select title,unit_price from book order by title;

OUTPUT:

Page 11: Web viewSQL>select title from book where unit_price>=300 and unit_price

2.) Display all titles of books with price in ascending order of year.

SQL>select title,unit_price pub_year from book order by pub_year desc;

AGGREGATE FUNCTIONS:

Aggregate functions are functions that take a collection of values as input and return a

single value.SQL offers five built-in aggregate functions.

Average: avg

Minimum: min

Maximum: max

Total: Sum

Count: count

1.) Avg:

SYNTAX : avg([distinct|all]n)

Purpose: Returns average value of n, ignoring null values.

Example:

SQL> select avg (Unit_price)”Average Price” from book;

OUTPUT:

2.) Min:

SYNTAX: min([distinct|All]expr)

Purpose:Returns minimum value for expression

Page 12: Web viewSQL>select title from book where unit_price>=300 and unit_price

Example:

SQL> select min(unit_price)”Minimum price” from book;

OUTPUT:

3.) Max:

Syntax:max([distinct|All]expr)

Purpose: Returns maximum value of expression.

Example:

SQL>select max(unit_price)”Maximum Price” from book;

OUTPUT:

4.) Sum:

Syntax:sum([distinct|All]expr)

Purpose: Sum of values.

Example:

SQL>select sum(unit_price)”Total” from book;

OUTPUT:

5.) Count :

Syntax: count([distinct|All]expr)

Purpose: Returns the number of rows where expression is not null.

Example:

Page 13: Web viewSQL>select title from book where unit_price>=300 and unit_price

SQL>select count(TITLE)”No. of Books” from book;

OUTPUT:

Page 14: Web viewSQL>select title from book where unit_price>=300 and unit_price

Queries:

Q1: Insert a single record into dept table.

Solution:

1. Decide the data to add in dept.

2. Add to dept one row at a time using the insert into syntax.

Ans: SQL> insert into dept values (1,'IT','Tholudur');

Q2: Insert more than a record into emp table using a single insert command.

Ans: SQL> insert into emp values(&empno,'&ename','&job',&deptno,&sal);

SQL> /

Page 15: Web viewSQL>select title from book where unit_price>=300 and unit_price

SQL> /

SQL> /

SQL> select * from emp;

Page 16: Web viewSQL>select title from book where unit_price>=300 and unit_price

Q3.Update the emp table to set the salary of all employees to Rs15000/- who are

working as ASP

Ans: SQL> select * from emp;

SQL> update emp set sal=15000 where job='ASP';

SQL> select * from emp;

Q4: select employee name, job from the emp table

Ans: SQL> select ename, job from emp;

Page 17: Web viewSQL>select title from book where unit_price>=300 and unit_price

Q5.Delete only those who are working as lecturer

Ans: SQL> select * from emp;

SQL> delete from emp where job='lect';

SQL> select * from emp;

Q7: List the records in the emp table orderby salary in ascending order.

Ans: SQL> select * from emp order by sal;

Page 18: Web viewSQL>select title from book where unit_price>=300 and unit_price

Q8: List the records in the emp table orderby salary in descending order.

Ans: SQL> select * from emp order by sal desc;

Q9. Display only those employees whose deptno is 1.

Solution: 1. Use SELECT FROM WHERE

Ans: SQL> select * from emp where deptno=1;

Q10: Display deptno from the table employee avoiding the duplicated values.

Solution: 1. Use SELECT FROM syntax.

2.Select should include distinct clause for the deptno.

Ans: SQL> select distinct deptno from emp;

Page 19: Web viewSQL>select title from book where unit_price>=300 and unit_price

RESULT:

Page 20: Web viewSQL>select title from book where unit_price>=300 and unit_price

Ex no: 4.2

Set OperationsDate

AIM: To perform set operations using DML Commands.

The rules to which the set operators are strictly adhere to :

The queries which are related by the set operators should have a same number of column

and column definition.

Such query should not contain a type of long.

Labels under which the result is displayed are those from the first select statement.

SQL commands:

Union: Returns all distinct rows selected by both the queries

Syntax:

Query1 Union Query2;

Union all: Returns all rows selected by either query including the duplicates.

Syntax:

Query1 Union all Query2;

Intersect: Returns rows selected that are common to both queries.

Syntax:

Query1 Intersect Query2;

Minus: Returns all distinct rows selected by the first query and are not by the second

Syntax:

Query1 minus Query2;

Queries:

Q1: Display all the dept numbers available with the dept and emp tables

avoiding duplicates.

Solution:

Page 21: Web viewSQL>select title from book where unit_price>=300 and unit_price

1. Use select from clause. 2. Use union select clause to get the result.

Ans:

SQL> select deptno from emp union select deptno from dept;

Q2: Display all the dept numbers available with the dept and emp tables.

Solution:

1. Use select from clause. 2. Use union all in select clause to get

the result. Ans:

SQL> select deptno from emp union all select deptno from dept;

Page 22: Web viewSQL>select title from book where unit_price>=300 and unit_price

Q3: Display all the dept numbers available in emp and not in dept tables and vice

versa.

Solution:

1. Use select from clause.

2. Use minus in select clause to get the result.

Ans:

SQL> select deptno from emp minus select deptno from dept;

SQL> select deptno from dept minus select deptno

from emp;

Page 23: Web viewSQL>select title from book where unit_price>=300 and unit_price

Result: