Você está na página 1de 50

4/7/19

BIM312  Database  Management  


Systems

Week  9 – SQL  Part  II


April  5th,  2019

SQL stands  for


Review: What  is  SQL? Structured  Query  Language
§ SQL  is  a  high-­‐level  special-­‐purpose  language  for  querying and  
manipulating data
§ Pronounced  as  “S-­‐Q-­‐L”  or  “sequel”
§ SQL  is  mostly  a  declarative  language
§ You  declare  what  you  want  without  specifying  how  you  want  to  get  answer
§ SQL  provides  a  limited  set  of  operations
§ Supported  by  most  DBMS
§ Mostly  implementations  of  Relational  Algebra  operations  (next  week)
§ SQL  programmer  needs  to  focus  on  readability  and  on  getting  the  right  
result  – do  not  need  to  worry  about  efficiency
§ Because  the  DBMS  optimizes  every  query  and  chooses  the  most  efficient  
implementation  for  each  operation

1
4/7/19

Review: SQL  is  a…


§ Data  Definition  Language  (DDL)
§ Define  relational  schemata
§ CREATE/ALTER/DROP tables  and  their  attributes

§ Data  Manipulation  Language  (DML)


§ INSERT/UPDATE/DELETE tuples  in  tables
§ Query  one  or  more  tables  – SELECT
§ Transaction  Control  – COMMIT/ROLLBACK

PART  I
Review:  Single  Table  Queries

2
4/7/19

QUERYING  /  RETRIEVING  INFORMATION  FROM  


A  DATABASE
§ SQL  has  one  basic  statement  for  retrieving  information  from  a  database  à
SELECT statement

§ The  SELECT statement  is  used  to  get  data  out  of  the  database

§ The  data  returned  is  stored  in  a  result  table,  called  the  result-­‐set,  which  is  
temporary,  hence  not  stored  in  the  database,  but  what  user  want  to  see

SELECT-­‐FROM-­‐WHERE  (SFW)  Query


§ A  simple  SQL-­‐query consists  of  the  three  clauses  select,  from,  where  
§ This  is  the  basic  form  of  the  SQL  SELECT  statement  is  also  called  a  
mapping or  a  select-­‐from-­‐where query  or  SFW query
Basic  form
SELECT <attributes> ü <attributes>  is  a  list  of  attribute  names  whose  values  
are  to  be  retrieved  by  the  query
FROM <one or more tables> ü <tables>  is  a  list  of  the  tables  required  to  process  the  
WHERE <conditions> query
ü <conditions>  is  a  conditional  (Boolean)  expression  
that  identifies  the  tuples  to  be  retrieved  by  the  query
Call  this  a  SFW query

3
4/7/19

Example: SFW  Query


Query: “List  student  names  with  GPA>3”
Result  
SELECT name Table
FROM Student
WHERE gpa>3

How  the  Query  is  Evaluated?


§ Each  tuple  of  Student  is  
inspected
§ Each  attribute  of  WHERE  
clause  is  substituted  with  
the  actual  tuple  value  
§ The  condition  is  then  
evaluated,  and  if  true  – this  
tuple  is  added  to  the  output  
relation
SELECT name
FROM Student
WHERE gpa>3

4
4/7/19

A  Simple  Example  – Unspecified  WHERE  and  *


Student
StudentNo Name Surname DateOfBirth
1 John Smith 1990-­‐10-­‐-­‐12
2 Anne Miller 1992-­‐07-­‐30
3 Betty Jones 1991-­‐03-­‐24 *  means  “project  
4 Boris Johnson 1992-­‐07-­‐30 all  attributes”

Query: “Give  me  all  available  data  about  all  students”


SELECT StudentNo, Name, Surname, DateOfBirth SELECT *
FROM Student; FROM Student;

This  returns  the  whole  table  Student as  shown  above

A  Simple  Example  – Selecting  Attributes


Query: “Output  student  number  and  name  of  all  students”
Student
StudentNo Name Surname DateOfBirth
1 John Smith 1990-­‐10-­‐12
2 Anne Miller 1992-­‐07-­‐30 Result Table
3 Betty Jones 1991-­‐03-­‐24 StudentNo Name
4 Boris Johnson 1992-­‐07-­‐30 1 John
2 Anne
SELECT StudentNo, Name
3 Betty
FROM Student;
4 Boris

5
4/7/19

A  Simple  Example  – SQL  DISTINCT


§ By  default  SQL  does  not  remove  duplicates
§ SQL  uses  a  multi-­‐set  (bag  semantic),  whereas  Relational  Algebra  (RA)  uses  set  
(next  week)
§ If  we  want  to  remove  duplicates,  we  have  to  use  the  keyword  DISTINCT

SELECT DateOfBirth SELECT DISTINCT DateOfBirth


FROM Student; FROM Student;
Result Table Result Table
DateOfBirth DateOfBirth
1990-­‐10-­‐12 1990-­‐10-­‐12
1992-­‐07-­‐30 1992-­‐07-­‐30
1991-­‐03-­‐24 1991-­‐03-­‐24
1992-­‐07-­‐30

WHERE  Clause
The  conditions  can  be  written  using:
§ Column  names
§ Logical  and  comparison  operators
§ Mathematical  expressions
§ Constants
§ Built-­‐in  DBMS  functions
§ Sub-­‐queries

6
4/7/19

A  Simple  Example  – with  WHERE  Clause


Query: “Give  me  all  available  data  about  students  with  a  
Student student  number  less  than  3”
StudentNo Name Surname DateOfBirth
1 John Smith 1990-­‐10-­‐12
2 Anne Miller 1992-­‐07-­‐30
3 Betty Jones 1991-­‐03-­‐24
4 Boris Johnson 1992-­‐07-­‐30
Result  Table
SELECT * StudentNo Name Surname DateOfBirth
FROM Student
1 John Smith 1990-­‐10-­‐12
WHERE StudentNo < 3;
2 Anne Miller 1992-­‐07-­‐30

Operators  in  The  WHERE  Clause


