Professional Documents
Culture Documents
RDBMS Day 4
ER/CORP/CRS/DB07/003
Recap
Introduction to SQL
DDL Statements
Create Table
Alter Table
Drop Table
Truncate Table
Create Index
Merits & Demerits of Index
Relational Operators
DML Statements
Insert into
Delete from
Select
Update
Copyright 2008, Infosys Technologies Ltd.
Session Plan
Group By
Relational Algebra
Cartesian Product
Inner Join
Self Join
Outer Join
Independent Sub-queries
ER/CORP/CRS/DB07/003
SELECT
Cust_ID,
FROM
SUM(Amount_in_Dollars)
Customer_Loan
GROUP BY
Cust_ID;
SQL Group By
SQL Group BY
To retrieve Number of Employees in each Department
SELECT
Employee_Manager
GROUP BY Department
SQL Group By
Demo on Group By
more than one column
Copyright 2008, Infosys Technologies Ltd.
10
11
ER/CORP/CRS/DB07/003
UNION
Customer_
Loan
SELECT Cust_ID
FROM Customer_Loan;
13
Union (Contd)
14
Union All
SELECT Cust_ID FROM Customer_Fixed_Deposit
UNION ALL
SELECT Cust_ID FROM Customer_Loan;
15
Union - Restrictions
The SELECT statements must contain the same number of
columns
Data type
16
INTERSECT
SELECT Cust_ID
FROM Customer_Loan;
17
Customer_Loan
Minus
Get All the Customer who have not
taken loan
SELECT Cust_ID
FROM
Customer_Account_details
Customer_Accoynt_
Details
MINUS
SELECT Cust_Id
FROM Customer_loan;
18
Customer_Loan
19
Restriction
Restricts the rows that can be chosen from a relation using a
WHERE clause
Takes a horizontal subset of values from the original relation
Example:
SELECT * FROM employee WHERE salary > 10000;
20
Projection
Projection is projecting a set of attributes of a relation so that rows of
values corresponding to those columns will figure in the output
This takes a vertical subset of the relation
Example:
SELECT empid, name, salary FROM employee;
21
JOINS
Cartesian Product
Inner join
Equi join
Outer join
Left-outer join
Right-outer join
Self join
22
FROM Table1,Table2;
23
Inner Joins
Common type of join
An inner join between two (or more) tables is the Cartesian product
that satisfies the join condition in the WHERE clause
24
25
26
SELECT a.Cust_Id,b.Cust_First_Name,b.Cust_Last_Name
FROM Customer_loan a, Customer_Account_details b
WHERE a.cust_id = b.cust_id;
27
Outer join
Retrieve all rows that match the WHERE clause and also those that
have a NULL value in the column used for join.
28
29
Example of left-join
List all cities of Table1 if there is match in cities in Table2 & also unmatched
Cities from Table1
SELECT Table1.Emp_ID, Table1.City, Table2.Cust_ID, Table2.City
FROM Table1, Table2
WHERE Table1.City
= Table2.City (+);
30
31
32
as
Emp.Employee_Last_Name
as
Emp.Manager_Id
as
Manager ID,
Manager.Employee_Last_Name
as
FROM employee_Manager
Emp, employee_Manager
WHERE Emp.Manager_ID
Employee ID,
33
Manager
Manager.Employee_ID;
34
Independent Sub-Queries
ER/CORP/CRS/DB07/003
Independent sub-queries
Inner query is independent of outer query.
Inner query is executed first and the results are stored.
Outer query then runs on the stored results.
36
>
(SELECT amount_in_dollars
FROM Customer_Loan
WHERE Cust_ID = 104);
Customer Data
37
38
Customer Data
39
Customer Data
Copyright 2008, Infosys Technologies Ltd.
40
Customer Data
41
42
Summary
The result of a query can be grouped based on a grouping column
While checking for conditions after grouping by a column , Having is
used instead of where
Grouped queries help look at data category wise
When the query consists of more than one component, it is
implemented in the form of a nested query depending on the nature of
the query
Sub queries help split a problem involving different levels of data
Relational algebra operations like union, intersect, difference,
restriction, projection and join help us get different combinations of data
from more than one table
43
Thank You
The contents of this document are proprietary and confidential to Infosys Technologies Ltd. and may
not be disclosed in whole or in part at any time, to any third party without the prior written consent of
Infosys Technologies Ltd.
2008 Infosys Technologies Ltd. All rights reserved. Copyright in the whole and any part of this
document belongs to Infosys Technologies Ltd. This work may not be used, sold, transferred, adapted,
abridged, copied or reproduced in whole or in part, in any manner or form, or in any media, without the
prior written consent of Infosys Technologies Ltd.
44
ER/CORP/CRS/DB07/003
46
47
48
49
50
51
52
FROM employees
WHERE last_name LIKE 'B%';
53
Pseudocolumns
Behaves like a table column
Not stored in table
Cannot change value of pseudocolumn
54
Pseudocolumns
ROWID
ROWNUM
55
Examples
SELECT ROWID, ENAME FROM EMP;
SELECT ROWNUM, ENAME FROM EMP ORDER BY ename;
56