CS3431 SQL : Query Language. CS3431 SELECT-FROM-WHERE SELECT * FROM Student WHERE sName=“Greg” AND address=“320 FL”  (sName=“Greg” AND address=“320 FL”)

Slides:



Advertisements
Similar presentations
SQL Queries Principal form: SELECT desired attributes FROM tuple variables –– range over relations WHERE condition about tuple variables; Running example.
Advertisements

Winter 2002Arthur Keller – CS 1806–1 Schedule Today: Jan. 22 (T) u SQL Queries. u Read Sections Assignment 2 due. Jan. 24 (TH) u Subqueries, Grouping.
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
1 Today’s Class  Relational Model  SQL CS F212 Database Systems.
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 90 Database Systems I SQL Queries.
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.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
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.
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.
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.
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Advanced SQL Murat Kantarcioglu Adapted from Silberchatz et al. slides.
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.
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.
From Relational Algebra to SQL CS 157B Enrique Tang.
Databases 1 Second lecture.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 Part a The Relational Algebra and Relational Calculus Hours 1,2.
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.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
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.
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.
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1.
CSE202 Database Management Systems
More SQL: Complex Queries,
CS3220 Web and Internet Programming More SQL
CPSC-310 Database Systems
Introduction to SQL.
SQL The Query Language R & G - Chapter 5
CPSC-608 Database Systems
SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin
SQL : Query Language Part II CS3431.
SQL Views CS542.
CS 405G: Introduction to Database Systems
SQL : Query Language CS3431.
The Relational Algebra and Relational Calculus
Relational Algebra : #I
SQL Views and Updates cs3431.
Instructor: Mohamed Eltabakh
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL: Structured Query Language
CMPT 354: Database System I
SQL: Structured Query Language
CSC 453 Database Systems Lecture
SQL: Structured Query Language
SQL: Structured Query Language
SQL: Structured Query Language
SQL: Structured Query Language
Presentation transcript:

CS3431 SQL : Query Language

CS3431 SELECT-FROM-WHERE SELECT * FROM Student WHERE sName=“Greg” AND address=“320 FL”  (sName=“Greg” AND address=“320 FL”) (Student) sNumbersNameaddressprofessor 1Dave311FLMM 2Greg320FLMM 3Matt320FLER Student sNumbersNameaddressprofessor 2Greg320FLMM

CS3431 Select-From Query SELECT sNumber, sName FROM Student  (sNumber, sName) (Student) sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM 3Matt320FLER Student sNumbersName 1Dave 2Greg 3Matt

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

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;  (S1.sNumber  num) (  (S1.sNumber >= 1) (  S1 (Student))) sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM 3Matt320FLER Student num 1 2 3

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 Operators Comparison Operators based on lexicographic ordering: =,, <>, >=, <= Concatenation operator: || Use two consecutive quotes (‘) to represent ‘ Pattern match: s LIKE p p denotes a pattern % : any sequence of 0 or more characters - : matches 1 character Patterns can explicitly declare escape characters as: s LIKE ‘x%am%’ ESCAPE ‘x’

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

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

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

CS3431 EXCEPT - Example sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM 3Matt320FLER 4Matt300FLER Student (SELECT sName FROM Student) EXCEPT (SELECT sName FROM Student WHERE address=‘320 FL’) sNumbersNameaddressprofessor Reminder : Set semantics !

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

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

CS3431 Cross Product - Example sNumbersNameaddressprofessor 1Dave320FL1 2Greg320FL1 3Matt320FL2 Student pNumberpNameaddress 1MM141FL 2ER201FL Professor sNumbersNameaddressprofessorpNumberpNameaddress 1Dave320FL11MM141FL 1Dave320FL12ER201FL 2Greg320FL11MM141FL 2Greg320FL12ER201FL 3Matt320FL21MM141FL 3Matt320FL22ER201FL

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

CS3431 Theta Join Example sNumbersNameaddressprofessorpNumberpNameaddress 1Dave320FL11MM141FL 2Greg320FL11MM141FL 3Matt320FL22ER201FL sNumbersNameaddressprofessor 1Dave320FL1 2Greg320FL1 3Matt320FL2 Student pNumberpNameaddress 1MM141FL 2ER201FL Professor Student ⋈ (professor=pNumber) Professor

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

CS3431 Natural Join - Example sNumbersNameaddresspNumber 1Dave320FL1 2Greg320FL1 3Matt320FL2 Student pNumberpNameaddress 1MM141FL 2ER201FL Professor sNumbersNameaddresspNumberpNameaddress 1Dave320FL1MM141FL 2Greg320FL1MM141FL 3Matt320FL2ER201FL 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 ⋈ o L Professor SELECT * FROM Student NATURAL RIGHT OUTER JOIN Professor Student ⋈ o R Professor

CS3431 Outer Joins - Example sNumbersNameaddresspNumber 1Dave320FL1 2Greg320FL1 3Matt320FL2 4Ben320FL4 Student pNumberpNameaddress 1MM141FL 2ER201FL 3MW168FL Professor sNumbersNameaddresspNumberpNameaddress 1Dave320FL1MM141FL 2Greg320FL1MM141FL 3Matt320FL2ER201FL 4Ben320FL4Null 3MW168FL Student ⋈ o Professor

CS3431 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 sNumbersNameaddressprofessor 1Dave320FL1 2Greg320FL1 3Matt320FL2 Student pNumberpNameaddress 1MM141FL 2ER201FL Professor sName Dave Greg SELECT sName FROM Student, Professor WHERE pName=‘MM’ AND pNumber=professor;