JOINS cis 407 Subqueries Correlated Subqueries Exists operator performance considerations Examples.

Slides:



Advertisements
Similar presentations
N.G.Acharya & D.K.Marathe college Chembur-E, Mumbai-71
Advertisements

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.
Said Salomon Unitrin Direct Insurance T-SQL Aggregate Functions Said Salomon.
Tools of the trade TSQL CIS 407. SQL Server Tools Books on line! Don’t use sql server authentication –Use windows authentication (safer) for developer.
Databases Lab 5 Further Select Statements. Functions in SQL There are many types of functions provided. The ones that are used most are: –Date and Time.
Tutorial 5 Multi-table queries. Tutorial 5 objectives Displaying Data from Multiple Tables –[ ]Write SELECT statements to access data from more than one.
Correlated Queries SELECT title FROM Movie AS Old WHERE year < ANY (SELECT year FROM Movie WHERE title = Old.title); Movie (title, year, director, length)
INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR.
Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.
Basic Commands Prepared By: RAHUL PATEL. Create Table Command CREATE TABLE CUSTOMER ( CustomerId int IDENTITY(1,1) PRIMARY KEY, CustomerNumber int NOT.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
SQL in Action Amit Bhawnani & Nimesh Shah. Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A.
Oracle FUNCTIONS. Comment ScreenShot (in 10g) General Example of null Foreign Key: create table deptcs( deptno NUMBER(4) primary key, hiredate DATE,
Basic SQL. Implementation Schemes: Once we have a set of relation schemes, we can translate them into implementation schemes. We use to express implementation.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
Chapter 5 Selected Single-Row Functions. Chapter Objectives  Use the UPPER, LOWER, and INITCAP functions to change the case of field values and character.
1 Joining Tables in Queries. 2 Objectives You will be able to Write SQL queries that use Join operations to retrieve information from multiple tables.
Final Exam Guide PHP NOTE: PHP CODE WILL BE BLUE, HTML IS BLACK EXAMPLE
1 Definition of a subquery Nested subqueries Correlated subqueries The ISNULL function Derived tables The EXISTS operator Mixing data types: CAST & CONVERT.
04 | Grouping and Aggregating Data Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
JOINS cis 407 Inner join Right and left outer join Full join Cross join.
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Objectives Explain Transact-SQL
In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.
Creating a dynamic search form with database paging Tony Rogerson SQL Server MVP Torver Computer Consultants.
SQL Server 2005 Implementation and Maintenance Chapter 3: Tables and Views.
Lecture 8 – SQL Joins – assemble new views from existing tables INNER JOIN’s The Cartesian Product Theta Joins and Equi-joins Self Joins Natural Join.
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.
1 MySQL and SQL. 2 Topics  Introducing Relational Databases  Terminology  Managing Databases MySQL and SQL.
Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.
Janet works on the Azure Stream Analytics team, focusing on the service and portal UX. She has been in the data space at Microsoft for 5 years, previously.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
College of Information Technology, Universiti Tenaga Nasional1 Lab 2: Single-row Functions CISB224 01A CCSB244 01A Semester I, 2008/2009.
Lab week 10 Aggregates and sub-queries And assignment details.
There’s a particular style to it… Rob Hatton
Create Stored Procedures and Functions Database Management Fundamentals LESSON 2.4.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
CFUNITED – The premier ColdFusion conference Beyond Basic SQL for CF Nate Nelson
Big Data Yuan Xue CS 292 Special topics on.
CS3220 Web and Internet Programming More SQL
Structured Query Language
In this session, you will learn to:
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
T-SQL: Simple Changes That Go a Long Way
Built-in Functions. Usage of Wildcards
© 2010, Mike Murach & Associates, Inc.
05 | Using Functions and Aggregating Data
Introduction to T-SQL Querying
20761B 10: Using Subqueries Module 10   Using Subqueries.
Data warehouse Design Using Oracle
Aggregations Various Aggregation Functions GROUP BY HAVING.
Chapter 4 Summary Query.
SQL Introduction Standard language for querying and manipulating data
Structured Query Language – The Fundamentals
COP 2700 – Data Structures - SQL
Contents Preface I Introduction Lesson Objectives I-2
Query Functions.
Advanced Joins IN ( ) Expression Subqueries with IN ( ) Expression
Building Queries using the Principle of Simplest Query (POSQ)
Joins and other advanced Queries
Lab 3: Single-row Functions
Database Management System
Trainer: Bach Ngoc Toan– TEDU Website:
Introduction to SQL Server and the Structure Query Language
T-SQL: Simple Changes That Go a Long Way
Grouping and Aggregating Data
Introduction to T-SQL Querying
Presentation transcript:

JOINS cis 407 Subqueries Correlated Subqueries Exists operator performance considerations Examples

Nested subquery Loose query syntax Select from where = (select from where )

Nested Subquery Select from where IN (select from where ) Find the product IDs for items sold on the first day Select DISTINCT o.orderDate, od.ProductID from orders o join [order details] od on o.orderID = od.orderid where orderDate = ‘7/4/1996’ – first order date in system –But bit of a kludge so…

Nested Subquery Select DISTINCT o.orderDate, od.ProductID from orders o join [order details] od on o.orderID = od.orderid where orderDate = (select min(orderdate) from orders)

Nested subquery w/ multiple values Use pubs – find all the stores that have discount records Select stor_id as ‘store id”, stor_name as “store name” from stores where stor_id in (select stor_id from discounts) Same as: select s.stor_id, sotr_name from stores s join discounts d on s.stor_id = d.stor_id

Not in and outerjoin Find all the stores that don’t have matching discount records Select s.stor_name from discounts d right outerjoin stores s on d.stor_id = s.stor_id where d.stor_id is null Select stor_id, stor_name from stores where stor_id not in (select stor_id from discounts where stor_id in not null)

Correlated subqueries PAY ATTENTION – Can be tricky Orderid & orderDate for first order in the system FOR EACH CUSTOMER Select o1.customerid, o.ordeid, o1.orderdate from orders o1 where o1.orderdate = (select min(o2.orderdate) from orders o2 where o2.customerid = o1.customerid)

Correlated subqueries Same query but want to add customer name Select cu.companyName, (select min(orderdate) from orders o where o.customerid = cu.customerid) as “order date” from customers cu

ISNULL function Isnull function accepts a variable or expression and tests it for a null value Orderid/orderDate for first order in the system FOR EACH CUSTOMER NAME Select cu.companyName, ISNULL(CAST ((select min(o.orderdate) from orders o where o.customerid = u.customerid) as varchar), ‘ never ordered’) from customers cu

Exists operator Customers that placed at least one order Select customerid, companyName from customers cu where exists (select orderid from orders o where o.customerid = cu.customerid) Select distinct cu.customerid, cu.companyname from customers cu join orders o on cu.customerid = o.customerid But top one does not require full row by row join

Customers who have not ordered anything Use northwind Select c.customerid, companyname from customers c left outerjoin orders o on c.customerid = o.customerid where o.customerid is null Select customerid, companyname from customers cu where not exists (select orderid from orders o where o.customerid = cu.customerid)

Other exist uses If exists (select * from sysobjects where id = object_id(N’[dbo.[shippers]’) and objectProperty(id,n’isusertable’) = 1) drop table [dbo].[shppers]

Query performance Page How things USUALLY work When in doubt – or even when your not! TEST!!!!!!!!

Math functions Pg 603 Note ceiling and floor

Metadata functions Functions about the database and DB objects Col_length, col_name, … Pg 608

Security Functions Pg 620 Has_DBaccess, is_member, is_sbvrolemember, suser_id, suser_name, suser_SID, user, user_id,

String Functions Pg 623 Ascii, char, charindex, differnce, left, len, lower, ltrim. Nchar, parindex, quotename, replace, replicate, reverse, right, rtrim, soundex, space, str, stuff, substring, nicode, upper

System functions Pg 628 App_name, case, cast and convert, coalesce, collationproperty, current_timestamp, current_user, datalength, formatmessage, getansinull, host_id, host_name, ident_current, ident_incr, ident_seed, identity, isdate, isnull, isnumeric, newid, nullif, parsename, permissions, rowcount_big, scope_property, session_user, sessionproperty, stats_date, system_user, user_name

Aggregate functions Pg 597 appendix B Avg, checksum, checksum=agg, count, count_big, grouping, max, min, stdev, stdevp, sum, var, varp

Date time functions Dateadd, Datediff, Datename, Datepart Day, Getdate, getUTCdate, month year See details pg 601 appendix B