Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4– Structured Query."— Presentation transcript:

1 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

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

3 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);

4 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)

5 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

6 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:

7 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

8 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

9 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

10 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

11 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’

12 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 30000 SELECT pid, pname FROM Professors WHERE psalary < 30000

13 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

14 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 40000 and 70000 SELECT pname FROM Professors WHERE psalary BETWEEN 40000 AND 70000

15 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 >= 80000 AND R.name <> ‘Joe Smith’

16 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

17 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.

18 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”

19 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

20 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

21 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”

22 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”

23 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”

24 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”

25 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)

26 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 2001. SELECT count(*) FROM Offering WHERE cname = “ICOM 4036” AND cterm = “Fall 2001”

27 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.

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

29 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

30 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

31 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

32 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

33 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


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

Similar presentations


Ads by Google