Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL In-Line Subquery.

Slides:



Advertisements
Similar presentations
Database Fundamentals
Advertisements

Advanced SQL (part 1) CS263 Lecture 7.
© Abdou Illia MIS Spring 2014
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,
Microsoft Enterprise Consortium Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Database Fundamentals Defining.
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.
SQL Sub (or Nested ) Query. Examples Q: Find students whose GPA is below the average. –The criteria itself requires a SQL statement. –SELECT * FROM student.
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.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
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.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Download three SQL script files from wiki page.
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.
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
1 Agenda – 03/25/2014 Login to SQL Server 2012 Management Studio. Answer questions about HW#7 – display answers. Exam is 4/1/2014. It will be in the lab.
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.
1 Chapter Overview Preparing to Upgrade Performing a Version Upgrade from Microsoft SQL Server 7.0 Performing an Online Database Upgrade from SQL Server.
Views Lesson 7.
Day 13, Slide 1 U:/msu/course/cse/103 CSE 103 Students: Review INNER and OUTER JOINs, Subqueries. Others: Please save your.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Select your database – Your database name is.
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.
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Sorting output.
1 Multiple Table Queries. 2 Objectives  Retrieve data from more than one table by joining tables  Using IN and EXISTS to query multiple tables  Nested.
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.
Chapter 12 Subqueries and Merge Statements
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL Equi-Join One-sided.
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.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Set Operators:
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
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.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals Data.
Create Views Using a Graphical Designer Database Administration Fundamentals LESSON 2.3b.
For more course tutorials visit CIS 336 All iLabs Week 1 to Week 7 Devry University CIS 336: All iLabs Week 1 to Week 7: Devry University.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Oracle Join Syntax.
SQL Server 2000 and Access 2000 limits
Working with Tables: Join, Functions and Grouping
Displaying Data from Multiple Tables
LESSON Database Administration Fundamentals Inserting Data.
20761A 10: Using Subqueries Module 10   Using Subqueries.
Dumps4Download Oracle 1z0-051 Exam Dumps - Oracle 1z0-051 Exam Dumps Questions
Free Braindumps - Pass Exam - Dumps4download
Using Subqueries to Solve Queries
Microsoft Enterprise Consortium Type I Subquery
Database Fundamentals
Oracle Join Syntax.
Access: SQL Participation Project
Structured Query Language
Using Subqueries to Solve Queries
Oracle Join Syntax.
Building Queries using the Principle of Simplest Query (POSQ)
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL In-Line Subquery 1 Microsoft Enterprise Consortium Microsoft Enterprise Consortium: Microsoft Faculty Connection/Faculty Resource Center

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas 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. If there is something besides the topics for this lesson that you’re not familiar with in this presentation, please review earlier lessons in the Advanced SQL presentations. 2

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium In-line subquery An in-line subquery is in the FROM clause of the outer query. Think of it has a regular but temporary table that is assigned a name through a table alias and must be joined with at least one other table, if more than one table is used. The use of subqueries is sometimes necessary but, even if the answer can be found without a subquery, it is often beneficial to use subqueries to decompose a problem statement. 3

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium In-line subquery example For this example, we’ll use the greenhouse database and create 3 temporary tables by using 3 subqueries. 4 List crop planting ID, crop, variety, and bay-bed. Count the # of harvests for each crop planting ID. List the crop planting ID and amendment, if the soil was amended with sulphur, potting mix, or mulch. Be sure to include the field(s) needed to join with other tables.

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium In-line example – 1 st subquery /* Subquery to list plantings of crop/varities. */ select cropPlanting_ID, crop, variety, bay_bed from tblcropPlanting join tblcropVariety on tblcropPlanting.cropVarID = tblCropVariety.cropVarID; 5

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium In-line example – 2 nd subquery /* Subquery to list the amendments for crops if the amendements are sulphur, potting mix, or mulch. */ select CropPlantingID, Amendment from tblAmendment join tblCropPlantingAmend on Amend_Code = AmendCode where Amendment IN ('Sulphur', 'Potting Mix', 'Mulch'); 6

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium In-line example – 3 rd subquery /* Get a count of the harvests for each crop-planting. */ select CropPlantingID, COUNT(*) as hrvst_count from tblCropHarvest group by CropPlantingID; 7

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium In-line example – Complete query /* In-line query using 3 subqueries in the FROM clause. This query uses table aliases. Note that the column aliases in the subquery are used just like "regular" columns in the outer query SELECT clause. */ select Crp.cropPlanting_ID, crop, variety, hrvst_count, BB from (select cropPlanting_ID, crop, variety, bay_bed as "BB" from tblcropPlanting join tblcropVariety on tblcropPlanting.cropVarID = tblCropVariety.cropVarID) Crp, (select CropPlantingID, Amendment from tblAmendment join tblCropPlantingAmend on Amend_Code = AmendCode where Amendment IN ('Sulphur', 'Potting Mix', 'Mulch')) Amd, (select CropPlantingID, COUNT(*) as hrvst_count from tblCropHarvest group by CropPlantingID) Hrv where amd.CropPlantingID = Crp.CropPlanting_ID and Crp.CropPlanting_ID = hrv.CropPlantingID order by hrvst_count desc; 8

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium In-line example – Query output /* In-line query using 3 subqueries in the FROM clause. This query uses table aliases. Note that the column aliases in the subquery are used just like "regular" columns in the outer query SELECT clause. */ OUTPUT: 9

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium What was covered … In-line subquery ◦Use in decomposing a problem and writing queries for each subquery. This can also help in debugging or troubleshooting a query. 10

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Resources Microsoft Faculty Connection—Faculty Resource Center Microsoft Transact-SQL Reference AdventureWorks Sample Database 11