1 IT420: Database Management and Organization SQL part 3 7 February 2006 Adina Crăiniceanu www.cs.usna.edu/~adina.

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Basic SQL Introduction Presented by: Madhuri Bhogadi.
 Database is SQL1.mdb ◦ import using MySQL Migration Toolkit 
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
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
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.
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 2-1 David M. Kroenke’s Chapter Two: Introduction to Structured Query.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
SQL SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
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.
David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design and Implementation Chapter Two: Introduction to Structured Query Language.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
1 Announcements Read 6.7 – 6.10 for Friday Homework 6, due Friday 10/29 Research paper –List of sources - due 10/29 Department Seminar –The Role of Experimentation.
INFS614 - Lecture week 9 1 More on SQL Lecture Week 9 INFS 614, Fall 2008.
1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu
Introduction to SQL J.-S. Chou Assistant Professor.
1 IT420: Database Management and Organization SQL - Data Manipulation Language 27 January 2006 Adina Crăiniceanu
SQL Queries and Subqueries Zaki Malik September 04, 2008.
Oracle Database Administration Lecture 2 SQL language.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
Using Special Operators (LIKE and IN)
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Oracle & SQL Introduction. Database Concepts Revision DB? DBMS? DB Application? Application Programs? DBS? Examples of DBS? Examples of DBMS? 2Oracle.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
1 IT420: Database Management and Organization SQL Views 15 February 2006 Adina Crăiniceanu
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
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.
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.
DAVID M. KROENKE’S DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-1 David M. Kroenke’s Chapter Two: Introduction to Structured Query.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
1 SY306 Web and Databases for Cyber Operations SQL: Structured Query Language.
1 SY306 Web and Databases for Cyber Operations Set #13: SQL SELECT Grouping and sub-queries.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
Drill Consider the following tables with the following fields: Student: FName, LName, StudentID, Age, Yr, Course Grades: ID, P1, P2, P3 1.Display the.
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.
April 2002 Information Systems Design John Ogden & John Wordsworth 1 Database Design SQL (1) John Wordsworth Department of Computer Science The University.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
Advanced Accounting Information Systems Day 12 Understanding the SQL Language September 21, 2009.
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 SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
COP Introduction to Database Structures
Oracle & SQL Introduction
SQL in Oracle.
Working with Tables: Join, Functions and Grouping
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
SQL: Structured Query Language
Contents Preface I Introduction Lesson Objectives I-2
Presentation transcript:

1 IT420: Database Management and Organization SQL part 3 7 February 2006 Adina Crăiniceanu

2 Announcements  Exam next Tuesday  2 hours  Closed book/closed notes  No computers  Covers all material  Labs returned on Friday

3 Previously  SQL DDL: Data Definition Language  CREATE, DROP, ALTER  DML: Data Manipulation Language  INSERT  DELETE  UPDATE  SELECT

4 Today  More about SELECT

5 The SQL SELECT Statement  Basic SQL Query: SELECT[DISTINCT] column_name(s) FROMtable_name(s) [WHEREconditions] [ORDER BYsome_column_names [ASC/DESC] ] *Terms between [ ] are optional

6 WHERE Clause Options  AND, OR  IN, NOT IN, BETWEEN  =, >,, >= SELECT SNb FROM Students S, Enrolled E WHERE S.SNb = E.Nb AND E.Cid NOT IN (‘ComSci’, ‘Math’)

7 Calculations in SQL  Simple arithmetic  Five SQL aggregate operators:  COUNT  SUM  AVG  MIN  MAX

8 Find the age of the youngest student  Students(Alpha, LName, FName, Class, Age)  SELECT MIN(Age) FROM Students

9 Aggregate Operators  So far, aggregate operations applied to all (qualifying) rows  We want to apply them to each of several groups of rows  Students(Alpha, LName, SName, Class, Age)  Find the age of the youngest student for each class

10 Example  Students(Alpha, LName, FName, Class, Age)  Find the age of the youngest student for each class  If class values go from 1 to 4 we can write 4 queries that look like this:  SELECT MIN (S.Age) FROM Students S WHERE S.Class = i

11 GROUP-BY Clause  SELECT grouping_columns(s), aggregates FROM table_name(s) [WHERE conditions] GROUP BY grouping_columns  SELECT Class, MIN(Age) FROM Students GROUP BY Class

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  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!

13 GROUP BY Exercise  Students(Alpha, LName, FName, Class, Age)  For each last name, find the number of students with same last name

14 HAVING Clause  SELECT [grouping_columns(s), aggregates FROM table_name(s) [WHERE conditions] GROUP BY grouping_columns HAVING group_conditions  HAVING restricts the groups presented in the result

15 Example  SELECT Class, MIN(Age) FROM Students WHERE MajDeptName = ‘ComSci’ GROUP BY Class HAVING Class > 2 What does the query compute?

16 Another GROUP BY Example  SKU_DATA(SKU, SKU_description, Buyer, Department)  SELECTDepartment, COUNT(*) AS Dept_SKU_Count FROMSKU_DATA WHERESKU <> GROUP BY Department HAVING COUNT (*) > 1 ORDER BYDept_SKU_Count;

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

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

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

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

21  SELECT FName, LName FROM Students S WHERE S.Alpha IN (SELECT Alpha FROM Enroll E WHERE E.CourseID = ‘IT420’)

22 Correlated Subqueries  SELECT FName, LName FROM Students S WHERE EXISTS (SELECT * FROM Enroll E WHERE E.CourseID = ‘IT420’ AND E.Alpha = S.Alpha)

23 Subqueries versus Joins  Subqueries and joins both process multiple tables.  Subquery can only be used to retrieve data from the top table.  Join can be used to obtain data from any number of tables  Correlated subquery can do work that is not possible with joins.

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 = ‘IT420’ AND E2.CourseID = ‘IT344’

25 JOIN ON Syntax  Students(Alpha, LName, FName, Class, Age)  Courses(CourseID, Description, Textbook)  Enroll(Alpha, CourseID, Semester, Grade)  Find the names of students enrolled in ‘IT420’ SELECT LName, FName FROM Students S JOIN Enroll C ON S.Alpha = C.Alpha WHERE CourseID = ‘IT420’  Find the names of students enrolled in ‘Database Management’

26 Multiple JOIN ON  Find the names of students enrolled in ‘Database Management’  SELECTLName, FName FROMEnroll E JOIN Courses C ON E.CourseID = C.CourseID JOIN Students ON E.Alpha = S.Alpha WHERE C.Description = ‘Database Management’

27 Outer Joins  Find all students and courses in which they are enrolled  SELECT S.Alpha, S.LName, S.FName, E.CourseID FROM Students S LEFT JOIN Enrolled E ON S.Alpha = E.Alpha

28 Class Exercise  Students(Alpha, LName, FName, Class, Age)  Courses(CourseID, Description, Textbook)  Enroll(Alpha, CourseID, Semester, Grade)  Find the age of youngest student older than 18, for each course with at least one such student enrolled in it