+ All Categories
Home > Technology > Structured Query Language

Structured Query Language

Date post: 05-Dec-2014
Category:
Upload: anish-kumar
View: 524 times
Download: 6 times
Share this document with a friend
Description:
About JOINS topic in structured query language and its types are discused in this presentation.
Popular Tags:
27
Structured Query Language Submitted by- Abdul Kalam Ashad.A (13MX01) Anish Kumar.B (13MX04) Mohan Raj.T (13MX30) Sapthagirinathan.D (13MX39) Sudharsan.S (13MX47) Team-7
Transcript
Page 1: Structured Query Language

Structured Query Language

Submitted by-Abdul Kalam Ashad.A (13MX01)Anish Kumar.B (13MX04)Mohan Raj.T (13MX30)Sapthagirinathan.D (13MX39)Sudharsan.S (13MX47)

Team-7

Page 2: Structured Query Language

A join is an SQL query that combines records from two or more tables. Oracle performs join whenever multiple tables appear in the FROM clause.

Joins

General form of traditional join:

SELECT TABLE1.COLUMN,TABLE2.COLUMN FROM TABLE1,TABLE2WHERE [(TABLE1.COLUMN_NAME=TABLE2.COLUMN_NAME)]|WHERE [(TABLE1.COLUMN_NAME(+)=TABLE2.COLUMN_NAME)]|WHERE [(TABLE1.COLUMN_NAME)=TABLE2.COLUMN_NAME)(+)];

Page 3: Structured Query Language

Types of JoinTheta Join Inner Join• Equality Joins-Join Methods

Outer Join• Left Outer Join• Right Outer Join• Full Outer Join

Cross JoinSelf JoinNon-Equi Joins

Page 4: Structured Query Language

Customers table(customer_no, lastname, firstname, address, city, state, zip, referred)

Orders table(order_no, customer_no, orderdate, shipdate, shipstreet, shipcity, shipstate, shipzip)

Books table(isbn, title, pubdate, pubid, cost, retail, category)

Promotion table(gift, minretail, maxretail)

Publisher table(pubid, name, email, phone)

Schema:

Page 5: Structured Query Language

Customer# lastname firstname address city state zip referred

1001 Morales Bonita P.O. box 651 Eastpoint fl 32328

1002 Thompson Ryan P.O. box 9835 Santa monica ca 90404

1003 Smith Leila P.O . Box 66 Tallahassee fl 32306

1004 Pierson Thomas 69821 south avenue Boise id 83707

1005 Girard Cindy P.O. 851 Seattle wa 98115

1006 Cruz Meshia 82 dirt road Albany ny 12211

1007 Giana Tammy 9153 main road Austin tx 78710 1003

1008 Jones Kenneth P.O. box 137 Cheyenne wy 82003

1009 Perez Jorge P.O. box 8564 Burbank ca 91510 1003

1010 Lucas Jake 114 east savannah Atlanta ga 30314

Customers table:

Page 6: Structured Query Language

Customer# lastname firstname address city state zip referred

1011 Mcgovern Reese P.O. box 18 Chicago il 60606

1012 Mckenzie William P.O. box 971 Boston ma 02110

1013 Nguyen Nicholas 357 white eagle ave. Clermont fl 34711 1006

1014 Lee Jasmine P.O. box 2947 Cody wy 82414

1015 Schell Steve P.O. box 677 Miami fl 33111

1016 Daum Michell 9851231 long road Burbank ca 91508 1010

1017 Nelson Becca P.O. box 563 Kalmazoo mi 49006

1018 Montiasa Greg 1008 grand avenue Macon ga 31206

1019 Smith Jennifer P.O. box 1151 Morristown nj 07962 1003

1020 Falah Kenneth P.O. box 335 Trenton nj 08607

Cont……..

Page 7: Structured Query Language

Books table:Isbn Title Pubdate Pubid Cost Retail Category

1059831198 Body build in 10 minutes a day 21-jan-01 4 18.75 30.95 Fitness

0401140733 Revenge of mickey 14-dec-01 1 14.2 22 Family life

4981341710 Building a car with toothpicks 18-mar-02 2 37.8 59.95 Children

8843172113 Database implementation 04-jun-99 3 31.4 55.95 Computer

3437212490 Cooking with mushrooms 28-feb-00 4 12.5 19.95 Cooking

3957136468 Holy grail of oracle 31-dec-01 3 47.25 75.95 Computer

1915762492 Handcranked computers 21-jan-01 3 21.8 25 Computer

