Subqueries Steve Perry 1.

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Module 6: Working with Subqueries. Overview Introduction to Subqueries Using a Subquery as a Derived Table Using a Subquery as an Expression Using a Subquery.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
CSEN 5314 Quiz What type of join is needed when you wish to include rows that do not have matching values? A. Equi-joinB. Natural join C. Outer.
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.
Exercise SELECT authorID, lastName FROM authors AuthorID FirstName
1 Section 5 - Grouping Data u The GROUP BY clause allows the grouping of data u Aggregate functions are most often used with the GROUP BY clause u GROUP.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 8: Subqueries.
CHAPTER 7 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
SQL/lesson 2/Slide 1 of 45 Retrieving Result Sets Objectives In this lesson, you will learn to: * Use wildcards * Use the IS NULL and IS NOT NULL keywords.
Views: Limiting Access to Data A view is a named select statement that is stored in a database as an object. It allows you to view a subset of rows or.
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
SQL/Lesson 4/Slide 1 of 45 Using Subqueries and Managing Databases Objectives In this lesson, you will learn to: *Use subqueries * Use subqueries with.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
SQL 101 for Web Developers 14 November What is a database and why have one? Tables, relationships, normalization SQL – What SQL is and isn’t – CRUD:
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Week 10 Quiz 9 Answers Group 28 Christine Hallstrom Deena Phadnis.
Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Getting to Know SQL. © Jim Hope 2002 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement TRANSFORM.
Day 13, Slide 1 U:/msu/course/cse/103 CSE 103 Students: Review INNER and OUTER JOINs, Subqueries. Others: Please save your.
SQL by Example By convention SQL keywords are written in uppercase. SELECT * FROM Books –This query returns all rows in the Books table. –SQL statements.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
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
1 IT420: Database Management and Organization SQL part 3 7 February 2006 Adina Crăiniceanu
While you are waiting for class to start... (1)Login to SQL Server 2012 Management Studio (2) Execute the file called “SQLLab4.sql”. It is located on the.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.
Grouping Data Steve Perry
WEEK# 12 Haifa Abulaiha November 02,
U:/msu/course/cse/103 Day 12, Slide 1 CSE 103 Students: Read Day 18 and learn about subqueries Others: Please save your.
LINQ to DATABASE-2.  Creating the BooksDataContext  The code combines data from the three tables in the Books database and displays the relationships.
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.
1 CS 430 Database Theory Winter 2005 Lecture 13: SQL DML - Modifying Data.
1 Section 10 - Embedded SQL u Many computer languages allow you to embed SQL statements within the code (e.g. COBOL, PowerBuilder, C++, PL/SQL, etc.) u.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
1 Section 8 - Manipulating Data u The INSERT statement adds rows of data to the database u The UPDATE statement changes columns of existing data u The.
1 Section 3 - Select Statement u The Select statement allows you to... –Display Data –Specify Selection Criteria –Sort Data –Group Data for reporting –Use.
Dr. Chen, Oracle Database System (Oracle) 1 Basic Nested Queries and Views Jason C. H. Chen, Ph.D. Professor of MIS School of Business Gonzaga University.
Joining Tables Steve Perry
Using Subqueries to Solve Queries
CS122 Using Relational Databases and SQL
MySQL Subquery Source: Dev.MySql.com
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Advanced select statement Join Other DML commands
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
Subqueries Schedule: Timing Topic 25 minutes Lecture
LINQ to DATABASE-2.
Chapter 4: Intermediate SQL Joins
Basic Nested Queries and Views
SQL Tutorial.
LINQ to DATABASE-2.
CMPT 354: Database System I
Section 4 - Sorting/Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
Manipulating Data Lesson 3.
CSC 453 Database Systems Lecture
Presentation transcript:

Subqueries Steve Perry 1

Subqueries A Subquery nests a SELECT statement in the WHERE clause of another SELECT, INSERT, UPDATE, or DELETE statement A Subquery’s Outer SELECT operates on rows returned from the Inner SELECT The Inner and Outer SELECT statements may refer to the same table 2

Example - without a Subquery Find all the books that have the same price as Straight Talk About Computers SELECT price FROM book WHERE title = 'Straight Talk About Computers'; SELECT title, price FROM book WHERE price = 19.99; 3

Example - Using a Subquery SELECT title, price FROM book WHERE price IN (SELECT price FROM book WHERE title = 'Straight Talk About Computers'); 4

Joins vs. Subqueries Join –Advantages: Can return results from all tables –Disadvantages: May need two statements to get desired results Subquery –Advantages: Can compare aggregates to other values. Can get information in a single statement –Disadvantages: Can only return results from the Outer Select statement 5

Example - Two Ways SELECT name, lastname, firstname FROM publisher JOIN author USING (city); SELECT name FROM publisher WHERE city IN (SELECT city FROM author); 6

The IN operator In a simple SELECT statement you can avoid specifying many OR’s by using an IN list –Instead of… SELECT lastname, firstname, state FROM author WHERE state='TN' or state='KS' or state='MD' –Do … SELECT lastname, firstname, state FROM author WHERE state IN ('TN', 'KS', 'MD') 7

The IN operator with a Subquery Use the ‘IN’ operator when the results of the inner query will have 0, one, or more values Once the inner query returns results, the outer query will make use of them SELECT lastname, firstname, state FROM author WHERE state IN (SELECT state FROM author WHERE state != 'CA') 8

Conceptually... Inner Query- SELECT state FROM author WHERE state != 'CA' Outer Query- SELECT name FROM author WHERE state IN ('KS', 'TN', 'OR', 'MI', 'IN', 'MD', 'UT'); 9

Exercise List the author’s name who appear as the third author on a book. 10

Answer SELECT lastname, firstname FROM author WHERE ssn IN (SELECT ssn FROM bookauthor WHERE author_order = 3); 11

Using Aggregate Functions Aggregate Functions always return a single value for a set of rows SELECT title, ytd_sales * price, advance FROM book WHERE ytd_sales * price > (SELECT MAX(advance) FROM book); 12

Insert, Update, and Delete UPDATE book SET price = price * 2 WHERE pub_id IN (SELECT pub_id FROM publisher WHERE name = 'New Age Books'); 13

Last Slide 14