Lecture 2 Joins and sub-queries. 2 Topics zJoins ySimple; Outer zSub-queries yaliases zIN Operator zNULL values zSaving and running files.

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 4 Aggregating Data Using Group Functions.
Advertisements

Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved.
12-1 Copyright  Oracle Corporation, All rights reserved. What Is a View? EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
Subqueries 11. Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve.
Copyright  Oracle Corporation, All rights reserved. 6 Subqueries.
6 6 Subqueries Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan (1999), published.
Multiple-Column Subqueries 12. Objectives After completing this lesson, you should be able to do the following: Write a multiple-column subquery Describe.
Copyright  Oracle Corporation, All rights reserved. 6 Writing Correlated Subqueries.
Writing Basic SQL statement 2 July July July Create By Pantharee Sawasdimongkol.
Session 3: SQL (B): Parts 3 & 4 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Chapter 1 Writing Basic SQL Statements Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina,
Copyright  Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
Displaying Data from Multiple Tables (Join). EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS.
SQL (DDL & DML Commands)
Subqueries.
Nested Queries (Sub Queries) A nested query is a form of a SELECT command that appears inside another SQL statement. It is also termed as subquery. The.
Subqueries.
2 Writing Basic SELECT Statements. 1-2 Copyright  Oracle Corporation, All rights reserved. Capabilities of SQL SELECT Statements Selection Projection.
Copyright  Oracle Corporation, All rights reserved. Writing Basic SQL Statements.
1 Information Retrieval and Use (IRU) CE An Introduction To SQL Part 1.
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)
An Introduction To SQL - Part 1 (Special thanks to Geoff Leese)
SQL.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
Copyright  Oracle Corporation, All rights reserved. 4 Accessing a Database Using JBCL.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
RELATSIOONILISED ANDMEBAASID(alg) SQLi VÕIMALUSED.
7 Multiple-Column Subqueries. 7-2 Objectives At the end of this lesson, you should be able to: Write a multiple-column subquery Describe and explain the.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
Copyright س Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
1 Writing Basic SQL Statements. 1-2 Objectives At the end of this lesson, you should be able to: List the capabilities of SQL SELECT statements Execute.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
1 Lecture 4 Data Integrity: Primary and Foreign Keys.
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
6 Subqueries. 6-2 Objectives At the end of this lesson, you should be able to: Describe the types of problems that subqueries can solve Define subqueries.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Grouping These slides are licensed under.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Collection Operators These slides are.
Agenda for Class - 03/04/2014 Answer questions about HW#5 and HW#6 Review query syntax. Discuss group functions and summary output with the GROUP BY statement.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
1-1 Copyright  Oracle Corporation, All rights reserved. Logging In to SQL*Plus From Windows environment:From Windows environment: From command line:From.
Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following: –List the capabilities of SQL SELECT statements.
Advanced SQL. SQL - Nulls Nulls are not equal to anything - Null is not even equal to Null where columna != ‘ABC’ --this will not return records where.
Copyright  Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
4 Displaying Data from Multiple Tables. 4-2 Objectives At the end of this lesson, you should be able to: Write SELECT statements to access data from more.
Copyright س Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
Multiple-Column Subqueries
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Aggregating Data Using Group Functions
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Aggregating Data Using Group Functions
Interacting with the Oracle Server
Writing Correlated Subqueries
جملة الاستعلام الأساسية
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Aggregating Data Using Group Functions
(SQL) Displaying Data from Multiple Tables
Subqueries Schedule: Timing Topic 25 minutes Lecture
Displaying Data from Multiple Tables
Subqueries Schedule: Timing Topic 25 minutes Lecture
Database Programming Using Oracle 11g
Presentation transcript:

Lecture 2 Joins and sub-queries

2 Topics zJoins ySimple; Outer zSub-queries yaliases zIN Operator zNULL values zSaving and running files

3 Joins zJoins are how to connect together the tables in a relational database zTo join tables we must have a column in each table that contains the same information zTwo main types: simple, outer.

4 Joins zIn our example tables (emp and dept), each contains a column: deptno (the name does not have to be the same). zThis column contains the dept number for the employee in the emp table and the dept table has departmental information

5

6

