SQL: An Introduction to Structured Query Language and Basic Queries, Slides of Database Management Systems (DBMS)

An introduction to sql, the structured query language used for managing and manipulating relational databases. Topics covered include the history of sql, creating and dropping tables, basic queries using the select, from, and where clauses, and examples of selection, projection, and join queries. The document also touches on set and bag semantics, operational semantics of sfw statements, and sql set and bag operations.

Typology: Slides

2011/2012

Uploaded on 01/28/2012

arold
arold 🇺🇸

4.7

(24)

372 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
SQL: Part I
CPS 216
Advanced Database Systems
2
Announcements (January 25)
Reading assignment for this week (Ailamaki et al.,
VLDB 2001) has been posted
Due Wednesday night
Hunt for related/follow-up work too!
Homework #1 due in two weeks
3
SQL
SQL: Structured Query Language
Pronounced “S-Q-L” or “sequel”
The standard query language support by most
commercial DBMS
A brief history
IBM System R
ANSI SQL89
ANSI SQL92 (SQL2)
ANSI SQL99 (SQL3)
ANSI SQL 2003 (+OLAP, XML, etc.)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download SQL: An Introduction to Structured Query Language and Basic Queries and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

SQL: Part I

CPS 216

Advanced Database Systems

2

Announcements (January 25)

™ Reading assignment for this week (Ailamaki et al., VLDB 2001) has been posted ƒ Due Wednesday night ƒ Hunt for related/follow-up work too!

™ Homework #1 due in two weeks

3

SQL

™ SQL: Structured Query Language

ƒ Pronounced “S-Q-L” or “sequel” ƒ The standard query language support by most commercial DBMS

™ A brief history ƒ IBM System R ƒ ANSI SQL ƒ ANSI SQL92 (SQL2) ƒ ANSI SQL99 (SQL3) ƒ ANSI SQL 2003 (+OLAP, XML, etc.)

Creating and dropping tables

™ CREATE TABLE table_name ( , column_namei column_typei , ); ™ DROP TABLE table_name ; ™ Examples create table Student (SID integer, name varchar(30), email varchar(30), age integer, GPA float); create table Course (CID char(10), title varchar(100)); create table Enroll (SID integer, CID char(10)); drop table Student; drop table Course; drop table Enroll; -- everything from -- to the end of the line is ignored. -- SQL is insensitive to white space. -- SQL is case insensitive (e.g., ...Course... is equivalent to -- ...COURSE...)

5

Basic queries: SFW statement

™ SELECT A 1 , A 2 , …, A (^) n FROM R 1 , R 2 , …, R (^) m WHERE condition ;

™ Also called an SPJ (select-project-join) query

™ Equivalent (not really!) to relational algebra query π A 1 , A 2 , …, A (^) n ( σ condition ( R 1 × R 2 × … × R (^) m ))

6

Example: reading a table

™ SELECT * FROM Student; ƒ Single-table query, so no cross product here ƒ WHERE clause is optional ƒ * is a short hand for “all columns”

A more complicated example

™ Titles of all courses that Bart and Lisa are taking together

Tip: Write the FROM clause first, then WHERE, and then SELECT

11

Why SFW statements?

™ Out of many possible ways of structuring SQL statements, why did the designers choose SELECT- FROM-WHERE?

12

Set versus bag semantics

™ Set

ƒ No duplicates ƒ Relational model and algebra use set semantics

™ Bag ƒ Duplicates allowed ƒ Number of duplicates is significant ƒ SQL uses bag semantics by default

Set versus bag example

SID CID 142 CPS 142 CPS 123 CPS 857 CPS 857 CPS 456 CPS ... ...

SID 142 123 857 456 ...

π SID Enroll

Enroll

SELECT SID FROM Enroll;

SID 142 142 123 857 857 456 ...

14

A case for bag semantics

™ Besides, SQL provides the option of set semantics with DISTINCT keyword

15

Operational semantics of SFW

™ SELECT [DISTINCT] E 1 , E 2 , …, En FROM R 1 , R 2 , …, R (^) m WHERE condition ; ™ For each t 1 in R 1 : For each t 2 in R 2 : … … For each tm in R (^) m : If condition is true over t 1 , t 2 , …, tm : Compute and output E 1 , E 2 , …, En If DISTINCT is present Eliminate duplicate rows in output

™ t 1 , t 2 , …, tm are often called tuple variables

Examples of set versus bag operations

™ Enroll ( SID , CID ), ClubMember ( club , SID ) ƒ (SELECT SID FROM ClubMember) EXCEPT (SELECT SID FROM Enroll);

ƒ (SELECT SID FROM ClubMember) EXCEPT ALL (SELECT SID FROM Enroll);

20

Table expression

™ Use query result as a table ƒ In set and bag operations, FROM clauses, etc. ƒ A way to “nest” queries

