Suppose we need to get/retrieve the data from multiple columns which exists in multiple tables...then we use joins..

Similar documents
Objectives. After completing this lesson, you should be able to do the following:

Real-World Performance Training SQL Introduction

@vmahawar. Agenda Topics Quiz Useful Links

Oracle DB-Tuning Essentials

Databases IIB: DBMS-Implementation Exercise Sheet 13

CSIT5300: Advanced Database Systems

CS2 Current Technologies Lecture 3: SQL - Joins and Subqueries

Objectives. After completing this lesson, you should be able to do the following:

Interpreting Explain Plan Output. John Mullins

ORACLE TRAINING CURRICULUM. Relational Databases and Relational Database Management Systems

1 SQL Structured Query Language

1 SQL Structured Query Language

Oracle Database: Introduction to SQL Ed 2

DRAFT SOLUTION. Query 1(a) DATABASE MANAGEMENT SYSTEMS PRACTICE 1

Database Design and Tuning

Miguel Anjo (IT/ADC)

Introduction to Views

SQL Tuning via Toad Tips for Optimizing SQL Performance

Physical Database Design and Tuning. Chapter 20

ORACLE VIEWS ORACLE VIEWS. Techgoeasy.com

Databases. Relational Model, Algebra and operations. How do we model and manipulate complex data structures inside a computer system? Until

Querying Data with Transact SQL

Semantic Errors in Database Queries

Physical Database Design and Tuning. Review - Normal Forms. Review: Normal Forms. Introduction. Understanding the Workload. Creating an ISUD Chart

Querying Data with Transact-SQL

A Unit of SequelGate Innovative Technologies Pvt. Ltd. All Training Sessions are Completely Practical & Real-time

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Q.1 Short Questions Marks 1. New fields can be added to the created table by using command. a) ALTER b) SELECT c) CREATE. D. UPDATE.

QUERY OPTIMIZATION [CH 15]

Sample Question Paper

Visit for more.

Optimizing and Simplifying Complex SQL with Advanced Grouping. Presented by: Jared Still

COSC 304 Introduction to Database Systems SQL. Dr. Ramon Lawrence University of British Columbia Okanagan

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

Join Methods. Franck Pachot CERN

20761 Querying Data with Transact SQL

Oracle Database 11g: SQL and PL/SQL Fundamentals

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances

Oracle Database10g Release 2

Query Optimization Overview

Administrivia. Physical Database Design. Review: Optimization Strategies. Review: Query Optimization. Review: Database Design

Chapter 16: Advanced MySQL- Grouping Records and Joining Tables. Informatics Practices Class XII. By- Rajesh Kumar Mishra

Exploring Best Practices and Guidelines for Tuning SQL Statements

INFSCI 2711 Database Analysis and Design Example I for Final Exam: Solutions

Database Management Systems (COP 5725) Homework 3

CUBE, ROLLUP, AND MATERIALIZED VIEWS: MINING ORACLE GOLD John Jay King, King Training Resources

SQL. Char (30) can store ram, ramji007 or 80- b

Advanced SQL and the Power of Rewriting Queries

Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course:

RNDr. Michal Kopecký, Ph.D. Department of Software Engineering, Faculty of Mathematics and Physics, Charles University in Prague

Oracle Database: SQL and PL/SQL Fundamentals NEW

CPSC 421 Database Management Systems. Lecture 19: Physical Database Design Concurrency Control and Recovery

An Introduction to Structured Query Language

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

ITCertMaster. Safe, simple and fast. 100% Pass guarantee! IT Certification Guaranteed, The Easy Way!

Physical Design. Elena Baralis, Silvia Chiusano Politecnico di Torino. Phases of database design D B M G. Database Management Systems. Pag.

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Outer Join, More on SQL Constraints

PASSTCERT QUESTION & ANSWER

Oracle. SQL(Structured Query Language) Introduction of DBMS. Build In Function. Introduction of RDBMS. Grouping the Result of a Query

Sun Certified MySQL Associate

5 Integrity Constraints and Triggers

Based on the following Table(s), Write down the queries as indicated: 1. Write an SQL query to insert a new row in table Dept with values: 4, Prog, MO

Question 1 (a) 10 marks