The  following  operators  can  be  used  in  the  WHERE clause
Operator Description
= Equal
<> Not  equal  (Note: In  some  versions  of  SQL  this  operator  may  be  written  as  != )
> Greater  than
< Less  than
>= Greater  than  or  equal  to
<= Less  than  or  equal  to
BETWEEN Between  an  inclusive  range
LIKE Search  for  a  pattern
IN To  specify  multiple  possible  values  for  a  column

7
4/7/19

SQL  AND,  OR  and  NOT  Operators

§ The  WHERE clause  can  be  combined  with  AND,  OR,  and  NOT operators

§ The  AND and  OR operators  are  used  to  filter  records  based  on  more  than  
one  condition
§ The  AND operator  displays  a  record  if  all  the  conditions  separated  by  AND  is  TRUE
§ The  OR operator  displays  a  record  if  any  of  the  conditions  separated  by  OR  is  TRUE

§ The  NOT operator  displays  a  record  if  the  condition(s)  is  NOT  TRUE

§ We  can  also  combine the  AND,  OR and  NOT operators!!!

Example: AND,  OR,  NOT  Operators


Query1: Give  me  all  data  about  Students  whose  name  is  “Betty”  and    from  “Berlin”
SELECT *
FROM Student
WHERE Name=‘Betty’ AND City=‘Berlin’;

Query2: Give  me  all  data  about  Students  whose  from  “Berlin”  or  “London”
SELECT *
FROM Student
WHERE City=‘Berlin’ OR City=‘London’;

Query3: Give  me  all  data  about  Students  whose  not  from  “Berlin”
SELECT *
FROM Student
WHERE NOT City=‘Berlin’;

8
4/7/19

Example: Combining  AND,  OR,  NOT  Operators


Query1: Give  me  all  data  about  Students  whose  country  is  “Germany”  AND  whose  
city  is  “Berlin”  OR  “Munich”  OR  “Frankfurt”
SELECT *
FROM Student
WHERE Country=‘Germany’
AND (City=‘Berlin’ OR City=‘Munich’ OR City=‘Frankfurt’);

Query2: Give  me  all  data  about  Students  whose  country  is  NOT  “Germany”  and  
NOT  “USA”
SELECT *
FROM Student
WHERE NOT Country=‘Germany’ AND NOT Country=‘USA’;

SQL  LIKE  Operator:  Simple  String  Pattern  Matching


§ The  LIKE operator  is  used  in  a  WHERE clause  to  search  for  a  specified  pattern  in  a  
column
SELECT …  FROM …  WHERE columnN LIKE ‘pattern’ -­‐ pattern  matching  on  strings
§ There  are  two  wildcards  used  in  conjunction  with  the  LIKE operator  (In  other  
words,  pattern  may  contain  two  special  symbols)
§ % -­‐ The  percent  sign  represents  zero,  one,  or  multiple characters  
(any  sequence  of  characters,  which  can  also  be  empty)
§ _   -­‐ The  underscore  represents  a  single character  (any  single  character)
§ The  %  and  _  can  also  be  used  in  combinations!
SELECT *
SELECT *
FROM Student
FROM Student
WHERE Name=‘Betty’
WHERE Name LIKE ‘B%’
OR Name=‘Boris’

9
4/7/19

Example: Different  LIKE  operators  with  ‘%’      


and  ‘_’  wildcards
LIKE  Operator Description
WHERE Name  LIKE ‘a%’ Finds  any  values  that  starts  with  "a"
WHERE Name  LIKE '%a' Finds  any  values  that  ends  with  "a"
WHERE Name  LIKE '%or%' Finds  any  values  that  have  "or"  in  any  position
WHERE Name  LIKE '_r%' Finds  any  values  that  have  "r"  in  the  second  position
WHERE Name  LIKE 'a_%_%' Finds  any  values  that  starts  with  "a"  and  are  at  least  3  
characters  in  length
WHERE Name  LIKE 'a%o' Finds  any  values  that  starts  with  "a"  and  ends  with  "o"

SQL  BETWEEN  Operator


§ The  BETWEEN operator  selects  values  within  a  given  range
§ The  values can  be  numbers,  text,  or  dates
§ The  BETWEEN operator  is  inclusive
§ begin  and  end  values  are  included
§ To  display  the  products  outside  of  some  range,  use    NOT  BETWEEN
SELECT *
FROM Student
WHERE StudentNo BETWEEN 10 AND 20;

SELECT *
FROM Student
WHERE StudentNo NOT BETWEEN 10 AND 20;

10
4/7/19

Example: BETWEEN  Operator


Query: “Give  me  the  names  and  surnames  of  all  students  who  were  
born  between  1992-­‐01-­‐01  and  1994-­‐01-­‐01”
SELECT Name, Surname
FROM Student
WHERE DateOfBirth BETWEEN 1992-01-01 AND 1994-01-01;

This  is  equivalent  with


SELECT Name, Surname
FROM Student
WHERE DateOfBirth >= 1992-01-01
AND DateOfBirth <= 1994-01-01;

SQL  IN  Operator


§ The  IN operator  allows  us  to  specify  multiple  values  in  a  WHERE clause
§ The  IN operator  is  a  shorthand  for  multiple  OR conditions
SELECT * SELECT *
FROM Student FROM Student
WHERE StudentNo IN (3, 5); WHERE Name IN (’Anne’, ‘Boris’);

SELECT *
FROM Student
WHERE Name NOT IN (’Anne’, ‘Boris’);

SELECT *
FROM Student
WHERE Name IN (SELECT LecturerName FROM Lecturer);

11
4/7/19

SQL  NULL  Values


§ NULL is  a  special  value  in  SQL
§ Whenever  we  don’t  have  a  value  for  an  attribute,  we  can  put  a  NULL
§ Can  mean  many  things
§ Value  does  not  exists  (unavailable)
§ Value  exists  but  is  unknown  
§ Value  not  applicable
§ The  schema  specifies  for  each  attribute  if  it  can  be  NULL (nullable attribute)  or  
not  (NOT  NULL)
§ If  an  attribute  in  a  table  is  optional,  it  is  possible  to  insert a  new  record  or  update
a  record  without  adding  a  value  to  that  field
§ Then,  the  attribute  will  be  saved  with  a  NULL value

