Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

Union, Intersection, Difference (subquery) UNION (subquery) produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
1 Lecture 12: Further relational algebra, further SQL
E-R Diagram for a Banking Enterprise
1 Database Systems Relations as Bags Grouping and Aggregation Database Modification.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 90 Database Systems I SQL Queries.
Dec 4, 2003Murali Mani SQL B term 2004: lecture 14.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
CS3431 SQL : Query Language. CS3431 SELECT-FROM-WHERE SELECT * FROM Student WHERE sName=“Greg” AND address=“320 FL”  (sName=“Greg” AND address=“320 FL”)
Murali Mani SQL. Murali Mani SELECT-FROM-WHERE SELECT * FROM Student WHERE sName=“Greg” AND address=“320 FL”  (sName=“Greg” AND address=“320 FL”) (Student)
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
SQL I. SQL – Introduction  Standard DML/DDL for relational DB’s  DML = “Data Manipulation Language” (queries, updates)  DDL = “Data Definition Language”
Nov 18, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
Murali Mani Relational Algebra. Murali Mani What is Relational Algebra? Defines operations (data retrieval) for relational model SQL’s DML (Data Manipulation.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Chapter 3 Single-Table Queries
Advanced SQL Murat Kantarcioglu Adapted from Silberchatz et al. slides.
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.
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.
NULLs & Outer Joins Objectives of the Lecture : To consider the use of NULLs in SQL. To consider Outer Join Operations, and their implementation in SQL.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
NULL VALUES CHAPTER 5 (6/E) CHAPTER 8 (5/E) 1. LECTURE OUTLINE  Dealing with null values Three-valued logic Effects in WHERE clauses IS NULL Effects.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
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.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
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.
Thinking in Sets and SQL Query Logical Processing.
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1 Part II.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Lecture # 10 July 10,2012 More SQL: Complex Queries, Triggers,
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1.
More SQL: Complex Queries,
CS3220 Web and Internet Programming More SQL
Slides are reused by the approval of Jeffrey Ullman’s
Introduction to SQL.
6/22/2018.
SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin
SQL Structured Query Language 11/9/2018 Introduction to Databases.
SQL : Query Language Part II CS3431.
CS 405G: Introduction to Database Systems
SQL : Query Language CS3431.
The Relational Algebra and Relational Calculus
Instructor: Mohamed Eltabakh
SQL – Entire Select.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CSCE 315 – Programming Studio Spring 2010 Project 1, Lecture 4
SQL: Structured Query Language
Contents Preface I Introduction Lesson Objectives I-2
Query Functions.
CSC 453 Database Systems Lecture
SQL: Structured Query Language
SQL: Structured Query Language
SQL: Structured Query Language
SQL: Structured Query Language
Presentation transcript:

Nov 24, 2003Murali Mani SQL B term 2004: lecture 12

Nov 24, 2003Murali Mani SELECT-FROM-WHERE SELECT * FROM Student WHERE sName=“Greg” AND address=“320 FL”  (sName=“Greg” AND address=“320 FL”) (Student)

Nov 24, 2003Murali Mani Project SELECT sNumber, sName FROM Student  (sNumber, sName) (Student)

Nov 24, 2003Murali Mani Extended Projection SELECT sNumber || sName AS info FROM Student WHERE address=“320 FL”  (sNumber||sName  info) (  (address=‘320 FL’) (Student)) In short,  L (  C (R)) becomes SELECT L FROM R WHERE C

Nov 24, 2003Murali Mani Renaming SELECT s1.sNumber as num FROM Student S1 WHERE s1.sNumber >= 1;  (s1.sNumber  num) (  (s1.sNumber >= 1) (  S1 (Student)))

Nov 24, 2003Murali Mani String comparison operators =,, <>, >=, <=: Based on lexicographic ordering. Concatenation operator: || ‘ represented in strings with two consecutive ‘ Pattern match: s LIKE p p = pattern % : any sequence of 0 or more characters - : matches 1 character Patterns can explicitly declare escape characters as: s LIKE ‘x%am%’ ESCAPE ‘x’

Nov 24, 2003Murali Mani Comparison with NULL values Arithmetic operations on NULL return NULL. Comparison operators on NULL return UNKNOWN. We can explicitly check whether a value is null or not, by IS NULL, IS NOT NULL.

Nov 24, 2003Murali Mani Truth table with UNKNOWN UNKNOWN AND TRUE = UNKNOWN UNKNOWN OR TRUE = TRUE UNKNOWN AND FALSE = FALSE UNKNOWN OR FALSE = UNKNOWN UNKNOWN AND UNKNOWN = UNKNOWN UNKNOWN OR UNKNOWN = UNKNOWN NOT UNKNOWN = UNKNOWN A WHERE clause is satisfied only when it evaluates to TRUE.

Nov 24, 2003Murali Mani UNION, INTERSECT, EXCEPT (SELECT sName FROM Student) EXCEPT (SELECT sName FROM Student WHERE address=‘320 FL’) UNION, INTERSECT, EXCEPT have set sematics. For bag semantics, use UNION ALL, INTERSECT ALL, EXCEPT ALL

Nov 24, 2003Murali Mani Joins SELECT sName FROM Student, Professor WHERE pName=‘MM’ AND pName=professor;  (sName) (Student ⋈ (pName=‘MM’ and pname=professor) Professor)

Nov 24, 2003Murali Mani Cross Product (Cartesian Product) SELECT * FROM Student CROSS JOIN Professor; Student X Professor can also be written as: SELECT sNumber, sName, pNumber, pName FROM Student, Professor

Nov 24, 2003Murali Mani Theta Join SELECT * FROM Student JOIN Professor ON professor=pname; Student ⋈ (professor=pname) Professor SELECT sNumber, sName, pNumber, pName FROM Student, Professor WHERE professor=pName;

Nov 24, 2003Murali Mani Natural Join SELECT * FROM Student NATURAL JOIN Professor Student ⋈ Professor

Nov 24, 2003Murali Mani Outer Joins SELECT * FROM Student NATURAL FULL OUTER JOIN Professor Student ⋈ o Professor SELECT * FROM Student NATURAL LEFT OUTER JOIN Professor Student ⋈ o L Professor

Nov 24, 2003Murali Mani Outer Joins SELECT * FROM Student NATURAL RIGHT OUTER JOIN Professor Student ⋈ o R Professor

Nov 24, 2003Murali Mani Sorting: ORDER BY clause SELECT * FROM Student WHERE sNumber >= 1 ORDER BY sNumber, sName  (sNumber, sName) (  (sNumber >= 1) (Student))

Nov 24, 2003Murali Mani Subqueries SELECT * FROM Student WHERE professor = (SELECT pname FROM Professor WHERE pnumber=1) Note: the inner subquery returns a relation, but SQL runtime ensures that the subquery returns a relation with one column and with one row, otherwise it is a run-time error.

Nov 24, 2003Murali Mani Subqueries We can use IN, EXISTS, ALL, ANY can be used with comparisons SELECT * FROM Student WHERE (sNumber, professor) IN (SELECT pNumber, pName FROM Professor)

Nov 24, 2003Murali Mani Subqueries SELECT * FROM Student WHERE sNumber > ALL (SELECT pNumber FROM Professor)

Nov 24, 2003Murali Mani Subqueries SELECT * FROM Student WHERE EXISTS (SELECT pname FROM Professor WHERE Student.professor=pName)

Nov 24, 2003Murali Mani Subqueries with negation SELECT * FROM Student WHERE (sNumber, professor) NOT IN (SELECT pNumber, pName FROM Professor) For ALL, ANY, EXISTS, we can do SELECT * FROM Student WHERE NOT sNumber > ALL (SELECT pNumber FROM Professor)

Nov 24, 2003Murali Mani Subqueries in FROM clause SELECT sName, pName FROM Student, (SELECT * FROM Professor WHERE pNumber=1) WHERE professor=pName;

Nov 24, 2003Murali Mani Duplicate Elimination SELECT DISTINCT * FROM Student;  (Student) SELECT DISTINCT address FROM Student WHERE sNumber >= 1;

Nov 24, 2003Murali Mani Aggregation SELECT COUNT (*) FROM Student; SELECT COUNT (sNumber) FROM Student; SELECT MIN (sNumber) FROM Student; SELECT MAX (sNumber) FROM Student; SELECT SUM (sNumber) FROM Student; SELECT AVG (sNumber) FROM Student; We can have distinct such as: SELECT COUNT (DISTINCT sNumber) FROM Student

Nov 24, 2003Murali Mani Grouping SELECT COUNT (sName) FROM Student GROUP BY address;  (COUNT (sName)) (  (address, COUNT (sName)) (Student)) SELECT COUNT (sName) FROM Student GROUP BY address HAVING COUNT (sNumber) >= 1;

Nov 24, 2003Murali Mani Aggregation and NULLs NULLs are ignored in any aggregation; except COUNT (*) However if the set of attributes to be grouped on has null values, then grouping is done on the null values as well.

Nov 24, 2003Murali Mani SQL Queries - Summary SELECT [DISTINCT] a1, a2, …, an FROM R1, R2, …, Rm [WHERE C1] [GROUP BY g1, g2, …, gl [HAVING C2]] [ORDER BY o1, o2, …, oj]