Databases - 4. Other relational operations and DDL. How to write RA expressions for dummies

Advanced Oracle Performance Troubleshooting. Query Transformations Randolf Geist

T-SQL Training: T-SQL for SQL Server for Developers

To understand the concept of candidate and primary keys and their application in table creation.

More SQL: Complex Queries, Triggers, Views, and Schema Modification

Oracle Syllabus Course code-r10605 SQL

20461: Querying Microsoft SQL Server 2014 Databases

An Introduction to Structured Query Language

Oracle Hints. Using Optimizer Hints

Pivot Tables Motivation (1)

SQL Data Query Language

Introduction to Oracle9i: SQL

ORACLE 12C NEW FEATURE. A Resource Guide NOV 1, 2016 TECHGOEASY.COM

University of Massachusetts Amherst Department of Computer Science Prof. Yanlei Diao

Interview Questions on DBMS and SQL [Compiled by M V Kamal, Associate Professor, CSE Dept]

An Introduction to Structured Query Language

CS Reading Packet: "Writing relational operations using SQL"

Aster Data Basics Class Outline

An Introduction to Structured Query Language

Databases - 3. Null, Cartesian Product and Join. Null Null is a value that we use when. Something will never have a value

Querying Data with Transact-SQL (20761)

Subquery: There are basically three types of subqueries are:

Top 6 SQL Query Interview Questions and Answers

Databases - 3. Null, Cartesian Product and Join. Null Null is a value that we use when. Something will never have a value

Greenplum SQL Class Outline

An Introduction to Structured Query Language

Oracle 9i Application Development and Tuning

Introduction to Data Management. Lecture #17 (Physical DB Design!)

Slicing and Dicing Data in CF and SQL: Part 2

Oracle Database 12c SQL Fundamentals

Lecture #16 (Physical DB Design)

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

AVANTUS TRAINING PTE LTD

EDUVITZ TECHNOLOGIES

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Transcription:

JOINS: why we need to join?? Suppose we need to get/retrieve the data from multiple columns which exists in multiple tables...then we use joins.. what is the condition for doing joins??...yes at least we have one common column (or common format column) from the both tables Lets say EMP table has "deptno" column and DEPT has "deptno" column. So we can join both the tables. TYPES Better to use tables aliases/column aliases when doing joins else we get column ambiguty. Cross Joins Equi Join/ Simple Join/ Normal Join Non equi Joins Self Join Outer Joins Cross Joins: Suppose we have EMP(14) and DEPT(4) tables eg: select e.empno, e.ename, d.deptno, d.dname from emp e, dept d Output we get like 56(14*4) rows selected..as internally Cartesian join performed.. So we have to analyze that, is that result correct or not?? --we have only 14 employees but we got 56 employees existing in the result...so this is not the correct output. So what should we do... Here we need to mention the 'JOIN' condition to get the proper results.. select e.empno, e.ename, d.deptno, d.dname from emp e, dept d

where e.deptno=d.deptno Here we mentioned in 'where' clause that...matching deptno of both tables... Question: suppose table A has 5 rows and B has 4 rows.. Then what is the output for below query? sql>select * from A,B analyze output is correct or not..what's the reason TITBITS >select * from emp where depno=10 >select * from emp where 1=0 Here we get "no rows selected" as output. Cause 'where' condition is not satisfied. We should notice that..if 'where' condition is correct then only we get correct results else "no rows selected" >select * from emp where 1=1 >select 1 from emp here we get 14 ones(1) as 14 records are existing >select 'srinu' from emp >select * from emp where null=null here we get "no rows selected" as 'null is not equal to null'...but check below one >select * from emp where null<>null here also we get "no rows selected" as we don t know the proper value for the null null is not zero value. It just an empty space..

then what will be correct one?? >select * from emp where null is null this is correct one. We have data for both EMP table and DEPT like above. Now we want get the dept name for each employee. But departname(dname) existing in DEPT table and ename is existing in EMP table. So if we want to get, we want to JOIN the tables (EMP and DEPT) Natural Join or Equi Join