Note: A NULL value  is  different  from a  zero  value  or  a  field  that  contains  spaces.  A  field  
with  a  NULL value  is  one  that  has  been  left  blank  during  record  creation!

How  to  Test  for  NULL  Values?


SELECT *
FROM Student
WHERE  Address  =  null;
X
The  returned  value  here  is  unknown
§ It  is  NOT  POSSIBLE  to  test  for  NULL values  with  comparison  operators,  
such  as  =,  <,  or  <>
§ Instead,  use  the  IS  NULL  and  IS  NOT  NULL  operators
§ …  WHERE  x IS  NULL
§ …  WHERE x  IS  NOT  NULL

Query: List  all  Students  that   Query: List  all  Students  that  
has  NO  address do  have  an  address
SELECT * SELECT *
FROM Students FROM Students
WHERE Address IS NULL; WHERE Address IS NOT NULL

12
4/7/19

How  does  SQL  cope  with  tables  that  have  NULLs?


§ For  numerical  operations,  NULL  -­‐>  NULL
§ If  x  =  NULL  then  4*(3-­‐x)/7  is  still  NULL

§ For  boolean operations,  in  SQL  there  are  three  values:


FALSE =   0
ü In  the  case  of  AND UNKNOWN =   0.5
x  AND  y      =    min(x,  y) TRUE =   1
ü In  the  case  of  OR If  x=NULL  then  x=“Joe”  is  UNKNOWN
x OR      y      =    max(x,  y)
ü In  the  case  of  NOT
NOT  x                  =    1  – x

How  does  SQL  cope  with  tables  that  have  NULLs?


Query:  “Give  me  all  data  about  Students whose  age  is  less  than  25,  height  is  
greater  than  1.70  cm,  and  weight  is  greater  than  90kg”
SELECT *
FROM Student
WHERE (age < 25) AND (height > 1.70) AND (weight > 90)

Rule  in  SQL: include  only  tuples  that  yield  TRUE  (1.0)

Won’t  return  e.g.


(age=20
height=NULL
weight=100)!

13
4/7/19

How  does  SQL  cope  with  tables  that  have  NULLs?


SELECT *
FROM Student
WHERE age < 25 OR age >= 25

§ Does  this  query  return  all  the  students?


§ Unexpected  behavior
Some  Students  are  not  included!
§ What  about  this  query?
SELECT *
FROM Student
WHERE age < 25 OR age >= 25 OR age IS NULL

Now  it  includes  all  Students!

SQL  ORDER  BY  Keyword:  Sorting  the  Results


§ When  you  use  the  SELECT statement  to  query  data  from  a  table,  the  
result  set  is  not sorted in  any  orders
§ To sort the  result  set,  use  the ORDER  BY  clause
§ The  ORDER  BY keyword  is  used  to  sort  the  result-­‐set  in  ascending or  
descending order
§ By  default the  ORDER  BY  keyword  sorts  the  records  in  ascending  
order  (ASC)
§ To  sort  the  records  in  descending  order,  use  the  DESC keyword
Ordering  is  ascending,  unless  you  specify  the  DESC  keyword

14
4/7/19

SQL  ORDER  BY  Keyword:  Sorting  the  Results


Query: “Select  all  students  whose  student  number  is  less  than  4  from  the  
”Student"  table,  sorted  by  the  ”Name"  column”
Student
StudentNo Name Surname DateOfBirth
1 John Smith 1990-­‐10-­‐12
2 Anne Miller 1992-­‐07-­‐30
3 Betty Jones 1991-­‐03-­‐24
4 Boris Johnson 1992-­‐07-­‐30
Result  Table
SELECT * StudentNo Name Surname DateOfBirth
FROM Student 2 Anne Miller 1992-­‐07-­‐30
WHERE StudentNo < 4
3 Betty Jones 1991-­‐03-­‐24
ORDER BY Name;
1 John Smith 1990-­‐10-­‐-­‐12

SQL  ORDER  BY  Keyword:  Sorting  the  Results


§ We  can  also  ORDER  BY  on  multiple  columns

Query: “Select  all  customers  from  the  ”Customer"  table,  sorted  DESCENDING  
by  the  “contactLastName”  and  ASCENDING  by  the  ”contactFirstName"  column
SELECT *
FROM Customer
ORDER BY contactLastName DESC,
contactFirstName ASC;

Note:  In  the  query  above,  the ORDER  BY clause  sorts  the  
result  set  by  the  last  name  in  descending  order  first  and  
then  sorts  the  sorted  result  set  by  the  first  name  in  
ascending  order  to  produce  the  final  result  set

15
4/7/19

TOP-­‐N  Analysis
§ Used  to  specify  the  number  of  records  to  return
§ Useful  on  large  tables  with  thousands  of  records,  since  returning  a  
large  number  of  records  can  impact  on  performance
§ Ex: Select  the  first  three  records  from  the  ”Student"  table

SQL  Server   MySQL


SELECT TOP 3 * SELECT *
FROM Student; FROM Student
LIMIT 3;

Example: TOP-­‐N  Analysis


§ TOP-­‐3  lowest  salaries § TOP-­‐4  largest  rooms

SELECT name, salary SELECT roomNo, capacity


FROM Employee FROM Room
ORDER BY salary ORDER BY capacity DESC
LIMIT 3; LIMIT 4;

16
4/7/19

PART  II
Multi-­‐Table  Queries

Accessing  More  than  One  Table


§ If  more  than  one  relation  is  specified  in  the  FROM-­‐clause  and    there  is  no  join  
condition,  then  the  CARTESIAN  PRODUCT  of  tuples  is  selected
Student(sid, sname, address, professorid)
Professor(pid, pname, address)
SQL:
SELECT * Query:  “Output  all  students  and  professors”
FROM Student, Professor;

RA:
𝑆𝑡𝑢𝑑𝑒𝑛𝑡  ×  𝑃𝑟𝑜𝑓𝑒𝑠𝑠𝑜𝑟

