interactive sql part ii prepared by: mitali sonar (assistant prof.) 1 10/17/2015
TRANSCRIPT
INTERACTIVE SQL PART II
Prepared By: Mitali Sonar (Assistant Prof.)
1
04
/20
/23
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
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
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
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
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
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
SELECT category_name FROM categories WHERE category_id =
( SELECT MIN(category_id) from movies);
8
04
/20
/23
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
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
Select * from temp;
11
Eid fname lname salary
433 McCal Alex 66500
543 Dev Derek 80000
04
/20
/23
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
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
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
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
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
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
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
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
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
04
/20
/23
21
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;
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
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;
04
/20
/23
25
Select id, name, amount, date from customers inner join orders on customers.Id = orders.Customer_id;
Order
Customer
04
/20
/23
26
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
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
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
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
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
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
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
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
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
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
SEMI JOIN - EMP WITH PAY OVER THE PREDICATE EMP.TITLE = PAY.TITLE,
04
/20
/23
37