So here we need to check how oracle perform JOIN operation. Here we joined both EMP and DEPT table..and we can see how oracle performing join. Oracle taking deptno of both tables and comparing and print the MATCHING results We can also write the above query in ANSI syntax (recommended) Select e.empno empno, e.ename emp_name, e.sal emp_sal, e.deptno emp_deptno, d.deptno dept_deptno, d.dname dept_name, d.loc dept_loc From emp e JOIN dept d ON(e.deptno=d.deptno)

But the above result is not the Proper(perfect result) as we have DEPTNO 40 also in DEPT table So Oracle should display DEPT has DEPTNO 40. But no employee existing in that..then it is perfect result Here OUTER JOINS comes into the picture. 1.RIGHT OUTER JOIN ANSI example

Observe carefully the above result. As we got 15 rows So here RIGHT OUTER JOIN giving all the results of right side table[dept].( see we get 40 deptno and there is no employees existing ie getting null values to the corresponding row(deptno 40)) You can just check LEFT OUTER JOIN by placing LEFT in RIGHT place of above query. You need to get the point here..that.. OUTER JOINS Gives both matching and un matching results ( Here deptno 40(DEPT table) is the un matching record) If we want to display left side table total info then use LEFT OUTER JOIN.. and try the below query..observe the difference. select e.empno,nvl(e.ename,'no EMPLOYEE') ename,e.sal,e.deptno,d.deptno,d.dname,d.loc from emp e right outer join dept d on (e.deptno=d.deptno) Here NVL replacing NULL value by our given value('no EMPLOYEE') SELF JOINS By the name we can understand that, we are joining the same table

Suppose we have EMP table Then SELF JOIN like EMP e1 join EMP e2) Eg: Get the employee managers.. Here in EMP table we can observe that. Manager column is there. But they are only EMPNO of managers. Here by doing SELF JOIN we are displaying the manager names of the employees CONFUSED???? Here we get the clear picture... here 1 st table is EMP (E) table and joining with same ( EMP (M) ) table..see query

first one is EMP table and second one is EMP(mgr table lets say)--self join for empno 7369 manager is 7902 and if we observe matching arrow( matching to same table MGR) That 7902 ename is ford and he is analyst If we check our output.. The record for employee num 7369 details come like below like above self join done for all empnos.. But here also a mistake... whats that??

Yes there is no employee 7839 in our result( see we got only 13 results instead of 14 employees) So why 7839 is not comes into the picture...as 7839 is President and has no manager..(i marked in sky blue line you can check). So 7839 is the un matching record.. there is no matching in the other table(here in our case sama(emp) table as self join)... so what will be the perfect output then?? we have to get like.. 7839 is existing in EMP table but has no manager...as he is president.. So which comes again into our picture??? OUTER JOINS Here we have used LEFT OUTER JOIN why??? Cause we need to have all employees...(we dont miss the employee :) ). so we get all the empno of emp table(e).. FULL OUTER JOIN:

You can understand yourself what is that?? Just place FULL instead of LEFT in the above query... So by FOJ we get both matching and un matching results from both the tables What You Need to Write Efficient SQL Understand Access Paths Full Scans Full Scans and Multiblock Read Counts Full Scans and the High-Water Mark ROWID Access ROWIDs to Speed Searches ROWID Ranges Index Scans B*Tree Structure Index-Unique Scans Index-Range Scans Index-Skip Scan Index Full Scan Index Fast-Full Scan Index Joins Cluster Scans Understand Joins Nested Loops Natural Joins with Nested Loops Outer Joins with Nested Loops Hash Joins Hash Natural Joins Hash Outer Joins Sort-Merge Joins Cartesian Joins Anti-Joins Full Outer Joins Schema Matters (Physical) Really Know SQL The ROWNUM Pseudo Column How ROWNUM Works Tuning with ROWNUM Reduce Function Calls Join Queries Pagination with ROWNUM

Top-N Query Processing with ROWNUM ROWNUM Wrap-up Scalar Subqueries Remove an Outer Join Use Two Scalar Subqueries Use a Single Scalar Subquery Use an Object Type Aggregate from Multiple Tables Select from Different Tables Join Rows to a Set of Tables Perform Lookups Analytics Find a Specific Row in a Partition Top-N in a Group Transposing (Pivoting) Prior Row/Next Row Parallelization Bin the Extents Group the Data Split into ROWID Ranges Parallelizing Analytics Wrap-up