SQL : Query Language CS3431.

Slides:



Advertisements
Similar presentations
Chapter 4 Joining Multiple Tables
Advertisements

พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Dec 4, 2003Murali Mani SQL B term 2004: lecture 14.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
Murali Mani SQL: Updates (DML) and Views (DDL). Murali Mani SQL DML (Updating the Data) Insert Delete Update.
SQL. 1.SQL is a high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
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)
Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update.
SQL SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Cs3431 Relational Algebra : #I Based on Chapter 2.4 & 5.1.
Nov 18, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
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.
Complex Queries (1) Product ( pname, price, category, maker)
Relational Algebra Instructor: Mohamed Eltabakh 1.
Chapter 9 Joining Data from Multiple Tables
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.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Relational Algebra - Chapter (7th ed )
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
ICS 321 Fall 2009 SQL: Queries, Constraints, Triggers Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow.
Week 10 Quiz 9 Answers Group 28 Christine Hallstrom Deena Phadnis.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
From Relational Algebra to SQL CS 157B Enrique Tang.
Relational Algebra MBAD 613 R. Nakatsu. Relational Data Manipulation Language Query-by-Example; Query-by-Form Transform-Oriented Languages Relational.
Advanced Relational Algebra & SQL (Part1 )
© D. Wong Normalization  Purpose: process to eliminate redundancy in relations due to functional or multi-valued dependencies.  Decompose relation.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
CMPT 258 Database Systems SQL Queries (Chapter 5).
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
1 SQL: The Query Language (Part II). 2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of.
Copyright © 2004 Pearson Education, Inc.. Chapter 6 The Relational Algebra and Relational Calculus.
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1 Part II.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1.
Chapter (6) The Relational Algebra and Relational Calculus Objectives
SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin
Chapter 4: Intermediate SQL Joins
SQL : Query Language Part II CS3431.
SQL Views CS542.
CS 405G: Introduction to Database Systems
The Relational Algebra and Relational Calculus
Relational Algebra : #I
Normalization Murali Mani.
Functional Dependencies and Normalization
SQL Views and Updates cs3431.
Instructor: Mohamed Eltabakh
SQL: Structured Query Language
More Complex Operators
Normalization cs3431.
Instructor: Mohamed Eltabakh
SQL: Structured Query Language
Chapter 19 (part 1) Functional Dependencies
SQL: Structured Query Language
SQL: Structured Query Language
Chapter 4 Relational Algebra
SQL: Structured Query Language
Presentation transcript:

SQL : Query Language CS3431

SELECT-FROM-WHERE  (sName=“Greg” AND address=“320 FL”) (Student) FROM Student WHERE sName=“Greg” AND address=“320 FL” Student sNumber sName address professor 1 Dave 311FL MM 2 Greg 320FL 3 Matt ER sNumber sName address professor 2 Greg 320FL MM  (sName=“Greg” AND address=“320 FL”) (Student) CS3431

Select-From Query SELECT sNumber, sName FROM Student address professor 1 Dave 320FL MM 2 Greg 3 Matt ER sNumber sName 1 Dave 2 Greg 3 Matt  (sNumber, sName) (Student) CS3431

Extended Projection SELECT sNumber || sName AS info FROM Student WHERE address=“320 FL” Student info 1Dave 2Greg 3Matt sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER  (sNumber||sNameinfo) ( (address=“320 FL”) (Student)) CS3431

SQL and Relational Algebra  L ( C (R)) becomes SELECT L FROM R WHERE C CS3431

Tuple Variables + Renaming SELECT S1.sNumber AS num FROM Student S1 WHERE S1.sNumber >= 1; Student num 1 2 3 sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER  (S1.sNumbernum) ( (S1.sNumber >= 1) ( S1 (Student))) CS3431

String Operators Comparison Operators based on lexicographic ordering: =, <, >, <>, >=, <= Concatenation operator: || Pattern match: s LIKE p p denotes a pattern % : any sequence of 0 or more characters - : matches 1 character Use two consecutive quotes (‘) to represent ‘ CS3431

