NURS6803 Clinical DB Design Katherine Sward, PhD, RN

Slides:



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

© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Chapter 4 Joining Multiple Tables
Ashley Ohmann June 20, * What is Custom SQL? * What can I do with it? * Join conditions * Unions and Self Joins * Ranks * Derived Tables.
Characteristic Functions. Want: YearCodeQ1AmtQ2AmtQ3AmtQ4Amt 2001e (from fin_data table in Sybase Sample Database) Have: Yearquartercodeamount.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
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.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Advanced SQL SMSU Computer Services Short Course.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 8: Subqueries.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
SQL Unit 5 Aggregation, GROUP BY, and HAVING Kirk Scott 1.
SQL (Chapter 2: Simple queries; Chapter 7 and 8: Nested and DML queries) Many of the examples in this document are based on the tables in the next slide.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Database Development Tr ươ ng Quý Quỳnh. References UDEMY: SQL Database MasterClass: Go From Pupil To Master! Database Systems - A Practical Approach.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
Chapter 12 Subqueries and Merge Statements
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Relational Databases. SQL Sub-queries: queries within queries  So far when data has been filtered the filter has been known and simply added to the Where.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Fall Lab 131 CS105 Lab 13 – Logical Operator Precedence and Joining Tables Announcements: MP 3 released Friday, 11/20 Honors project due: Tuesday,
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-row Subqueries, Correlated Subqueries.
1 CS 430 Database Theory Winter 2005 Lecture 13: SQL DML - Modifying Data.
Day 5 - More Complexity With Queries Explanation of JOIN & Examples Explanation of JOIN & Examples Explanation & Examples of Aggregation Explanation &
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
Slide 1 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla In this session, you will learn to: Query data by using joins Query.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Lecture 7: Subqueries Tarik Booker California State University, Los Angeles.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
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.
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements.
SQL Query Getting to the data ……..
Structured Query Language
MySQL Subquery Source: Dev.MySql.com
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
References: Text Chapters 8 and 9
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
Analyzing Table Data.
Subqueries Schedule: Timing Topic 25 minutes Lecture
NURS6803 Clinical DB Design Katherine Sward, PhD, RN
Using Subqueries to Solve Queries
David M. Kroenke and David J
Database Queries.
JOINS (Joinining multiple tables)
Using SQL with Access I am adding queries to the stu table in SecondDB.accdb – a database that I created in class. SQL stands for structured query language.
SELECT & FROM Commands Farrokh Alemi, PhD
Using Subqueries to Solve Queries
Lecture 3 Finishing SQL
M1G Introduction to Database Development
Reporting Aggregated Data Using the Group Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
Database Systems: Design, Implementation, and Management Tenth Edition
Topic 12 Lesson 2 – Retrieving Data with Queries
Reporting Aggregated Data Using the Group Functions
Using Subqueries to Solve Queries
Reporting Aggregated Data Using the Group Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
JOINS (Joinining multiple tables)
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

NURS6803 Clinical DB Design Katherine Sward, PhD, RN More SQL NURS6803 Clinical DB Design Katherine Sward, PhD, RN 1

Planning the query (the process I use) Write the template Select From Where Determine which table(s) have the data I need What columns do I want to return What do I need in order to determine what rows I want

Planning the query If it’s complex, I do just a portion at a time Once that’s working the way I want, then do the next part You’ll see this in action as we work through examples

Subqueries In relational theory… the result of manipulating a relation (or multiple relations) is also a relation We think of relation as a table But the result of a query is also a “relation” So we can use the result of a query like it’s a table (temporary table – does not copy data to a new physical table) The result of one query can be the “source” for another query

View When you save the definition of a query (save the query, in Access) you create a VIEW. A view is simply a saved query definition You do not have to save the query as a view in order to base another query on it’s results

Subqueries You can embed a complete SELECT statement (complete query) within another query This is called a nested query (or subquery) Subquery returns A single value A single row of data with multiple columns Multiple rows of data with one or more columns

Subqueries Suppose all the columns you want to display are in one table But the criteria to determine what ROWS to display are in another table Let’s say we want patient ID and name for everyone who is the same race as Deanna Troi This is all in the demog table

Build the query Names are formatted Last, First So first find Deanna’s race SELECT race FROM demog WHERE name = 'Troi, Deanna' NOTE: straight quotes, not curvy ones! YES ' NO ‘ or “ MAYBE "

Build the query Now find the people who have the same value as Deanna for race SELECT pt_id, name FROM demog WHERE race = (SELECT race WHERE name = 'Troi, Deanna')

