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

Database Management System

Chapter 5 semester 3

Uploaded by

shindenanadu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Database Management System

Chapter 5 semester 3

Uploaded by

shindenanadu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Relational

DataMore
2-27
DBMS (MSBTE-Sem. 3-Comp)
2.16.1 Arithmetic Operators
Syntax y where value
Consider two operands
x
and ofx is 10 while y
is 5.
name) from name;
table
SELECT distinct( column Operators Deseriptions Examples
It adds the operands X+y=l5
Example + (Add)
-
(Subtract) It subtracts right hand operand x-y=s
emp;
select distinct(job) from from left hand operand

* (Multiply) It multiply both operands x*y= 50


Output
Itdivides left hand operand by x/y=2
/(Divide)
Job right hand operand
Tt divides left hand operand by x% y=0
% (Modulo)
Clerk
right hand operandand returns
reminder
Manager

Salesman
2.16.2 Comparison Operators
y
Consider two operands x and y where value of x is 10 while is 5
Operators- Arithmetic
Syllabus Topic : SQL
escriptions Examples
Operators, Comparison Operators, Logical Operators
Operators Check whether both the X=y is not true
operands have same values, if
SQL Operators yes condition becomes true.
2.16
Check whether left operand is X>yis true
a.2.16.1 Explain SQL Operators. greater than right, if yes
(Hefer sections 2.16 to 2.16.5) (4 Marks) condition becomes true
< Check whether left operand is X<y is not true
An operator is a character or reserved word used in SQL less than right, if yes condition
statements to perform different operations like arithmetic or becomes true
comparison. Check whether left operand is X>=y is true
greater than or equal to the right
Operators are used to specify conditions in an SQL statement operand or not, if yes condition
and also used to integrate multiple conditions in SOL
become true
Check whether left operand is not
statement. is X<=y
less than or equal to the
right true
SQL Operators operand or not, if yes condition
become true
Check whether both the
1. Arithmetíc operators x !=y is true
operands have same
not, if not values or
2. Comparison operators condition become
true. wh
3. Logical operators Check whether Ou
operands have same both the XOy is true
not, if not values or
4. Bit wise operators condition become
true.
5. Cornpound operators A Check whether
not greater left operand not
operators than the value is x>=y is
Fig. 2.16.1: SQL right operand of true
Check whether Sele
not less left operand
than the right is
value operand |
xk=y is true whe
Ddel
DBMS (MSBTE-Sem. 3-Comp)
2-28 Rolational Data Modol

Table
5. Consider following

Table 2. 16.1 : Emp


EMPNO ENAME JOB MGR HIREDATE SAL COMM DETNO
7839 KING PRESIDENT 11/17/1981 5000 10
7698 BLAKE MANAGER 7839 0S/01/19S1 2850 30
7782 CLARK MANAGER 7839 06/09/1981 2450 10
7566 JONES MANAGER 7839 04/02/1981 2975 20
7783 SCOTT ANALYST 20
7566 12/09/1982 3000
7902 FORD ANALYST 7566 20
12/03/1981 3000
-
7369 SMITH CLERK 12/17/1980 20
7902 800
7499 ALLEN SALESMAN 300 30
7698 02/20/1981 1600
5.
7521 WARD SALESMAN 7698 02/22/1981 1250 S00 30

7654 MARTIN SALESMAN 7698 09/28/198 1


1250 1400 30)

e
7844 TURNER SALESMAN 7698 09/08/1981 1500 30

„7876 ADAMS CLERK 7788 01/12/1983 1100 20)

r 7900 JAMES CLERK 7698 12/03/1981 950 30

MILLER CLERK 01/23/1982 1300 10


J934 7782

e G =Equal to

Select * from emp

where ename =KING'

Output

ENAME MGR IIREDATE SAL COMM DEPTNO


ot EMPNO JOB
PRESIDENT 11/17/1981 5000 10
7839 KING

PGreater Than

Select * from emp

where
sal>3000;
Output

MGR IIIREDATE SAL COMM DErTNO


EMPNO ENAME JOB
11/17/198 1 5000 10
7839 KING PRESIDENT
ot
<Less Than

