interactive sql part ii prepared by: mitali sonar (assistant prof.) 1 10/17/2015

37
INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 0 6 / 2 7 / 2 2

Upload: roger-taylor

Post on 01-Jan-2016

221 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

INTERACTIVE SQL PART II

Prepared By: Mitali Sonar (Assistant Prof.)

1

04

/20

/23

Page 2: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

GROUP BY Group rows based on distinct values for columns divides a table into groups of rows used in

conjunction with the aggregate functions to group the result-set by one or more columns.

Find out how many students are there in each sub.

2

04

/20

/23

Page 3: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

Find out the number of students for each value of marks.

select count(marks) as "no of students", marks from student group by marks;

3

04

/20

/23

Page 4: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

Employee0

4/2

0/2

3

4

Eid Ename Dept_Name

E1 Aa Computer

E2 Bb It

E3 Cc Computer

E4 Dd Ec

E5 Ee It

E6 Ff Computer

E7 Gg Ec

e8` Hh it

Find out the number of employees working in each department

Select count(dept_name) as No. of employees, dept_name from employee group by dept_name

Page 5: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

HAVING CLAUSE Used to restrict the group It imposes a condition on group by clause which filters

groups

Find out students group having count more than 1select count(marks) as "no of students",marks from student group by marks having count(marks)>1;

5

04

/20

/23

Page 6: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

 SELECT name, category, winter FROM sales           WHERE category="Literary"     GROUP BY winter HAVING winter > 700

list the product names of literacy category  that have winter_sales value more than 700

6

04

/20

/23

Page 7: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

SUB QUERY (NESTED QUERY) A SQL statement that appear inside another SQL

statement Ex : - select … from ( select …from…..) Syntax :- Select column from table

Where condition (select column from table Where condition)

Inner query executed first that returns certain rows Next outer query is executed based on result of this

inner query

7

04

/20

/23

Page 8: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

SELECT category_name FROM categories WHERE category_id =

( SELECT MIN(category_id) from movies);

8

04

/20

/23

Page 9: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

Employee (fname, lname, salary, dept_id) Dept (dept_id, dept_name)

Find out employee detail working in finance department.

Select dept_id from dept where dept_name = ‘finance’

Select fname,lname from employee where dept_id =

Select fname,lname from employee

where dept_id =

(Select dept_id from dept

where dept_name = ‘finance’)9

04

/20

/23

Page 10: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

CREATING A TABLE USING SUB QUERY

10

Name Null? Type

Eid Not null Number(3)

Fname Not null Varchar2(20)

Lname Varchar2(20)

Salary Number(6)

04

/20

/23

Page 11: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

Select * from temp;

11

Eid fname lname salary

433 McCal Alex 66500

543 Dev Derek 80000

04

/20

/23

Page 12: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

INSERT USING SUB QUERY Existing table can be populated with sub query INSERT into TABLENAME

(SELECT columnnames FROM tablename WHERE condition)

Ex :- INSERT INTO temp(eid, lname, fname) SELECT eid, lname, fname from EMPLOYEE WHERE dept_id=10;

Temp

12

Eid fname lname salary

433 McCal Alex 66500

543 Dev Derek 80000

111 Smith John

123 Roberts Sandi

222 Chen Sunny

04

/20

/23

Page 13: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

UPDATE USING SUB QUERY Updates can be performed using sub-query Syntax :- UPDATE table-name SET column-name = value WHERE condition (SELECT SUBQUERY)

Ex:- update salary of employees using dept-id of finance department UPDATE employeeSET salary = salary *1.10WHERE dept_id = (SELECT dept_id FROM dept WHERE d_name =

‘Finance’)

13

04

/20

/23

Page 14: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

DELETE USING SUB-QUERY A row or rows from table can be deleted based on value

returned by subquery Syntax:- DELETE FROM tablename

WHERE condition (SELECT subquery)

Ex:- delete all employees in Accounting department using dept id.

DELETE FROM employee WHERE dept_id = (SELECT dept_id FROM department where dept_name = ‘Accounting’);

14

04

/20

/23

Page 15: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

MULTIPLE ROW SUB QUERY Multiple row sub query returns more than one row.

15

Operator Use

IN Equal to any value in a listEx:- identify items that are low in stock

ALL, ANY or SOME

Compare the given value to any value returned by sub queryUse =, <>, >, <, <= or >= operator is must

04

/20

/23

Page 16: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

IN operator looks for at least one match from the list of values provided

Find out student details handled by computer dept. faculty.

Student Faculty

select s_id, lastname, firstname, faculty_id from student

where faculty_id in

(select faculty_id from faculty where dept_name =‘computer’);

16

S_id Lastname Firstname Faculty_id

100 Diaz Jose 123

102 Patel Rajesh 111

S_id Firstname lastname Faculty_id

Faculty_id Firstname lastname Dept_name

04

/20

/23

Page 17: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

EX:- IN Identify items that are low (having quantity <20) in

stock Two Tables

Sales Order Items

Product

Select * From SalesOrderItems

Where ProductID IN

( Select ID From Products Where Quantity < 20 )17

S_id Order_id Company_name Cost Product_Id

ID Product Product_name Quantity

04

/20

/23

Page 18: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

ANY operator compare a value with any value in a list. Ex:- Find out the agent who worked for customer live in the

city ‘AHMEDABAD'. Agent

Customer

SELECT ID,agent_name,working_area,commissionFROM agentsWHERE ID = ANY (

SELECT agent_id FROM customer WHERE city=‘Ahmedabad'); 18

IDagent_name

working_areacommission

phone_no

Cust_ IDcust_name

agent_id city

04

/20

/23

Page 19: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

JOIN OPERATION0

4/2

0/2

31

9

Working with multiple table Manipulate data from all tables Tables are joined on columns having same data type

combination of operations selection, projection and Cartesian product

Cross Join Natural Join Inner Join Outer join

•Full outer join•Left outer join•Right outer join

Page 20: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

CROSS JOIN Combines every row from left table to every row in right

table Not preferred :- may run for long time,

produces a huge result set that may not be useful

CUSTOMERS

ORDERS

Select id, name, amount, date from customers CROSS JOIN orders;

04

/20

/23

20

Page 21: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

04

/20

/23

21

Page 22: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

NATURAL JOIN0

4/2

0/2

32

2 Most common type of join is a “natural join”

(often just called “join”). R S conceptually is: Compute R X S The associated tables have one or more pairs of

identically named columns Select rows where attributes that appear in both

relations have equal values

Select * FROM table1 NATURAL JOIN table2;

Page 23: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

NATURAL JOIN0

4/2

0/2

32

3 Select * from r1 NATURAL JOIN s1

R1S1

R1 S1 =

sid sname rating age bid day

22 dustin 7 45.0 101 10/10/9658 rusty 10 35.0 103 11/12/96

sid sname rating age

22 dustin 7 45.0

31 lubber 8 55.5 58 rusty 10 35.0

sid bid day

22 101 10/10/96 58 103 11/12/96

Page 24: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

INNER JOIN0

4/2

0/2

32

4 creates a new result table

by combining column values of two tables (table1 and table2)

based upon the join-predicate.

SELECT table1.column1, table2.column2...

FROM table1 INNER JOIN table2

ON condition;

Page 25: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

04

/20

/23

25

Select id, name, amount, date from customers inner join orders on customers.Id = orders.Customer_id;

Order

Customer

Page 26: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

04

/20

/23

26

Page 27: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

OUTER JOIN0

4/2

0/2

32

7 returns all rows from both the participating tables which

satisfy the join condition and Selects those tuples that donot satisfies the join

condition Left Outer Join On two relations R and S It selects matching tuples of both relations R and S Along with unmatched tuples from relation R Missing values in relation s is set to NULL.

R S

R S

Selected records with matching values

Records of R

Page 28: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

SELECT * FROM

table1 LEFT JOIN table2

ON table1.common_filed = table2.common_field;

Employee

Department

Select * from employee LEFT JOIN Department ON

employee.Dept_Name = Department.Dept_Name

04

/20

/23

28

Name EmpId Dept_Name

Dept_name Manager

Page 29: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

LEFT OUTER JOIN0

4/2

0/2

32

9

Employee

Name EmpIdDeptNam

e

Harry 3415 Finance

Sally 2241 Sales

George 3401 Finance

Harriet 2202 Sales

Tim 1123Executive

Dept

DeptName

Manager

Sales Harriet

Production

Charles

Name EmpId DeptName Manager

Harry 3415 Finance NULL

Sally 2241 Sales Harriet

George 3401 Finance NULL

Harriet 2202 Sales Harriet

Tim 1123 Executive NULL

Page 30: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

RIGHT OUTER JOIN0

4/2

0/2

33

0 JoinOn two relations R and S It selects matching tuples of both relations R and S Along with unmatched tuples from relation S Missing values in relation R is set to NULL R S

R S

Selected records with

matching values

Records of S

Page 31: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

SELECT * FROM

table1 RIGHT OUTER JOIN table2

ON table1.column_name=table2.column_name;

Employee

Department

Select * from employee RIGHT JOIN Department ON

employee.Dept_Name = Department.Dept_Name

04

/20

/23

31

Name EmpId Dept_Name

Dept_name Manager

Page 32: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

RIGHT OUTER JOIN0

4/2

0/2

33

2

Name EmpId DeptName Manager

Sally 2241 Sales Harriet

Harriet 2202 Sales Harriet

NULL NULL Production Charles

Employee

Name EmpIdDeptNam

e

Harry 3415 Finance

Sally 2241 Sales

George 3401 Finance

Harriet 2202 Sales

Tim 1123Executive

Dept

DeptName

Manager

Sales Harriet

Production

Charles

Page 33: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

FULL OUTER JOIN0

4/2

0/2

33

3 JoinOn two relations R and S It selects matching tuples of both relations R and S Along with unmatched tuples from relation R and

relation S R S

R S

Selected records with

matching values

Records of SRecords of R

Page 34: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

Select * FROM

table1 FULL OUTER JOIN table2

ON table1.column_name=table2.column_name;

Employee

Department

Select * from employee FULL OUTER JOIN Department ON

employee.Dept_Name = Department.Dept_Name

04

/20

/23

34

Name EmpId Dept_Name

Dept_name Manager

Page 35: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

04

/20

/23

35

Name EmpId DeptName Manager

Harry 3415 Finance NULL

Sally 2241 Sales Harriet

George 3401 Finance NULL

Harriet 2202 Sales Harriet

Tim 1123 Executive NULL

NULL NULL Production Charles

Employee

Name EmpIdDeptNam

e

Harry 3415 Finance

Sally 2241 Sales

George 3401 Finance

Harriet 2202 Sales

Tim 1123Executive

Dept

DeptName

Manager

Sales Harriet

Production

Charles

Page 36: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

SEMI JOIN0

4/2

0/2

33

6

Join on relation R and S contains tuples of R that participate in join operation of R and S

Only attributes of relation R is projected Advantage decreases the number of tuples to be handled to form join

Page 37: INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015

SEMI JOIN - EMP WITH PAY OVER THE PREDICATE EMP.TITLE = PAY.TITLE,

04

/20

/23

37