SQL Subquery.

Slides:



Advertisements
Similar presentations
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Advertisements

Virtual training week 4 structured query language (SQL)
Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error)
DatabaseDatabase cs453 Lab8 1 Ins.Ebtesam AL-Etowi.
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]
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.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
18 Copyright © Oracle Corporation, All rights reserved. Advanced 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.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
Banner and the SQL Select Statement: Part Four (Multiple Connected Select Statements) Mark Holliday Department of Mathematics and Computer Science Western.
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 DB2 9 Fundamentals.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
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.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
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.
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.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
SQL-5 (Group By.. Having). Group By  Need: To apply the aggregate functions to subgroups of tuples in a relation, where the subgroups are based on some.
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
1 Multiple Table Queries. 2 Objectives  Retrieve data from more than one table by joining tables  Using IN and EXISTS to query multiple tables  Nested.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Structured Query Language Introduction. Basic Select SELECT lname, fname, phone FROM employees; Employees Table LNAMEFNAMEPHONE JonesMark SmithSara
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
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
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
IFS180 Intro. to Data Management Chapter 11 - Subqueries.
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Subqueries.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
1 Introduction to Database Systems, CS420 SQL JOIN, Group-by and Sub-query Clauses.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
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.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Using Subqueries to Solve Queries
Multiple-Column Subqueries
MySQL Subquery Source: Dev.MySql.com
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Chapter 1 Introduction.
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
ATS Application Programming: Java Programming
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Working with Tables: Join, Functions and Grouping
Using Subqueries to Solve Queries
Writing Correlated Subqueries
Chapter 1 Introduction.
HAVING,INDEX,COMMIT & ROLLBACK
Using Subqueries to Solve Queries
Chapter 1 Introduction.
Chapter 1 Introduction.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
Subqueries.
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
Presentation transcript:

SQL Subquery

What is Subquery? A Subquery or Inner query or a Nested query is a query within another SQL query (main query) Usually located in WHERE or HAVING clause A subquery is used to return data or set of data that will be used in the main query as a condition to further restrict the data to be retrieved Subqueries can be used with the following SQL statements along with the comparison operators like !=, <>, =, <, >, >=, <=, IN, NOT IN

MULTIPLE-ROW SUBQUERY What is Subquery? SCALAR SUBQUERY MULTIPLE-ROW SUBQUERY

MULTIPLE-COLUMN SUBQUERY What is Subquery? MULTIPLE-COLUMN SUBQUERY

Where do we start? FINAL RESULT R3 R2 R1 4 MAIN QUERY Subquery will be execute first, result from subquery will be passed to the main query for further process When reading or writing SQL subqueries, you should start from the bottom upwards, working out which data is to be passed to the next query up. R2 R1 SUBQUERIES

Questions Display employee number, first name, last name and salary for any employees whose salary is greater than average salary? Display department number and department name that do not involve in any projects. Display employee number, first name, last name, sex, education level, and department number who has similar sex and education level with employee number 000280

ROLLBACK VS COMMIT list command options [ENTER] TYPE THESE CODE list command options [ENTER] update command options using c off [ENTER] select empno, firstnme from employee where empno = 000280 [ENTER] update employee set firstnme = 'MALIK' where empno = 000280 [ENTER] commit rollback [ENTER]