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.

Slides:



Advertisements
Similar presentations
© Abdou Illia MIS Spring 2014
Advertisements

Sometimes you need to use data from more than one table. In example1, the report displays data from two separate tables. Employee IDs exist in the EMPLOYEES.
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
View (virtual table). View A VIEW is a virtual table A view contains rows and columns, just like a real table. The fields in a view are fields from one.
Operators: SQL supports various arithmetic, logical, character, and relational r operators. Arithmetic Operators: Arithmetic operators in SQL will return.
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
Subqueries 11. Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve.
Set operators (UNION, UNION ALL, MINUS, INTERSECT) [SQL]
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.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
1 DDL – subquery Sen Zhang. 2 Objectives What is a subquery? Learn how to create nested SQL queries Read sample scripts and book for different kinds of.
Database Systems More SQL Database Design -- More SQL1.
SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
Banner and the SQL Select Statement: Part Four (Multiple Connected Select Statements) Mark Holliday Department of Mathematics and Computer Science Western.
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
ITBIS373 Database Development
Relational Model Concepts. The relational model represents the database as a collection of relations. Each relation resembles a table of values. A table.
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
Nested Queries (Sub-Queries). Objectives Learn how to run a query as a nested sub-query Condition on nested query Application of nested query Restriction.
SQL (DDL & DML Commands)
DATABASE TRANSACTION. Transaction It is a logical unit of work that must succeed or fail in its entirety. A transaction is an atomic operation which may.
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- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
I NTRODUCTION TO SQL II. T ABLE JOINS A simple SQL select statement is one that selects one or more columns from any single table There are two types.
1 JOIN SUBQUERY Structured Query Language (SQL) - Part III.
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Chapter 12 Subqueries and Merge Statements
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
DATA, INFORMATION AND KNOWLEDGE Data: Data can be anything such as a number, a person’s name, images, crops name, sounds and so on. It can be defined as.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
CSCI N311: Oracle Database Programming 4-1 Chapter 12: When One Query Depends Upon Another Correlated Subqueries. Subqueries in the where clause. Anti-Joins.
Use of Data Security Commands Security: Information is vital to success, but when damaged or in the wrong hands, it can threaten success. OREACLE provides.
Subqueries.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
VIEWS A view is like a window through which one can view or change information in the table. Although view does not contain any data of their own. They.
ORDER BY clause in SELECT command: Normally, the result of the query will not be in ordered format. If we want to get the result of the query in specific.
SQL advanced select using Oracle 1 Multiple Tables: Joins and Set Operations Subqueries: Nested Queries.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Lecture 2 Joins and sub-queries. 2 Topics zJoins ySimple; Outer zSub-queries yaliases zIN Operator zNULL values zSaving and running files.
Copyright © 2016 Pearson Education, Inc. CHAPTER 7: ADVANCED SQL (PART I) Modern Database Management 12 th Edition Jeff Hoffer, Ramesh Venkataraman, Heikki.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
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.
Using Subqueries to Solve Queries
Multiple-Column Subqueries
MySQL Subquery Source: Dev.MySql.com
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
Writing Correlated Subqueries
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Using Subqueries to Solve Queries
SQL Subquery.
Chapter 8 Advanced SQL.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
Manipulating Data Lesson 3.
Concept of grouping SELECT statement have:
Presentation transcript:

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 SELECT command containing a subquery are referred as parent statement. The row returned by the subquery are used by the parent statement. Use of Nested Query: Develop complex commands (query) Define the sets of rows to be inserted into the target table of INSERT or CREATE table command or CREATE VIEW Ex :for INSERT Insertion with subquery SQL> INSERT INTO S_EMP SELECT * FROM EMP WHERE dept = ‘Sales’ Ex : for CREATE table (To remove particular column, delete sex field from emp table)

1.SQL> CREATE table t_emp as ( SELECT eno,ename,dept,post,basic from emp); 2.SQL> DROP table emp; 3.SQL> RENAME t_emp to emp; To define one or more values to be assigned to existing rows in an UPDATE command. To provide values for conditions in WHERE, HAVING clauses of SELECT, UPDATE and DELETE command.

EnoEnameDeptpostbasic 111KumarSalesManager UmarPurchaseAccountant VinoSalesClerk RajMarketingManager MuniyaPurchaseManager OmnaSalesAsst.Manager12000

Using Sub query in SELECT command Ex:1 To find all employees who have the same post as Raj. ordinary way step 1: SQL> select post from emp where ename = ‘Raj’; output - Manager step 2: SQL> select * from emp where post = ‘Manager; output -111 kumar Sales Manager raj Marketing Manager Sub query way SQL> select * from emp where post = ( select post from emp where ename = ‘Raj’);

