SQL Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 48

Introduction to Database Systems

In the Lecture Series Introduction to Database Systems


SQL
Presented by Stphane Bressan
Introduction to Database Systems
Structured Query Language
Originally developed in the System-R
project of IBM (1974)
Industry standard for relational databases
(SQL92 is an ANSI/ISO standard)
Introduction to Database Systems
Structured Query Language
Data Definition Language for defining
relations, views, integrity constraints,
triggers
Data Manipulation Language for updating,
and querying
Database Control Language for defining
access rights, concurrency control, etc.
Querying One Table
Find all the information about students
SELECT *
FROM st udent ;
SELECT name, emai l , year , f acul t y, depar t ment , gr aduat e,
FROM st udent ;
Introduction to Database Systems
name email year faculty department graduate
XIE XIN xiexin2011@gmail.com 1/1/2007 Faculty of
Science
Chemistry
HUANG
RAN
huangran1991@yahoo.com 1/8/2007 Faculty of
Science
Biology
GOH ENG
CHYE
gohengchye1992@msn.com 1/8/2007 School of
Computing
CS
GOH HUI
YING
gohhuiying1989@gmail.com 1/1/2008 Faculty of
Science
Biology

Selecting Columns
Find the names and emails of students
SELECT name, emai l
FROM st udent ;
Introduction to Database Systems
name email
XIE XIN xiexin2011@gmail.com
HUANG RAN huangran1991@yahoo.com
GOH ENG CHYE gohengchye1992@msn.com
GOH HUI YING gohhuiying1989@gmail.com

Selecting Rows
Find the names and emails of computer science students
SELECT name, emai l
FROM st udent
WHERE depar t ment =' CS' ;
Introduction to Database Systems
name email
GOH ENG CHYE gohengchye1992@msn.com
ZHOU HUICHAN zhouhuichan1990@msn.com
LIU SHAOJ UN liushaojun2010@msn.com
QIN YIYANG qinyiyang2010@hotmail.com

Selecting Rows
Find the names and emails of students who graduated after '01/08/10'
SELECT name, emai l , gr aduat e
FROM st udent
WHERE gr aduat e>= ' 01/ 08/ 10' ;
The condition must be TRUE (not FALSE, not UNKNOWN)
Introduction to Database Systems
name email graduate
GERALDINE LEE glee@msn.com 01/08/10
ADELINE WONG awong007@msn.com 01/08/10
TANG CHEE YONG tcy@hotmail.com 01/08/10
Querying Multiple Tables
Find the names of students and the titles of the books they own
SELECT st udent . name, book. t i t l e
FROM st udent , copy, book
WHERE st udent . emai l =copy. owner
AND copy. book=book. I SBN13;
Introduction to Database Systems
name title
XIE XIN The Law
XIE XIN Microsoft Office Access 2007: Comprehensive
Concepts and Techniques (Shelly Cashman)
XIE XIN Student Atlas of World Geography
XIE XIN Fire from Ice: Searching for the Truth Behind
the Cold Fusion Furor (Wiley Science
Editions)

Tuple Variables
Find the names of students and the titles of the books they own
SELECT s. name, b. t i t l e
FROM st udent s, copy c, book b
WHERE s. emai l =c. owner
AND c. book=b. I SBN13;
Introduction to Database Systems
name title
XIE XIN The Law
XIE XIN Microsoft Office Access 2007: Comprehensive
Concepts and Techniques (Shelly Cashman)
XIE XIN Student Atlas of World Geography
XIE XIN Fire from Ice: Searching for the Truth Behind
the Cold Fusion Furor (Wiley Science
Editions)

Renaming
Find the names of students who lent a book returned after 2010-03-04 to
anniechapman1991@yahoo.com
SELECT s. name AS owner
FROM l oan l , st udent s
WHERE s. emai l =l . owner
AND l . r et ur ned > ' 2010- 03- 04'
AND l . bor r ower = ' anni echapman1991@yahoo. com' ;
Introduction to Database Systems
owner

YEO J IA HAO
DENNIS BECKHAM
TSO HUI LIN
GE DUO
YEO J IA HAO

Duplicates
Find the different names of students who lent a book returned after 2010-03-04
to anniechapman1991@yahoo.com
SELECT DI STI NCT s. name AS owner
FROM l oan l , st udent s
WHERE s. emai l =l . owner
AND l . r et ur ned > ' 2010- 03- 04'
AND l . bor r ower = ' anni echapman1991@yahoo. com' ;
Introduction to Database Systems
owner

