+ All Categories
Home > Documents > Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant...

Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant...

Date post: 02-Apr-2018
Category:
Upload: hoangnguyet
View: 220 times
Download: 1 times
Share this document with a friend
101
DATABASE SYSTEMS IT 0303 5 TH Semester D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT
Transcript
Page 1: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

DATABASE SYSTEMS IT 0303 5TH Semester 

D.Hemavathi,R.Venkatalakshmi

Assistant Professor,

SRM University, Kattankulathur

School of Computing, Department of IT

Page 2: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Unit3:SQL

Page 3: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Disclaimer

The contents of the slides are solely for the purpose of teaching students at SRM University. All copyrights and Trademarks of organizations/persons apply even if not specified explicitly. 

Page 4: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Basic Structure SQL – Structured Query Language

– DDL (Data Definition Language) – commands‐create, alter,drop

– DML (Data Manipulation Language)‐ insert, update, delete,(select)

– DCL (Data Control Language)‐ grant, revoke

Page 5: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

DDL Commands

Create a table as depts with the following fields :

deptno number(2), dname varchar(15), location varchar(20) .

SQL> create table depts(deptno number(2),dname

varchar(15),location varchar(20));

Table created.

Add deptno as primary key .

SQL> alter table depts add primary key(deptno);

Table altered.

Page 6: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

DDL Commands

Create table employees with following fields :empno number(4),ename varchar(20),designation            varchar(15),manager number(4),join_dt date,salary  number(10,2),deptno number(2) .

SQL> create table employees (empno number(4),ename varchar(20),

2  designation varchar(15),3  manager number(4),4  join_dt date,5  salary number(10,2),6  deptno number(2));Table created.Add empno as primary key .SQL> alter table employees add primary key(empno);Table altered.

Page 7: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Depts Employee

DeptnoEmpno

Page 8: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

• Add deptno as foreign key .

SQL> alter table employees add foreign key(deptno) references depts(deptno);

DDL Commands

Page 9: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

DML Commands

Insert records into depts table .

SQL> insert into depts values(1,'diwaagar','chennai');

1 row created.

SQL> insert into depts values(2,'ajay','tnagar');

1 row created.

Page 10: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

DML Commands

Insert records into employees table .

SQL> insert into employees values (1000,'hari','manager',1020,'01‐jan‐98',103000.00,10);

1 row created.

SQL> insert into employees values (1000,'hari','analyst',1020,'01‐jan‐98',10300.00,1);

Page 11: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

DATA RETRIEVAL

Select all records from depts table .

SQL> select * from depts;

DEPTNO DNAME           LOCATION                                                                  

‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

2 ajay            tnagar                                                                    

1 diwaagar        chennai 

Page 12: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

DATA RETRIEVAL – SELECT COMMAND

SQL> select * from employees;EMPNO ENAME  DESIGNATION   MANAGER JOIN_DT      SALARY    DEPTNO

‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐

1000  hari    analyst      1020    01‐JAN‐98    10300         1

1001  mani    clerk         1021    14‐MAR‐99     10300         2

1002  rahul   clerk         1022    03‐JUN‐00     1030         1

1003  sohail  manager   1023    03‐JUL‐89      13430         2

1004  munna   salesman  1024    19‐MAY‐89     11430         1

1005  lokesh  manager   1025    22‐MAY‐89     15430         1

1006  kamala  clerk         1030    07‐MAR‐78     100012       2

1007  nadim   clerk         1031    07‐MAR‐78     10012         1

8 rows selected.

Page 13: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Retrieve only the required columns

Display location,dname from depts table .

SQL> select location,dname from depts;

LOCATION          DNAME                                                                    

‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

tnagar                   ajay                                                                         

chennai                diwaagar 

Display designation from employees table

Page 14: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Retrieve only the required columns 

SQL> select designation from employees;DESIGNATION                                                                                         ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐analyst                                                                                             clerk                                                                                               clerk                                                                                               manager                                                                                             salesman                                                                                            manager  clerkclerk                                                                                           8 rows selected.

Page 15: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Dintinct ‐ Eliminate the duplicate results

Eliminate the duplicate results from the previous    result

SQL> select distinct(designation) from employees;DESIGNATION ---------------analyst clerk manager salesman 4 rows selected

Page 16: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

RENAME THE COLUMN

Display dname from depts table with the column 

heading as "Department Name " .