Select from emp


*
where
sal<3000;
DBMS (MSBTE-Sem. 3-Comp) 2-29 Relational
Data
Output

EMPNO | ENAME JOB MGR| HIREDATESAL|COMM DEPTNO


7698 BLAKE MANAGER 7839 05/01/1981 2850 30
7782 CLARK MANAGER 06/09/1981 2450
7839 10
7566 JONES MANAGER 7839 04/02/1981 2975 20
7369 SMITH CLERK 7902 12/17/1980 800 20
7499 ALLEN SALESMAN 7698 02/20/1981 1600 300 30
7521 WARD SALESMAN 7698 02/22/1981 1250 500 30
7654 MARTIN SALESMAN 7698 09/28/1981 1250 1400 30
7844 TURNER SALESMAN 7698 09/08/1981 1500 0 30
7876 ADAMS CLERK 7788 01/12/1983 1100 20
7900 JAMES CLERK 7698 12/03/1981 950 30
7934 MILLER CLERK 7782 01/23/1982 1300 10
>= Greater Than Equal To

Select* from emp

where sal >3000;

Output

EMPNO ENAME JOB MGR uDEDA TESALL


COMM DEPTNO
7839 KING PRESIDENT 11/17/1981 5000 10
7788 SCOTT ANALYST 7566 12/09/1982 3000 20
7902 FORD ANALYST 7566 12/03/1981 3000 20
<= Less Than
Equal To

Select * from emp

where sal s=3000;

Output

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


7698 BLAKE MANAGER 7839 05/01/1981 2850 30
7782 CLARK MANAGER 7839 06/09/1981 2450 10
7566 JONES MANAGER 7839 04/02/1981 2975 20
7788 SCOTT ANALYST 7566 12/09/1982 3000 20
7902 FORD ANALYST 7566 12/03/1981 3000 20
7369 SMITH CLERK 7902 12/17/1980 800
20
7499 ALLEN SALESMAN 7698 02/20/1981 1600 300 30
7521 WARD SALESMAN 7698 02/22/1981 1250 500 30
-al Data Mode DBMS (MSBTE-Sem. Gomp)
2-20 Rolational Data del

