7 7 Chapter 7 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 7th Edition Peter Rob & Carlos Coronel.

Slides:



Advertisements
Similar presentations
Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.
Advertisements

7 7 SQL Data Definition 4Spread throughout chapter 7.
Database Systems: Design, Implementation, and Management Tenth Edition
Introduction to Structured Query Language (SQL)
5 Chapter 5 Structured Query Language (SQL2) Revision.
Introduction to Structured Query Language (SQL)
Structured Query Language (SQL)
1 Query Languages: How to build or interrogate a relational database Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Structured Query Language Part I Chapter Three CIS 218.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Microsoft Access 2010 Chapter 7 Using SQL.
Chapter 7: SQL, the Structured Query Language Soid Quintero & Ervi Bongso CS157B.
Introduction to SQL J.-S. Chou Assistant Professor.
Chapter 3 Single-Table Queries
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
Chapter 3 Section 3.4 Relational Database Operators
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
6 1 Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) Structured Query Language Introduction to SQL Structured Query Language.
Advanced SQL Advanced SQL Complex Queries, Joining Tables.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 5 © Akhilesh Bajaj, 2000, 2002, 2003, All.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
SQL – Structured Query Language
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Lecture 8 – SQL Joins – assemble new views from existing tables INNER JOIN’s The Cartesian Product Theta Joins and Equi-joins Self Joins Natural Join.
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.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
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.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
1 SQL Chapter 9 – 8 th edition With help from Chapter 2 – 10 th edition.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
SQL, the Structured Query Language
Structured Query Language (SQL) DML
Database Systems: Design, Implementation, and Management Tenth Edition
Concept of Aggregation in SQL
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin
CS 405G: Introduction to Database Systems
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Chapter 7 Introduction to Structured Query Language (SQL)
CS4222 Principles of Database System
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
CSC 453 Database Systems Lecture
Database Systems: Design, Implementation, and Management Tenth Edition
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

7 7 Chapter 7 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 7th Edition Peter Rob & Carlos Coronel

7 7 SQL 4Standardized 4Data Manipulation and Data Definition 4Can be embedded into general programming languages 4Statements specify what is to be done, NOT how to do it

7 7 DML - Retrieval 4SELECT statement - can do MANY different things u List whole table SELECT * FROM STUDENT u Relational Algebra PROJECT SELECT StdID,LNAME,FNAME FROM STUDENT l Unlike RA, duplicates aren’t (automatically) removed. E.g. SELECT MAJOR FROM STUDENT l If we want duplicates removed, specify DISTINCT after SELECT SELECT DISTINCT MAJOR FROM STUDENT

7 7 DML - RESTRICT 4Relational Algebra RESTRICT/SELECT u e.g. list all students who are freshmen SELECT * FROM STUDENT WHERE YEAR = ‘Fr’ 4PROJECT and RESTRICT together SELECT StdID,LNAME,FNAME FROM STUDENT WHERE YEAR = ‘Fr’

7 7 More Retrieval 4WHERE condition can be as complicated as you need it to be. E.g. freshmen with poor grades SELECT StdID,LNAME,FNAME FROM STUDENT WHERE YEAR = ‘Fr’ AND GPA < 2.5

7 7 Ordering 4With a little extra complexity, we can get our output in order by some particular attribute(s) E.g. order students by major SELECT StdID,LNAME,FNAME, MAJOR FROM STUDENT ORDER BY MAJOR DESC

7 7 The Microsoft Access QBE and its SQL

7 7 Joining Tables 4As you know, an important task with relational databases is relating info from more than one table (for instance, natural join in RA) E.g. show all students with the class indices of enrollments SELECT StudID,LNAME,FNAME, INDEX FROM STUDENT, ENROLLMENTS WHERE StudID = Enrollments.Student

7 7 Restrict with Join 4Sometimes you don’t want the entire join - you might want to restrict the results (sometimes called select- project-join) E.g. show all students enrolled in a particular section SELECT StdID,LNAME,FNAME FROM STUDENT, ENROLLMENTS WHERE STUDENT. StdID = ENROLLMENTS.Student AND ENROLLMENTS.INDEX = 70238