DENNIS BECKHAM
GE DUO
TSO HUI LIN
YEO J IA HAO

Ordering Rows
Find the names of students who lent a book returned after 2010-03-04 to
anniechapman1991@yahoo.com in descending alpha-numerical order
SELECT s. name AS owner
FROM l oan l , st udent s
WHERE s. emai l =l . owner
AND l . r et ur ned > ' 2010- 03- 04'
AND l . bor r ower = ' anni echapman1991@yahoo. com'
ORDER BY name DESC;
Introduction to Database Systems
owner

ZENG YIHUI
YEO J IA HAO
YEO J IA HAO

Ordering Rows
Find the ISBN14 of the books that have been borrowed by
anniechapman1991@yahoo.com , their borrowing and return dates in
ascending order of the borrowing and return dates
SELECT l . book, l . bor r owed, l . r et ur ned
FROM l oan l
WHERE l . bor r ower =' anni echapman1991@yahoo. com'
ORDER BY l . bor r owed, l . r et ur ned;
Introduction to Database Systems
book borrowed returned
978-1405072878 1/1/2010 17/3/2010
978-0596007126 1/1/2010 14/4/2010
978-9562913621 2/1/2010 3/1/2010
978-0470526705 4/1/2010 11/1/2010

Arithmetic
Find the price of the Schaums Outline books and add 50% to it
SELECT m. pr i ce * 1. 5
FROM mgh m
WHERE m. t i t l e LI KE ' %Schaums Out l i ne% ;
Introduction to Database Systems
ISBN13 title authors price
978-0071508612 Schaums Outline of
Calculus
Frank Ayres, Elliott
Mendelson
10
978-0071635264 Schaums Outline of
Chinese Grammar
Claudia Ross 12
978-0071639309 Practice Makes Perfect
Spanish Verb Tenses
Dorothy Richmond 25
(No column name)
15
18
mgh
Aggregate Queries: Counting Rows
Find the total number of (different) books
SELECT COUNT( *)
FROM book b;
SELECT COUNT( DI STI NCT *)
FROM book b;
Introduction to Database Systems
(No column name)
311
Aggregate Queries: Counting Rows
Find the total number of titles
SELECT COUNT( b. t i t l e)
FROM book b;
SELECT COUNT( ALL b. t i t l e)
FROM book b;
Introduction to Database Systems
(No column name)
311
Aggregate Queries: Counting Rows
Find the total number of different titles
SELECT COUNT( DI STI NCT b. t i t l e)
FROM book b;
Introduction to Database Systems
(No column name)
301
Aggregate Queries: Average, Minimum, etc.
Find the average price of a book from McGrawHill;
SELECT AVG( m. pr i ce)
FROM mgh m;
Introduction to Database Systems
ISBN13 title authors price
978-0071508612 Schaums Outline of
Calculus
Frank Ayres, Elliott
Mendelson
10
978-0071635264 Schaums Outline of
Chinese Grammar
Claudia Ross 12
978-0071639309 Practice Makes Perfect
Spanish Verb Tenses
Dorothy Richmond 25
(No column name)
15.66
mgh
Aggregate Queries: Grouping
Find, for each day, the number of books borrowed by
anniechapman1991@yahoo.com
SELECT COUNT( l . book)
FROM l oan l
WHERE l . bor r ower =' anni echapman1991@yahoo. com'
GROUP BY l . bor r owed
Can we get 0?
Introduction to Database Systems
(No column name)
2
1
1
2

Aggregate Queries: Grouping


This one eliminates duplicates
SELECT l . book
FROM l oan l
GROUP BY l . book;
SELECT DI STI NCT l . book
FROM l oan l ;
Introduction to Database Systems
Aggregate Queries: Grouping
Find, for each day, the number of books borrowed by
anniechapman1991@yahoo.com, print the day and the quantity
SELECT l . bor r ower , l . bor r owed, COUNT( l . book)
FROM l oan l
WHERE l . bor r ower =' anni echapman1991@yahoo. com'
GROUP BY l . bor r owed;
Introduction to Database Systems
borrowed Expr1001
1/1/2010 2
2/1/2010 1
4/1/2010 1
12/1/2010 1
16/1/2010 1
Aggregate Queries: Grouping
Find, for each day, the number of books borrowed by
anniechapman1991@yahoo.com, print the borrower, the day and the
quantity
SELECT l . bor r ower , l . bor r owed, COUNT( l . book)
FROM l oan l
WHERE l . bor r ower =' anni echapman1991@yahoo. com'
GROUP BY l . bor r owed
"not a GROUP BY expression"
Introduction to Database Systems
Aggregate Queries: Grouping
Find, for each day, the number of books borrowed by
anniechapman1991@yahoo.com, print the borrower, the day and the
quantity
SELECT l . bor r ower , l . bor r owed, COUNT( l . book)
FROM l oan l
WHERE l . bor r ower =' anni echapman1991@yahoo. com'
GROUP BY l . bor r owed, l . bor r ower
Introduction to Database Systems
borrower borrowed Expr1002
anniechapman1991@yahoo.com 1/1/2010 2
anniechapman1991@yahoo.com 2/1/2010 1
anniechapman1991@yahoo.com 4/1/2010 1
anniechapman1991@yahoo.com 12/1/2010 1