String Matching Example SELECT s1.sNumber AS num FROM Student S1 WHERE s1.sName LIKE ‘Star_ _ _ _’ or S1.professor LIKE ‘Mad Cow’’s%’ ; CS3431

Set Operations in SQL (SELECT sName FROM Student) EXCEPT Operators : UNION, INTERSECT, and EXCEPT (SELECT sName FROM Student) EXCEPT (SELECT sName FROM Student WHERE address=‘320 FL’) For set semantics, use UNION, INTERSECT, EXCEPT. For bag semantics, use UNION ALL, INTERSECT ALL, EXCEPT ALL CS3431

EXCEPT - Example (SELECT sName FROM Student) EXCEPT WHERE address=‘320 FL’) Reminder : Set semantics ! Student sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER 4 300FL sName CS3431

Joins + SQL Multiple relations in FROM clause : SELECT * FROM Student, Professor CS3431

Cross (Cartesian) Product SELECT * FROM Student, Professor Student X Professor SELECT * FROM Student CROSS JOIN Professor; CS3431

Cross Product - Example Student Professor sNumber sName address professor 1 Dave 320FL 2 Greg 3 Matt pNumber pName address 1 MM 141FL 2 ER 201FL sNumber sName address professor pNumber pName 1 Dave 320FL MM 141FL 2 ER 201FL Greg 3 Matt CS3431

Theta Join SELECT * FROM Student, Professor WHERE professor=pNumber; Student ⋈(professor=pNumber) Professor SELECT * FROM Student JOIN Professor ON professor=pNumber; CS3431

Theta Join Example Student Professor sNumber sName address professor 1 Dave 320FL 2 Greg 3 Matt pNumber pName address 1 MM 141FL 2 ER 201FL sNumber sName address professor pNumber pName 1 Dave 320FL MM 141FL 2 Greg 3 Matt ER 201FL Student ⋈(professor=pNumber) Professor CS3431

Natural Join SELECT * FROM Student , Professor Reminder: Join columns must have same names in both relations SELECT * FROM Student , Professor WHERE Student.pnumber = Professor.pnumber ; Student ⋈ Professor SELECT * FROM Student NATURAL JOIN Professor CS3431

Natural Join - Example Student Professor Student ⋈ Professor sNumber sName address pNumber 1 Dave 320FL 2 Greg 3 Matt pNumber pName address 1 MM 141FL 2 ER 201FL sNumber sName address pNumber pName 1 Dave 320FL MM 141FL 2 Greg 3 Matt ER 201FL Student ⋈ Professor CS3431

Outer Joins SELECT * FROM Student NATURAL FULL OUTER JOIN Professor Student ⋈o Professor SELECT * FROM Student NATURAL LEFT OUTER JOIN Professor Student ⋈oL Professor SELECT * FROM Student NATURAL RIGHT OUTER JOIN Professor Student ⋈oR Professor CS3431

Outer Joins - Example Student Professor Student ⋈o Professor sNumber sName address pNumber 1 Dave 320FL 2 Greg 3 Matt 4 Ben pNumber pName address 1 MM 141FL 2 ER 201FL 3 MW 168FL sNumber sName address pNumber pName 1 Dave 320FL MM 141FL 2 Greg 3 Matt ER 201FL 4 Ben Null MW 168FL CS3431 Student ⋈o Professor

Putting it together: Joins+more SELECT sName FROM Student, Professor WHERE pName=‘MM’ AND pNumber=professor; (sName)(Student ⋈(pName=‘MM’ and pNumber=professor) Professor) CS3431

Joins - example SELECT sName FROM Student, Professor WHERE pName=‘MM’ sNumber sName address professor 1 Dave 320FL 2 Greg 3 Matt pNumber pName address 1 MM 141FL 2 ER 201FL SELECT sName FROM Student, Professor WHERE pName=‘MM’ AND pNumber=professor; sName Dave Greg CS3431