Subquery returning more than one column: Ex: To find all employees who have same post and basic as ‘Raj’ SQL> select * from emp where (post,basic) = (select post,basic from emp where ename = ‘Raj’; output: 444 Raj Marketing Manager Muniya PurchaseManager15000 Subquery returning more than one row: (Ex : ANY operator) Ex: To find all employees who earn more than any employee in Purchase department SQL> select * from emp where basic > ANY (select basic from emp where dept = ‘Purchase’; output:

(Ex : ALL operator) Ex: To find all employees who earn more than all employee in Purchase department SQL>select * from emp where basic > ALL (select basic from emp where dept = ‘Purchase’; output: Multiple Subqueries: The WHERE clause of a query may contain any combination of ordinary conditions and subqueries with the help of AND or OR operator. Ex : To list all employees with either the same department as ‘Kumar’ or a basic grater than or equal to ‘Raj’ select * from emp where dept = (select dept from emp where ename = ‘Kumar’) or basic >= (select basic from emp where ename = ‘RAj’); Outout:

Correlated Subquery: In the ordinary subquery ( previous examples), each subquery was executed once and the resulting value was used by the WHERE clause of the main query. We can also frame a subquery that can be executed repeatedly once for each candidate in each through the main query, Which is called correlated subquery. Ex: To find employees who earn more then the average salary in their own department. SQL> select * from emp X where basic > (select avg(sal) from emp where X.dept = dept); Output: 111 Kumar Sales Manager Muniya Purchase Manager Omna Sales Asst.Manager 12000

Joining Data from multiple tables Rows in one table may be joined to rows in another by common value in corresponding columns. The ability of the relational ‘join’ operator is an important feature of relational systems. A join makes it possible to select data from more than one table by means of a single statement. The joining of table is done in SQL by specifying the table to be joined in the FROM clause of the SELECT command using WHERE conditions. Basic Syntax: SELECT columns FROM table1, table2, …… WHERE ;

Ex: Consider two tables such as EMP and SALARY EnoEnameDeptpost 111KumarSalesManager 222UmarPurchaseAccountant 333VinoSalesClerk 444RajMarketingManager 555MuniyaPurchaseManager 666OmnaSalesAsst.Manager 888JeevaMarketingClerk

SALARY EnoBasicDAHRAMAPF

Ex: To list all employee’s personal and salary details from the table EMP and SALARY. SQL> SELECT x.eno,x.ename,x.dept,x.post,y.basic,y.da,y.hra,y.ma,y.pf FROM emp x, salary y WHERE x.eno = y.eno; OR SQL> SELECT emp.eno,emp.ename,emp.dept,emp.post,salary.basic, salary.da,salary.hra,salary.ma,salary.pf FROM emp, salary WHERE emp.eno = salary.eno; ENO ENAME DEPT POST BASIC DA HRA MA PF Kumar Sales Manager Umar Purchase Account Vino Sales Clerk Raj Marketing Manager Muniya Purchase Manager Omna Sales As.manager

Types of Join Three types, such as 1.Equijoin 2.Non-equijoin 3.Outer join Equijoin When we join two or more tables using the relational operator = (equal) in the join condition, then it is Equijoin. In joining tables, a join condition that specifies a relationship = ( equal) is called Equijoin. Ex: Above example, The type of join in the above example is termed as equijoin because the comparison operator in the join condition is = (equal) SQL> SELECT x.eno,x.ename,x.dept,x.post,y.basic,y.da,y.hra,y.ma,y.pf FROM emp x, salary y WHERE x.eno = y.eno;

Non – Equijoin When we join two or more tables using the relational operator except = (equal) in the join condition, then it is Non -Equijoin. In joining tables, a join condition that specifies a relationship other than = ( equal) is called Non - Equijoin. Ex: To find the salaries and jobs of employees who earn more than employee no 666 (Omna) SQL> SELECT x.eno,x.basic,x.da,x.hra,x.ma,x.pf FROM salary x, salary y WHERE x.basic > y.basic AND y.eno = 666; ENO BASIC DA HRA MA PF

Self – Join Note that in the above example, the same table has been joined to achieve the required result such type of join statement involving the joining of the same table is termed as self-join. Outer – Join Suppose one or more rows in one of the table does not satisfy the join condition, that rows ordinarily will not appear in the query’s result. If we still wish to display those rows (not satisfied rows), then use Outer-Join operator. ie (+)  a plus enclosed in parenthesis

Ex:To list all employee’s personal and salary details from the table EMP and SALARY SQL> SELECT x.eno,x.ename,x.dept,x.post,y.basic,y.da,y.hra,y.ma,y.pf FROM emp x, salary y WHERE x.eno = y.eno(+); ENO ENAME DEPT POST BASIC DA HRA MA PF Kumar Sales Manager Umar Purchase Account Vino Sales Clerk Raj Marketing Manager Muniya Purchase Manager Omna Sales As.manager Jeeva Marketing Clerk

SQL> SELECT x.eno,x.ename,x.dept,x.post,y.basic,y.da,y.hra,y.ma,y.pf FROM emp x, salary y WHERE x.eno(+) = y.eno; ENO ENAME DEPT POST BASIC DA HRA MA PF Kumar Sales Manager Umar Purchase Account Vino Sales Clerk Raj Marketing Manager Muniya Purchase Manager Omna Sales As.manager