SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos & A. Pavlo Lecture#6: Rel. model - SQL part1 (R&G, chapter.
Structured Query Language.  Basic queries  Set operations  Aggregate operators  Null values  Joins.
7 7 Chapter 7 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 7th Edition Peter Rob & Carlos Coronel.
Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.
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.
Introduction to Structured Query Language (SQL)
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (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.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Chapter 3 Single-Table Queries
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation An Introduction to XQuery.
Relational Query Languages. Languages of DBMS  Data Definition Language DDL  define the schema and storage stored in a Data Dictionary  Data Manipulation.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
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:
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.
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.
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
Structured Query Language
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
Advanced Relational Algebra & SQL (Part1 )
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.
Restrictions Objectives of the Lecture : To consider the algebraic Restrict operator; To consider the Restrict operator and its comparators in SQL.
Query Processing – Implementing Set Operations and Joins Chap. 19.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
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.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Structured Query Language (SQL) DDL
CSE202 Database Management Systems
Database Systems Chapter 6
slides by: Pavle Morgan & Hui Ma
Chapter (6) The Relational Algebra and Relational Calculus Objectives
Structured Query Language (SQL) DML
More SQL: Complex Queries,
Plan for Intro to DB Systems I
Chapter 3 Introduction to SQL(2)
COMP3017 Advanced Databases
CS580 Advanced Database Topics
Database Systems SQL cont. Relational algebra
Introduction to SQL.
Retrieval Queries in SQL(DML)
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
Basic SQL Lecture 6 Fall
CS 405G: Introduction to Database Systems
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
The Relational Algebra and Relational Calculus
SQL: The Query Language Part 1
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Chapter 7 Introduction to Structured Query Language (SQL)
CS4222 Principles of Database System
CMPT 354: Database System I
Contents Preface I Introduction Lesson Objectives I-2
CSC 453 Database Systems Lecture
SQL: Set Operations & Nested Queries. Presented by: Dr. Samir Tartir
CSC 453 Database Systems Lecture
Presentation transcript:

SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin

Plan for the SQL DML Topic Data Manipulation Language Single table queries Multiple table queries Nested queries Reading: Chapter 4 Chapter 5 PostgreSQL Manual

Queries in SQL SELECT is the basic SQL statement for retrieving data from a database SELECT [ DISTINCT ] 〈attribute_list〉 FROM 〈table_list〉 [ WHERE 〈condition〉 ] SELECT statement has nothing to do with relational algebra select operation. 〈attribute_list〉 defines relational algebra projection 〈condition〉 contains conditional expression of relational algebra select operation

Queries in SQL 〈attribute_list〉= attributes whose values will be retrieved by query ( “∗” denotes all table attributes) e.g. FName, LName, CName, Grade 〈table_list〉= relations needed to process the query (tables containing attributes from 〈attribute_list〉 must be included), e.g. Student, Course, Grades 〈condition〉 is a Boolean expression defining the properties of the tuples to be retrieved, and join conditions (optional clause) e.g. StudId = 007007 SQL considers a relation to be a multiset (or bag) of tuples, not a set ⇒ allows duplicates! SELECT statement has nothing to do with relational algebra select operation. 〈attribute_list〉 defines relational algebra projection 〈condition〉 contains conditional expression of relational algebra select operation

Conditional Expression (Reference) Assume   { =, <, <=, >, >=, < > }, A and B attributes or function of attributes, ai dom (A ), i = 1,… Conditional expression of the WHERE clause can be any plausible combination of the following: [(A  a] [A  B ] [A IS [ NOT ] NULL] [A [ NOT ] BETWEEN a1 AND a2 ] [A [ NOT ] LIKE 〈pattern〉 ] (string matching ) [A [NOT] SIMILAR TO 〈regular expression〉 ] [A [ NOT ] IN 〈value_list〉] [A  ANY 〈value_list〉] [A  SOME 〈value_list〉] [A  ALL 〈value_list〉] [(EXISTS | NOT EXISTS) 〈sub query〉]

The University Database Student LName FName StudId Major Smith Susan 131313 Comp Bond James 007007 Math 555555 Cecil John 010101 Grades StudId CourId Grade 007007 C302 A+ 555555  C301 A M214 131313 C201 B- C 010101 Course CName CourId Points Dept DB Sys C302 15 Comp SofEng C301 DisMat M214 22 Math Pr&Sys C201 Here, we suppose that every student can have at most one grade for any course. That means that only pass grades are recorded in the database, and non pass grades are forgotten. Otherwise, {StudId, CourId } wouldn't be a key. A special symbol  is used to denote an unknown value, so called null value. In tutorial, discuss the options: a student can have more than one grade for a course, but all hers/his grades for the same course are different, a student can have more than one grade for a course, and more than one grade can be same for the same course.

Single Table Queries Retrieve the first and last names of Comp students SELECT FName, LName FROM Student WHERE Major = 'Comp'; Find all different grades SELECT DISTINCT Grade FROM Grades ; FName LName Susan Smith Grade A+ A B- C 

Substring Comparisons Can match to SQL patterns: 〈string〉 LIKE 〈pattern〉 '%' to replace an arbitrary number of characters, and ‘ _ ‘ to replace exactly one character. Can extract part of a string with the function substring(<string>, <start pos>, <length>) e.g. Retrieve course names of all 300 level courses (have '3' as the second character in CourId) SELECT CName FROM Course WHERE CourId LIKE '_3%'; or SELECT CName FROM Course WHERE substring(CourId, 2,1) = ’3’; PName DB Sys SofEng

Arithmetic Operations, Sorting SQL provides capability to perform four basic arithmetic operations (+, -, *, / ) that can be applied to numeric attributes and constants only SELECT 2 + 2; Sorting of the query result tuples is done using ORDER BY {〈attribute_name〉 [(ASC | DESC)], …} clause after the WHERE clause (ASC is default) SELECT * FROM Grades ORDER BY StudId ASC, CourId DESC;

Qualification and Aliasing Attributes in different relation schemas can have the same names. How do we prevent ambiguity? In the SELECT clause, we prefix attributes by table name: SELECT Student.StudId … To change name of an attribute in the result, alias the attribute name using AS : SELECT CourId AS CourseId In the FROM clause, specify a tuple variable from the table: FROM Course c, Grades g, Student s In the WHERE clause, prefix an attribute by the tuple variable: WHERE c.CourId = g.CourId

Multiple Table Queries and Joins To retrieve data from more than one table, we need a new operation: JOIN There are different joins: INNER (theta join, equi join, natural join) OUTER (left, right, full) Most often, we use the equi-join Each join operation is based on concatenating those tuples from two relations, which satisfy the join condition An equi-join concatenates tuples with equal join attribute values An equi-join is most frequently based on a (FK, PK) pair

SELECT * FROM t1, t2 WHERE t1.B = t2.B ; A JOIN Example SELECT * FROM t1, t2 WHERE t1.B = t2.B ; Join Condition t1 t2 t1 EQUI-JOIN t2 A B C 1 b1 c1 2 b2 3 b3 4 b4 c2 5 

Multiple Table Queries Retrieve course names with grades, and the surname for student James SELECT CName, Grade, LName AS Surname, FROM Student s, Grades g, Course c WHERE FName = 'James' AND s.StudId = g.StudId AND c.CourId = g.CourId ; Conditional expression is blue, Join condition is red CName Grade Surname DB Sys A+ Bond SofEng A DisMat Pr&Sys

Set Theoretic Operations SQL has directly implemented set operations UNION, EXCEPT (difference), and INTERSECT on union compatible relations (same attributes, in the same order) e.g. Retrieve student ids of the students that didn't enroll M214 (SELECT StudId FROM Student) EXCEPT (SELECT StudId FROM Grades WHERE CourId = 'M214'); StudId 131313 555555 010101 Student that didn't enroll are not recorded in the Grades table

Nested Queries Some queries require comparing a tuple to a collection of tuples (eg, students doing courses that have more than 100 students) This task can be accomplished by embedding a SQL query into WHERE clause of another query The embedded query is called nested query, The query containing the nested query is called outer query The comparison is made by using IN,  ANY,  SOME, and  ALL operators, where { =, <, <=, >=, >, < > } Note: IN  =ANY and IN  =SOME SQL allows multiple nesting

Example Nested Query Retrieve first names of students that sat M214 SELECT FName FROM Student s WHERE s.StudId IN (SELECT StudId FROM Grades WHERE CourId = 'M214' AND Grade IS NOT NULL); A nested query defined by using IN (or =ANY) operator can be (often but not always) expressed as a single block query SELECT FName FROM Student s, Grades g WHERE s.StudId = g.StudId AND g.CourId = 'M214' AND g.Grade IS NOT NULL; FName James Nested Query To evaluate the query: Retrieve first names of students that passed M214 SQL firstly computes the set of id's of those students that passed M214, and then compares every student id from Student table. If the comparison evaluates true, the student's name is included in the answer.

Correlated Nested Queries Let the variable s contain the current tuple of the outer query If the nested query doesn’t refer to s (in its WHERE clause): The nested query computes the same result for each tuple in s The outer query and the nested query are said to be uncorrelated If a condition in the WHERE clause of the nested query refers to some attribute of a relation declared in the outer query, the two queries are said to be correlated Have to compute the inner query for each tuple considered by the outer query Correlated nested queries consume more computer time than uncorrelated ones

Correlated Nested Query Retrieve id's and surnames of those students that sat at least one course SELECT s.StudId, FName FROM Student s WHERE s.StudId IN (SELECT StudId FROM Grades WHERE s.StudId = StudId AND Grade IS NOT NULL); When s.StudId = 131313, ⇒ result of the nested query is {131313, 131313}, ⇒ (131313, Susan) is in the final result When s.StudId = 010101, ⇒ result of the nested query is { }, ⇒ (010101, John) is NOT in the final result Evaluation of the correlated nested query is done in the following way: for every s.StudId value, SQL computes the multiset of StudentId values, and if that multiset is not empty, student data is included into query result. Obviously, the result will not contain freshman data.

Correlated Nested Query Again, the nested query can be expressed as a single block query: SELECT DISTINCT s.StudId, s.LName FROM Student s, Grades g WHERE s.StudId = g.StudId AND Grade IS NOT NULL; Have to be careful of duplicates! Have to be careful, not all nested queries can be transformed into single block queries Equi-join condition This query retrieves students that passed at least one exam.

EXISTS and NOT EXISTS Retrieve Id‘s and surnames of students who sat at least one course exam: SELECT s.StudId, LName FROM Student s WHERE EXISTS (SELECT * FROM Grades WHERE s.StudId = StudId AND Grade IS NOT NULL); Retrieve Id's and surnames of students who didn't sat exam of any course: SELECT s.StudId, LName FROM Student s WHERE NOT EXISTS (SELECT * FROM Grades WHERE s.StudId = StudId AND Grade IS NOT NULL ); EXIST and NOT EXIST are used in conjunction with correlated nested queries. So, for every tuple of the outer query, there is a specific set of tuples computed in the nested query. Specific means that the nested query is computed by placing the values of the current outer query tuple into the WHERE clause of the nested query. EXIST (Q), where Q is the nested query, evaluates true if there is at least one tuple returned by Q. NOT EXIST (Q) evaluates true if there is no tuples returned by Q. In other words, EXIST checks whether nested query returns a non empty set, and NOT EXIST checks whether nested query returns an empty set.

Summary SQL query language Next lecture, SQL advanced options: Queries against a single table Queries against multiple tables Substring comparisons Arithmetic operations Sorting Nested queries (outer and inner - nested query) Correlated nested queries Next lecture, SQL advanced options: Joined tables, Aggregate functions Grouping Having