1 SY306 Web and Databases for Cyber Operations Set #13: SQL SELECT Grouping and sub-queries.

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
 Database is SQL1.mdb ◦ import using MySQL Migration Toolkit 
Introduction to Database Systems 1 SQL: The Query Language Relation Model : Topic 4.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
CS 166: Database Management Systems
SQL: Queries, Constraints, Triggers
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Database Management Systems 1 Raghu Ramakrishnan SQL: Queries, Programming, Triggers Chpt 5.
SQL: The Query Language Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein and etc for some slides.
SQL Part II: Advanced Queries. 421B: Database Systems - SQL Queries II 2 Aggregation q Significant extension of relational algebra q “Count the number.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
CS 405G: Introduction to Database Systems
SQL (2).
M ATH IN SQL. 222 A GGREGATION O PERATORS Operators on sets of tuples. Significant extension of relational algebra. SUM ( [DISTINCT] A): the sum of all.
SQL Review.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 90 Database Systems I SQL Queries.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
Rutgers University SQL: Queries, Constraints, Triggers 198:541 Rutgers University.
--The SQL Query Language DML--1 LIKE  LIKE allows to select character strings which have some element in common by using wild cards:  Wild cards:  “%”
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
1 SQL: Structured Query Language Chapter 5. 2 SQL and Relational Calculus relationalcalculusAlthough relational algebra is useful in the analysis of query.
1 Rewriting Intersect Queries Using In SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ INTERSECT.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
CSC343 – Introduction to Databases - A. Vaisman1 SQL: Queries, Programming, Triggers.
Computer Science 101 Web Access to Databases SQL – Extended Form.
Chapter 5.  Data Manipulation Language (DML): subset of SQL which allows users to create queries and to insert, delete and modify rows.  Data Definition.
INFS614 - Lecture week 9 1 More on SQL Lecture Week 9 INFS 614, Fall 2008.
1 IT420: Database Management and Organization SQL - Data Manipulation Language 27 January 2006 Adina Crăiniceanu
Chapter 3 Single-Table Queries
SQL Queries and Subqueries Zaki Malik September 04, 2008.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) Structured Query Language Introduction to SQL Structured Query Language.
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
Using Special Operators (LIKE and IN)
SQL Examples CS3754 Class Note 11 CS3754 Class Note 11, John Shieh,
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
SQL: Queries, Programming, Triggers. Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for.
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 5 © Akhilesh Bajaj, 2000, 2002, 2003, All.
1 Database Systems ( 資料庫系統 ) October 24, 2005 Lecture #5.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Example Instances R1 S1 S2  We will use these instances of the Sailors and Reserves relations in our.
1 IT420: Database Management and Organization SQL part 3 7 February 2006 Adina Crăiniceanu
ICS 321 Spring 2011 The Database Language SQL (iii) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 3/14/20111Lipyeow.
Introduction to SQL ; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Using SQL as a Query Language COSC 6340.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
1 SY306 Web and Databases for Cyber Operations SQL: Structured Query Language.
CMPT 258 Database Systems Final Exam Review.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
1 SQL: The Query Language (Part II). 2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
Database Management Systems 1 Raghu Ramakrishnan SQL: Queries, Programming, Triggers Chpt 5 Jianping Fan.
COMP 430 Intro. to Database Systems Grouping & Aggregation Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!
SQL: The Query Language Part 1 R&G - Chapter 5 1.
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
SQL Reminder Jiankang Yuan Martin Lemke. SQL Reminder - SELECT SELECT column_name1, column_name2, … FROM table_name SELECT * FROM table_name.
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
SQL: The Query Language Part 1 R&G - Chapter 5 Lecture 7 The important thing is not to stop questioning. Albert Einstein.
COP Introduction to Database Structures
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
Working with Tables: Join, Functions and Grouping
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
CS 405G: Introduction to Database Systems
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
SQL – Entire Select.
SQL: Structured Query Language
Section 4 - Sorting/Functions
Presentation transcript:

1 SY306 Web and Databases for Cyber Operations Set #13: SQL SELECT Grouping and sub-queries

2 Recap: (partial)  SELECT [DISTINCT] column_name(s) FROM table_name(s) WHERE conditions ORDER BY column_name(s) [ASC/DESC]

Today’s Agenda  Math in SQL  Grouping  Subqueries 3

4 Calculations in SQL  Simple arithmetic  Five SQL Built-in Functions:  COUNT  SUM  AVG  MIN  MAX

5 Simple Arithmetic  SELECT NbHours* HourlyRate AS Charge FROM FlightEvents  SELECT Concat(SFirstName,‘ ’, SLastName) FROM Students Charge (No column name) John Doe Brad Johnson Jessica Smith Mary Davis

6 Aggregate Operators  SELECT COUNT(*) as nb FROM Students  SELECT COUNT(DISTINCT SName) FROM Students WHERE SNb > 700  SELECT AVG(Age) FROM Students WHERE SNb LIKE ’16____’