17
4/7/19

SELECT *
Cross-­‐Product  (×) -­‐ Example FROM Student, Professor;
Student Professor
sid sname address professorid pid pname address
1 Dave 320FL 1 ×
1 MM 141FL
2 Greg 320FL 1
2 ER 201FL
3 Matt 320FL 2
𝑺𝒕𝒖𝒅𝒆𝒏𝒕𝒔  ×  𝑷𝒓𝒐𝒇𝒆𝒔𝒔𝒐𝒓
List  of  tables  without   sid sname address professorid pid pname address
any  condition  in  the  
WHERE  clause  produces   1 Dave 320FL 1 1 MM 141FL
…  Unexpected  result!! 1 Dave 320FL 1 2 ER 201FL
2 Greg 320FL 1 1 MM 141FL
Cartesian  Products   2 Greg 320FL 1 2 ER 201FL
usually  don’t  make  a   3 Matt 320FL 2 1 MM 141FL
lot  of  sense 3 Matt 320FL 2 2 ER 201FL

SQL  JOINs  and  Foreign  Key  constraints


§ We  have  the  following  schema:
Student(sid, sname, address, professorid)
Professor(pid, pname, address)

§ We  impose  the  following  constraint:

We  say  that  professorid is  a  foreign  key that  refers  to  Professor

18
4/7/19

How  do  we  Declare  Foreign  Keys?


Student(sid, sname, address, professorid)
Professor(pid, pname, address)

CREATE TABLE Student(


sid INT NOT NULL,
sname VARCHAR(50),
address CHAR(100),
professorid INT NOT NULL,
PRIMARY KEY (sid),
FOREIGN KEY (professorid) REFERENCES Professor(pid);
)
A FOREIGN KEY is a key used to link two tables together
A FOREIGN KEY in a table points to a PRIMARY KEY in another table

Foreign  Keys  and  UPDATE  operations


Student(sid, sname, address, professorid)
Professor(pid, pname, address)

§ What  if  we  insert  a  tuple  into  student,  but  no  corresponding  professor?
§ INSERT  is  rejected  (foreign  keys  are  constraints)!
The  FOREIGN  KEY  constraint  is  used  to  
prevent  actions  that  would  destroy  links  
between  tables
§ What  if  we  delete  a  professor?
§ Disallow  the  delete The  FOREIGN  KEY  constraint  also  
prevents  invalid  data  from  being  
inserted  into the  foreign  key  column,  
because  it  has  to  be  one  of  the  values  
contained  in  the  table  it  points  to

19
4/7/19

A  join between  tables  returns  


all  unique  combinations  of  their  
The  JOIN  Operation tuples  which  meet  some  
specified  join  condition

§ The  JOIN  operation  links  across  several  tables as  part  of  a  SELECT  
operation
§ You  must  tell  the  JOIN  how  to  use  the  keys  that  make  the  connection  
between  the  tables  using  an  ON  clause
SELECT sname, pname Several  equivalent  ways  to  write  a  
FROM Student, Professor basic  join  in  SQL
WHERE professorid = pid

SELECT sname, pname


FROM Student
JOIN Professor ON ( professorid = pid )

Be  Careful…
§ Joining  two  tables  without  an  On  clause  (OR  without  any  condition  in  the  WHERE  
clause)  gives  all  possible  combinations  of  rows  (3  x  2  =  total  6  tuples)  à Cartesian  
Product
§ When  there  is  no  ON  clause,  we  are  getting  rows,  where  two  things  do  not  match
§ So,  the  On  clause  is  really  important  to  throw  away  non-­‐matching  rows
§ With  ON  clause  you  would  see  only  the  matching  rows,  which  is  what  you  want
sid sname address professorid pid pname address

1 Dave 320FL 1 1 MM 141FL


1 Dave 320FL 1 2 ER 201FL
2 Greg 320FL 1 1 MM 141FL
2 Greg 320FL 1 2 ER 201FL
3 Matt 320FL 2 1 MM 141FL
3 Matt 320FL 2 2 ER 201FL

20
4/7/19

Example: Keys  and  Foreign  Keys


Company
CName StockPrice Country
What  is  a  
GizmoWorks 25 USA
foreign  key  
Canon 65 Japan
here?
Hitachi 15 Japan

Product
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi

Example: Joins
Product(PName, Price, Category, Manufacturer)
FK Company(CName, StockPrice, Country)

Query: “Find  all  products  under  $200  manufactured  in  Japan;


return  their  names  and  prices  

SELECT PName, Price


A  join between  tables  returns  
FROM Product, Company
all  unique  combinations  of  
WHERE Manufacturer = CName
their  tuples  which  meet  
AND Country=‘Japan’
some  specified  join  condition
AND Price <= 200;

21
4/7/19

Example: Joins
Product
Company
PName Price Category Manuf
Cname Stock Country
Gizmo $19 Gadgets GWorks
GWorks 25 USA
Powergizmo $29 Gadgets GWorks
Canon 65 Japan
SingleTouch $149 Photography Canon
Hitachi 15 Japan
MultiTouch $203 Household Hitachi

SELECT PName, Price


FROM Product, Company
WHERE Manufacturer = CName PName Price
AND Country=‘Japan’
SingleTouch $149
AND Price <= 200;

Example: Joins
Product(PName, Price, Category, Manufacturer)
FK Company(CName, StockPrice, Country)

SELECT PName, Price


FROM Product, Company Several  equivalent  ways  to  write  a  
WHERE Manufacturer = CName basic  join  in  SQL
AND Country=‘Japan’
AND Price <= 200;

SELECT PName, Price


FROM Product
JOIN Company ON ( Manufacturer = Cname AND Country=‘Japan’ )
WHERE Price <= 200;

22
4/7/19

Semantics  of  JOINs  (2  tables) SELECT R.A


FROM R,  S
WHERE R.A  =  S.B

Recall:  Cross  product  (A  X  B)  is  the  set  of  