EMPNOENAME JOB
MGRIREDATE| SAL,COMMDEVINO
7654 MARTIN SALESMAN
(D2B/19%1 1230 1400
7844 TURNER SALESMAN
(KIR/198I 150
7876 ADAMS CURK 778% 01/12/1983 I100
7900 JAMES CLERK
798 12/03W1981 950
7934 MILLER CLERK 7782 0123/1982 1300

oNot Equal To

Select from emp


bere sal<> 3000:
Outpat
EMPNO ENAME HIREDATE SALCOMM DEPTNO
JOBMGR
7839 KING PRESIDENT 11/17/1981 5000 10

2850 30
7698 BLAKE MANAGER 7839 0S/01/1981

2450 10
7782 CLARK MANAGER 7839 06/09/1981

04/02/1981 2975 20
7566 JONES MANAGER 7839

CLERK 12/17/1980 800 20


7369 SMITH 7902
SALESMAN 300 30
7499 ALLEN 7698 02/120/1981 1600

SALESMAN 7698 02/22/1981 1250 500 30


7521 WARD
1698 09/28/1981 1250 1400 30
MARTIN SALESMAN
7654
7698 09/08/1981 1500 0 30
TURNER SALESMAN
7844
01/12/1983 1100 20
ADAMS CLERK 7788
7876
12/03/1981 950 30
JAMES CLERK 7698
7900
7782
01/23/1982 1300
MILLER CLERK
7934

Combination of operators
can be used with AND operator

Select from emp

where sal >=3000 and sal <5000;


Output
SALIcOMMDEPTNO
MGRIIREDATE
EMPNO ENAME JOB
10
1/17/1981 5000
PRESIDENT
7839 KING 20
12/09/1982 3000
ANALYST 7566
7788 SCOTT 20
12/03/1981 3000
ANALYST 7566
7902 FORD
Relatonal
Data
DBMS (MSBTE-Sem. 3-Comp) 2-31 W
Set operations are supportedi by SQL to be performeh n

2.16.3 Logical Operators data. For these operations special operators


are used to join the
Operatot
Description Operators are used. Set operators Select
are TRUE. statements,.
AL TRUE if all of a set of comparisons of multiple SELECT uniot
are TRUE.
AND TRUE if both Boolean expressions get meaningful results
These operators help to sclect
is
TRUE if any one of a set of comparisons Queries which
ANY under different specific conditions. contin
TRUE Outpu
operators are called compound queries.
BETWEEN TRUE if the operand is within a range.
as
EXISTS TRUE if a sub-query contains any rows. The different Set Operators are follows
TRUE if the operand is equal to one of a list of
IN
Set operators
Cxpressions.

LIKE TRUE if the operand matches a patterm.


Union 2.17.
NOT Reverses the value of any other Boolean operator.
OR TRUE if either Boolean expression is TRUE. 2. Union All
SOME TRUE if some of a set of comparisons are TRUE. query 1

3. Intersect
2.16.4 Bitwise Operators
4. Minus
Operator Description
Fig. 2.17.1 : Set operators Select
& Bitwise AND
Consider following two tables Emp and Dept Union
Bitwise OR
Bitwise excusive OR Table 2.17.1: Emp Select

2.16.5 Compound Operators Empno Ename Job DeptNo Salary


101 Rahul Manager 10 17000 Select
Operator Description 102 Vinay Clerk 20 12000
Add equals 103 Union
Kunal Manager 30 18000
Subtract equals 104 Rajesh select
Salesman 20 13000
Multiply equals 105 Kushal Clerk 10 11000 Output
Divide equals
Table 2.17,2 : Dept
%= Modulo equals
DeptNo DeptName
& Bitwise AND equals Loc
10 Sales Mumbai
Bitwise exclusive equals 20 Production Pune
Bitwise OR equals 30 Accounts Nasik
40 Research Bangalore
Syllabus Topic : Set Operators 2.17.1 Union

2.17 Set Operators The


union operator retums
all distinct rows selected by
eith
query
(MSBTE- W-13, S-14,
W-17)
Syntax
a.2.17.1 Explain the set operators
of SQL. Select column name
(Refer sections 2. 17 to 2.17.4) from table
Union
W-13, S-14,W-17, 4 Marks
Select column name
from table 2
DBMS (MSBTE-Sem. 3-Comp)
2-32 Relational Data Model
On
tah o Example
2.17.3 Intersect
emp
eresul Select DeptNo from
The intersect operator returms only those rows which are
ainion common to both the queries
Om select Deptno from
dept
dat G Syntax
ntain Output
Select column namne from table I

intersect
20
30 Select column name from table2
40
G'Example
2.17.2 Union AI
Select
l
emp
GDeptNo from
The Union All operator returns all rows selected by either
Intersect
query including duplicates.
selectDeptno from dept
G Syntax
Output
Select column name from table 1

10
Union all?
20

Select column name from table


2 30

Example 2.17.4 Minus


lary
are present in the
Minus operator displays the rows which
000 Select DeptNo
from emp query, with no duplicates and
first query but absent in the second
000 Union all
data is arranged in ascending order by default.
000
select Deptno from dept
-000 G Syntax
000 Output
aoalumn name fromn table 1
Select

10 minus

20 Select column name


2
from table

30 G Example

20 Select DeptNo from dept


10
intersect
eith
dby
10
select Deptno from emp
20

30
Output

40 40
Relational
Data Moce
-lDBMS
2-33
DBMS (MSBTE-Sem. 3-Comp)
Between r Examp
Operators-
: Range Searching Query
Syllabus Topic
- Between select * fro
Range Searching Operators (MSBTE -W-13
2.18 where
enan
W-13,2 Marks
"BETWEEN (Refer section
2.18) Output
Explain range searching operator,
.2.18.1 keyword is used in this
predicate.
range of values. The AND
to specify certain
Between predicate is used

Syntax
expression AND end expression
test expression [NOTI BETWEEN begin
G Exar
F Example Fue

Select from emp select

where comm between 300 and 500; en


where

Output Output
HIREDATE SAL COMM DEPTNO
EMPNO ENAME JOBMGR
7499 ALLEN SALESMAN 7698 02/20/1981 1600 300

7521 WARD SALESMAN 7698 02/22/1981 1250 500 30

In "Between" predicate the NOT keyword can also be used

Select from emp Exa

Ewhere conmm not between 300 and 500;

Output select

EMPNO ENAME where


JOB MGR HIREDATE SAL COMM DEPTNO
Output
7654 MARTIN SALESMAN 7698 09/28/1981 1250 1400 30
7844 TURNER SALESMAN 7698 09/08/1981 1500 0 30
Syllabus Topic : Pattern Matching
Operators -Like

2.19 Pattern Matching Operators- Like


(MSBTE-W-13)
|Q.2.19.1 Explain pattern matching operator"LIKE" (Aeter section2.19)
W-13, 2 Marks
Like operator determines whether a
specific character string matches
the given pattem or not. In the pattern we can use regular
characters and wildcard characters, In this pattern select
matching, it is necessary that regular characters must
exactly match the chaa
specified in the character string. However, for
the wildcard characters arbitrary fragment matching the where
of character string is done.
of wildcard characters makes the LIKE operator more flexible.
aMode Relational Data Model
DBMS (MSBTE-Sem.
3-Comp)
2-34
names starts letter J"
Example: Display ecords of employee whose
with
G

select from emp


W-13)
where
ename like J%:
Marks
Output
HIREDATE SAL COMM DEPTNO
EMPNO ENAME JOB MGR

MANAGER 7839 04/02/1981 2975 20


7566 JONES

950 30
7900 JAMES CLERK 7698 12/03/1981

names ends with letter *N"


Display records of employee whose
r Example :

Query

Select from emp

%N;
where ename like

Output
HIREDATE SAL |COMM DEPTNO
JOB MGR
EMPNOENAME
7698 1600 300 30
SALESMAN 02/20/1981
7499 ALLEN

SALESMAN 7698 09/28/1981 1250 1400 30


MARTIN
7654
as second character.
employee whose names contains L'
Example: Display records
of

Query

select * fromemp

where ename like L%;

Output

ENAME JOB MGR HIREDATE SAL COMM DEPTNO


EMPNO
30
MANAGER 7839 05/01/1981 2850
7698 BLAKE
10
7839 06/09/1981 2450
7782 CLARK MANAGER

02/20/1981 1600 300 30


SALESMAN 7698
7499 ALLEN

W-13) names contains character "A' anywhere;


EXample : Display records of emplovee whose

Warks
G Query

egul select
from emp
aractes

Theus
Where ename like '%A%;
Belational Data
2-35
M
(MSBTE-Sem. 3-Comp)

COMM DEPTNO
Output IREDATE SAL
MGR
EMPNO ENAME JOB
2850 30
05/01/1981
MANAGER 7839
7698 BLAKE 10
06/09/198
1

2450
MANAGER 7839
CLARK
7782 300 30
02/20/198 1 1600
SALESMAN 7698
7499 ALLEN
1250 500 30
SALESMAN 7698 02/22/198 1

7521 WARD (i)


09/28/1981 1250 1400 30
SALESMAN 7698
7654 MARTIN
(ii)
01/12/1983 1100 20
CLERK 7788
7876 ADAMS
30 (iii)
12/03/1981 950
CLERK 7698
7900 JAMES
(iv)

: a. 2

a. 2.19.4 Consider following schema


Q. 2.19.2 Consider the following schema.
Employee (emp_no, emp_ name, dat
STUDENT (Name, Mark, Age, Place, Phone,
Birthdate) designation, salary, Dept_location)
:
Write SQL queries for following: Solve following queries
) To list name of studernt who do not have () list all Managers in Mumbai location
phone number.
(ii) set salary of all 'project leaders' to 70000
(0) To list student frorn Nashik and Pune.
(i) employees with having alphabet 'A'
list
(i) To change mark of Monika to 88 instead
second letter in their name.
of 80.
(iv) To list the student from Amit's age group. (iv) display details of those employees wt
2.19) work in Mumbai or Chennai.
(Refer section W-13,4 Marks
(Refer section 2. 19)
(i) Select * from STUDENT where phone is NULL;
W-14, W-17, 4 Marks
(ii) Select * from STUDENT where city in('Nashik', 'Pune'): () s
) Select * from employee where Dept location = 'Mumbai
(ii) Update STUDENT set marks = 88 where name = 'Monika'; (ii) S
(ii) Update employee set sal = 70000 where designaiot
(iv) Select * from emp where age = (select age from emp where 'project leader'; in
name ='Amit');
(i) Select * from employee where emp_name like (ii) Se
Q.2.19.3 Consider following database and solve queries
emp (empno, ename, ph, sal, dept no,comm) (iv) Select
in('Mumtu (iv) Se
*
from employee where Dept_ location
)Change employee name "Rahul' to 'Chennai'); Q.2.1
Ramesh.
Q.2.19.5 Given
() Give increment of 20 %
in salary to all
employees. (Refer section2.19) Employee(EMP ID,FIRST NAME,
LAST NAME, SALARY,
W-14, W-15, 4 Marks
(i) Update emp set ename = Ramesh' where ename = 'Rahul':
JOINNG DATE,DEPARTMENT)
Write SQL queries for
(ii) Update emp set sal = sal *0.2; NAME
LAST
0Get FIRST NAME,
employee table.
laDBMS (MSBTE-Sem. 3-Comp) 2-36 Relational Data Model
(i) Get uniquo DEPARTMENT from
employeo table. Wite SQL queries for the following:
i) Get FIRST_NAME from employee
(i) Display total number of books for subject
table DBM.
using alias name "Employee Name
(v) Get FIRSTNAME from employee (i) Get authorwise list of all books,
table
after removing white spaces from left
side. 6S (i) Display all books whose prices are
(Refer section 2.19) between Rs. 200 and Rs. 500.
S-15,4 Marks
(Refer section 2.19) S-16, S-17,4 Marks
Select first_name, last_name from employee:
(i) Select count(*) from Book master where
G) Select uniquedepartment) from employee; book name ='DBM':