™ Example: names of students who are in more clubs than classes

(SELECT SID FROM ClubMember) EXCEPT ALL (SELECT SID FROM Enroll)

SELECT DISTINCT name FROM Student, ( ) AS S WHERE Student.SID = S.SID;

21

Summary of SQL features covered so far

™ Basic CREATE/DROP TABLE

™ SELECT-FROM-WHERE statements (select-project-join queries)

™ Set and bag operations

™ Nesting queries using table expressions

) So far, not much more than relational algebra

) Next: aggregation

Aggregates

™ Standard SQL aggregate functions: COUNT, SUM, AVG, MIN, MAX

™ Example: number of students under 18, and their average GPA ƒ SELECT COUNT(), AVG(GPA) FROM Student WHERE age < 18; ƒ COUNT() counts the number of rows

23

GROUP BY

™ SELECT … FROM … WHERE …

GROUP BY list_of_columns ;

™ Example: find the average GPA for each age group ƒ SELECT age, AVG(GPA) FROM Student GROUP BY age;

24

Operational semantics of GROUP BY

SELECT … FROM … WHERE … GROUP BY …;

™ Compute FROM (×)

™ Compute WHERE (σ)

™ Compute GROUP BY: group rows according to the values of GROUP BY columns

™ Compute SELECT for each group (π)

)One output row per group in the final output

Examples of invalid queries

™ SELECT SID, age FROM Student GROUP BY age;

ƒ Recall there is one output row per group ƒ There can be multiple SID values per group ™ SELECT SID, MAX(GPA) FROM Student;

ƒ Recall there is only one group for an aggregate query with no GROUP BY clause ƒ There can be multiple SID values ƒ Wishful thinking (that the output SID value is the one associated with the highest GPA) does NOT work

29

HAVING

™ Used to filter groups based on the group properties (e.g., aggregate values, GROUP BY column values)

™ SELECT … FROM … WHERE … GROUP BY … HAVING condition ; ƒ Compute FROM (×) ƒ Compute WHERE (σ) ƒ Compute GROUP BY: group rows according to the values of GROUP BY columns ƒ Compute HAVING (another σ over the groups) ƒ Compute SELECT (π) for each group that passes HAVING

30

HAVING examples

™ Find the average GPA for each age group over 10 ƒ SELECT age, AVG(GPA) FROM Student GROUP BY age HAVING age > 10;

™ List the average GPA for each age group with more than a hundred students ƒ SELECT age, AVG(GPA) FROM Student GROUP BY age HAVING COUNT(*) > 100;

Summary of SQL features covered so far

™ Basic CREATE/DROP TABLE

™ SELECT-FROM-WHERE statements

™ Set and bag operations

™ Table expressions

™ Aggregation and grouping ƒ More expressive power than relational algebra

) Next: NULL’s

32

Incomplete information

™ Example: Student ( SID , name , age , GPA )

™ Value unknown

ƒ We do not know Nelson’s age

™ Value not applicable ƒ Nelson has not taken any classes yet; what is his GPA?

33

Solution 1

™ A dedicated special value for each domain (type)

ƒ GPA cannot be –1, so use –1 as a special value to indicate a missing or invalid GPA ƒ Leads to incorrect answers if not careful

  • SELECT AVG(GPA) FROM Student; ƒ Complicates applications
  • SELECT AVG(GPA) FROM Student WHERE GPA <> -1; ƒ Remember the pre-Y2K bug?
  • 09/09/99 was used as a missing or invalid date value

Three-valued logic

™ When we compare a NULL with another value (including another NULL) using =, >, etc., the result is UNKNOWN

™ TRUE = 1, FALSE = 0, UNKNOWN = 0. ™ x AND y = min( x , y )

™ x OR y = max( x , y ) ™ NOT x = 1 – x

™ WHERE and HAVING clauses only select rows for output if the condition evaluates to TRUE ƒ UNKNOWN is insufficient

38

Unfortunate consequences

™ SELECT AVG(GPA) FROM Student; SELECT SUM(GPA)/COUNT(*) FROM Student;

™ SELECT * FROM Student; SELECT * FROM Student WHERE GPA = GPA;

) Be careful: NULL breaks many equivalences

39

Another problem

™ Example: Who has NULL GPA values? ƒ SELECT * FROM Student WHERE GPA = NULL;

ƒ

ƒ Introduced built-in predicates IS NULL and IS NOT NULL

  • SELECT * FROM Student WHERE GPA IS NULL;

Summary of SQL features covered so far

™ Basic CREATE/DROP TABLE

™ SELECT-FROM-WHERE statements

™ Set and bag operations

™ Table expressions

™ Aggregation and grouping

™ NULL’s

) Next: subqueries, modifications, constraints, and views