all  unique  tuples  in  A,B
Ex:  {a,b,c}  X  {1,2}
=  {(a,1),  (a,2),  (b,1),  (b,2),  (c,1),  (c,2)}

=  Filtering!

=  Returning  only  some


attributes
Remembering  this  order  is  critical  to  understanding  the  output  of  certain  
queries  (see  later  on…)

An  example  of   A
SELECT R.A Output
SQL  semantics FROM R,  S 3

A WHERE R.A  =  S.B 3

1 A B C
3 1 2 3

Cross  Product 1 3 4 Apply  


B C Apply   Projection
1 3 5 Selections  /  
2 3 Conditions
3 2 3 A B C
3 4
3 3 4 3 3 4
3 5
3 3 5 3 3 5

23
4/7/19

Note:  we  say  “semantics”  not  “execution  order”

§ The  preceding  slides  show  what  a  join  means

§ Not  actually  how  the  DBMS  executes  it  under  the  covers

Aliases:  Tuple  Variable  Ambiguity  in  Multi-­‐Table


Person(name, address, worksfor)
Company(name, address)

SELECT DISTINCT name, address


FROM Person, Company
WHERE worksfor = name;

Which  “address”  does  this  refer  to?

Which  “name”s??

24
4/7/19

Aliases:  Tuple  Variable  Ambiguity  in  Multi-­‐Table


Person(name, address, worksfor)
SELECT DISTINCT name, address
Company(name, address) FROM Person, Company
WHERE worksfor = name;

SELECT DISTINCT Person.name, Person.address


FROM Person, Company
All  are   WHERE Person.worksfor = Company.name;
equivalent  
ways  to  resolve   SELECT DISTINCT p.name, p.address
variable   FROM Person AS p, Company AS c
ambiguity WHERE p.worksfor = c.name;

SELECT DISTINCT p.name, p.address


FROM Person p, Company c
WHERE p.worksfor = c.name;

Aliases
§ SQL  aliases  are  used  to  give  a  table,  or  a  column  in  a  table,  a  temporary  name
§ Aliases  can  also  be  used  to  make  column  names  more  readable
§ An  alias  only  exists  for  the  duration  of  the  query
Result Table
StudentNo Name
Alias  for  Columns
1 John
SELECT StudentNo, Name 2 Anne Result Table
FROM Student; 3 Betty No Student
4 Boris 1 John
SELECT StudentNo AS No, Name AS Student 2 Anne
FROM Student; 3 Betty
4 Boris

25
4/7/19

Aliases
Note: It  requires  double  quotation  marks  or  square  brackets  if  the  alias  
name  contains  spaces
SELECT StudentNo AS “Student No”, Name AS “Student Name”
FROM Student;

Ex: Create  an  alias  named  "Address"  that  combine  four  columns  
(Address,  PostalCode,  City  and  Country)  from  customer  table

SELECT Address + ', ' + PostalCode + ’, ' + City + ', ' + Country AS Address
FROM Customer;

SELECT CONCAT(Address, ‘, ‘, PostalCode, ‘, ‘, City, ‘, ‘, Country) AS Address


FROM Customer;

More  on  JOINs


§ Different  join  variants  are  available  in  SQL
§ (INNER)  JOIN:  Returns  records  that  have  matching  values  in  both  tables
§ LEFT  (OUTER)  JOIN:  Return  all  records  from  the  left  table,  and  the  matched  records  
from  the  right  table
§ RIGHT  (OUTER)  JOIN:  Return  all  records  from  the  right  table,  and  the  matched  records  
from  the  left  table
§ FULL  (OUTER)  JOIN:  Return  all  records  when  there  is  a  match  in  either  left  or  right  
table

26
4/7/19

Inner  Joins
By default,  joins  in  SQL are  “inner  joins”
Product(name, category)
Purchase(prodName, store)

SELECT Product.name, Purchase.store


FROM Product
JOIN Purchase ON Product.name = Purchase.prodName
Both  equivalent:
SELECT Product.name, Purchase.store
Both  INNER  JOINS!
FROM Product, Purchase
WHERE Product.name = Purchase.prodName

However: Products  that  never  sold  (with  no  Purchase  tuple)  will  be  lost!

Inner  Joins Purchase


Product prodName store
name category Gizmo Wiz
Gizmo gadget Camera Ritz
Camera Photo Camera Wiz
OneClick Photo Mamaro Kiz

SELECT Product.name, Purchase.store name store


FROM Product Gizmo Wiz
INNER JOIN Purchase
ON Product.name = Purchase.prodName Camera Ritz
Camera Wiz
Note: another  equivalent  way  to  
write  an  INNER  JOIN!

27
4/7/19

Outer  Joins
§ An  outer  join returns  tuples  from  the  joined  relations  that  
don’t  have  a  corresponding  tuple  in  the  other  relations
§ I.e.  If  we  join  relations  A  and  B  on  a.X =  b.X,  and  there  is  an  entry  in  A  
with  X=5,  but  none  in  B  with  X=5…
§ A  LEFT  OUTER  JOIN  will  return  a  tuple  (a,  NULL)!

SELECT Product.name, Purchase.store


§ Left  outer  joins  in  SQL: FROM Product
LEFT OUTER JOIN Purchase ON
Product.name = Purchase.prodName

Now  we’ll  get  products  even  if  they  didn’t  sell

LEFT  OUTER  JOIN Purchase


Product prodName store
name category Gizmo Wiz
Gizmo gadget Camera Ritz
Camera Photo Camera Wiz
OneClick Photo Mamaro Kiz

name store
SELECT Product.name, Purchase.store Gizmo Wiz
FROM Product Camera Ritz
LEFT OUTER JOIN Purchase
ON Product.name = Purchase.prodName Camera Wiz
OneClick NULL

28
4/7/19

RIGHT  OUTER  JOIN


§ The  RIGHT  JOIN  keyword  returns  all  records  from  the  right  table  
(table2),  and  the  matched  records  from  the  left  table  (table1)
§ The  result  is  NULL from  the  left  side,  when  there  is  no  match

SELECT Product.name, Purchase.store