Page 8: Structured Query Language

Isbn Title Pubdate Pubid Cost Retail Category

9959789321 E-business the easy way 01-mar-02 2 37.9 54.5 Computer

2491748320 Painless child-rearing 17-jul-00 5 48 89.95 Family life

0299282519 The wok way to cook 11-sep-00 4 19 28.75 Cooking

8117949391 Big bear and little dove 08-nov-01 5 5.32 8.95 Children

0132149871 How to get faster pizza 11-nov-02 4 17.85 29.95 Self help

9247381001 How to manage the manager 09-may-99 1 15.4 31.95 Business

2147428890 Shortest poems 01-may-01 5 21.85 39.95 Literature

Cont…….

Page 9: Structured Query Language

Orders table:Order# Customer# Orderdate Shipdate Shipstreet Shipcity Shipstate Shipzip

1000 1005 31-mar-03 02-apr-03 1201 orange ave Seattle Wa 98114

1001 1010 31-mar-03 01-apr-03 114 east savannah Atlanta Ga 30314

1002 1011 31-mar-03 01-apr-03 58 tila circle Chicago il 60605

1003 1001 01-apr-03 01-apr-03 958 magnolia lane Eastpoint Fl 32328

1004 1020 01-apr-03 05-apr-03 561 roundabout way Trenton Nj 08601

1005 1018 01-apr-03 02-apr-03 1008 grand avenue Macon Ga 31206

1006 1003 01-apr-03 02-apr-03 558a capitol hwy Tallahassee Fl 32307

1007 1007 02-apr-03 04-apr-03 9153 main street Austin Tx 78710

1008 1004 02-apr-03 03-apr-03 69821 south avenue Boise Id 83707

Page 10: Structured Query Language

Order# Customer# Orderdate Shipdate Shipstreet Shipcity Shipstate Shipzip

1009 1005 03-apr-03 05-apr-03 9 lightening rd Seattle Wa 98110

1010 1019 03-apr-03 04-apr-03 384 wrongway home Morristown Nj 07960

1011 1010 03-apr-03 05-apr-03 102 west lafayette Atlanta Ga 30311

1012 1017 03-apr-03 1295 windy avenue Kalmazoo Mi 49002

1013 1014 03-apr-03 04-apr-03 7618 mountain rd Cody Wy 82414

1014 1007 04-apr-03 05-apr-03 9153 main street Austin Tx 78710

1015 1020 04-apr-03 557 glitter st. Trenton Nj 08606

1016 1003 04-apr-03 9901 seminole way Tallahassee Fl 32307

1017 1015 04-apr-03 05-apr-03 887 hot asphalt st Miami Fl 33112

1018 1001 05-apr-03 95812 highway98 Eastpoint Fl 32328

1019 1018 05-apr-03 1008 grand avenue Macon Ga 31206

1020 1008 05-apr-03 195 jamison lane Cheyenne Wy 82003

Cont…..

Page 11: Structured Query Language

Promotion table:

Gift Minretail Maxretail

Bookmarker 0 12

Booklabels 12.01 25

Bookcover 25.01 56

Freeshipping 56.01 999.99

Page 12: Structured Query Language

Publisher table:

Pubid Name Email Phone

1 Messi [email protected] 9043490984

2 Ronaldo [email protected] 9790345579

3 Pele [email protected] 9677441238

4 Rooney [email protected] 7418889919

5 Beckham [email protected] 9876543210

Page 13: Structured Query Language

Inner Join It is also known as Equi Joins or Simple Joins, represents default join-type. In Inner Join, the Where condition compares two columns from two tables

using equality operator =. It combines the rows that have equivalent values for specified columns. It requires each record in both the joined tables to have a matching record.

Example

Select title, name from books, publisher where books.pubid=publisher.pubid;

The theta join between two relations R and S defines a relation contains tuples(rows) satisfying a predicate from the cartesian product of R and S and is defined by R.a ϴ S.b, where ‘ϴ’ may be one of the comparison operators(=,<>,<,<=,>,>=).

Theta Join

Page 14: Structured Query Language

Equality Join

3 Approaches

The NATURAL JOIN key words will automatically create a join between two tables that have commonly defined field.

Joins Based on a column that has the same name and definition in both tables can be created with the USING clause.

When the tables join do not have commonly defined field in that case adds a WHERE clause(with traditional join) or add the ON clause to the join keyword to specify how the tables are related.

Page 15: Structured Query Language

General Form