SQL> select dname "department name" from depts;

department name                                                                                     

‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

ajay                                                                                                

diwaagar 

Page 17: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

SELECT COMMAND WITH WHERE CLAUSE

Display empno,ename,designation,deptno for all the 

employees who are working in depart number 2.

SQL> select empno,ename,designation,deptno from employees where deptno=2;EMPNO ENAME          DESIGNATION                        DEPTNO                                            

‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐

1001  mani                 clerk                   2                                            

1003  sohail               manager             2 

1006 kamala           clerk                    2

Page 18: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

SELECT COMMAND WITH WHERE CLAUSE

• List out all the Managers with their respective Name . 

• Find the employees with their empno,name,joining  date and department number in the organization  with their joining date is on or before 1st JANUARY 1998. 

• List out all the employees with their empno,  employee name,designation,salary with their designation,salary with their designation as  "Engineer" or their pay>10000.

• Find out all MANAGERS who are not in Deptno 1

• Find everyone in dept no 2 who is neither clerk salesman or analyst 

Page 19: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

IN, NOT IN OPERATOR

Find everyone in dept no 2 who is neither clerk 

salesman or analyst .

SQL> select * from employees where deptno=2 and designation not in ('clerk','salesman','analyst');

EMPNO ENAME  DESIGNATION  MANAGER JOIN_DT   SALARY    DEPTNO              

‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐

1003  sohail   manager      1023    03‐JUL‐89  13430         2 

Page 20: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

BETWEEN…AND

Display all the employees with empname,join date, designation where between 1‐jan‐88 and  1‐jan 99.

SQL> select ename,designation,join_dt from employees where join_dt between '01‐jan‐89' and '01‐jan‐99';

ENAME                DESIGNATION     JOIN_DT                                                        

‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐

hari                  analyst          01‐JAN‐98                                                      

sohail                manager          03‐JUL‐89                                                      

munna                 salesman         19‐MAY‐89                                                      

lokesh               manager          22‐MAY‐89 

Page 21: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

BETWEEN…AND

Display all the employees with empname,join date, designation where  not between 1‐jan‐88 and  1‐jan 99.

SQL> select ename,designation from employees where join_dt not between '01‐jan‐89' and '01‐jan‐99';

ENAME                 DESIGNATION                                                             

‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

mani                  clerk                                                                          

rahul                 clerk  

kamala clerk

nadim clerk 

Page 22: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

IN OPERATOR

Find all the employees who are Analyst,Salesman,and  

Engineer.

SQL> select * from employees where designation in ('analyst','engineer','salesman');

EMPNO ENAME  DESIGNATION  MANAGER JOIN_DT      SALARY    DEPTNO              

‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐

1000  hari     analyst       1020       01‐JAN‐98        10300         1              

1004  munna   salesman      1024       1 ‐MAY‐89       11430         1 

Page 23: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

IN OPERATOR

Find all the employees who are not  Analyst,Salesman,and Engineer.

Page 24: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

WILD CARD CHARS

Find out all employees who s name starts with K.

SQL> select ename from employees where ename like ‘k%';

EMPNO ENAME  DESIGNATION  MANAGER JOIN_DT      SALARY    DEPTNO              

‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐

1006 kamala   clerk         1030    07‐MAR‐78  100012         2

Page 25: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

WILD CARD CHARS

Find out all employees who s first letter is N and third letter is d and rest no specification.

SQL> select * from employees where ename like 'n_d%';EMPNO ENAME  DESIGNATION  MANAGER JOIN_DT      SALARY    DEPTNO              

‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐

1007   nadim  clerk         1031      07‐MAR‐78     10012         1

Page 26: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

ORDER BY 

List out all the employees with their empno,empname and designation. Display the employees in ascending order.

SQL> select empno,ename,designation from employees order by ename;

EMPNO ENAME                DESIGNATION                                                          ‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

1000 hari                  analyst                                                              1005 lokesh                manager                                                              1001 mani                  clerk                                                                1004 munna                 salesman                                                             1002 rahul                 clerk                                                                1003 sohail                manager   1006 kamala                clerk               

1007 nadim                 clerk 

Page 27: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

ORDER BY

List out all the employees from dept no 2 with their empno,empname and designation. Display the employees in descending order

SQL> select empno,ename,designation from employees where deptno=2 order by ename desc;

EMPNO ENAME                DESIGNATION                                                     

‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