7 Grouping rows  Find the age of the youngest student for each class year  SELECT MIN (S.Age) FROM Students S WHERE S.ClassYear = 2016 (no column name) 21

8 GROUP-BY Clause  SELECT [DISTINCT] column_name(s) | aggregate_expr FROM table_name(s) [WHERE conditions] GROUP BY grouping_columns  Example: SELECT ClassYear, MIN(Age) FROM Students GROUP BY ClassYear ClassYear(no column name)

9 Conceptual Evaluation  Semantics of an SQL query defined as follows:  Compute the cross-product of tables in FROM (table_names)  Discard resulting rows if they fail WHERE conditions  Delete columns that are not in SELECT or GROUP BY(column_names or grouping-columns)  Remaining rows are partitioned into groups by the value of the columns in grouping-columns  One answer row is generated per group  Note: Does not imply query will actually be evaluated this way!

10 HAVING Clause  SELECT [DISTINCT] column_name(s) | aggregate_expr FROM table_name(s) [WHERE conditions] GROUP BY grouping_columns HAVING group_conditions  GROUP BY groups the rows  HAVING restricts the groups presented in the result

11 Example- HAVING  SELECT ClassYear, MIN(Age) FROM Students WHERE MajDeptName = ‘ComSci’ GROUP BY ClassYear HAVING COUNT(*) > 20

12 Conceptual Evaluation  SQL query semantics:  Compute the cross-product of table_names  Discard resulting rows if they fail conditions  Delete columns that are not specified in SELECT, GROUP BY  Remaining rows are partitioned into groups by the value of the columns in grouping-columns  One answer row is generated per group  Discard resulting groups that do not satisfy group_conditions

13 Example  SELECT Class, MIN(Age) FROM Students WHERE MajDeptName = ‘ComSci’ GROUP BY Class HAVING COUNT(*) > 2

14 Class Exercise  Students(SNb, SName, )  Courses(Cid,CName, Dept)  Enrolled(SNb,Cid, Semester)  List all course names, and the number of students enrolled in the course

15 SELECT Summary  SELECT [DISTINCT] column_name(s) | aggregate_expr FROM table_name(s) WHERE row_conditions GROUP BY grouping_columns HAVING group_conditions ORDER BY column_name(s) [ASC/DESC]

16 Sub-queries  Aggregate Operators Limitations  Return only one row  Not in WHERE clause

17 Select age of oldest student  SELECT MAX (Age) FROM Students S

18 Select oldest students and their age  SELECT S.SName, MAX (Age) FROM Students S  SELECT S.SName, S.Age FROM Students S WHERE S.AGE = (SELECT MAX(Age) FROM Students) Correct! Illegal! Sub-query

19 Select students with age higher than average  SELECT * FROM Students WHERE Age > AVG(Age)  SELECT * FROM Students WHERE Age > (SELECT AVG(Age) FROM Students) Correct! Illegal!

20 Subqueries  SELECT * FROM Students WHERE Age > (SELECT AVG(Age) FROM Students)  Second select is a subquery (or nested query)  You can have subqueries in SELECT, FROM or HAVING clause also

21 Subqueries in FROM Clause  Find name of students enrolled in both ‘IT360’ and ‘IT334’  SELECT FName + ‘ ’ + LName AS StudentName FROM Students S, (SELECT Alpha FROM Enroll WHERE CourseID = ‘IT360’ AND Alpha IN (SELECT Alpha FROM Enroll WHERE CourseID = ‘IT334’) ) AS ResultAlphaTable WHERE S.Alpha = ResultAlphaTable.Alpha

22 Subqueries Exercise  Students(Alpha, LName, FName, Class, Age)  Enroll(Alpha, CourseID, Semester, Grade) 1.Find alpha for students enrolled in both ‘SY306’ and ‘SI485’ 2.Find name of students enrolled in both ‘SY306’ and ‘SI485’

23 Class Exercise  Students(Alpha, LName, FName, Class, Age)  Enroll(Alpha, CourseID, Semester, Grade)  Find the name of students enrolled in ‘SY306’  Usual way  Use subqueries

24 Class Exercise  What does this query compute:  SELECT FName, LName FROM Students S, Enroll E1, Enroll E2 WHERE S.Alpha = E1.Alpha AND S.Alpha = E2.Alpha AND E1.CourseID = ‘IT360’ AND E2.CourseID = ‘IT344’

Efficiency  Many way to write a SQL query  Most efficient – use as few tables as possible  If more than one table needed – use joins  Less efficient – use sub-queries  Least efficient – use correlated sub- queries 25

26 SELECT Summary  SELECT [DISTINCT] column_name(s) | aggregate_expr FROM table_name(s) WHERE row_conditions GROUP BY grouping_columns HAVING group_conditions ORDER BY column_name(s) [ASC/DESC]