ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4– Structured Query.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
1 Query-by-Example (QBE). 2 v A “GUI” for expressing queries. –Based on the Domain Relational Calulus (DRC)! –Actually invented before GUIs. –Very convenient.
Chapter 6 Additional Relational Operations Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.
CS 166: Database Management Systems
Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke1 Query-by-Example (QBE) Online Chapter Example is the school of mankind,
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Query-by-Example (QBE) Chapter 6 Example is the school of mankind, and they will learn at no.
Data Warehousing/Mining 1 Data Warehousing/Mining Comp 150 Aggregation in SQL (not in book) Instructor: Dan Hebert.
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
SQL Review.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Database Systems More SQL Database Design -- More SQL1.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
SQL Operations Aggregate Functions Having Clause Database Access Layer A2 Teacher Up skilling LECTURE 5.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Chapter 3 Introduction to SQL Yonsei University 1 st Semester, 2015 Sanghyun Park.
Chapter 3 Single-Table Queries
SQL Queries and Subqueries Zaki Malik September 04, 2008.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
CSc-340 2b1 Introduction to SQL Chapter 3 [2 of 2] Null Values Aggregate Functions Nested Subqueries Modification of the Database.
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.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
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.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 5 – September 4 th,
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
CMPT 258 Database Systems SQL Queries (Chapter 5).
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez-Martínez Electrical and Computer Engineering Department Lecture 12 – Introduction to.
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: 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 CSE 480: Database Systems Lecture 16: Relational Algebra.
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez-Martínez Electrical and Computer Engineering Department Lecture 10 – September 18,
1 Schema for Student Registration System Student Student (Id, Name, Addr, Status) Professor Professor (Id, Name, DeptId) Course Course (DeptId, CrsCode,
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
More SQL: Complex Queries,
Chapter 3 Introduction to SQL
Structured Query Language (Data Manipulation Language)
CS 480: Database Systems Lecture 12 February 11, 2013.
Introduction to SQL.
Chapter 3 Introduction to SQL(3)
Concept of Aggregation in SQL
Chapter 2: Intro to Relational Model
Lecture#7: Fun with SQL (Part 2)
CS 405G: Introduction to Database Systems
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CS4222 Principles of Database System
SQL: Structured Query Language
Chapter 2: Intro to Relational Model
Presentation transcript:

ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4– Structured Query Language (SQL) ©Manuel Rodriguez – All rights reserved

ICOM 6005Dr. Manuel Rodriguez Martinez2 Readings Read –Chapter 5 of of textbook

ICOM 6005Dr. Manuel Rodriguez Martinez3 Better Schema Students(sid:string, sname:string, saddr:string, sage:integer, sgpa: float); Courses(cname:string,ccreds:integer,clevel:string, cdesc:string); Offering(cname:string,csection:integer,cterm:string, croom:string, ccapacity:integer); Professor(pid:string, pname:string, pdept:string, psalary:float, prank:string); Taking(sid:string, cname:string, csection:integer, cterm:string, cgrade:string); Teaching(pid:string, cname:string, csection:integer, cterm:string);

ICOM 6005Dr. Manuel Rodriguez Martinez4 Writing Queries to a Database System Relational Algebra is for humans Database Management System needs to receive the queries in some language –Like code in a programming language How to implement this? –Option 1 – Extend Programming Language Add instructions to access DBMS –Language-specific and not portable –Option 2 – New Language specific for queries Map this language to API for DBMS access on specific programming language –Now the queries is portable Option 2 = Structured Query Language (SQL)

ICOM 6005Dr. Manuel Rodriguez Martinez5 Structured Query Language (SQL) Developed by IBM as part of System-R project Became widely used by early DBMS prototypes De-facto standard, then became ANSI Standard –SQL-86, SQL-89, SQL-92, SQL-99 SQL is based on Relational Algebra SQL specifies the query to be sent to DBMS –Declarative language Modes of use: –String send from Command prompt –String send from API in a Programming Language –Embedded within a Programming Language

ICOM 6005Dr. Manuel Rodriguez Martinez6 Basic Structure SQL queries have the following form: SELECT A1, A2, …,An FROM R1, R2, …, Rk WHERE P –Projection List: A1, A2, …,An –Table List: R1, R2, …, Rk –Selection condition: Predicate P The result is not really a relation because duplicates are permitted If we remove duplicates, the expression is:

ICOM 6005Dr. Manuel Rodriguez Martinez7 SELECT Clause The Select clause projects one or more attributes from a set of tables. SELECT sid, sname FROM Students SQL is not case sensitive –Table STUDENTS is the same as students or Students SQL does not allow to have char ‘_’ in names –Student_name is illegal –Need to call it Student-name Use * to select all attributes from a table: SELECT * FROM Students

ICOM 6005Dr. Manuel Rodriguez Martinez8 SELECT Clause and duplicates SQL does not remove duplicates by default SELECT sname FROM Students –You need to tell it to do Use the keyword distinct for this purpose: SELECT distinct sname FROM Students –This will only return one of each name in the database

ICOM 6005Dr. Manuel Rodriguez Martinez9 FROM Clause FROM is used to specify the tables that are accessed in the query SELECT sid,sname,sgpa FROM Students Unless equi-join condition is given in where clause, FROM clause forms a Cartesian product: SELECT sid, sname, sgpa, cname, csection FROM Students, Taking –This query forms a Cartesian product of the relations Students and Taking

ICOM 6005Dr. Manuel Rodriguez Martinez10 WHERE Clause The WHERE Clause is used to: –Specify selection conditions for a table –Specify Equi-join conditions for two-way joins, three-way joins, four-way joins, …, n-way joins –Specify Theta join conditions for two-way joins, three-way joins, four-way joins, …, n-way joins –Natural Join would have to specify all the equi-joins for all the common attributes There is also a Natural Join clause, we will see it later on SELECT sid,sname,sage FROM Students WHERE sage < 22 SELECT sid,cname FROM Students,Taking WHERE Students.sid = Taking.sid

ICOM 6005Dr. Manuel Rodriguez Martinez11 Examples of SQL Queries Get the name and id for all students under 21 with a gpa of at least 3.50 SELECT sname, sid FROM Students WHERE sage = 3.50 Get all the information about courses taught during the Spring of 2002 SELECT * FROM Offering WHERE cterm = ‘Spring 2002’

ICOM 6005Dr. Manuel Rodriguez Martinez12 Examples of SQL Queries Get the distinct names of courses being taught in room S-113 SELECT distinct cnames FROM Offering WHERE croom = ‘S113’ Get the name, and id of professors making less than SELECT pid, pname FROM Professors WHERE psalary < 30000

ICOM 6005Dr. Manuel Rodriguez Martinez13 Complex WHERE Clauses You can used the following logical connector to make complex WHERE Clauses –P1 AND P2, where P1 and P2 are predicates –P1 OR P2, where P1 and P2 are predicates –NOT P1, where P1 is a predicate Range queries are also supported –Queries that request values that fall between a given range Get the students whose is between 18 and 21 years. –Format is: Attribute BETWEEN Value 1 AND Value 2

ICOM 6005Dr. Manuel Rodriguez Martinez14 Example of between clause Get all the information about students whose gpa is between 3.70 and 4.00 SELECT * FROM Students WHERE sgpa BETWEEN 3.70 and 4.00 Get the names of all professors whose salary is a between and SELECT pname FROM Professors WHERE psalary BETWEEN AND 70000

ICOM 6005Dr. Manuel Rodriguez Martinez15 Rename Operator The rename operator in SQL allow you to –Rename a Table –Rename individual attributes –General Form: old-name AS new-name Examples: SELECT sname AS Name, sage AS Age FROM Students SELECT R.pid, R.pname, R.psalary FROM Professors as R WHERE R.psalary >= AND R.name <> ‘Joe Smith’

ICOM 6005Dr. Manuel Rodriguez Martinez16 Generalized Projections Allow you project arbitrary expression –Often use rename to get the a real name for the projected attribute –General form is: SELECT F1, F2, …,Fn FROM R1, R2, Rn WHERE P –Example: For each student in the university, get the name, id and adjusted gpa. The adjusted gpa is the gpa – 0.02 points. SELECT sname, sid, sgpa-0.02 AS agpa FROM Students

ICOM 6005Dr. Manuel Rodriguez Martinez17 Joins In SQL Joins come in two flavors: –Those done with the SELECT clause –Those done with the JOIN clause. SELECT clause is often used for equi-join –You need to project what you want “by hand” JOIN clause behaves like the join operation in relational algebra There is also an OUTER JOIN clause. It is recommended to use the various JOIN clause since the query looks more clean. But, not all systems care the support this SQL operator.

ICOM 6005Dr. Manuel Rodriguez Martinez18 Joins with the SELECT clause Simply write an Equi-join in the WHERE clause Example: Get the name and id of all students taking ICOM 4035 during the Fall of 2002 SELECT S.sid, S.name FROM Students as S, Taking as T WHERE S.sid = T.sid AND T.cname = “ICOM 4035” AND T.cterm = “Fall 2002”

ICOM 6005Dr. Manuel Rodriguez Martinez19 Joins with the SELECT clause More complicated case: Four-way join Get the name, id, gpa, section for all students taking MATE 6061 with Joe Ferrington during the Spring of 2003 SELECT S.sid, S.name, S.gpa, T.section FROM Students as S, Taking as T, Teaching as T2, Professors as P WHERE T.cname = “MATE 6061” AND P.pname = “Joe Ferrington” AND T.cterm = “Spring 2003” AND T.sid = S.sid AND T2.pid = P.pid AND T.cname = T2.cname AND T.cterm = T2.cterm AND T.csection = T2.csection

ICOM 6005Dr. Manuel Rodriguez Martinez20 Joins with JOIN Clause Join Clause comes in three flavors: –Equi-join –Natural join –Theta join The key word is : inner join General form for Equijoin –R1 inner join R2 using (A1,A2,A3) : equi-join on A1,A2,A3 General form for Natural Join –R1 natural inner join R2 : natural join between R1 and R2 General form for Theta Join –R1 inner join R2 on P : Theta join on predicate P

ICOM 6005Dr. Manuel Rodriguez Martinez21 Equijoin Get the name, id and gpa of all students that have taken ICOM 6005 SELECT R.sname, R.sid, R,sgpa FROM (Students inner join Taking using (sid)) AS R WHERE R.cname = “ICOM 6005” Get the name and id of all professors making more than $50,000 that teach MATE 4040 SELECT R.pname, R.pid FROM (Professor inner join Teaching using (pid)) AS R WHERE R.psalary > 50,000 AND R.cname = “MATE 4040”

ICOM 6005Dr. Manuel Rodriguez Martinez22 Natural Join Get the name of all courses that have been taught by professor with id “5050” in which student with id “2938” was registered. SELECT R.cname FROM (Teaching natural inner join Taking) AS R WHERE R.sid = “2938” AND R.pid = “5050”

ICOM 6005Dr. Manuel Rodriguez Martinez23 Natural Join Get the name, id, gpa, section for all students taking MATE 6061 with Joe Ferrington during the Spring of 2003 SELECT R.sname, R.sid. R.gpa, R.csection FROM (((Students natural inner join Taking) natural inner join Teaching) natural inner join Professors) AS R WHERE R.cname = “MATE 6061” AND R.pname = “Joe Ferrington” AND R.cterm = “Spring 2003”

ICOM 6005Dr. Manuel Rodriguez Martinez24 Theta Joins Get the names of course not taught by Prof. Tom Davis SELECT R.cname FROM (Professor AS P inner join Teaching AS T on P.pid <> T.pid) AS R WHERE R.pname = “Tom Davis”

ICOM 6005Dr. Manuel Rodriguez Martinez25 Outer Joins Syntax is like joins, but you add the type of outer join: –R1 left outer join R2 –R1 right outer join R2 –R1 full outer join R2 –Each one can be combined with: Natual Using On P Ex: –Students left outer join Taking using (sid)

ICOM 6005Dr. Manuel Rodriguez Martinez26 Aggregates The use of aggregates is vital for OLAP (On-line analytical Processing) –Answer statistical queries about the data Aggregates supported: count, min, max, avg, sum Examples: –Get the average gpa for the students SELECT avg(sgpa) FROM Students –Get the total number of section for ICOM 4036 during the Fall of SELECT count(*) FROM Offering WHERE cname = “ICOM 4036” AND cterm = “Fall 2001”

ICOM 6005Dr. Manuel Rodriguez Martinez27 Aggregates Get the total number of students from Ponce taking the course MATE 3001 SELECT count(*) FROM (Students natural inner join Taking) AS R WHERE R.saddr = “Ponce” AND R.cname = “ MATE 3001” All these aggregate are computed over the entire relation, which is considered as a single group. Sometimes, we need to split the relation into groups and compute aggregates over each individual group.

ICOM 6005Dr. Manuel Rodriguez Martinez28 Group by The Group-by clause allows for the computation of aggregates on sub-group of tuples Tuples in each sub-group have one or more attributes in common For each group, the following is projected: –Common attributes used to form the groups –One or more attributes computed on the group-by All attributes that appear in the group-by clause must be projected!

ICOM 6005Dr. Manuel Rodriguez Martinez29 Examples of Group by queries Get the professor id and number of sections being taught by each professor in the university SELECT R.pid, count(*) AS numcourses FROM (Professor natural inner join Teaching) AS R GROUP BY R.pid Get the number of sections for each undergraduate course for each term SELECT R.cname, R.cterm, count(*) FROM (Courses natural inner join Offering) AS R WHERE R.clevel = “Undegraduate” GROUP BY R.cname, R.cterm

ICOM 6005Dr. Manuel Rodriguez Martinez30 Multiple aggregates in a query You can compute multiple aggregates in a given query –For a single group –For multiple groups Get the minimum and average gpa for all students SELECT min(sgpa) as mingpa, avg(sgpa) as avggpa FROM Students Get the minimum, maximum and average faculty salary per departmet SELECT pdept, min(psalary) as minsal, max(psalary) as maxsal, avg(psalary) as avgsal From Professor Group by pdept

ICOM 6005Dr. Manuel Rodriguez Martinez31 Filtering aggregates with Having clause Sometimes you need to restrict the types of groups that are returned –Based on the values of the statistics SQL provides Having clause for this purpose –Think of it as a “where” clause that you apply to the aggregate values Having clause is always computed after the groups are formed and the aggregates are fully computed

ICOM 6005Dr. Manuel Rodriguez Martinez32 Examples with Having clause Get the number of sections for each undergraduate course for each term, but only if more than two sections are given: SELECT R.cname, R.cterm, count(*) FROM (Courses natural inner join Offering) AS R WHERE R.clevel = “Undegraduate” GROUP BY R.cname, R.cterm HAVING count(*) > 2

ICOM 6005Dr. Manuel Rodriguez Martinez33 Examples with Having clause Get the minimum, maximum and average faculty salary per departmet, but only for those departments with minimum salary greater that 30,000 SELECT pdept, min(psalary) as minsal, max(psalary) as maxsal, avg(psalary) as avgsal From Professor Group by pdept Having min(psalary) > 30000