§ Right  Outer  Joins  in  SQL: FROM Product
RIGHT OUTER JOIN Purchase ON
Product.name = Purchase.prodName

RIGHT  OUTER  JOIN Purchase


Product prodName store
name category Gizmo Wiz
Gizmo gadget Camera Ritz
Camera Photo Camera Wiz
OneClick Photo Mamaro Kiz

name store
SELECT Product.name, Purchase.store Gizmo Wiz
FROM Product Camera Ritz
RIGHT OUTER JOIN Purchase
ON Product.name = Purchase.prodName Camera Wiz
NULL Kiz

29
4/7/19

FULL  OUTER  JOIN


§ The  FULL  OUTER  JOIN  keyword  return  all  records  when  there  is  a  match  in  
either left (table1)  or  right (table2)  table  records
§ Returns  all  the  rows  from  the  left  table  (table1),  and  all  the  rows  from  the  right  table  
(table2)  
§ Plus,  if  there  are  rows  in  ”table1"  that  do  not  have  matches  in  ”table2",  or  if  there  are  
rows  in  ”table2"  that  do  not  have  matches  in  ”table1",  those  rows  will  be  listed  as  well
§ Note: FULL  OUTER  JOIN  can  potentially  return  very  large  result-­‐sets!

§ Full  Outer  Joins  in  SQL: SELECT Product.name, Purchase.store


FROM Product
FULL OUTER JOIN Purchase ON
Product.name = Purchase.prodName

FULL  OUTER  JOIN


Purchase
Product prodName store
name category Gizmo Wiz
Gizmo gadget Camera Ritz
Camera Photo Camera Wiz
OneClick Photo Mamaro Kiz name store
Gizmo Wiz

SELECT Product.name, Purchase.store Camera Ritz


FROM Product Camera Wiz
FULL OUTER JOIN Purchase
OneClick NULL
ON Product.name = Purchase.prodName
NULL Kiz

30
4/7/19

Tips  on  Outer  Joins


§ Left  outer  join
§ Include  the  left  tuple  even  if  there’s  no  match

§ Right  outer  join


§ Include  the  right  tuple  even  if  there’s  no  match

§ Full  outer  join


§ Include  the  both  left  and  right  tuples  even  if  there’s  no  match

PART  III
Set  Operators  &  Nested  Queries

31
4/7/19

Set  Operations
§ The  usual  set  operations  are  also  available  in  SQL
§ UNION
§ INTERSECT
§ EXCEPT  (set  difference)

SQL  UNION
§ The  UNION operator  is  used  to  combine  the  result-­‐set  of  two  or  more  
SELECT statements
§ Each  SELECT statement  within  UNION must  have  the  same  number  of  
columns
§ The  columns must  also  have  similar  data  types
§ The  columns in  each  SELECT statement  must  also  be  in  the  same  order

SELECT column_name(s) A  ∪ 𝐵
There  will  be  NO  duplicates!!  
FROM table1 Only  distinct  values
UNION A B
SELECT column_name(s)
Why  aren’t  there  duplicates?
FROM table2
By  default:  SQL  retains set  semantics  
for  UNIONs,  INTERSECTs!

32
4/7/19

Example: UNION

What  if  we  want  


duplicates?

SQL  UNION  ALL


§ The  UNION operator  selects  only  distinct  values  by  default
§ In  contrast  to  SELECT,  UNION  does  remove  duplicates  
automatically  
§ To  allow  duplicate  values,  use  UNION  ALL

A  ∪ 𝐵
SELECT column_name(s)
ALL  indicates  
FROM table1
UNION ALL A B Multiset
SELECT column_name(s) operations
FROM table2

33
4/7/19

SQL  EXCEPT  -­‐ SQL  INTERSECT

SELECT column_name(s) SELECT column_name(s)


FROM table1 FROM table1
EXCEPT INTERSECT
SELECT column_name(s) SELECT column_name(s)
FROM table2 FROM table2
A  ∩ 𝐵
A  \B

A B A B
What  is  the  
multiset  
versions?

Example: EXCEPT

34
4/7/19

Example:  INTERSECT

Nested  Queries:  Sub-­‐queries  return  relations


§ SQL  allows  nested  queries
§ I.e,  there  may  be  more  than  one  sfw-­‐block  
§ This  nested  sfw-­‐block  can  appear  in  the  where,  from,  and  even  the  
select clause  
§ Basically,  the  “inner”  query  computes  some  kind  of  intermediate  
result  that  is  used  in  the  “outer”  query
§ We  can  do  nested  queries  because  SQL  is  compositional
§ Everything  (inputs  /  outputs)  is  represented  as  multisets-­‐ the  output  of  one  
query  can  thus  be  used  as  the  input  to  another  (nesting)!
§ This  is  extremely  powerful!

35
4/7/19

Nested  queries:  Sub-­‐queries  Returning  Relations


Company(name, city)
Product(name, maker)
Purchase(id, product, buyer)

SELECT c.city “Cities  where  one  


FROM Company c can  find  
WHERE c.name IN ( companies  that  
SELECT pr.maker manufacture  
FROM Purchase p, Product pr products  bought  
WHERE p.product = pr.name by  Joe  Blow”
AND p.buyer = ‘Joe Blow‘)

Example: Nested  Queries

SELECT DISTINCT c.city SELECT DISTINCT c.city


FROM Company c, FROM Company c
Product pr, WHERE c.name IN (
Purchase p SELECT pr.maker
WHERE c.name = pr.maker FROM Purchase p, Product pr
AND pr.name = p.product WHERE p.product = pr.name
AND p.buyer = ‘Joe Blow’ AND p.buyer = ‘Joe Blow‘)

They  are  equivalent

36
4/7/19

Nested  queries:  Sub-­‐queries  Returning  Relations


We  can  also  use  operations  of  the  form:        
§ s  >  ALL R
§ s  <  ANY R
§ EXISTS R
Ex: Product(name, price, category, maker)

SELECT name Find  products  that  


