603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 20 A First Course in Database Systems.

Slides:



Advertisements
Similar presentations
Fundamentals of Database Systems Fourth Edition El Masri & Navathe
Advertisements

SQL DESIGN AND IMPLEMENTATION CONTENT SOURCES: ELAMSARI AND NAVATHE, FUNDAMENTALS OF DATABASE MANAGEMENT SYSTEMSELAMSARI AND NAVATHE, FUNDAMENTALS OF.
COMPANY schema EMPLOYEE
Lecture 1 Relational Algebra and Relational Calculus.
Basic Queries. 2 Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
SQL Query Slides Sharif University Of Technology Database Systems CE 384 Prepared By: Babak Bagheri Hariri
Your Logo Fundamentals of Database Systems Fourth Edition El Masri & Navathe Instructor: Mr. Ahmed Al Astal Chapter 8 (Cont.) SQL-99: Schema Definition,
Displaying Data from Multiple Tables. EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS
Data Definition Languages Atif Farid Mohammad UNCC.
FEN More about SELECT, Nested selects GROUP BY, HAVING, ORDER BY Other joins Aggregate functions Views More about SQL.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
1 CSE 480: Database Systems Lecture 10: SQL - DML Reference: Read Chapter 4 of the textbook.
METU Department of Computer Eng Ceng 302 Introduction to DBMS SQL-99: Schema Definition, Basic Constraints, and Queries by Pinar Senkul resources: mostly.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
CS 104 Introduction to Computer Science and Graphics Problems Introduction to Database (2) Basic SQL 12/05/2008 Yang Song.
관계 연산자 & SQL. Selection SELECT * FROM r WHERE A=B AND D>5.
SQL The relational DB Standard CS-450 Dr. Ali Obaidi.
Al-Imam University Girls Education Center Collage of Computer Science 1 ST Semester, 1432/1433H Chapter 8 Part 4 SQL-99 Schema Definition, Constraints,
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
FEN  Queries: SELECT  Data Manipulation: INSERT, UPDATE, DELETE SQL: Structured Query Language – Part 2.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
1 CSE 480: Database Systems Lecture 11: SQL. 2 SQL Query SELECT FROM WHERE –In MySQL, FROM and WHERE clauses are optional –Example:
SQL: Schema Definition, Constraints, & Queries and Views.
Relational Algebra - Chapter (7th ed )
Query Processing and Optimization
CS 380 Introduction to Database Systems (Chapter 8: SQL-99: Schema Definition, Constraints, Queries, and Views)
 Employee (fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno)  Department (dname, dnumber, mgrssn, mgrstartdate) 