1003      sohail                manager                                           

1001       mani                  clerk                                                   

1006        kamala clerk

Page 28: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

More on INSERT Commands

• Insert a record into the employees table with values only for empno,empname,join_dt and deptno .

SQL> insert into employees(empno,ename,join_dt,deptno) values(1501,'ravi','01-jan-89',2);

Page 29: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

More on INSERT Commands

• Insert a record into the employees table with values only for empname,join_dt and deptno. Verify the result and justify why this command is not working .

Page 30: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

More on INSERT Commands

Try to insert a record into employees table with a value for deptno which is not available in depts table. Verify the result and command .

SQL> insert into employees values(001,'Sai','clerk',1010,'01‐jan‐88',100.21,3);

ERROR at line 1:

ORA‐02291: integrity constraint (ITABATCH2.SYS_C00600) violated ‐ parent key not found

Page 31: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

More on CREATE COMMAND

Create a table called 'mkting'.select rows from employees table (using create command only)

SQL> create table mkting(empno,ename,pay) as select empno,ename,salary from employees;

Table created.

Page 32: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

More on ALTER COMMAND

Write a query to add new column 'number of dependents' into employees table .

SQL> alter table employees add (number_of_dependents number(2));

Table altered.

Page 33: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

More on ALTER COMMAND

