Microsoft Enterprise Consortium Type I Subquery

Slides:



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

© Abdou Illia MIS Spring 2014
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals Physical.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Self-joins,
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Create Student-Team.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Aggregate.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals The Data.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals Data.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Evaluating.
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.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL Greenhouse Database.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL Type II (Correlated)
Microsoft Access 2010 Chapter 7 Using SQL.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Introduction.
Advanced Query Formulation with SQL Week 10 Quiz Jaymond Huynh Tim Nguyen.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Using two.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals SQL Server.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Component 4/Unit 6f Topic VI: Create simple querying statements for the database The SELECT statement Clauses Functions Joins Subqueries Data manipulation.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals Test.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals SELECT … FROM.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL Nested aggregate.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Aggregating.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals SELECT … FROM.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals Physical.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Sorting output.
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.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Logical operators.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL In-Line Subquery.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL Equi-Join One-sided.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Set Operators:
Component 4: Introduction to Information and Computer Science Unit 6: Databases and SQL Lecture 6 This material was developed by Oregon Health & Science.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals WHERE clause.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL The relational.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals Data.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Chapter 12 Introducing Databases. Objectives What a database is and which databases are typically used with ASP.NET pages What SQL is, how it looks, and.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
Using Subqueries to Solve Queries
Multiple-Column Subqueries
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
LESSON Database Administration Fundamentals Inserting Data.
20761A 10: Using Subqueries Module 10   Using Subqueries.
Microsoft Access 2003 Illustrated Complete
Using Subqueries to Solve Queries
David M. Kroenke and David J
20761B 12: Using Set Operators Module 12   Using Set Operators.
20761B 10: Using Subqueries Module 10   Using Subqueries.
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Database Fundamentals
Access: SQL Participation Project
Structured Query Language
SQL Fundamentals in Three Hours
Using Subqueries to Solve Queries
Access/SQL Server Eliminate Duplicates with SELECT DISTINCT
M1G Introduction to Database Development
Contents Preface I Introduction Lesson Objectives I-2
Subqueries Schedule: Timing Topic 25 minutes Lecture
Lesson Objectives Aims You should know about: 1.3.2: (a) indexing (d) SQL – Interpret and Modify (e) Referential integrity (f) Transaction processing,
Using Subqueries to Solve Queries
Topic 12 Lesson 2 – Retrieving Data with Queries
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
Shelly Cashman: Microsoft Access 2016
New Perspectives on Microsoft
Presentation transcript:

Microsoft Enterprise Consortium Type I Subquery Advanced SQL Type I Subquery Microsoft Enterprise Consortium: http://enterprise.waltoncollege.uark.edu Microsoft Faculty Connection/Faculty Resource Center http://www.facultyresourcecenter.com J Kreie, New Mexico State University

Microsoft Enterprise Consortium What you’ll need … Log in to MEC for this lesson and into MSSMS (Microsoft SQL Server Management Studio). Be sure to select your account ID under Database in the Object Explorer pane, similar to the example shown here. You should know the SQL covered in the SQL Fundamental series. J Kreie, New Mexico State University

Microsoft Enterprise Consortium Type I Subquery A subquery typically appears in the WHERE clause or the HAVING clause but may also be in the FROM clause. The Type I subquery is executed one time— before the outer query—and the output is a termporary data set used by the outer query. Let’s take a look at a simple example … J Kreie, New Mexico State University

Type I Subquery - Example Microsoft Enterprise Consortium Type I Subquery - Example List the students working on the auto shop database project. /* S-T: List students working on the auto shop database project. Subquery: Find the team ID for the team working on the auto shop project. Outer query: List students assigned to that team ID. */ /* Subquery */ select teamID from teams where project like '%auto shop%‘ /* Outer query lists students */ select stdid, stdfname, stdlname, std_teamID from students where std_teamID IN (select teamID where project like '%auto shop%'; J Kreie, New Mexico State University

Type I Subquery – Another Example Microsoft Enterprise Consortium Type I Subquery – Another Example List the herb crop/varieties planted in sector B of the South Seed zone. /* Greenhouse: Show crops of type herb planted in sector B of the South Seed zone. Subquery: List bay-beds for zone South Seed, sector B. Outer query: List the crop_type, bay_bed, crop, and variety for the herb crop type. */ /* Subquery */ Select bay_bed From tblBay_Bed Where zone = 'South Seed‘ and sector = 'B'; /* Outer query */ select crop_type, bay_bed, tblcrop.crop, variety from tblCropPlanting, tblCropVariety, tblCrop where tblCrop.crop = tblCropVariety.crop and tblCropVariety.cropVarID = tblCropPlanting.cropVarID and crop_type = 'Herb' and bay_bed IN (Select bay_bed Where zone = 'South Seed‘ and sector = 'B'); J Kreie, New Mexico State University

Type I Subquery – versus Distinct Microsoft Enterprise Consortium Type I Subquery – versus Distinct /* Subquery versus DISTINCT Show students who did evaluations about their teammates. Show the student's ID and name. */ select stdid, stdfname, stdlname from students, evaluations where students.stdid = evaluations.evaluatorID; /* Remove duplicates */ select distinct stdid, stdfname, stdlname /* Try this with a Type I subquery. Subquery: List the evaluator IDs. Outquery: List the students that have an ID in the subquery list. */ from students where stdid IN (select evaluatorID from evaluations); J Kreie, New Mexico State University

Type I Subquery for deleting records Microsoft Enterprise Consortium Type I Subquery for deleting records If you want to delete records from a table based on criteria in another table, you must use a Type I subquery. /* For instance, see which students didn’t do any evaluations. Note the use of NOT IN. */ select stdid, stdfname, stdlname from students where stdid NOT IN (select evaluatorID from evaluations); /* If we wanted to delete these students, we would use the following statement. */ delete However, we can’t execute this delete statement because it conflicts with the evaluatee foreign key constraint. J Kreie, New Mexico State University

Type I Subquery in the HAVING clause Microsoft Enterprise Consortium Type I Subquery in the HAVING clause Find out who got an average evaluation score higher than the class average . /* Subquery */ Select avg(score) From eval_items_scores; /* List students with an above average score. *; select stdid, stdlname, std_teamID, avg(score) from students, evaluations, eval_items_scores where students.stdid = evaluations.evaluateeID and evaluations.eval_id = eval_items_scores.eval_id group by stdid, stdlname, std_teamID HAVING avg(score) > (Select avg(score) From eval_items_scores); J Kreie, New Mexico State University

Microsoft Enterprise Consortium What was covered … Type I subquery in the WHERE clause. The IN/NOT IN operator acts like a join between the outer query and the subquery. The inner query doesn’t reference the outer query. Do data is displayed directly from the subquery. Deleting data using the Type I query. A Type I subquery in the HAVING clause. J Kreie, New Mexico State University

Microsoft Enterprise Consortium Resources http://enterprise.waltoncollege.uark.edu/mec.asp Microsoft Faculty Connection—Faculty Resource Center http://www.facultyresourcecenter.com/ Microsoft Transact-SQL Reference http://msdn.microsoft.com/en-us/library/aa299742(v=SQL.80).aspx AdventureWorks Sample Database http://msdn.microsoft.com/en-us/library/ms124659%28v=sql.100%29.aspx J Kreie, New Mexico State University