--The SQL Query Language DML--1 LIKE  LIKE allows to select character strings which have some element in common by using wild cards:  Wild cards:  “%”

Slides:



Advertisements
Similar presentations
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Advertisements

TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
CSC271 Database Systems Lecture # 11.
SQL CSET 3300.
COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
SQL (2).
M ATH IN SQL. 222 A GGREGATION O PERATORS Operators on sets of tuples. Significant extension of relational algebra. SUM ( [DISTINCT] A): the sum of all.
1 Today’s Class  Relational Model  SQL CS F212 Database Systems.
E-R Diagram for a Banking Enterprise
Information Resources Management February 27, 2001.
Subqueries Example Find the name of the producer of ‘Star Wars’.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
--The SQL Query Language DML--1 The SQL Query Language DML The SQL Query Language DML (SELECT)
The SQL Query Language DML1 The SQL Query Language DML Odds and Ends.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
SQL By: Toan Nguyen. Download Download the software at During the installation –Skip sign up for fast installation.
Microsoft Access 2010 Chapter 7 Using SQL.
SQL Operations Aggregate Functions Having Clause Database Access Layer A2 Teacher Up skilling LECTURE 5.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
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
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
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.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
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.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
IST 210 SQL Todd Bacastow IST 210: Organization of Data.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
SQL Aggregation Oracle and ANSI Standard SQL Lecture 9.
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
1 SQL: The Query Language (Part II). 2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
IST 210 More SQL Todd Bacastow IST 210: Organization of Data.
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.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1 Part II.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
More SQL: Complex Queries,
Slides are reused by the approval of Jeffrey Ullman’s
Structured Query Language (Data Manipulation Language)
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
Chapter 3 Introduction to SQL(3)
SQL Structured Query Language 11/9/2018 Introduction to Databases.
CS 405G: Introduction to Database Systems
IST 210: Organization of Data
SQL – Entire Select.
Chapter 4 Summary Query.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CS4222 Principles of Database System
Section 4 - Sorting/Functions
Projecting output in MySql
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
Shelly Cashman: Microsoft Access 2016
Instructor: Zhe He Department of Computer Science
Presentation transcript:

--The SQL Query Language DML--1 LIKE  LIKE allows to select character strings which have some element in common by using wild cards:  Wild cards:  “%” means “zero, one or more characters”  “_” means “one single character”

--The SQL Query Language DML--2 SELECT: Queries Over Several Tables  Which films name ends in “100”? SELECT Title FROM Film WHERE Title LIKE ‘%100’  Which films name contains the character a, b and c in the first, third and fifth positions? SELECT Title FROM Film WHERE Title LIKE ‘a_b_c%’

--The SQL Query Language DML--3 Correlated Subqueries  Subquery must be re-evaluated for each tuple in outer SELECT  Table variable is used  Find the customers who live at more than one address (assume the key of Customer is (CustomerID, Address)). SELECT Name FROM Customer C WHERE CustomerID IN (SELECT D.CustomerID FROM Customer D WHERE C.Address <> D.Address)

--The SQL Query Language DML--4 Multiple Attribute Producing Subquery  The subquery produces a table with several columns  EXISTS  true if subquery produced table has a tuple  NOT EXISTS  true if subquery produced table is empty

--The SQL Query Language DML--5 EXISTS  List the customers who live in Dublin and have a film reserved. SELECT Name FROM Customer WHERE City = ’Dublin’ AND EXISTS ( SELECT * FROM Reserved WHERE Reserved.CustomerID = Customer.CustomerID)

--The SQL Query Language DML--6 EXISTS, cont.  Often, EXISTS can be replaced with another correlation name.  List the customers who live in Dublin and have a film reserved. SELECT Name FROM Customer, Reserved WHERE City = ’Dublin’ AND Customer.CustomerID = Reserved.CustomerID

--The SQL Query Language DML--7 NOT EXISTS  List the customers who live in Dublin but have no films reserved. SELECT Name FROM Customer WHERE City = ’Dublin’ AND NOT EXISTS(SELECT * FROM Reserved WHERE Reserved.CustomerID = Customer.CustomerID)

--The SQL Query Language DML--8 Outline - The SELECT statement  Single table  Projection  Selection  Multiple tables  Cartesian product and join  Set operations  Subqueries  Optional clauses  Ordering results  Computing aggregates on groups  Additional joins