Aggregate Queries: Grouping


Find, for each day, the number of books borrowed, print the borrower, the day
and the quantity
SELECT l . bor r ower , l . bor r owed, COUNT( l . book)
FROM l oan l
GROUP BY l . bor r owed, l . bor r ower ;
Introduction to Database Systems
borrower borrowed Expr1002
angjiayi1990@hotmail.com 1/1/2010 1
anniechapman1991@yahoo.com 1/1/2010 2
davidhall1992@yahoo.com 1/1/2010 1
dennispalmer1992@yahoo.com 1/1/2010 1

Aggregate Queries: Grouping


What does this query find?
SELECT l . bor r ower , l . bor r owed, COUNT( l . book)
FROM l oan l
GROUP BY l . bor r owed, l . bor r ower , l . book;
Introduction to Database Systems
Aggregate Queries: Condition
Find the students who borrowed more that one book on any given day
SELECT l . bor r ower
FROM l oan l
GROUP BY l . bor r owed, l . bor r ower
WHERE COUNT( l . book) >1
Introduction to Database Systems
Incorrect syntax near the keyword 'WHERE'.
Aggregate Queries: Condition
Find the students who borrowed more that one book on any given day
SELECT l . bor r ower
FROM l oan l
GROUP BY l . bor r owed, l . bor r ower
HAVI NG COUNT( l . book) >1;
Introduction to Database Systems
borrower
angjiayi1990@hotmail.com
angjiayi1990@hotmail.com
anniechapman1991@yahoo.com
anniechapman1991@yahoo.com

Aggregate Queries: Condition


Find the different students who borrowed more that one book on any given day
SELECT DI STI NCT l . bor r ower
FROM l oan l
GROUP BY l . bor r owed, l . bor r ower
HAVI NG COUNT( l . book) >1;
Introduction to Database Systems
borrower
angjiayi1990@hotmail.com
anniechapman1991@yahoo.com
anupamaanghan2010@yahoo.com
chewsoennam1989@msn.com