7 7 Multi-way Join 4We can join more than two tables together (which is a good thing; joining students with enrollments was a little unsatisfying because we didn’t see any info about the section. Let’s show all students with the section info for sections they enrolled in SELECT STUDENT.*, SECTION.* FROM STUDENT, ENROLLMENTS, SECTION WHERE STUDENT.StdID = ENROLLMENTS.Student AND ENROLLMENTS.INDEX = SECTION.INDEX

7 7 Alias 4Alternate name (for a table) 4SELECT STUDENT.*, SECTION.* FROM STUDENT A, ENROLLMENTS B, SECTION C WHERE A.StdID = B.student AND B.index = C.index; 4 Here A,B, and C are aliases 4Used as a convenience, not necessary

7 7 Queries 4Special Operators u BETWEEN - used to define range limits. u IS NULL - used to check whether an attribute value is null u LIKE - used to check for similar character strings. u IN - used to check whether an attribute value matches a value contained within a (sub)set of listed values. u EXISTS - used to check whether an attribute has a value. In effect, EXISTS is the opposite of IS NULL. l Can also be used to check if a subquery returns any rows

7 7 4Special Operators BETWEEN is used to define range limits. SELECT * FROM STUDENT WHERE GPA BETWEEN 2.0 AND 2.1; Queries

7 7 4Special Operators IS NULL is used to check whether an attribute value is null. SELECT INDEX, DEPT, CLASS, TIME FROM SECTION WHERE ROOM IS NULL; Queries

7 7 4Special Operators LIKE is used to check for similar character strings. SELECT * FROM CATALOG_CLASS WHERE TITLE LIKE ‘%Lang%’; u % stands for 0 or more char wildcard u _ stands for a one char wildcard u e.g. WHERE TITLE LIKE ‘%Network%’ finds classes whose title includes the substring ‘Network’ u NOTE: While SQL commands are not case-sensitive, SQL strings are Queries

7 7 4Special Operators IN is used to check whether an attribute value matches a value contained within a (sub)set of listed values. SELECT * FROM ENROLLMENT WHERE INDEX IN (66415, 66421); EXISTS is used to check whether an attribute has value. SELECT * FROM SECTIONs WHERE PROFESSOR EXISTS; Queries

Can do computation 4e.g. SELECT SECTION.*,stop - enroll FROM SECTION 4gives for all sections, the number of remaining seats 4E.g. SELECT room.*, capacity / NumbStudentWorkstations AS StdPerComp FROM room 4Gives number of students per computer in the rooms 4Usual mathematical operators  + - * / ^

7 7 Some Basic SQL Numeric Functions Some SQL Numeric Aggregate Functions

7 7 Aggregate Functions - AVG 4e.g. find ave GPA for all students SELECT AVG(GPA) FROM STUDENT 4What is the average GPA of all freshmen SELECT AVG(GPA) FROM STUDENT WHERE YEAR = ‘Fr’

7 7 COUNT 4How many sections are offered SELECT COUNT(*) FROM SECTION 4How many computer science majors are there? SELECT COUNT(*) FROM STUDENT WHERE MAJOR = ‘CS’ 4How many distinct classes are being offered SELECT COUNT(DISTINCT DEPT,CLASS) FROM SECTION

7 7 Group By - Subtotals 4Total Enrollments by Dept SELECT DEPT, SUM(enroll) FROM SECTION GROUP BY DEPT

7 7 4Grouping Data GROUP BY

7 7 Improper Use of the GROUP BY Clause

7 7 Having 4Total Enrollments by Dept for depts offering more than 10 sections SELECT DEPT, SUM(enroll) FROM SECTION GROUP BY DEPT HAVING COUNT(*) > 10 4Average Evening Enrollments by Depts with low ave enrollment SELECT DEPT, AVG(enroll) FROM SECTION WHERE Sect >= 40 GROUP BY DEPT HAVING AVG(enroll) < 15

7 7 Having 4Total Enrollments by Dept for depts offering more than 10 sections SELECT DEPT, SUM(credits) FROM CATALOGCOURSE GROUP BY DEPT HAVING COUNT(*) > 10 4Average Highest Enrollment (capacity) for upper division courses by Depts – for depts with many upper division sections SELECT DEPT, AVG(MaxEnroll) FROM SECTIONS WHERE Course >= 200 GROUP BY DEPT HAVING COUNT(*) >= 10

7 7 An Application of the HAVING Clause

7 7 Nested Queries 4Sometimes the result of one query can be used in another query - thus we can have nested queries 4e.g. find students whose GPA is above average SELECT * FROM STUDENT WHERE GPA > ( SELECT AVG(GPA) FROM STUDENT )

7 7 Nested Queries 4If is fairly common for a nested query to use the set operation IN - which tests whether a value is a member of a set of values. So for instance, suppose we want to know all sections in which there are any freshman SELECT DISTINCT CLASSINDEX FROM ENROLLMENTS WHERE STDSSN IN ( SELECT SSN FROM STUDENT WHERE YEAR = ‘Fr’ )

7 7 Left Outer Join SELECT P_CODE, VENDOR.V_CODE, V_NAME FROM VENDOR LEFT JOIN PRODUCT ON VENDOR.V_CODE = PRODUCT.V_CODE

7 7 The Left Outer Join Results

7 7 Right Outer Join SELECT PRODUCT.P_CODE, VENDOR.V_CODE, V_NAME FROM VENDOR RIGHT JOIN PRODUCT ON VENDOR.V_CODE = PRODUCT.V_CODE

7 7 The Right Outer Join Results

7 7 End Queries – Next - Updates