Introduction to SQL Elements and process: relations  database (with XAMPP) Select-From-Where Statements Grouping and Aggregation 1 Slides by Jeff Ullman.

Slides:



Advertisements
Similar presentations
1 Introduction to SQL Select-From-Where Statements Subqueries Grouping and Aggregation.
Advertisements

1 Introduction to SQL Select-From-Where Statements Multirelation Queries Subqueries.
SQL Queries Principal form: SELECT desired attributes FROM tuple variables –– range over relations WHERE condition about tuple variables; Running example.
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.
SQL CSET 3300.
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
CS411 Database Systems Kazuhiro Minami 06: SQL. Join Expressions.
1 Database Systems Relations as Bags Grouping and Aggregation Database Modification.
1 Introduction to SQL Multirelation Queries Subqueries Slides are reused by the approval of Jeffrey Ullman’s.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.
IS698: Database Management Min Song IS NJIT. Overview  Query processing  Query Optmization  SQL.
1 Introduction to SQL Select-From-Where Statements Subqueries Grouping and Aggregation Source: slides by Jeffrey Ullman.
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.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
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.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #3.
Database Systems More SQL Database Design -- More SQL1.
1 More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #2.
Chapter 6 Notes. 6.1 Simple Queries in SQL SQL is not usually used as a stand-alone language In practice there are hosting programs in a high-level language.
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
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
SCUHolliday6–1 Schedule Today: u SQL Queries. u Read Sections Next time u Subqueries, Grouping and Aggregation. u Read Sections And then.
Databases : SQL-Introduction 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
1 Relational Algebra & SQL. 2 Why SQL & Relational Algebra? uSQL is a very-high-level language. wSay “what to do” rather than “how to do it.” wAvoid a.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Onsdag The concepts in a relation data model SQL DDL DML.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Computational Biology Dr. Jens Allmer Lecture Slides Week 6.
1 Introduction to SQL Select-From-Where Statements Multirelation Queries Subqueries Slides are reused by the approval of Jeffrey Ullman’s.
Databases 1 Second lecture.
1 More SQL uDatabase Modification uDefining a Database Schema uViews.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
1 Introduction to SQL Database Systems. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation.
Phase 2 of database design: Mapping an (E)ER diagram to a relational model Information Structures and Implications 2015 Bettina Berendt Last updated:
Introduction to SQL Introduction Select-From-Where Statements Queries over Several Relations Subqueries.
1 Lecture 6 Introduction to SQL part 4 Slides from
1 Chapter 6 More SQL uDatabase Modification uDefining a Database Schema uViews.
1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
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 Database Principles. 2 Chapter3-1 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.
1 Introduction to SQL Select-From-Where Statements Subqueries Grouping and Aggregation.
1 Introduction to Database Systems, CS420 SQL JOIN, Aggregate, Grouping, HAVING and DML Clauses.
1 Database Design: DBS CB, 2 nd Edition SQL: Select-From-Where Statements & Multi-relation Queries & Subqueries Ch. 6.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Select-From-Where Statements Multirelation Queries Subqueries
Schedule Today: Jan. 28 (Mon) Jan. 30 (Wed) Next Week Assignments !!
Slides are reused by the approval of Jeffrey Ullman’s
CPSC-310 Database Systems
Computational Biology
Databases : More about SQL
CPSC-310 Database Systems
Introduction to Database Systems, CS420
CPSC-608 Database Systems
CS 440 Database Management Systems
IST 210: Organization of Data
CSCE 315 – Programming Studio Spring 2010 Project 1, Lecture 4
Projecting output in MySql
CPSC-608 Database Systems
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
CPSC-608 Database Systems
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

Introduction to SQL Elements and process: relations  database (with XAMPP) Select-From-Where Statements Grouping and Aggregation 1 Slides by Jeff Ullman (infolab.stanford.edu/~ullman/dscb/pslides/sql1.ppt), with example modified and some additions Bettina Berendt, ISI 2015 Last updated

Why SQL? uSQL is a very-high-level language. wSay “what to do” rather than “how to do it.” wAvoid a lot of data-manipulation details needed in procedural languages like C++ or Java. uDatabase management system figures out “best” way to execute query. wCalled “query optimization.” 2

Select-From-Where Statements SELECT desired attributes FROM one or more tables WHERE condition about tuples of the tables 3

