subject name & code semester number - srm institute … d.hemavathi,r.venkatalakshmi assistant...
TRANSCRIPT
DATABASE SYSTEMS IT 0303 5TH Semester
D.Hemavathi,R.Venkatalakshmi
Assistant Professor,
SRM University, Kattankulathur
School of Computing, Department of IT
Unit3:SQL
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.
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
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.
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.
Depts Employee
DeptnoEmpno
• Add deptno as foreign key .
SQL> alter table employees add foreign key(deptno) references depts(deptno);
DDL Commands
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.
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);
DATA RETRIEVAL
Select all records from depts table .
SQL> select * from depts;
DEPTNO DNAME LOCATION
‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
2 ajay tnagar
1 diwaagar chennai
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.
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
Retrieve only the required columns
SQL> select designation from employees;DESIGNATION ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐analyst clerk clerk manager salesman manager clerkclerk 8 rows selected.
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
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
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
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
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
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
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
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
IN OPERATOR
Find all the employees who are not Analyst,Salesman,and Engineer.
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
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
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
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
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);
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 .
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
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.
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.
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
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)
DDL ‐ DROP COMMAND
Write a query to drop marketing table .
SQL> drop table marketing;
Table dropped.
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.
• Change salary of employee where empno=1111;
DELETE COMMAND
Remove employee 'munna' from mkting table .
SQL> delete from mkting where ename='munna';
1 row deleted.
• Delete employee with no 1111 from mkting table empno=1111 .
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
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
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
• Display the salaries of all employees and 10% of salary as bonus with empno and ename .Display bonus amount in separate column .
ARITHMETIC OPERATIONS
• ABS
• CEIL
• FLOOR
• MOD
• POWER
• SQRT
GROUP FUNCTIONS
• SUM
• AVG
• MIN
• MAX
• AVG
• COUNT
• Find out the sum of salaries of the managers from employees table .
SQL> select sum(salary) from employees where designation='manager';
SUM(SALARY)
‐‐‐‐‐‐‐‐‐‐‐
28860
• 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
• Find out total number of employees working in the organization .
SQL> select count(*) from employees;
COUNT(*)
‐‐‐‐‐‐‐‐‐
10
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
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
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
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
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
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
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
LTRIM, RTRIM
Identify the use of LTRIM and RTRIM functions .
SQL> select ltrim(' mark'),rtrim('hai ') from dual;
LTRI RTR
‐‐‐‐ ‐‐‐
Mark HAI
LENGTH
Display the number of characters in the name field .
SQL> select length('Mark allen') from dual;
LENGTH('MARKALLEN')
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
10
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
3
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
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 .
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 .
DATE FUNCTIONS
• a: ADDMONTHS(d,n)
• b: GREATEST(D1,D2,...)
• c: LEAST(D1,D2,...)
• d: MONTHS_BETWEEN(d1,d2)
• e: SYSDATE
Identify increment date of all employees based on
their joining date.
SQL> select add_months(join_dt,10) from employees;
NULL
SQL> select ename,commission from employees where commission is null;
ENAME COMMISSION
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐‐‐‐‐
ravi
mani
sohail
kamala
• Display all the rows with the designation field with NULL value .
NVL
Display commission amount ascending order .
SQL> select commission from employees order by nvl(commission,0);COMMISSION ‐‐‐‐‐‐‐‐‐‐
154.5 1501.8 1545 1714.5
• Display salary+commission amount in ascending order.
JOINS
• EQUI JOIN
• NON EQUI JOIN
• SELF JOIN
• OUTER JOIN
– LEFT OUTER JOIN
– RIGHT OUTER JOIN
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
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
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
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.
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
SELF JOIN
Find out the names designation,salaries of those
employees in the organisation who are paid the same
or more than employee ‘mani’ .
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
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
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
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 .
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
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;
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
SQL> select empno, ename, designation, manager, join_dt, salary, depts.deptno,dname, location from employees,depts where employees.deptno(+)=depts.deptno;
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
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
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
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
SUB‐QUERIES OR NESTED QUERIES
• Identify the female employees earning more than the highest salary of male employee?
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
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.
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
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’
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
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
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
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
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
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
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)
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
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.
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?