Database Management Systems. NESTING OF QUERIES  Some queries require that existing values in the database be retrieved and then used in a comparison.
Announcements Written Homework 1 due Friday –If you have fourth edition make sure you do the right problems Program 3 out today, due next Friday Nov 10.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 17 A First Course in Database Systems.
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.
NOEA/IT - FEN: Databases/SQL1 SQL – part 1 SQL: DDL and DML.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
Structured Query Language
Structured Query Language (2) The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems,
DatabaseIM ISU1 Fundamentals of Database Systems Chapter 6 The Relational Algebra.
Announcements Written Homework 1 due Nov 2 –See course web page –Exercises 5.12, 5.15, 6.17, 6.20, 6.22 (a,c,f only). Today –SQL (chapter 8) –Instructional.
Faculty of Information Science and Technology Mahanakorn University of Technology Topic 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
Chapter 11 SQL DML. Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 10 A First Course in Database Systems.
Announcements Written Homework 1 due Nov 2 –See course web page –Exercises 5.12, 5.15, 6.17, 6.20, 6.22 (a,c,f only). Today –continue with SQL (chapter.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Chapter SQL DML. Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 11 A First Course in Database Systems.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 9 A First Course in Database Systems.
FEN Introduction to the database field:  SQL: Structured Query Language Seminar: Introduction to relational databases.
COMP163 Database Management Systems September 18, 2008 Lecture 8 – Sections SQL Queries.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 18 A First Course in Database Systems.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 16 A First Course in Database Systems.
ISC321 Database Systems I Chapter 5: SQL: Advanced Queries, Assertions, Triggers, and Views Fall 2015 Dr. Abdullah Almutairi.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 (SQL Basics) Lecture # 9 July 7,2012.
1 COMP 1100 Basic SQL David J. Stucki. Outline SQL Overview Retrievals Schema creation Table creation Constraints Inserts Updates 2.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus تنبيه : شرائح العرض.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Lecture # 10 July 10,2012 More SQL: Complex Queries, Triggers,
CS580 Advanced Database Topics Chapter 8 SQL Irena Pevac.
CS580 Advanced Database Topics
SQL: SchemaDefinition, Constraints, and Queries and Views
6/22/2018.
Retrieval Queries in SQL(DML)
11/9/2018.
Session - 6 Sequence - 2 SQL: The Structured Query Language:
SQL-99: Schema Definition, Constraints, and Queries and Views
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
SQL: Set Operations & Nested Queries. Presented by: Dr. Samir Tartir
Presentation transcript:

603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 20 A First Course in Database Systems

SQL Selection in SQL: The selection operator of relational algebra, and much more, is available through the WHERE clause in SQL. SELECTL FROMR WHEREC   L (  C (R))

SQL SELECTL FROMR WHEREC   L (  C (R)) We start with the relation in the FROM clause, apply to each tuple whatever condition is indicated in the WHERE clause, and then project onto the list of attributes and / or expressions in the SELECT clause.

SQL The following query asks for all the movies made after 1970 that are in black-and-white. SELECTtitle FROMmovie WHEREyear > 1970 AND NOT inColor ; In this condition, we have the AND of two booleans. The first is an ordinary comparison, but the second is the attribute inColor, negated.

SQL Consider the Query: SELECTtitle FROMMovie WHERE(year OR length < 90) AND studioName = ‘MGM’ ; The parentheses are needed here because the precedence of logical operators in SQL is the same as in most other languages: AND takes precedence over OR, and NOT takes precedence over both.

SQL Retrieve the name and address of all employees who work in the ‘Research’ department. SELECTFNAME, LNAME, ADDRESS FROMEMPLOYEE, DEPARTMENT WHEREDNAME = ‘Research’ AND DNUMBER = DNO ; FNAMELNAMEADDRESS JohnSmith731 Fondren, Houston, Tx FranklinWong638 Voss, Houston, Tx RameshNarayan975 Fire Oak, Humble, Tx JoyceEnglish5631 Rice, Houston, Tx

SQL For every project located in ‘Stafford’, list the project number, the controlling department number, and the department managers’ last name, address, and birthdate. SELECTPNUMBER, DNUM, LNAME, ADDRESS, BDATE FROM PROJECT, DEPARTMENT, EMPLOYEE WHEREDNUM=DNUMBER AND MGRSSN=SSN AND PLOCATION=‘Stafford’ ;

SQL SELECTPNUMBER, DNUM, LNAME, ADDRESS, BDATE FROM PROJECT, DEPARTMENT, EMPLOYEE WHEREDNUM=DNUMBER AND MGRSSN=SSN AND PLOCATION=‘Stafford’ ; The JOIN condition DNUM=DNUMBER relates a project to its controlling department

SQL SELECTPNUMBER, DNUM, LNAME, ADDRESS, BDATE FROM PROJECT, DEPARTMENT, EMPLOYEE WHEREDNUM=DNUMBER AND MGRSSN=SSN AND PLOCATION=‘Stafford’ ; The JOIN condition MGRSSN=SSN relates the controlling department to the employee who manages that department

The Database Language SQL Dealing With Ambiguous Attribute Names and Aliasing: In SQL the same name can be used for two (or more) attributes ==> attributes must be in different relations Given that this happens and a query refers to two or more attributes with the same name ==> we must qualify the attribute with the relation name to prevent ambiguity

SQL Language Dealing With Ambiguous Attribute Names and Aliasing: To qualify the attribute name with the relation name prefix the relation name to the attribute name and separate the two by a period. Ex. COMPANY Schema - DNO and LNAME of EMPLOYEE relation were called DNUMBER and NAME and DNAME attribute of DEPARTMENT was called NAME.

SQL Language Ex. COMPANY Schema - DNO and LNAME of EMPLOYEE relation were called DNUMBER and NAME and DNAME attribute of DEPARTMENT was called NAME. SELECTFNAME, EMPLOYEE.NAME, ADDRESS FROMEMPLOYEE, DEPARTMENT WHEREDEPARTMENT.NAME=‘Research’ AND DEPARTMENT.DNUMBER=EMPLOYEE.DNUMBER ;

SQL Ambiquity also arises when queries refer to the same relation twice!! Ex. For each employee, retrieve the employee’s first name and last name and the first and last name of his or her immediate supervisor. SELECTE.FNAME, E.LNAME, S.FNAME, S.LNAME FROMEMPLOYEE E EMPLOYEE S WHEREE.SUPERSSN = S.SSN E & S ==> aliases for EMPLOYEE relation

SQL Unspecified WHERE clauses and use of ‘*’: A missing WHERE clause indicates no condition on tuple selection, hence, all tuples of the relation specified in the FROM clause qualify to be selected for query result A missing WHERE clause  condition WHERE TRUE  for every row in the tuple

SQL Query: Select all EMPLOYEE SSNs. SELECTSSN FROMEMPLOYEE SSN  for all tuples in EMPLOYEE

SQL Select all combinations of an EMPLOYEE SSN and a Department DNAME. SELECTSSN, DNAME FROMEMPLOYEE, DEPARTMENT result is the Cartesian Crossproduct

SQL Retrieve all attribute values of EMPLOYEE tuples who work in DEPARTMENT number 5. SELECT* FROMEMPLOYEE WHEREDNO=5 ; RESULT => all attributes for employees of DNO=5.

SQL Retrieve all the attributes of an EMPLOYEE and the attributes of the DEPARTMENT he or she works in for every employee of the ‘Research’ department. SELECT* FROMEMPLOYEE, DEPARTMENT WHEREDNAME=‘Research’ AND DNO=DNUMBER ;

SQL Specify the Cross Product of the EMPLOYEE and DEPARTMENT relations. SELECT* FROMEMPLOYEE, DEPARTMENT

SQL Tables as Sets in SQL: In general SQL does not treat a relation as a set ==> duplicate tuples can appear more than once or in the result of a query

SQL SQL does not automatically eliminate duplicate tuples in the results of queries for the following reasons: *Duplicate elimination is an expensive operation. One way to implement it is to sort the tuples first and then eliminate them. *The user may want to see duplicate tuples in the result of a query. *When an aggregate function is applied to tuples, in most cases we don’t want to eliminate duplicates.

SQL If you want to eliminate duplicates of tuples from the result of a SQL query use the keyword DISTINCT in the SELECT clause ==> distinct tuples should remain in the result ==> a relation that is a set of tuples

SQL If we are interested only in distinct salary values (each value appearing once) use keyword DISTINCT: SELECTDISTINCT SALARY FROMEMPLOYEE SALARY

SQL Next Lecture MORE SQL