(iii) Select fist_name "Employee Name" fromemployee; (ii) Select count * from Book_master order by author;

óv) Select Itrim(first_name) from employee; (iii) Select count * from Book _master where price between 200

o. 219.6 Consider the following database: and 500;

Employee (emp_id, emp_name, Q.2.19.8 Consider the following database schema:


emp_city,
ernp_addr, emp _dept,join_date) Emp (Empno, Ename, job, mgr, joindate,
Solve the following query :
salary, comm, deptrno,loc).
Display the namnes of employees in capita! Write the SQL queries for the following :
letters. Write a query to find list of employees
(ü) Display the empid of employee who live whose salary is not less 5000.
in city Pune and Mumbai. () Wite a query to find list of employees
whose job title is either "Manager or
(üi) Dísplay the details of employees whose
"Analyst".
joining date is after '01-Apr.-1997.
(iv)
(ii) Change the location of deptno 40 to Pune
Dispiay the total number of employees
from Chandrapur.
whose departmnent numnber is 10
(iv) Display the Ename and salary of
(Befer section2.19) S16,4 Marks
employees who eam more than Rs.
) Select ucase(emp_name) from employee; 50,000 and are in deptno 10 or 30.
(Aefer section2.19) W-16, 4 Marks
(ü) Select emp_id from employee where emp_city
in("Pune','Mumbai');: (i) Select * from emp where salary NOT < 5000

(i) Select * from employee where join_date > '01-Apr.- 1997; (ii) Select * from emp where job in ("Manager' or “Analyst');

(iv) Select count(*) from employee where deptno = 10; (iii) Update emp set loc = 'Pune' where deptno = 40 and
loc = Chandrapur
Q.219.7 Consider the structure for book table as
Book master (book id, book _name, subcode, (iv) Select ename,salary from emp where salary > 50000 and
author,no_ of_copies, price). deptno in(10,30);

You might also like