FROM Product are  more  expensive  
WHERE price > ALL( than  all  those  
SELECT price produced  by  
FROM Product “Gizmo-­‐Works”
WHERE maker = ‘Gizmo-Works’)

Nested  queries:  Sub-­‐queries  Returning  Relations


We  can  also  use  operations  of  the  form:        
§ s  >  ALL R
§ s  <  ANY R
§ EXISTS R
Ex: Product(name, price, category, maker) Find  ‘copycat’  
products,  i.e.  
SELECT p1.name products  made  by  
FROM Product p1 competitors  with  
WHERE p1.maker = ‘Gizmo-Works’ the  same  names  as  
AND EXISTS(
SELECT p2.name
products  made  by  
FROM Product p2 “Gizmo-­‐Works”
<>  means  !=
WHERE p2.maker <> ‘Gizmo-Works’
AND p1.name = p2.name)

37
4/7/19

Nested  queries  as  alternatives  to  INTERSECT  and  


EXCEPT
(SELECT R.A, R.B SELECT R.A, R.B
FROM R) FROM R
INTERSECT WHERE EXISTS(
(SELECT S.A, S.B SELECT *
FROM S) FROM S
WHERE R.A=S.A AND R.B=S.B)

(SELECT R.A, R.B SELECT R.A, R.B


FROM R) FROM R
EXCEPT WHERE NOT EXISTS(
(SELECT S.A, S.B SELECT *
FROM S) FROM S INTERSECT  and  
WHERE R.A=S.A AND R.B=S.B) EXCEPT  not  in  some  
DBMSs!

PART  IV
Aggregation  -­‐ GROUP  BY  -­‐ HAVING

38
4/7/19

SQL  Functions
§ SQL  has  many  built-­‐in  functions  for  performing  calculations  on  data
§ SQL  String  Functions
§ SQL  Date  Functions
§ SQL  Aggregate  Functions

Function Description
SQL  String   CHARINDEX
Searches  an  expression  in  a  string  expression  and  returns  its  
starting  position  if  found

Functions CONCAT()

LEFT()

LEN()  /  LENGTH() Returns  the  length  of  the  value  in  a  text  field

LOWER()  /  LCASE() Converts  character  data  to  lower  case

LTRIM()

SUBSTRING()  /  
Extract  characters  from  a  text  field
MID()

PATINDEX()

REPLACE()

RIGHT()

RTRIM()

UPPER()  /  UCASE() Converts  character  data  to  upper  case

39
4/7/19

SQL  Date  Functions


Function Description
NOW() Returns  the  current  date  and  time

CURDATE() Returns  the  current  date

CURTIME() Returns  the  current  time

DATE() Extracts  the  date  part  of  a  date  or  date/time  expression

EXTRACT() Returns  a  single  part  of  a  date/time

DATE_ADD() Adds  a  specified  time  interval  to  a  date

DATE_SUB() Subtracts  a  specified  time  interval  from  a  date

DATEDIFF() Returns  the  number  of  days  between  two  dates

DATE_FORMAT() Displays  date/time  data  in  different  formats

SQL  Aggregate  Functions


SQL  aggregate  functions  return  a  single  value,  calculated  from  values  in  a  column
Function Description
AVG() Returns  the  average  value
COUNT() Returns  the  number  of  rows
FIRST() Returns  the  first  value
LAST() Returns  the  last  value
MAX() Returns  the  largest  value
MIN() Returns  the  smallest  value
ROUND() Rounds  a  numeric  field  to  the  number  of  decimals  specified
SUM() Returns  the  sum

40
4/7/19

Possible  Aggregations  in  SQL

SELECT  COUNT (*)  FROM  Student;


SELECT  COUNT (sNumber)  FROM  Student;
SELECT  MIN (sNumber)  FROM  Student;
SELECT  MAX (sNumber)  FROM  Student;
SELECT  SUM (sNumber)  FROM  Student;
SELECT  AVG (sNumber)  FROM  Student;

Example: Aggregation

SELECT AVG(price) SELECT COUNT(*)


FROM Product FROM Product
WHERE maker = “Toyota” WHERE year > 1995

Except  COUNT,  all  aggregations  


apply  to  a  single  attribute!!

41
4/7/19

Aggregation:  COUNT

COUNT  applies  to  duplicates,  unless  otherwise  stated

SELECT COUNT(category)
FROM Product Note: Same  as  COUNT(*)
WHERE year > 1995

We  probably  want:
SELECT COUNT(DISTINCT category)
FROM Product
WHERE year > 1995

More  Examples
Purchase(product, date, price, quantity)

SELECT SUM(price * quantity)


FROM Purchase

What  do  these  mean?


SELECT SUM(price * quantity)
FROM Purchase
WHERE product = ‘bagel’

42
4/7/19

Simple  Aggregations
Purchase
Product Date Price Quantity
bagel 10/21/2005 1 20
banana 10/03/2005 0.5 10
banana 10/10/2005 1 10
bagel 10/25/2005 1.50 20

SELECT SUM(price * quantity)


FROM Purchase 50    (=  1*20  +  1.50*20)
WHERE product = ‘bagel’

Grouping  and  Aggregation


Purchase(product, date, price, quantity)

SELECT product, Find  total  sales  


SUM(price * quantity) AS TotalSales after  10/1/2005  
FROM Purchase per  product
WHERE date > ‘10/1/2005’
GROUP BY product

The GROUP BY statement is often used with aggregate functions


(COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns

Let’s  see  what  this  means…

43
4/7/19

Grouping  and  Aggregation


Semantics  of  the  query:

1. Compute  the  FROM and  WHERE clauses

2. Group  by  the  attributes  in  the  GROUP  BY

3. Compute  the  SELECT clause:  grouped  attributes  and  aggregates

1.  Compute  the  FROM and WHERE clauses


SELECT product, SUM(price*quantity) AS TotalSales
FROM Purchase
WHERE date > ‘10/1/2005’
GROUP BY product

Purchase
Product Date Price Quantity
FROM bagel 10/21/2005 1 20
banana 10/03/2005 0.5 10
banana 10/10/2005 1 10
bagel 10/25/2005 1.50 20

44
4/7/19

2.  Group  by  the  attributes  in  the  GROUP  BY


SELECT product, SUM(price*quantity) AS TotalSales
FROM Purchase
WHERE date > ‘10/1/2005’
GROUP BY product

Product Date Price Quantity GROUP BY Product Date Price Quantity


Bagel 10/21/2005 1 20 10/21/2005 1 20
Bagel
Bagel 10/03/2005 1.50 20 10/03/2005 1.50 20
Banana 10/10/2005 0.5 10 10/10/2005 0.5 10
Banana
Banana 10/25/2005 1 10 10/25/2005 1 10

3.  Compute  the  SELECT clause:  


Grouped  attributes  and  Aggregates
SELECT product, SUM(price*quantity) AS TotalSales
FROM Purchase
WHERE date > ‘10/1/2005’
GROUP BY product

Product Date Price Quantity


SELECT Product TotalSales
10/21 1 20
Bagel
10/25 1.50 20 Bagel 50
10/3 0.5 10
Banana Banana 15
10/10 1 10

45
4/7/19

HAVING  Clause
The HAVING clause was added to SQL because the WHERE keyword
could not be used with aggregate functions
SELECT product, SUM(price*quantity) Same  query  as  
FROM Purchase before,  except  that  
WHERE date > ‘10/1/2005’ we  consider  only  
GROUP BY product
HAVING SUM(quantity) > 100
products  that  have  
more  than
100  buyers
HAVING  clauses  contains  conditions  on  aggregates

Whereas  WHERE  clauses  condition  on  individual  tuples…

General  form  of  Grouping  and  Aggregation


SELECT S
FROM R1,…,Rn
WHERE C1
GROUP BY a1,…,ak
HAVING C2

§ S    =  Can  ONLY  contain  attributes  a1,…,ak and/or  aggregates  over  other  attributes
§ C1 =  is  any  condition  on  the  attributes  in  R1,…,Rn
§ C2 =  is  any  condition  on  the  aggregate  expressions

46
4/7/19

General  form  of  Grouping  and  Aggregation


SELECT S
FROM R1,…,Rn
WHERE C1
GROUP BY a1,…,ak
HAVING C2

Evaluation  steps
1. Evaluate  FROM-­‐WHERE:  apply  condition  C1 on  the    attributes  in  R1,…,Rn
2. GROUP  BY  the  attributes  a1,…,ak
3. Apply  condition  C2 to  each  group  (may  have  aggregates)
4. Compute  aggregates  in  S  and  return  the  result

Group-­‐by  v.s.  Nested  Query


Author(login, name)
Wrote(login, url)

§ Find  authors  who  wrote  ³ 10  documents:


§ Attempt  1: with  nested  queries
SELECT DISTINCT Author.name This  is
FROM Author SQL  by
a  novice
WHERE COUNT(
SELECT Wrote.url
FROM Wrote
WHERE Author.login = Wrote.login) > 10

47
4/7/19

Group-­‐by  v.s.  Nested  Query


§ Find  all  authors  who  wrote  at  least  10  documents:
§ Attempt  2: SQL  style  (with  GROUP  BY)

SELECT Author.name This  is


SQL    by
FROM Author, Wrote an  expert
WHERE Author.login = Wrote.login
GROUP BY Author.name
HAVING COUNT(Wrote.url) > 10

No  need  for  DISTINCT:  automatically  from  GROUP  BY

Group-­‐by  vs.  Nested  Query


Which  way  is  more  efficient?

§ Attempt  #1-­‐ With  nested:  How  many  times  do  we  do  a  SFW  query  
over  all  of  the  Wrote  relations?

§ Attempt  #2-­‐ With  group-­‐by:  How  about  when  written  this  way?

With  GROUP  BY  can  be  much more  efficient!

48
4/7/19

Writing  Queries:  Rules  of  Thumb


§ Start  with  the  FROM clause
§ Which  table(s)  do  you  need?  
§ If  you  need  more  than  one  table,  determine  the  necessary  join  conditions
§ for  N  tables,  you  typically  need  N  – 1  join  conditions    
§ is  an  outer  join  needed  – i.e.,  do  you  need  to  include  unmatched  tuples?  
§ Determine  if  a  GROUP  BY  clause  is  needed
§ are  you  performing  computations  involving  subgroups?  
§ Determine  any  other  conditions  that  are  needed
§ if  they  rely  on  aggregate  functions,  put  in  a  HAVING  clause  
§ otherwise,  add  to  the  WHERE  clause  
§ Is  a  subquery  needed?  
§ Fill  in  the  rest  of  the  query:  SELECT,  ORDER  BY?

Summary  of  SQL  Queries


§ A  query  in  SQL  can  consist  of  up  to  six  clauses
§ Only  the  first  two,  SELECT and  FROM,  are  mandatory
§ The  clauses  are  specified  in  the  following  order

SELECT <attribute list>


FROM <table list>
[WHERE <condition> ]
[GROUP BY <grouping attribute(s)> ]
[HAVING <group condition> ]
[ORDER BY <attribute list> ]

49
4/7/19

Summary  of  SQL  Queries


§ The  SELECT-­‐clause  lists  the  attributes  or  functions  to  be  retrieved
§ The  FROM-­‐clause specifies  all  relations  (or  aliases)  needed  in  the  
query  but  not  those  needed  in  nested  queries
§ The  WHERE-­‐clause specifies  the  conditions  for  selection  and  join  of  
tuples  from  the  relations  specified  in  the  FROM-­‐clause
§ GROUP  BY  specifies  grouping  attributes
§ HAVING specifies  a  condition  for  selection  of  groups
§ ORDER  BY  specifies  an  order  for  displaying  the  result  of  a  query
§ A  query  is  evaluated  by  first  applying  the  WHERE-­‐clause,  then  GROUP  
BY  and  HAVING,  and  finally  the  SELECT-­‐clause

Summary

SQL  is  a  rich  programming  language  


that  handles  the  way  data  is  processed  
declaratively

50

Você também pode gostar