SQL : Query Language Part II CS3431.

Slides:



Advertisements
Similar presentations
1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
Advertisements

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 
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
SQL (2).
CREATE VIEW SYNTAX CREATE VIEW name [(view_col [, view_col …])] AS [WITH CHECK OPTION];
Chapter 11 Group Functions
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.
Dec 4, 2003Murali Mani SQL B term 2004: lecture 14.
Murali Mani The Relational Model. Murali Mani Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still.
Murali Mani SQL: Updates (DML) and Views (DDL). Murali Mani SQL DML (Updating the Data) Insert Delete Update.
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
Introduction to Oracle9i: SQL1 SQL Group Functions.
Murali Mani SQL. Murali Mani SELECT-FROM-WHERE SELECT * FROM Student WHERE sName=“Greg” AND address=“320 FL”  (sName=“Greg” AND address=“320 FL”) (Student)
Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Murali Mani Relational Algebra. Murali Mani What is Relational Algebra? Defines operations (data retrieval) for relational model SQL’s DML (Data Manipulation.
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
Chapter 3 Single-Table Queries
SQL Queries and Subqueries Zaki Malik September 04, 2008.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 5 © Akhilesh Bajaj, 2000, 2002, 2003, All.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
SQL Aggregation Oracle and ANSI Standard SQL Lecture 9.
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.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
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 Four Single-Table Queries.
1 SQL: The Query Language (Part II). 2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of.
Agenda for Class - 03/04/2014 Answer questions about HW#5 and HW#6 Review query syntax. Discuss group functions and summary output with the GROUP BY statement.
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.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
1 Schema for Student Registration System Student Student (Id, Name, Addr, Status) Professor Professor (Id, Name, DeptId) Course Course (DeptId, CrsCode,
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1 Part II.
1 SQL Chapter 9 – 8 th edition With help from Chapter 2 – 10 th edition.
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1.
More SQL: Complex Queries,
Slides are reused by the approval of Jeffrey Ullman’s
CS580 Advanced Database Topics
6/22/2018.
Chapter 3 Introduction to SQL(3)
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
CS 405G: Introduction to Database Systems
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
SQL Views CS542.
SQL : Query Language CS3431.
SQL Views and Updates cs3431.
SQL – Entire Select.
Built in Functions Massaging the data.
Chapter 4 Summary Query.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL: Structured Query Language
SQL: Structured Query Language
SQL Aggregation.
Normalization cs3431.
SQL: Structured Query Language
Section 4 - Sorting/Functions
SQL: Structured Query Language
SQL: Structured Query Language
SQL.
SQL: Structured Query Language
Group Operations Part IV.
Presentation transcript:

SQL : Query Language Part II CS3431

Sorting: ORDER BY clause SELECT * FROM Student WHERE sNumber >= 1 ORDER BY sNumber, sName  (sNumber, sName) ( (sNumber >= 1) (Student)) CS3431

Subqueries Subquery returns a relation Nest subqueries into WHERE clause Nest subqueries into FROM clause CS3431

Subqueries SELECT * FROM Student WHERE professor = (SELECT pName FROM Professor WHERE pNumber=1) Note: The inner subquery returns a relation, but SQL runtime ensures that subquery returns a relation with one column and with one row, otherwise it is a run-time error. CS3431

Subqueries - Example Student SELECT * FROM Student WHERE professor = (SELECT pName FROM Professor WHERE pNumber=1) sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER Professor pNumber pName address 1 MM 141FL 2 ER 201FL sNumber sName address professor 1 Dave 320FL MM 2 Greg CS3431

Subqueries We can use IN, EXISTS, NOT IN, and NOT EXISTS ALL, ANY can be used with comparisons SELECT * FROM Student WHERE (sNumber, professor) IN (SELECT pNumber, pName FROM Professor) CS3431

Subqueries - Example Student Professor SELECT * FROM Student sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER pNumber pName address 1 MM 141FL 2 ER 201FL SELECT * FROM Student WHERE (sNumber, professor) IN (SELECT pNumber, pName FROM Professor) sNumber sName address professor 1 Dave 320FL MM CS3431

Subqueries: EXISTS Professor Student SELECT * FROM Student WHERE EXISTS (SELECT pName FROM Professor WHERE Student.professor=pName) Professor pNumber pName address 1 MM 141FL 2 ER 201FL Student sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER CS3431

Subqueries with negation SELECT * FROM Student WHERE (sNumber, professor) NOT IN (SELECT pNumber, pName FROM Professor) Professor pNumber pName address 1 MM 141FL 2 ER 201FL Student sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER sNumber sName address professor 2 Greg 320FL MM 3 Matt ER CS3431

Subqueries with negation Student Professor sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER pNumber pName address 1 MM 141FL 2 ER 201FL SELECT * FROM Student WHERE NOT EXISTS (SELECT pName FROM Professor WHERE Student.professor=pName) sNumber sName address professor CS3431

Subqueries: ALL, ANY CS3431

Subqueries: ALL - Example Student Professor sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER pNumber pName address 1 MM 141FL 2 ER 201FL SELECT * FROM Student WHERE sNumber > ALL (SELECT pNumber FROM Professor) sNumber sName address professor 3 Matt 320FL ER CS3431

Subqueries: ANY - Example Student Professor sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER pNumber pName address 1 MM 141FL 2 ER 201FL SELECT * FROM Student WHERE sNumber = ANY (SELECT pNumber FROM Professor) sNumber sName address professor 1 Dave 320FL MM 2 Greg CS3431

Subqueries: NOT ALL - Example Student Professor sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER pNumber pName address 1 MM 141FL 2 ER 201FL SELECT * FROM Student WHERE NOT sNumber > ALL (SELECT pNumber FROM Professor) sNumber sName address professor 1 Dave 320FL MM 2 Greg CS3431

Subqueries: NOT ANY - Example Student Professor sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER pNumber pName address 1 MM 141FL 2 ER 201FL SELECT * FROM Student WHERE NOT sNumber = ANY (SELECT pNumber FROM Professor) sNumber sName address professor 3 Matt 320FL ER CS3431

Subqueries: Correlation CS3431

Subqueries in FROM clause SELECT sName, pName FROM Student, (SELECT * FROM Professor WHERE pNumber=1) WHERE professor=pName; Professor pNumber pName address 1 MM 141FL 2 ER 201FL Student sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER sName pName Dave MM Greg CS3431

Duplicate Elimination SELECT DISTINCT address FROM Student WHERE sNumber >= 1; SELECT DISTINCT * FROM Student; (Student) ( (address) ( (sNumber >= 1) (Student))) Student sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER address 320FL CS3431

Aggregation + GroupBy CS3431

Aggregation Functions SELECT COUNT (*) FROM Student; SELECT COUNT (sNumber) FROM Student; SELECT MIN (sNumber) FROM Student; SELECT MAX (sNumber) FROM Student; SELECT SUM (sNumber) FROM Student; SELECT AVG (sNumber) FROM Student; We can have distinct such as: SELECT COUNT (DISTINCT sNumber) FROM Student CS3431

Grouping SELECT COUNT (sName) as cnum FROM Student GROUP BY address; (COUNT (sName) as snum) ( (address, COUNT (sName)) (Student)) Student sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER COUNT (sName) 3 CS3431

Grouping SELECT address, COUNT (sNumber) FROM Student WHERE sNumber > 1 GROUP BY address HAVING COUNT (sNumber) > 1; Student sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER 4 Ben 300FL address COUNT (sNumber) 320FL 2 CS3431

Aggregation and NULLs NULLs are ignored in any aggregation; except COUNT (*) However if the set of attributes to be grouped on has null values, then grouping is done on the null values as well. CS3431

SQL Queries - Summary SELECT [DISTINCT] a1, a2, …, an FROM R1, R2, …, Rm [WHERE C1] [GROUP BY g1, g2, …, gl [HAVING C2]] [ORDER BY o1, o2, …, oj] CS3431