SELECT table1.column,table2.column FROM table1 [NATURAL JOIN table2]| [JOIN table2 USING (column_name)]|[JOIN table2 ON (table1.column_name=table2.column_name)]|[LEFT|RIGHT|FULL OUTER JOIN table2 ON (table1.column_name=table2.column_name)]|[CROSS JOIN table2];

select title, pubid, name from publisher natural join books; select title, pubid, name from publisher join books using(pubid); select title, pubid, name from publisher join books on publisher.pubid=books.pubid;

Example

Page 16: Structured Query Language

Outer Join An Inner Join, returns all the rows that are returned by an Inner Join.It also

returns all those rows of one table that do not match with rows of other table. It does not require each record in the two joined tables to have a matching

record. Plus [+] operator is used to indicate outer join.

Example

Select last_name, first_name,order_no from customers c, orders o where c.customer_no=o.customer(+);

Page 17: Structured Query Language

Outer joins can be

Left Outer Join Right Outer Join Full Outer Join

Page 18: Structured Query Language

Left Outer Join

A left outer join returns all the values from the left table, plus matching values from right table or NULL in case of no matching join predicate.

Example:

Select last_name, order_no from customers c left outer join orders o on c.customer_no=o.customer_no order by c.customer_no;

Select last_name, order_no from customers c,orders o where c.customer_no=o.customer_no(+) order by c.customer_no;

Page 19: Structured Query Language

Right Outer Join

A right outer join returns all the values from the right table and matched values from left table or NULL in case of no matching join predicate.

EXAMPLE

Select last_name, order_no from customers c right outer join orders o on c.customer_no=o.customer_no order by c.customer_no;

Select last_name, order_no from customers c,orders o where c.customer_no(+)=o.customer_no order by c.customer_no;

Page 20: Structured Query Language

Full Outer Join

A full outer join combines the results of both left and right outer joins. The joined table contains all records from both tables with Nulls for missing matches on either side.

EXAMPLE

Select last_name, order_no from customers c full outer join orders o on c.customer_no=o.customer_no order by c.customer_no;

Select last_name, order_no from customers c,orders o where c.customer_no(+)=o.customer_no(+) order by c.customer_no;

Page 21: Structured Query Language

Cross Join A cross join always returns a Cartesian product. It combines every row

from the left table with every row in the right table. It selects all possible combinations of rows and columns from both tables.

Cartesian ProductThe condition in a join plays an important role. If the condition is not

specified, the query returns all rows from one table with all the rows from the other table. This result set is known as a Cartesian product.

Cross Join-Traditional Methodselect title,name from books,publishers;

Cross Join-Join Methodselect title,name from books cross join publishers;

Example

Page 22: Structured Query Language

Self Join

A table can be joined to it self, creating a self join. Joining a table to it self is useful when values from one column need to be compared with other values in the same column, same table.

Display the name of customer who referred another customer:

Example:-

Select r.first_name, r.last_name, c.last_name, c.referred from customers c, customers r where c.referred=r.customer_no;

Page 23: Structured Query Language

Non-Equi Joins

A Non-Equi join uses an operator other than the equality operator [=] in the join. Non-Equi joins are rarely used.

These operators are:

Not-equal [<>]Less than [<]Greater than [>]Less than or equal to [<=]Greater than or equal to [>=]LIKE INBETWEEN

Page 24: Structured Query Language

Syntax for Non-Equi join

SELECT table1.column, table2.column FROM table1 [JOIN table2 ON (table1.column_name<table2.column_name)]| [JOIN table2 ON (table1.column_name>table2.column_name)]| [JOIN table2 ON (table1.column_name<=table2.column_name)]| [JOIN table2 ON (table1.column_name>=table2.column_name)]| [JOIN table2 ON (table1.column BETWEEN table2.coll AND table2.col2)];

Example: Select last_name,first_name,order_no from customers c join orders o on

c.customer_no<o.customer_no; Select last_name,first_name,order_no from customers c join orders o on

c.customer_no<=o.customer_no;

Page 25: Structured Query Language

Select title, gift from books, promotion where retail between minretail and maxretail;

Select title, gift from books join promotion on retail between minretail and maxretail;

Select last_name,first_name,order_no from customers c join orders o on c.customer_no>o.customer_no;

Select last_name,first_name,order_no from customers c join orders o on c.customer_no>=o.customer_no;

Page 26: Structured Query Language

ANY QUERY ?

Page 27: Structured Query Language

THANK YOU


Recommended