Subqueries Now, suppose the criterion is in a different table Let’s say we want patient ID and name for every patient who has a low WBC (less than 3.8). We don’t want the wbc value, just the ID and name.

What tables?

What tables I only want to return columns from demog But the rows will be determined by data in the vitals table The things we want to return are the outer query The filter is the subquery So we will use vitals for the subquery

WHERE WBC < 3.8; returns a list of pt_id SELECT pt_id FROM blood WHERE WBC < 3.8; returns a list of pt_id pt_id 103 133 15 155

Now move up a layer Put parentheses around the subquery and get rid of the semi-colon (SELECT pt_id FROM blood WHERE WBC < 3.8)

IN We will use the keyword IN to tell the database that we are providing a list You saw IN with a list previously when we made a list of zip codes Our query returned a list, so we can use the query in place of typing out the results

The whole query SELECT pt_id, name FROM demog WHERE pt_id IN FROM blood WHERE WBC < 3.8); pt_id name 103 Cole, Nat-King 133 Newman, Paul 15 Drexel, Clyde 155 Robert, Jake 2 Minamino, Yoko 213 Troi, Deanna 214 Devil, Tazmanian 247 null, null 314 378 396 454 485

The whole query SELECT pt_id, name FROM demog WHERE pt_id IN FROM blood WHERE WBC < 3.8) ORDER BY cint(pt_id); pt_id name 2 Minamino, Yoko 15 Drexel, Clyde 103 Cole, Nat-King 133 Newman, Paul 155 Robert, Jake 213 Troi, Deanna 214 Devil, Tazmanian 247 null, null 314 378 396 454 485

Subqueries Suppose your criteria for “what rows” includes an aggregate function or a list of items You can use any legal comparator that makes sense for the query (less than, greater than, etc.) But be careful. Know whether your subquery returns one row, or multiple

This time everything we want is in the vitals table For example, we want to know the pt_id for everyone who has a SBP higher than the average SBP for the group This time everything we want is in the vitals table We will use a subquery to find the average SBP Then use the outer query to find everyone higher than that

SELECT pt_id FROM vitals WHERE SBP > (SELECT avg(SBP) FROM vitals) Note that the field in the outer table is to the LEFT of the > symbol, and the subquery is to the right SELECT pt_id FROM vitals WHERE SBP > (SELECT avg(SBP) FROM vitals)

We can even do math with the subquery We can even do math with the subquery. Suppose we wanted people who have an SBP higher than 1.5 times the average SBP for the group….

SELECT pt_id FROM vitals WHERE SBP > 1.5 * (SELECT avg(SBP) FROM vitals)

Other uses You can use a subquery in the SELECT clause You can use a subquery in the FROM clause (this is a view when used in the FROM clause)

Getting tricky… Suppose we want to know how each person’s highest SBP compares to the average for the group—we want to know how much above average is the highest SBP, for those folks who have higher-than-average SBP

Building a little at a time Start with the query that finds the average SBP for the group SELECT avg(SBP) FROM vitals

Building a little at a time Who has higher than average SBP? SELECT pt_id FROM vitals WHERE SBP > (SELECT avg(SBP) FROM vitals)

Building a little at a time What is max SBP for people who have higher than average SBP? SELECT pt_id, max(SBP) as MaxBP FROM vitals WHERE SBP > (SELECT avg(SBP) FROM vitals) GROUP BY pt_id

Building a little at a time What is difference between each individual’s max SBP and the overall average SBP? SELECT pt_id, max(SBP) as MaxBP, max(SBP) – (SELECT avg(SBP) from vitals) As BPDiff FROM vitals WHERE SBP > (SELECT avg(SBP) FROM vitals) GROUP BY pt_id ORDER BY cint(pt_id) pt_id MaxSBP BPDiff 1 130 5.08943089430895 3 138 13.0894308943089 6 140 15.0894308943089 14 150 25.0894308943089 16 17

ANY and ALL If the subquery returns a single column (can be multiple rows, but a single column)… You can use ALL (only true if ALL values of the subquery are matched) You can use ANY (true if at least one value in the subquery is matched). ANY is same as SOME. If you do not specify, then “ANY” is used

Example Suppose you have a table with letters of the alphabet (go with me here for the sake of syntax example…) SELECT Letter From Alphabet where Letter > ALL (Select letter from alphabet Where letter < D)

Example SELECT Letter From Alphabet where Letter > ANY (A, B, C) SELECT Letter From Alphabet where Letter > ALL (A, B, C) Without ALL, it will return B, C, D, E….. (B > A, C>B and C>A) With ALL then B and C are not in the results

Subquery is less commonly used than what we will do next – joining tables When you want data from more than one table in your results….