Nested Queries
Find the names of the students from whom anniechapman1991@yahoo.com
borrowed a book and returned it after 2010-03-04
SELECT s. name
FROM st udent s
WHERE s. emai l = ANY ( SELECT l . owner
FROM l oan l
WHERE l . r et ur ned > ' 2010- 03- 04'
AND l . bor r ower = ' anni echapman1991@yahoo. com' ) ;
22 rows
Introduction to Database Systems
Nested Queries
Find the names of the students from whom anniechapman1991@yahoo.com
borrowed a book and returned it after 2010-03-04
SELECT s. name
FROM st udent s
WHERE emai l I N ( SELECT l . owner
FROM l oan l
WHERE l . r et ur ned > ' 2010- 03- 04'
AND l . bor r ower = ' anni echapman1991@yahoo. com' ) ;
22 rows
Introduction to Database Systems
Nested Queries
Find the names of the students from whom anniechapman1991@yahoo.com
borrowed a book and returned it after 2010-03-04
SELECT s. name
FROM l oan l , st udent s
WHERE s. emai l =l . owner
AND l . r et ur ned > ' 2010- 03- 04'
AND l . bor r ower = ' anni echapman1991@yahoo. com' ;
27 rows
Introduction to Database Systems
Nested Queries
Find the different students from whom anniechapman1991@yahoo.com never
borrowed
SELECT s. emai l
FROM st udent s
WHERE s. emai l NOT I N ( SELECT l . owner
FROM l oan l
WHERE l . bor r ower = ' anni echapman1991@yahoo. com' ) ;
72 rows
Introduction to Database Systems
Nested Queries
Find the different students from whom anniechapman1991@yahoo.com never
borrowed
SELECT s. emai l
FROM st udent s
WHERE s. emai l <> ALL ( SELECT l . owner
FROM l oan l
WHERE l . bor r ower =' anni echapman1991@yahoo. com' ) ;
72 rows
Introduction to Database Systems
Nested Queries
Find the different students from whom anniechapman1991@yahoo.com never
borrowed
SELECT s. emai l
FROM st udent s
WHERE NOT EXI STS ( SELECT l . owner
FROM l oan l
WHERE s. emai l = l . owner
AND l . bor r ower = ' anni echapman1991@yahoo. com' ) ;
72 rows
Introduction to Database Systems
Introduction to Database Systems
Nested Queries (Scope)
An attribute can only be used within the
SELECT and WHERE clauses of the
query in which its relation is declared
(FROM clause) and within sub-queries
queries
Introduction to Database Systems
Nested Queries
There can be multiple nested queries and
multiple levels of nested queries
Nested queries can appear in the WHERE
but also the HAVING clauses
Union
SELECT *
FROM st udent T
WHERE T. depar t ment = CS
UNI ON
SELECT *
FROM st udent T
WHERE T. depar t ment = I S
Find all the information about students in the computer science
department or in the information systems department
Introduction to Database Systems
Intersection
SELECT T1. emai l
FROM st udent T
WHERE T1. depar t ment = CS
I NTERSECT
SELECT T2. owner AS emai l
FROM copy T2
WHERE T2. book=' 978- 0262033848
Find the emails of students in the computer science department owning
a book with ISBN14 978-0262033848
Introduction to Database Systems
(Non-Symmetric) Difference
SELECT T1. emai l
FROM st udent T1
WHERE T1. depar t ment = CS
MI NUS
SELECT T2. owner AS emai l
FROM copy T2
WHERE T2. book=' 978- 0262033848
Find all the mails of students in the computer science department but
those owning a book with ISBN14 978-0262033848
Introduction to Database Systems
J oin
SELECT T1. emai l
FROM st udent T1, copy T2
WHERE T1. depar t ment = CS
AND T2. owner =T1. emai l
AND T2. book=' 978- 0262033848
Find all the mails of students in the computer science department
owning a book with ISBN14 978-0262033848
Introduction to Database Systems
Inner J oin
SELECT T1. emai l
FROM st udent T1 I NNER J OI N copy T2
ON T2. owner =T1. emai l
WHERE T1. depar t ment = CS
AND T2. book=' 978- 0262033848
Find all the mails of students in the computer science department
owning a book with ISBN14 978-0262033848
Introduction to Database Systems
Left Outer J oin
Sel ect T1. name, T2. book
FROM st udent T1, copy T2
WHERE T1. emai l =T2. owner
UNI ON
SELECT T3. name, CAST( NULL AS CHAR( 14) AS book
FROM st udent T3
WHERE NOT EXI STS ( SELECT *
FROM copy T4
WHERE T3. emai l =T4. owner )
Find the names of the students and the titles of the books they own. If a
student does not own any book, print a NULL value
Introduction to Database Systems
Left Outer J oin
Sel ect DI STI NCT T1. name, T2. book
FROM st udent T1 LEFT OUTER J OI N copy T2
ON T1. emai l =T2. owner
Find the names of the students and the titles of the books they own. If a
student does not own any book, print a NULL value
Introduction to Database Systems
Right Outer J oin
Sel ect DI STI NCT T2. t i t l e, T1. owner
FROM copy T1 RI GHT OUTER J OI N book T2
ON T1. book=T2. I SBN14
Find the title of books and the emails of their owner. If a book does not
have an owner , print a NULL value
Introduction to Database Systems
Full Outer J oin
Sel ect DI STI NCT T2. a, T1. c
FROM t abl e1 T1 FULL OUTER J OI N t abl e T2
ON T1. b=T2. b
Introduction to Database Systems
Other J oin
(EQUI) J OIN
NATURAL J OIN USING
CROSS J OIN
Introduction to Database Systems
Summary
1. FROM
2. WHERE
3. GROUP BY
4. HAVING
5. ORDER BY
6. SELECT
Introduction to Database Systems
Introduction to Database Systems
Credits
The content of this lecture is based
on chapter 5 of the book
Introduction to database
Systems
By
S. Bressan and B. Catania,
McGraw Hill publisher
Animated characters are animated
using VocaliseTTS under
license from Digital Curiosty
Clipart and media are licensed from
Microsoft Office Online Clipart
and Media
Copyright 2014 by Stphane Bressan

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy