Database Systems CSE 303. Outline. Lecture 06: SQL. What is Sub-query? Sub-query in WHERE clause Subquery

Similar documents
Database Management Systems,

Objectives. After completing this lesson, you should be able to do the following:

SQL-Nested Queries & Aggregate functions. Lecture By Binu Jasim 02-Aug-2016

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

Subquery: There are basically three types of subqueries are:

What Are Group Functions? Reporting Aggregated Data Using the Group Functions. Objectives. Types of Group Functions

School of Computing, Engineering and Information Sciences University of Northumbria. Set Operations

Querying Data with Transact SQL

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

In This Lecture. More SQL Select. SQL SELECT Overview. Aliases. Example. Example. More SQL Select. Database Systems Lecture 8 Natasha Alechina

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

EXISTS NOT EXISTS WITH

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

Three types of sub queries are supported in SQL are Scalar, Row and Table sub queries.

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Querying Data with Transact-SQL

A subquery is a nested query inserted inside a large query Generally occurs with select, from, where Also known as inner query or inner select,

CS2 Current Technologies Lecture 3: SQL - Joins and Subqueries

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

Introduction to Oracle9i: SQL

CS 582 Database Management Systems II

CMP-3440 Database Systems

[AVNICF-MCSASQL2012]: NICF - Microsoft Certified Solutions Associate (MCSA): SQL Server 2012

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances

Aggregate Functions. Eng. Mohammed Alokshiya. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Dept. Database Lab (ECOM 4113)

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

In This Lecture. Yet More SQL SELECT ORDER BY. SQL SELECT Overview. ORDER BY Example. ORDER BY Example. Yet more SQL

CS3DB3/SE4DB3/SE6M03 TUTORIAL

COSC 304 Introduction to Database Systems SQL. Dr. Ramon Lawrence University of British Columbia Okanagan

Database Programming with SQL

Advanced SQL GROUP BY Clause and Aggregate Functions Pg 1

Lab # 6. Data Manipulation Language (DML)

Alkérdések II. Copyright 2004, Oracle. All rights reserved.

Interview Questions on DBMS and SQL [Compiled by M V Kamal, Associate Professor, CSE Dept]

SQL Recursion, Window Queries

CS352 Lecture - Introduction to SQL

SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName

Database Management and Tuning

Testing Masters Technologies

Querying Microsoft SQL Server 2014

Assignment 6: SQL III Solution

SQL queries II. Set operations and joins

Query Tuning. 1) How is this query executed? 2) How to make it run faster? Execution is too slow

QUERY OPTIMIZATION [CH 15]

Advanced SQL Processing Prepared by Destiny Corporation

Querying Data with Transact-SQL

SQL. A Short Note. November 14, where the part appears between [ and ] are optional. A (simple) description of components are given below:

CSE414 Midterm Exam Spring 2018

An Introduction to Structured Query Language

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

SQL:Union, Intersection, Difference. SQL: Natural Join

Chapter 19 Query Optimization

DUE: 9. Create a query that will return the average order total for all Global Fast Foods orders from January 1, 2002, to December 21, 2002.

Note that it works well to answer questions on computer instead of on the board.

An Introduction to Structured Query Language

An Introduction to Structured Query Language

Chapter 2: The Relational Algebra

The SQL data-definition language (DDL) allows defining :

Writing Queries Using Microsoft SQL Server 2008 Transact-SQL. Overview

Querying Data with Transact-SQL (761)

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Wentworth Institute of Technology COMP2670 Databases Spring 2016 Derbinsky. Physical Tuning. Lecture 12. Physical Tuning

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

CS Reading Packet: "Sub-selects, concatenating columns, and projecting literals"

Chapter 2: The Relational Algebra

Handout 9 CS-605 Spring 18 Page 1 of 8. Handout 9. SQL Select -- Multi Table Queries. Joins and Nested Subqueries.

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql

SQL Data Query Language

Section 3 Worksheet. Part 1. Movies and Directors

Querying Data with Transact-SQL

Based on the following Table(s), Write down the queries as indicated: 1. Write an SQL query to insert a new row in table Dept with values: 4, Prog, MO

COURSE OUTLINE MOC 20461: QUERYING MICROSOFT SQL SERVER 2014

CMPT 354: Database System I. Lecture 4. SQL Advanced

Missing Information. We ve assumed every tuple has a value for every attribute. But sometimes information is missing. Two common scenarios:

Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course:

Retrieving Data from Multiple Tables

Learning Alliance Corporation, Inc. For more info: go to

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries. Slides are reused by the approval of Jeffrey Ullman s

Teradata SQL Features Overview Version

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

MySQL DML Commands - Joins and subqueries

Querying Data with Transact-SQL (20761)

DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E)

An Introduction to Structured Query Language

An Introduction to Structured Query Language

Chapter 3: Introduction to SQL

SQL Recursion, Window Queries

Reporting Aggregated Data Using the Group Functions. Copyright 2004, Oracle. All rights reserved.

20461: Querying Microsoft SQL Server 2014 Databases

CSE 344 JANUARY 19 TH SUBQUERIES 2 AND RELATIONAL ALGEBRA

Intermediate SQL ( )

Assignment 6: SQL III

Course Outline. Querying Data with Transact-SQL Course 20761B: 5 days Instructor Led

Chapter 3: Introduction to SQL

Slicing and Dicing Data in CF and SQL: Part 1

NURSING_STAFF NNbr NName Grade

Querying Data with Transact-SQL

SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

Transcription:

Database Systems CSE 303 Lecture 06: SQL 2016 Subquery Outline What is a Subquery Subquery in WHERE clause >ALL, >ANY, >=ALL, <=ANY etc. Subquery in FROM clause 1 2 What is Sub-query? Sub-query in WHERE clause 2 1 Queries within query Query as a part of another query SELECT <attributes> FROM <one or more relations> WHERE <conditions> 3 4 1

Sub-query in WHERE clause Find the employee(s) with the highest paid salary First thought FROM WHERE salary = 75000 Sub-query in WHERE clause Find the employee(s) with the highest paid salary Suppose you are not allowed to use any aggregate function?? FROM WHERE salary = (SELECT MAX(salary) FROM ) 5 6 Sub-query in WHERE clause Find the employee with the highest paid salary > ALL >= ALL < ALL <= ALL > ANY >= ANY < ANY <= ANY FROM WHERE salary >= ALL (SELECT salary FROM ) Chain of sub queries in the WHERE clause Find the employee(s) with the highest paid salary FROM WHERE salary > ALL (SELECT salary FROM WHERE salary < ANY (SELECT salary FROM ) 7 ) 8 2

Chain of sub queries in the WHERE clause Find the highest paid employee(s) in each department, dept FROM WHERE (dept, salary) IN (SELECT dept, MAX(salary) FROM GROUP BY dept); Correlated sub queries in the WHERE clause Find the employees that earn above average SELECT * FROM WHERE salary > 60000 9 10 Correlated sub queries in the WHERE clause Find the employees that earn above average SELECT * FROM WHERE salary > (SELECT AVG(salary) FROM ) Correlated sub queries in the WHERE clause Find the employees that earn above average of the employees within respective department SELECT * FROM s WHERE salary > (SELECT AVG(salary) FROM t WHERE s.dept = t.dept); 11 12 3

EXISTS and NOT EXISTS Find all manufacturers that make both and Laptop SELECT <attributes> FROM <one or more relations> WHERE EXISTS (sub-query) 1001 A 1002 B Laptop 2001 A 2002 B Empty FALSE Not Empty TRUE 13 1003 C SELECT DISTINCT FROM WHERE EXISTS (SELECT * FROM Laptop WHERE. = Laptop.) 2003 D 14 A B Find all manufacturers that make s but not Laptops 1001 A 1002 B 1003 C SELECT DISTINCT FROM WHERE NOT EXISTS (SELECT * FROM Laptop WHERE. = Laptop.) Laptop 2001 A 2002 B 2003 D 15 C SELECT FROM WHERE speed = (SELECT MAX(speed) FROM ); Practice correlated sub queries Find the with maximum processing speed using and without using aggregate function SELECT FROM WHERE speed >= ALL (SELECT speed FROM ) MODEL SPEED RAM HD PRICE 1001 2.66 1024 250 2114 1002 2.1 512 250 995 1003 1.42 512 80 478 1004 2.8 1024 250 649 1005 3.2 512 250 630 1006 3.2 1024 320 1049 1007 2.2 1024 200 510 1008 2.2 2048 250 770 1009 2 1024 250 650 1010 2.8 2048 300 770 1011 1.86 2048 160 959 1012 2.8 1024 160 649 16 1013 3.06 512 80 529 4

Correlated sub queries in the FROM clause 17 Product Find manufacturers of at least two different computers ( s or laptops) with speeds of at least 2.80 Maker Model Type A 1001 pc A 1002 pc A 1003 pc A 2004 laptop A 2005 laptop A 2006 laptop B 1004 pc B 1005 pc B 1006 pc B 2007 laptop C 1007 pc. MODEL SPEED RAM HD PRICE 1001 2.66 1024 250 2114 1002 2.1 512 250 995 1003 1.42 512 80 478 1004 2.8 1024 250 649 1005 3.2 512 250 630.. MODEL SPEEDRAM HD SCREENPRICE 2001 2 2048 240 20.1 3673 2002 1.73 1024 80 17 949 2003 1.8 512 60 15.4 549 2004 2 512 60 13.3 1150....... Laptop Find manufacturer of at least two different s with speed of at least 2.0 Find manufacturer of at least two different Laptops with speed of at least 2.0 SELECT FROM Product, WHERE Product. =. AND speed >= 2.0 GROUP BY HAVING COUNT(.) >= 2 SELECT FROM Product, Laptop WHERE Product. = Laptop. AND speed >= 2.0 GROUP BY HAVING COUNT(Laptop.) >= 2 19 20 5

Find manufacturers of at least two different computers ( s or laptops) with speeds of at least 2.80 Think combined SELECT FROM Product, (SELECT, speed FROM UNION SELECT, speed FROM Laptop) T WHERE Product. = T. AND speed >= 2.0 GROUP BY HAVING COUNT(T.) >= 2 21 6