--The SQL Query Language DML--9 ORDER BY  Can sort the result of a select, using ORDER BY.  Who has reserved a film? SELECT Name FROM Customer, Reserved WHERE Customer.CustomerID = Reserved.CustomerID ORDER BY Name  Can also sort in descending order, via DESC (ASC is the default). SELECT Name FROM Customer, Reserved WHERE Customer.CustomerID = Reserved.CustomerID ORDER BY Name DESC  Only columns in the select list can be used for the ordering.

--The SQL Query Language DML--10 Select in the From Clause  The table in a from clause can itself be a select statement.  List the customers who have reserved an expensive film. SELECT CustomerID FROM Reserved,(SELECT * FROM Film WHERE RentalPrice > 4) AS Expensive WHERE Reserved.FilmID = Expensive.FilmID  A correlation name is required in such cases.

--The SQL Query Language DML--11 Aggregates  Aggregates operate on the set of values of a column of a table, and return a single value.  SUM: sum of values  AVG: average value  MAX: maximum value  MIN: minimum value  COUNT: number of values  How many reservations are there? SELECT COUNT (*) FROMReserved

--The SQL Query Language DML--12 Aggregates, cont.  What is the total rental price of all films? SELECT SUM(RentalPrice) FROMFilm  Eliminate duplicates before computing aggregate with DISTINCT  In how many cities do customers reside? SELECT COUNT(DISTINCT City) FROMCustomer

--The SQL Query Language DML--13 Aggregates, cont.  What is the average rental price of reserved films? SELECT AVG(RentalPrice) FROMReserved, Film WHEREReserved.FilmID = Film.FilmID  Find the film(s) with the highest rental price. SELECT Title FROM Film WHERE RentalPrice IN (SELECT MAX(RentalPrice) FROM Film)

--The SQL Query Language DML--14 GROUP BY  The table can be partitioned into several groups specifying GROUP BY  Each group has the same attribute values for the indicated columns. Name Age Pam4 Pat1 Joe3 2 Pat1Pam4 Joe3 2 Groups GROUP BY Name

--The SQL Query Language DML--15 GROUP BY, cont.  Aggregate is applied to each group.  What is the oldest age for each name? SELECT Name, MAX(Age) FROMPerson GROUP BY Name Name Age Pam4 Pat1 Joe3 2 Groups Pam4 Pat1 Joe3 2 MAX is 1 MAX is 4 MAX is 3 Pam4 Pat1 Joe3 Name Max(Age)

--The SQL Query Language DML--16 GROUP BY, cont.  What is the average rental price of reserved films, by kind? SELECT Kind, AVG(RentalPrice) FROMReserved, Film WHERE Reserved.FilmID = Film.FilmID GROUP BY Kind

--The SQL Query Language DML--17 HAVING  Individual groups can be eliminated by using HAVING.  List the names and average age per name, as long as the average is more than 2. Name Age Pam4 Pat1 Joe3 2 Groups Pam4 Pat1 Joe3 2 AVG is 1 AVG is 4 AVG is 2.5 Pam4.0 Joe2.5 Name AVG(Age) SELECT Name, AVG(Age) FROMPerson GROUP BY Name HAVING AVG(Age) > 2

--The SQL Query Language DML--18 HAVING, cont.  Columns in HAVING clause must appear in the GROUP BY (or be contained within an aggregate).  List the customers whose average rental price for reserved films is greater than $3. SELECT Name FROMCustomer, Reserved, Film WHERE Customer.CustomerID = Reserved.CustomerID AND Reserved.FilmID = Film.FilmID GROUP BY Name HAVING AVG(RentalPrice) > 3  Note, RentalPrice appears within an aggregate

--The SQL Query Language DML--19 Summary  A query in SQL can consist of up to six clauses, but only the first two, SELECT and FROM, are mandatory.  The clauses are specified in the following order.  SELECT column(s)  FROM table list  WHERE condition  GROUP BY grouping column(s)  HAVING group condition  ORDER BY sort list

--The SQL Query Language DML--20 Summary, cont.  The query is evaluated in a different order.  The tables in the from clause are combined using Cartesian products.  The where predicate is then applied.  The resulting tuples are grouped according to the group by clause.  The having predicate is applied to each group, possibly eliminating some groups.  The aggregate(s) are applied to each remaining group.  The select clause is performed last (followed by order clause).

--The SQL Query Language DML--21 Order of Clause Evaluation 1.FROM 2.WHERE – optional 3.GROUP BY - optional, defaults to 1 group 4.HAVING – optional 5.SELECT - (generalized) projection 6.ORDER BY - optional