Download presentation
Presentation is loading. Please wait.
Published byPosy Wade Modified over 9 years ago
1
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 2015-10-21
2
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
3
Select-From-Where Statements SELECT desired attributes FROM one or more tables WHERE condition about tuples of the tables 3
4
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
5
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
6
So first, we need to turn these relations into actual tables! uWe will use XAMPP (https://www.apachefriends.org), 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.https://www.apachefriends.org uA short tutorial is here: https://www.siteground.com/tutorials/phpmyadmin/phpmyadmin_create_database.htm https://www.siteground.com/tutorials/phpmyadmin/phpmyadmin_create_database.htm 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 http://programmers.stackexchange.com/questions/120178/whats-the-difference-between-mariadb-and-mysqlhttp://programmers.stackexchange.com/questions/120178/whats-the-difference-between-mariadb-and-mysql 6
7
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
8
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.
9
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
10
Operational Semantics 10 Check if 2012 Title Year 2012 Com- position tv Include tv.Title in the result …
11
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
12
* 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
13
Result of Query: 13 Now, the result has each of the attributes of Agenda item.
14
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
15
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
16
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).
17
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
18
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
19
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
20
Surprising Example uFrom the following Sells relation: barbeerprice Joe’s BarBudNULL SELECT bar FROM Sells WHERE price = 2.00; 20 UNKNOWN
21
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
22
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
23
Result 23
24
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
25
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
26
to output Example 26 MEP_ID Family_name MEP_ID Inst_Acronym Role tv1 tv2 1003 PPE vice-chair 1003 Pisoni Parliament_memberIn_political_function check these are equal check for role
27
Joins with more tables: In what languages do these people give speeches? 27
28
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
29
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
30
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
31
Example: DISTINCT (What does this mean? What would you get without DISTINCT?) 31
32
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
33
Example: Aggregation uFrom Session, find the average month: SELECT AVG(Month) FROM Session; Why? 33
34
Eliminating Duplicates in an Aggregation uUse DISTINCT inside an aggregation. uExample: Session_day has 4 days in January 2012 and 1 in February 2012. 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
35
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
36
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
37
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
38
Example: Grouping 38
39
Grouping over >1 table 39
40
Grouping over >1 table, sorted (Note: ORDER BY also for other attributes) 40
41
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
42
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
43
Example: HAVING 43
44
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
45
Reading Book III, uCh.1, 207-213 about basics (literal values, variables, functions) uCh.2, 231-234, 236-240, 248-255 about SELECT statements uCh.4, 308-313 about SELECT statements on JOINed tables. 45
46
Next week uMore on SQL! 46
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.