7 Joins zTo join the two tables to select, say, the employee name and the name of their department we use a join-condition in the WHERE clause: SELECTename, dname FROMemp, dept WHEREemp.deptno = dept.deptno;

8

9 Joins (example2) To find ALLEN’s location enter: SELECTename, loc FROMemp, dept WHEREename = ‘ALLEN’ ANDemp.deptno = dept.deptno;

10

11 Joins zIn general: SELECT columns FROM table1, table2, … WHERE join-condition; zThe join-condition must join all the tables: for two tables we need a single condition, for three we require two conditions etc

12 Multiple Table Joins zSELECT columns FROM tab1, tab2, tab3 WHEREjoin-condition1 AND join-condition2;

13 Simple Joins zIf a column appearing in SELECT has the same name in both tables, we MUST specify which one we require SELECTename, dname, dept.deptno FROMemp, dept WHEREemp.deptno = dept.deptno;

14 Outer joins zReturn rows from table which have no match in other table SELECT columns FROM table1, table2 WHEREjoin-condition1 = join-condition2 (+);

15 Outer joins zSELECT dept.deptno, dname, sum(sal) FROMemp, dept WHEREemp.deptno (+) = dept.deptno GROUP BYdept.deptno, dname ORDER BYdept.deptno zAppend outer join symbol to table without matching rows

16

17 Subqueries zThis is when one of the parts of WHERE clause is a query itself. zConsider the following question: list the name and salary of all employees who earn greater than the average salary? zWe need to determine what the average salary is before we can ask who earns more than it.

18 Sub-queries zTo determine the average salary: SELECT AVG(sal) FROM emp; now who earns more than this SELECT ename, sal FROM emp WHERE sal > (SELECT AVG(sal) FROM emp);

19

20 Sub-queries zThe inner query is executed just once, i.e. the average salary is found and the outer query determines who earns more than the average salary. zQueries can be composed where the sub- query is executed once for each row in the outer query

21 Sub-queries zList employees, department number and salary for those employees who earn more than their departmental average salary. SELECT ename, deptno, sal FROM emp WHERE sal > (average salary of candidate employee’s department); zYou also need a subquery that calculates the average salary of each candidate employee’s department

22 SELECT AVG(SAL) FROMemp WHERE deptno = (candidate row’s value of DEPTNO) zAs the main query considers each candidate row, it must invoke the subquery and ‘tell’ it the employee’s dept number. zThe subquery must then compute the average salary for that employee’s dept. zThe main query must then compare the employee’s salary to the department’s average salary.

23 Subqueries zHow do we tie the department no in the inner query to the department no in the outer to get that individuals department’s average salary? zThe trick is to alias the name of the table emp in the outer query

24 Sub-queries zSELECT ename, deptno, sal FROM emp aliasemp WHERE sal > (SELECT AVG(sal) FROM emp WHERE aliasemp.deptno = deptno);

25

26 More aliasing zExample: for each manager list their staff SELECT manager.ename, worker.ename FROM emp manager, emp worker WHERE worker.mgr = manager.empno; zManager here does not mean job = ‘MANAGER’

27

28 IN Operator zMatches any one in list. zExample: list average salary of only CLERKs and ANALYSTs SELECT AVG(sal), job FROM emp WHERE job IN (‘CLERK’, ‘ANALYST’) GROUP BY job ORDER BY job

29

30 NULL values zCare must exercise when performing calculations that involve NULL (empty) values. A useful function is NVL(column, 0) which converts NULL values to 0 for the calculation.

31 NULL values zWe can use NULL in SQL commands: SELECT * FROM emp WHERE comm IS NOT NULL; INSERT INTO emp VALUES (7256, ‘GILES’, ‘CLERK’, 7788, ‘15-AUG-80’, 1100, NULL, 20);

32 SQLplus zCommand files Instead of typing commands directly in Oracle, they can be placed in a file (using Notepad).

33 SQLplus zThe sequence of commands in a file can be run with the command: START zThis means that complicated sequences of commands (like reports) can be written outside of SQL*Plus and then run

34 SQLplus zWe can save the current SQL command to a file with: SAVE zTo load a file but not run it: GET

35 Summary zJoins ySimple; Outer zSub-queries yaliases zIN Operator zNULL values zSaving and running files