A note about case-sensitivity uSQL is case-sensitive inside strings. uMySQL is case-sensitive on some operating systems (e.g. Mac). uIt isn‘t on Windows. 4

Our Running Example All our SQL queries will be based on the following database schema Session (Month, Year) Session_Day (Day, Month, Year) Agenda_Item (Agenda_item_ID, Title, Day, Month, Year, Number) Speech (Speech_ID, Spoken_text, Language, Video_URI, Agenda_item_ID, Number, MEP_ID) Parliament_Member (MEP_ID, Date_of_birth, Given_name, Family_name) Country (Acronym, Name, EU_member_since) Role (Name) Political_Institution (Acronym, Institution_label) Represents (MEP_ID, CountryAcronym) In_Political_Function (MEP_ID, RoleName, Inst_Acronym, Start_date, End_date) EU_Party (Inst_Acronym) National_Party (Inst_Acronym) EU_Committee (Inst_Acronym) 5

So first, we need to turn these relations into actual tables! uWe will use XAMPP ( which installs a web server, a database management system, and a browser-based interface for your databases on your computer. The first is Apache, the second MariaDB (was: MySQL*), and the third phpmyadmin. uA short tutorial is here: uIn the lecture, we will build up the database step by step. uTo load the already-implemented and –populated database: 1.Download the file eup.sql from Toledo 2.create a database named (e.g.) eup 3.Go to Import, choose the file eup.sql, click OK * For our purposes, the same. See 6

Example uUsing Agenda_Item (Agenda_item_ID, Title, Day, Month, Year, Number), what titles were discussed in 2012? SELECT Title FROM Agenda_Item WHERE Year=2012; 7

Result of Query 8 The answer is a relation with a single attribute, Title, and tuples with the title of each agenda item discussed in 2012.

Meaning of Single-Relation Query (“Formal Semantics”) uBegin with the relation in the FROM clause. uApply the selection indicated by the WHERE clause. uApply the extended projection indicated by the SELECT clause. 9

Operational Semantics 10 Check if 2012 Title Year 2012 Com- position tv Include tv.Title in the result …

Operational Semantics uTo implement this algorithm think of a tuple variable ranging over each tuple of the relation mentioned in FROM. uCheck if the “current” tuple satisfies the WHERE clause. uIf so, compute the attributes or expressions of the SELECT clause using the components of this tuple. 11

* In SELECT clauses uWhen there is one relation in the FROM clause, * in the SELECT clause stands for “all attributes of this relation.” uExample using Agenda_item: SELECT * FROM Agenda_Item WHERE Year=2012; 12

Result of Query: 13 Now, the result has each of the attributes of Agenda item.

Complex Conditions in WHERE Clause uFrom Agenda_item, find the titles of agenda items in February 2012: SELECT Title FROM Agenda_Item WHERE Year=2012 AND Month=02; 14

Patterns uWHERE clauses can have conditions in which a string is compared with a pattern, to see if it matches. uGeneral form: LIKE or NOT LIKE uPattern is a quoted string with % = “any string”; _ = “any character.” 15

Example uFrom Agenda_item, find the agenda items whose title includes “Composition” : SELECT Title, Month, Year FROM Agenda_Item WHERE Title LIKE "%Composition%"; 16 Note that SQL is case-sensitive inside strings, and MySQL is case-sensitive on some operating systems (e.g. Mac).

NULL Values uTuples in SQL relations can have NULL as a value for one or more components. uMeaning depends on context. Two common cases: wMissing value : e.g., we know Louise Weiss has some birth date, but we don’t know what it is. wInapplicable : e.g., the value of attribute spouse for an unmarried person. 17

Comparing NULL’s to Values uThe logic of conditions in SQL is really 3- valued logic: TRUE, FALSE, UNKNOWN. uWhen any value is compared with NULL, the truth value is UNKNOWN. uBut a query only produces a tuple in the answer if its truth value for the WHERE clause is TRUE (not FALSE or UNKNOWN). 18

Three-Valued Logic uTo understand how AND, OR, and NOT work in 3-valued logic, think of TRUE = 1, FALSE = 0, and UNKNOWN = ½. uAND = MIN; OR = MAX, NOT(x) = 1-x. uExample: TRUE AND (FALSE OR NOT(UNKNOWN)) = MIN(1, MAX(0, (1 - ½ ))) = MIN(1, MAX(0, ½ ) = MIN(1, ½ ) = ½. 19

Surprising Example uFrom the following Sells relation: barbeerprice Joe’s BarBudNULL SELECT bar FROM Sells WHERE price = 2.00; 20 UNKNOWN

Multirelation Queries uInteresting queries often combine data from more than one relation. uWe can address several relations in one query by listing them all in the FROM clause. uDistinguish attributes of the same name by “. ” 21

Example uUsing relations Parliament_member and In_Political_Function, find the names of MEPs who are chairs (or co-chairs, vice-chairs, …) of some institution. SELECT Given_name, Family_name, Inst_Acronym, RoleName FROM Parliament_Member, In_Political_Function WHERE Parliament_Member.MEP_ID = In_Political_Function.MEP_ID AND RoleName LIKE "%chair"; 22

Result 23

Formal Semantics uAlmost the same as for single-relation queries: 1.Start with the product of all the relations in the FROM clause. 2.Apply the selection condition from the WHERE clause. 3.Project onto the list of attributes and expressions in the SELECT clause. 24

Operational Semantics uImagine one tuple-variable for each relation in the FROM clause. wThese tuple-variables visit each combination of tuples, one from each relation. uIf the tuple-variables are pointing to tuples that satisfy the WHERE clause, send these tuples to the SELECT clause. 25

to output Example 26 MEP_ID Family_name MEP_ID Inst_Acronym Role tv1 tv PPE vice-chair 1003 Pisoni Parliament_memberIn_political_function check these are equal check for role

Joins with more tables: In what languages do these people give speeches? 27

Explicit Tuple-Variables uSometimes, a query needs to use two copies of the same relation. uDistinguish copies by following the relation name by the name of a tuple- variable, in the FROM clause. uIt’s always an option to rename relations this way, even when not essential. 28

Example uFrom Speech, find all pairs of speeches by the same MEP. wDo not produce pairs like (1,1). wProduce pairs in ascending order, e.g. (1,2), not (2,1). 29

Controlling Duplicate Elimination uForce the result to be a set by SELECT DISTINCT... uForce the result to be a bag (i.e., don’t eliminate duplicates) by ALL, as in... UNION ALL... 30

Example: DISTINCT (What does this mean? What would you get without DISTINCT?) 31

Aggregations uSUM, AVG, COUNT, MIN, and MAX can be applied to a column in a SELECT clause to produce that aggregation on the column. uAlso, COUNT(*) counts the number of tuples. 32

Example: Aggregation uFrom Session, find the average month: SELECT AVG(Month) FROM Session; Why? 33

Eliminating Duplicates in an Aggregation uUse DISTINCT inside an aggregation. uExample: Session_day has 4 days in January 2012 and 1 in February uWhat is the result of these queries? SELECT count( * ) FROM Session_day; SELECT count( Month ) FROM Session_day; SELECT count( DISTINCT Month ) FROM Session_day; 34

NULL’s Ignored in Aggregation uNULL never contributes to a sum, average, or count, and can never be the minimum or maximum of a column. uBut if there are no non-NULL values in a column, then the result of the aggregation is NULL. 35

Example: Effect of NULL´s uAn incomplete version of the database has  SELECT count(*) FROM `speech`  628  SELECT count(agenda_item_ID) FROM `speech`  0 36

Grouping uWe may follow a SELECT-FROM-WHERE expression by GROUP BY and a list of attributes. uThe relation that results from the SELECT-FROM-WHERE is grouped according to the values of all those attributes, and any aggregation is applied only within each group. 37

Example: Grouping 38

Grouping over >1 table 39

Grouping over >1 table, sorted (Note: ORDER BY also for other attributes) 40

Restriction on SELECT Lists With Aggregation uIf any aggregation is used, then each element of the SELECT list must be either: 1.Aggregated, or 2.An attribute on the GROUP BY list. 41

HAVING Clauses uHAVING may follow a GROUP BY clause. uIf so, the condition applies to each group, and groups not satisfying the condition are eliminated. 42

Example: HAVING 43

Requirements on HAVING Conditions uThese conditions may refer to any relation or tuple-variable in the FROM clause. uThey may refer to attributes of those relations, as long as the attribute makes sense within a group; i.e., it is either: 1.A grouping attribute, or 2.Aggregated. 44

Reading Book III, uCh.1, about basics (literal values, variables, functions) uCh.2, , , about SELECT statements uCh.4, about SELECT statements on JOINed tables. 45

Next week uMore on SQL! 46