1 Definition of a subquery Nested subqueries Correlated subqueries The ISNULL function Derived tables The EXISTS operator Mixing data types: CAST & CONVERT.

Slides:



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

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.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
Query Manager. QM is a collection of tools you can use to obtain information from the AS/400 database Used to –select, arrange, and analyze information.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Introduction to SQL Session 2 Retrieving Data From Multiple Tables.
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)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Copyright © 2014 Pearson Education, Inc. 1 CHAPTER 7: ADVANCED SQL Essentials of Database Management Jeffrey A. Hoffer, Heikki Topi, V. Ramesh.
Introduction to Databases Chapter 7: Data Access and Manipulation.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Stored Procedures, Transactions, and Error-Handling
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.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 6 Additional Database Objects Oracle 10g: SQL.
1 Intro to JOINs SQL INNER JOIN SQL OUTER JOIN SQL FULL JOIN SQL CROSS JOIN Intro to VIEWs Simple VIEWs Considerations about VIEWs VIEWs as filters ALTER.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 6 Procedural Language SQL and Advanced SQL Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
Programming in R SQL in R. Running SQL in R In this session I will show you how to: Run basic SQL commands within R.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 12 Subqueries and Merge Statements
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
1 Intro stored procedures Declaring parameters Using in a sproc Intro to transactions Concurrency control & recovery States of transactions Desirable.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
There’s a particular style to it… Rob Hatton
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
MySQL Subquery Source: Dev.MySql.com
Top 50 SQL Interview Questions & Answers
Relational Database Design
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
Database Systems: Design, Implementation, and Management Tenth Edition
PROCEDURES, CONDITIONAL LOGIC, EXCEPTION HANDLING, TRIGGERS
Introduction to Oracle9i: SQL
David M. Kroenke and David J
Using the Set Operators
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Contents Preface I Introduction Lesson Objectives I-2
Relational Database Design
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
Improving the Performance of Functions
Presentation transcript:

1 Definition of a subquery Nested subqueries Correlated subqueries The ISNULL function Derived tables The EXISTS operator Mixing data types: CAST & CONVERT Performance considerations Writing scripts Retrieving identity values Batches Control-of-flow statements Steen Jensen, autumn 2013

 A subquery is a query nested inside another query  Generally used for one of a few needs: To break a query into a series of logical steps Tp provide a listing to be the target of a WHERE clause To provide a lookup driven by each individual record in a parent query  Most subqueries (but not all) can also be written as a join 2

 A nested subquery is a query, which only goes in one direction – returning either a single value for use in the outer query, or a full list of values to bused with the IN operator  See examples page 215 – 218bot 3

 A correlated subquery works in two directions  In a correlated subquery the inner query runs on information provided by the outer query, and vice versa  See examples page 218bot – 223bot 4

 The ISNULL function accepts a variable or expression and tests it for a NULL value  If the value is NULL, the function returns some other specified value  If the value is not NULL, the original value is returned  See examples page 223bot – 225bot 5

 A derived table is made up of the columns and rows of a result set from a query  To create a derived table, you must do two things: Enclose the query that generates the result set in parentheses Alias the results of the query, so the alias can be referenced as a table  See examples page 215bot – 227bot 6

7  Using common table expressions (CTEs): page 228top – 232top

 When you use EXISTS, you don’t return data but a simple TRUE/FALSE  See examples page 232 – 234bot 8

 Both CAST and CONVERT perform data type conversions  You would use CAST and CONVERT in situations, when SQL Server would not implicitly make the conversions for you  See examples page 236top – 239top 9

10

 Experiment running & changing the different subqueries at SQLZOO:

 A script isn’t a script until you store it in a file, where it can be pulled up and reused  Scripts are usually treated as a unit  Scripts can use both system functions and local variables  You can declare one variable (a scalar variable) at a time or several  The are three ways to set the value of a variable: Initialize it in the DECLARE statement Use a SELECT statement Use a SET statement  See examples page 384 – 388bot 12

13

 By using the system function called SCOPE_IDENTITY() the value of the last inserted key can be obtained  See examples page 391 – 394mid 14

15  Generating sequences: page 395 – 398bot  Running from the command prompt - SQLCMD: page 405top – 409bot  Dynamic SQL – using the EXEC command: page 409bot – 415bot

 By using the system function called the number of affected rows can be obtained  See examples page 398bot – 399mid 16

 A batch is a grouping of SQL statements into one logical unit  If a statement fails at parsetime, nothing runs  If a statement fails at runtime, all statements until the erroneous statement have already been run  To separate a script into multiple batches, you make use of the GO statement  The GO statement: Must be on a separate line Causes all statements prior to the GO statement to be sent independently to the server Is not a SQL statement  See examples page 400mid – 401top – 405top 17

 Batches are used, when something has to happen either before or separately from everything else in a script  The following commands require their own batch: CREATE DEFAULT CREATE PROCEDURE CREATE RULE CREATE TRIGGER CREATE VIEW  Batches can be used to establish precedence 18

 Control-of-flow statements makes it possible to use programming language constructs in SQL  The control-of-flow statements include: IF...ELSE GOTO WHILE WAITFOR TRY/CATCH  See examples page bot – 420top + 421bot – 423top – 426mid + 427mid + 429bot – 430mid 19

 Make ex.1 on page 432 (chapter 11) in the book without looking at the answer  Then check your answer up against the indicative answer on page