Write a query to alter the data type of deptno to string in the marketing table .SQL> alter table marketing modify (deptno varchar(20));Table altered.SQL> desc marketingName                            Null?    Type‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐ ‐‐‐‐EMPNO                                    NUMBER(4)ENAME                                    VARCHAR2(20)DESIGNATION                        VARCHAR2(5)MANAGER                               NUMBER(4)JOIN_DT                                  DATESALARY                                   NUMBER(10,2)DEPTNO                                   VARCHAR2(20

Page 34: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

More on ALTER COMMAND

Write a query to remove deptno from marketing table.SQL> alter table marketing drop column deptno;Table alteredSQL> desc marketingName                            Null?    Type‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐ ‐‐‐‐EMPNO                                    NUMBER(4)ENAME                                    VARCHAR2(20)DESIGNATION                              VARCHAR2(5)MANAGER                                  NUMBER(4)JOIN_DT                                  DATESALARY                                   NUMBER(10,2)

Page 35: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

DDL ‐ DROP COMMAND

Write a query to drop marketing table .

SQL> drop table marketing;

Table dropped.

Page 36: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

DML – UPDATE COMMAND

Change empno and salary of employee where name='munna'.                                                     

SQL> update mkting set empno=1111,pay=(pay*0.1) where ename='munna';

1 row updated.

Page 37: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

• Change salary of employee where empno=1111;

Page 38: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

DELETE COMMAND

Remove employee 'munna' from mkting table .

SQL> delete from mkting where ename='munna';

1 row deleted.

Page 39: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

• Delete employee with no 1111 from mkting table empno=1111 .

Page 40: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

ROLLBACK AND COMMIT 

Verify rollback and commit commands .

SQL> rollback

2  ;

Rollback complete.

SQL> select * from mkting;

EMPNO ENAME                      PAY                                              

‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐

1004 munna                    11430   

1110 palani 1111019

Page 41: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

ROLLBACK and COMMIT

SQL> delete from mkting where ename='munna';

1 row deleted.

SQL> delete from mkting where empno=1110;

1 row deleted.

SQL> commit

2  ;

Commit complete.

SQL> select * from mkting;

no rows selected

Page 42: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

WORKIG WITH NUMBERS AND CHARACTERS

Display the salaries of all employees in a format such as 9,99,999.99 with empno and ename .

SQL> column salary format 9,99,999.99;

SQL> select ename,empno,salary from employees;

ENAME                    EMPNO       SALARY                                                         

‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐

ravi                      1501                                                                      

hari                      1000    10,300.00                                                         

mani                      1001    10,300.00                                                         

rahul 

Page 43: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

• Display the salaries of all employees and 10% of salary as bonus with empno and ename .Display bonus amount in separate column .

Page 44: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

ARITHMETIC OPERATIONS 

• ABS

• CEIL

• FLOOR

• MOD

• POWER

• SQRT

Page 45: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

GROUP FUNCTIONS

• SUM

• AVG

• MIN

• MAX

• AVG

• COUNT

Page 46: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

• Find out the sum of salaries of the managers from employees table .

SQL> select sum(salary) from employees where designation='manager';

SUM(SALARY)                                                                                         

‐‐‐‐‐‐‐‐‐‐‐

28860 

Page 47: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

• Find out the average salary of all employees in department No.1 from employees table .

SQL> select avg(salary) from employees where deptno=1;

AVG(SALARY)                                                                                         

‐‐‐‐‐‐‐‐‐‐‐

9640.4 

Page 48: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

• Find out total number of employees working in the organization .

SQL> select count(*) from employees;

COUNT(*)                                                                                           

‐‐‐‐‐‐‐‐‐

10 

Page 49: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

GROUP BY AND HAVING CLAUSE

List the average salary of all employees in each department .

SQL> select deptno,avg(salary) from employees group by deptno;

DEPTNO AVG(SALARY) --------- -----------

1 9640.4 2 41247.333

Page 50: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

GROUP BY AND HAVING CLAUSE

Find the average salary of managerial staff .SQL> select designation,avg(salary) from employees group by designation having designation not in ('manager','managing director','General manager');

DESIGNATION AVG(SALARY) --------------- -----------Analyst 10300 clerk 30338.5 salesman 11430

Page 51: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

GROUP BY AND HAVING CLAUSE

List the average salary for all the designation group consisting of more than one employee .

SQL> select designation,avg(salary) from employees group by designation having count(*)>1;

DESIGNATION AVG(SALARY) --------------- -----------clerk 30338.5 manager 14430

Page 52: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

CHARACTER FUNCTIONS

Display dname and location column from depts table  

into a single column name as departments .

SQL> select deptno||' '||dname "Departments" from depts;

Departments                                                                                         

‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

2 ajay                                                                                              

1 diwaagar 

Page 53: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

DECODE

Generate a classification table for all employees as  follows :Clerk‐level 1 Salesman‐level 4Manager‐level 3 Analyst‐level 5  Others‐level 6 

SQL> select designation,decode (designation,'clerk',1,'manager',2,'vice president',3,'MD',5,6) "Grade"from employees; 

DESIGNATION         Grade                                                                           ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐analyst                 6                                                                           clerk                   1                                                                           clerk                   1                                                                           manager                 2                                                                           salesman                6                                                                           manager                 2                                                                           

Page 54: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

INITCAP, UPPER, LOWER

Display all the employees with their first letter in  

Uppercase , all the letters in upper case and all   

the letters in lower case.

SQL> select initcap(ename),upper(ename),lower(ename) from employees;

INITCAP(ENAME)       UPPER(ENAME)         LOWER(ENAME)                                              

‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐

Ravi                 RAVI                 ravi                                                      

Hari                 HARI                 hari                                                      

Mani                 MANI                 mani                                                      

Rahul                RAHUL                rahul                                                     

Page 55: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

LPAD

Find out the difference in output for the following 

query: select LPAD(dname,20),location from depts;

SQL> select lpad(dname,20) location from depts;

LOCATION                                                                                            

‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

ajay                                                                                

diwaagar 

Page 56: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

LTRIM, RTRIM

Identify the use of LTRIM and RTRIM functions .

SQL> select ltrim('  mark'),rtrim('hai  ') from dual;

LTRI RTR                                                                                            

‐‐‐‐ ‐‐‐

Mark HAI 

Page 57: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

LENGTH

Display the number of characters in the name field .   

SQL> select length('Mark allen') from dual;

LENGTH('MARKALLEN')                                                                                 

‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

10 

Page 58: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

INSTR

Find in which position 'AND' string is assorted in  

the field of location.

SQL> select instr(location,'and') from depts.;

INSTR(LOCATION,'AND')                                                                               

‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

0                                                                               

Page 59: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

DATE FUNCTIONS

List out empname and join date from employees  

table.And assure that the join date is in the  

format of dd/mm/yy.

SQL> select ename,to_char(join_dt,'dd/mm/yy') from employees;

ENAME                TO_CHAR(                                                                       

‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐

ravi                 01/01/89                                                                       

hari                 01/01/98                                                                       

mani                 14/03/99                                                                       

rahul                03/06/00                                                                       

sohail               03/07/89 

Page 60: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

List out empname and join date from employees 

table.And assure that the join date is in the format    

of day,month dd,yyyy.

List out empname and join date from employees 

table.And assure that the join date is in the format 

of dy dd mon, yy .

Page 61: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

List out empname and join date from employees 

table.And assure that the join date is in the format 

of month dd,yyyy hh:mi pm .

Page 62: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

DATE FUNCTIONS

• a: ADDMONTHS(d,n)

• b: GREATEST(D1,D2,...)

• c: LEAST(D1,D2,...) 

• d: MONTHS_BETWEEN(d1,d2)   

• e: SYSDATE

Page 63: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Identify increment date of all employees based on 

their joining date.

SQL> select add_months(join_dt,10) from employees;

Page 64: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

NULL

SQL> select ename,commission from employees where commission is null;

ENAME                COMMISSION                                                                     

‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐

ravi                                                                                                

mani                                                                                                

sohail                                                                                              

kamala 

Page 65: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

• Display all the rows with the designation field with NULL value .

Page 66: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

NVL

Display commission amount ascending order .           

SQL> select commission from employees order by nvl(commission,0);COMMISSION                                                                                          ‐‐‐‐‐‐‐‐‐‐

154.5                                                                                          1501.8                                                                                          1545                                                                                          1714.5                                                                                          

Page 67: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

• Display salary+commission amount in ascending order.

Page 68: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

JOINS 

• EQUI JOIN

• NON EQUI JOIN

• SELF JOIN

• OUTER JOIN

– LEFT OUTER JOIN

– RIGHT OUTER JOIN

Page 69: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

EmployeeEMPNO ENAME  DESIGNATION   MANAGER JOIN_DT     SALARY    DEPTNO

1000  hari    analyst        1020    01‐JAN‐98     10300         11001  mani    clerk          1021    14‐MAR‐99    10300         21002  rahul   clerk          1022    03‐JUN‐00       1030         11003  sohail  manager        1023    03‐JUL‐89      13430         21004  munna   salesman       1024    19‐MAY‐89     11430         11005  lokesh  manager        1025    22‐MAY‐89     15430         11006  kamala  clerk          1030    07‐MAR‐78   100012         21007  nadim   clerk          1031    07‐MAR‐78     10012         1

DeptsDEPTNO DNAME LOCATION

1 Admin Chennai 2 Accounts Mumbai

Page 70: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

EmployeeEMPNO ENAME  DESIGNATION   MANAGER JOIN_DT     SALARY    DEPTNO

1000  hari    analyst        1020    01‐JAN‐98     10300         11001  mani    clerk          1021    14‐MAR‐99    10300         21002  rahul   clerk          1022    03‐JUN‐00       1030         11003  sohail  manager        1023    03‐JUL‐89      13430         21004  munna   salesman       1024    19‐MAY‐89     11430         11005  lokesh  manager        1025    22‐MAY‐89     15430         11006  kamala  clerk          1030    07‐MAR‐78   100012         21007  nadim   clerk          1031    07‐MAR‐78     10012         1

DeptsDEPTNO DNAME LOCATION

1 Admin Chennai 2 Accounts Mumbai

Page 71: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

EQUI JOIN

Find the location of the department where Mr.Hari  

works .

SQL> select empno,ename,designation,location,dname from employees,depts where employees.deptno=depts.deptno and ename='hari' 

EMPNO ENAME DESIGNATION     LOCATION          DNAME                       

‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐

1000 hari ANALYST Chennai             ADMIN 

Page 72: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

NON EQUI JOIN

Create a table as grades as follows :

Grade Low High

1 1500 2500

2 2501 6500

3 6501 8000

4 8001 15000

5 15001 300000

Find the salary grade of each employee based on their  salary 

SQL> create table grades(grade number(1),low number(6),high number(6));

Table created.

Page 73: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

NON EQUI JOIN

SQL> select empno,ename,grade from employees,grades where employees.salary>=grades.low and employees.salary<=grades.high;

EMPNO ENAME      GRADE                                                            

‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐

1000 hari                         5                                                            

1001 mani                       3                                                            

1003 sohail                      2                                                            

1004 munna                    1                                                            

1005 lokesh                     5                                                            

1007 nadim                      5 

Page 74: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

SELF JOIN

Find out the names designation,salaries of those   

employees in the organisation who are paid the same  

or more than employee ‘mani’ .

Page 75: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

EmployeeEMPNO ENAME   DESIGNATION   MANAGER JOIN_DT      SALARY    DEPTNO

1000  hari    analyst        1020    01‐JAN‐98     10300         11001  mani    clerk          1021    14‐MAR‐99    10300         21002  rahul   clerk          1022    03‐JUN‐00       1030         11003  sohail  manager        1023    03‐JUL‐89      13430         21004  munna   salesman       1024    19‐MAY‐89     11430         11005  lokesh  manager        1025    22‐MAY‐89     15430         11006  kamala  clerk          1030    07‐MAR‐78   100012         21007  nadim   clerk          1031    07‐MAR‐78     10012         1

Page 76: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Employee XEMPNO ENAME  DESIGNATION   MANAGER JOIN_DT     SALARY    DEPTNO1000  hari    analyst       1020    01‐JAN‐98     10300         11001  mani    clerk          1021    14‐MAR‐99    10300         21002  rahul   clerk          1022    03‐JUN‐00       1030         11003  sohail  manager        1023    03‐JUL‐89      13430         21004  munna   salesman       1024    19‐MAY‐89     11430         11005  lokesh  manager        1025    22‐MAY‐89     15430         11006  kamala  clerk          1030    07‐MAR‐78   100012         21007  nadim   clerk          1031    07‐MAR‐78     10012         1

Employee YEMPNO ENAME DESIGNATION MANAGER JOIN_DT SALARY DEPTNO1000 hari analyst 1020 01-JAN-98 10300 11001 mani clerk 1021 14-MAR-99 10300 21002 rahul clerk 1022 03-JUN-00 1030 11003 sohail manager 1023 03-JUL-89 13430 21004 munna salesman 1024 19-MAY-89 11430 11005 lokesh manager 1025 22-MAY-89 15430 11006 kamala clerk 1030 07-MAR-78 100012 21007 nadim clerk 1031 07-MAR-78 10012 1

Page 77: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

SELF JOINFind out the names designation,salaries of those   

employees in the organisation who are paid the same  

or more than employee ‘mani’ .

SQL> select x.ename,x.designation,x.salary from employees x,employees y where x.salary>=y.salary and y.ename='mani';

ENAME  DESIGNATION  SALARY    hari analyst       10300         mani clerk          10300         sohail manager        13430         munna salesman       11430         lokesh manager        15430         kamala  clerk            100012       

Page 78: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

LEFT OUTER JOIN

Assume depts table has an entry for EDP department.  

There are no corresponding entries in the employees   

table.Join the tables to show the department name 

which is not having corresponding entries in   

employees table .

Page 79: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Example of equi JoinSQL> select empno,ename,dname from employees,depts where 

employees.deptno=depts.deptno;

EmployeeEMPNO ENAME  DESIGNATION   MANAGER JOIN_DT     SALARY    LOCATION

1000  hari    analyst        1020    01‐JAN‐98     10300         Chennai1001  mani    clerk          1021    14‐MAR‐99    10300         Mumbai1002  rahul   clerk          1022    03‐JUN‐00       1030         Chennai1003  sohail  manager        1023    03‐JUL‐89      13430         Mumbai1004  munna   salesman       1024    19‐MAY‐89     11430         Chennai1005  lokesh  manager        1025    22‐MAY‐89     15430         Chennai1006  kamala  clerk          1030    07‐MAR‐78   100012         Mumbai1007  nadim   clerk          1031    07‐MAR‐78     10012         Mumbai

Page 80: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

LEFT OUTER JOIN

Assume depts table has an entry for EDP department.  

There are no corresponding entries in the employees   

table.Join the tables to show the department name 

which is not having corresponding entries in   

employees table .

SQL> select empno, ename, designation, manager, join_dt, salary,    location  from employees,depts where employees.deptno(+)=depts.deptno;

Page 81: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

EmployeeEMPNO ENAME  DESIGNATION   MANAGER JOIN_DT     SALARY    LOCATION

1000  hari    analyst        1020    01‐JAN‐98     10300         Chennai1001  mani    clerk          1021    14‐MAR‐99    10300         Mumbai1002  rahul   clerk          1022    03‐JUN‐00       1030         Chennai1003  sohail  manager        1023    03‐JUL‐89      13430         Mumbai1004  munna  salesman      1024    19‐MAY‐89     11430         Chennai1005  lokesh manager        1025    22‐MAY‐89     15430         Chennai1006  kamala clerk          1030    07‐MAR‐78   100012         Mumbai1007 nadim  clerk         1031    07‐MAR‐78     10012         Mumbai

Page 82: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

SQL> select empno, ename, designation, manager, join_dt, salary,    depts.deptno,dname, location  from employees,depts where employees.deptno(+)=depts.deptno;

Page 83: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

EmployeeEMPNO ENAME  DESIGNATION   MANAGER JOIN_DT     SALARY    DEPTNO DNAME LOCATION

1000  hari    analyst        1020    01‐JAN‐98     10300         1 Admin Chennai

1001  mani    clerk          1021    14‐MAR‐99    10300         2 Accounts Mumbai

1002  rahul   clerk          1022    03‐JUN‐00       1030         1 Admin Chennai

1003  sohail  manager      1023    03‐JUL‐89      13430       2 Accounts Mumbai

1004  munna   salesman    1024    19‐MAY‐89     11430         1 Admin Chennai

1005  lokesh  manager     1025    22‐MAY‐89     15430         1 Admin Chennai

1006  kamala  clerk          1030    07‐MAR‐78   100012         2 Accounts Mumbai

1007  nadim   clerk          1031    07‐MAR‐78     10012         1 Admin Chennai

Page 84: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

SUB‐QUERIES OR NESTED QUERIES

Find all the employees who have the same job as 

‘hari’.

SQL> select ename,empno,designation from employees where designation=(select designation from employees where ename='hari');

ENAME            EMPNO DESIGNATION                                                          

‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

hari                      1000 analyst 

Page 85: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

SUB‐QUERIES OR NESTED QUERIES

Display the employee name who is getting the minimum salary.

SQL> select empname from employees where salary =(select min(salary) from employees);EMPNAME --------------------Mark Nadia Nadal Diwaagar Charles

Page 86: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

SUB‐QUERIES OR NESTED QUERIES

Who is the youngest male employeeSQL> select empname,dob from employees where dob=(select max(dob) from employees where sex='Male');

EMPNAME DOB -------------------- ---------charles 29-DEC-98

Page 87: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

SUB‐QUERIES OR NESTED QUERIES

• Identify the female employees earning more than the highest salary of male employee?

Page 88: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Set Operations

• The set operations union, intersect, and except operate on relations and correspond to the relational algebra operations ∪, ∩, −.

• Each of the above operations automatically eliminates duplicates; to retain all duplicates use the corresponding multiset versions union all, intersect all and except all.

Suppose a tuple occurs m times in r and n times in s, then, it occurs:– m  + n times in r union all s

– min(m,n) times in r intersect all s

– max(0, m – n) times in r except all s

Page 89: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Set Operations• Find all customers who have a loan, an account, or both:

(select customer-name from depositor)except(select customer-name from borrower)

(select customer-name from depositor)intersect(select customer-name from borrower)

Find all customers who have an account but no loan.

(select customer-name from depositor)union(select customer-name from borrower)

Find all customers who have both a loan and an account.

Page 90: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Views

• Provide a mechanism to hide certain data from the view of certain users.  To create a view we use the command:

create view v as <query expression>

where:<query expression> is any legal expressionThe view name is represented by v

Page 91: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Example Queries

• A view consisting of branches and their customers

Find all customers of the Perryridge branch

create view all-customer as(select branch-name, customer-namefrom depositor, accountwhere depositor.account-number = account.account-number)union

(select branch-name, customer-namefrom borrower, loanwhere borrower.loan-number = loan.loan-number)

select customer-namefrom all-customerwhere branch-name = ‘Perryridge’

Page 92: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Update of a View

• Create a view of all loan data in loan relation, hiding the amount attribute

create view branch‐loan asselect branch‐name, loan‐numberfrom loan

• Add a new tuple to branch‐loan

insert into branch‐loanvalues (‘Perryridge’, ‘L‐307’)

This insertion must be represented by the insertion of the tuple

(‘L‐307’, ‘Perryridge’, null)

into the loan relation

• Updates on more complex views are difficult or impossible to translate, and hence are disallowed. 

• Most SQL implementations allow updates only on simple views (without aggregates) defined on a single relation

Page 93: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Joined Relations – Datasets for Examples

• Relation loan

Relation borrower

customer-name loan-number

JonesSmithHayes

L-170

L-230

L-155

amount

3000

4000

1700

branch-name

DowntownRedwoodPerryridge

loan-number

L-170 L-230L-260

Note: borrower information missing for L-260 and loan information missing for L-155

Page 94: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Joined Relations –Examples 

• loan inner join borrower onloan.loan‐number = borrower.loan‐number

loan left outer join borrower onloan.loan-number = borrower.loan-number

branch-name amount

DowntownRedwood

3000

4000

customer-name loan-number

JonesSmith

L-170

L-230

loan-number

L-170

L-230

branch-name amount

DowntownRedwoodPerryridge

3000

4000

1700

customer-nameloan-number

JonesSmithnull

L-170

L-230

null

loan-number

L-170

L-230

L-260

Page 95: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Joined Relations –Examples

• loan natural inner join borrower

loan natural right outer join borrower

branch-name amount

DowntownRedwood

3000

4000

customer-name

JonesSmith

loan-number

L-170

L-230

branch-name amount

DowntownRedwoodnull

3000

4000

null

customer-name

JonesSmithHayes

loan-number

L-170

L-230

L-155

Page 96: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Joined Relations – Examples

• loan full outer join borrower using (loan‐number)

Find all customers who have either an account or a loan (but not both) at the bank.

branch-name amount

DowntownRedwoodPerryridgenull

3000

4000

1700

null

customer-name

JonesSmithnullHayes

loan-number

L-170

L-230

L-260

L-155

select customer-namefrom (depositor natural full outer join

borrower)where account-number is null or loan-number

is null

Page 97: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Domain Types in SQL

• char(n). Fixed length character string, with user‐specified length n.• varchar(n).  Variable length character strings, with user‐specified 

maximum length n.• int.  Integer (a finite subset of the integers that is machine‐dependent).• smallint. Small integer (a machine‐dependent subset of the integer 

domain type).• numeric(p,d). Fixed point number, with user‐specified precision of p

digits, with n digits to the right of decimal point. • real, double precision. Floating point and double‐precision floating 

point numbers, with machine‐dependent precision.• float(n). Floating point number, with user‐specified precision of at least 

n digits.• Null values are allowed in all the domain types.  Declaring an attribute to 

be not null prohibits null values for that attribute.• create domain construct in SQL‐92 creates user‐defined domain types

create domain person‐name char(20) not null

Page 98: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Create Table Construct

• An SQL relation is defined using the create table command:

create table r (A1 D1, A2 D2, ..., An Dn,(integrity‐constraint1),...,(integrity‐constraintk))

– r is the name of the relation

– each Ai is an attribute name in the schema of relation r

– Di is the data type of values in the domain of attribute Ai

• Example:

create table branch(branch‐name char(15) not null,branch‐city char(30),assets integer)

Page 99: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Integrity Constraints in Create Table• not null

• primary key (A1, ..., An)

• check (P), where P is a predicate

Example: Declare branch-name as the primary key for branch and ensure that the values of assets are non-negative.

create table branch(branch-namechar(15),branch-city char(30)assets integer,primary key (branch-name),check (assets >= 0))

primary key declaration on an attribute automatically ensures not null in SQL-92 onwards, needs to be explicitly stated in SQL-89

Page 100: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Bibliography

1. Raghu Ramakrishnan, Johannes Gehrke, Database Management System, McGraw Hill., 3rd Edition 2003.

2. Elmashri & Navathe, Fundamentals of Database System, Addison-Wesley Publishing, 3rd Edition,2000.

3. Date C.J, An Introduction to Database, Addison-Wesley Pub Co, 7th Edition , 2001.

4. Jeffrey D. Ullman, Jennifer Widom, A First Course in Database System, Prentice Hall, AWL 1st Edition ,2001.

5. Peter rob, Carlos Coronel, Database Systems – Design, Implementation, and Management, 4th Edition, Thomson Learning, 2001.

Page 101: Subject Name & Code Semester Number - SRM Institute … D.Hemavathi,R.Venkatalakshmi Assistant Professor, SRM University, Kattankulathur School of Computing, Department of IT ...

Review questions

• Define relation, tuple & tuple variable, relationship data model, atomic domain.

• Define foreign key, referring relation & referenced relation.• What is meant by schema diagram?• Give the differences between schema &E-R diagrams.• Define procedural &nonprocedural language with example.• Define relational algebra.• List some of the fundamental operations of relational algebra.• What is meant by unary &binary operations?• Give the use of additional operations of relational algebra.• Define aggregate fns with an example.• What is meant by outer join? give its types• Define left, right & full outer join.• Define view.• What is meant by materialized views & view of maintenance?


Recommended