1 SELECT statement. 2 Sample database Supplier Part supplies (0,n) colour s# snamep# pname amount...

66
1 SELECT statement

Transcript of 1 SELECT statement. 2 Sample database Supplier Part supplies (0,n) colour s# snamep# pname amount...

1

SELECT statementSELECT statement

2

SELECT statementSELECT statement

• Sample database• Sample database

Supplier Part

supplies

(0,n) (0,n)

colours# sname p# pnameamount

city dob price qualitydate

3

SELECT statementSELECT statement

• Sample database

Supplier( s#, sname, city, dob )

• Sample database

Supplier( s#, sname, city, dob )

primary key

4

SELECT statementSELECT statement

• Sample database

Supplier( s#, sname, city, dob )

Part( p#, pname, colour, weight, quality )

• Sample database

Supplier( s#, sname, city, dob )

Part( p#, pname, colour, weight, quality )

primary key

primary key

5

SELECT statementSELECT statement

• Sample database

Supplier( s#, sname, city, dob )

SP( s#, p#, sdate, amount )

Part( p#, pname, colour, weight, quality )

• Sample database

Supplier( s#, sname, city, dob )

SP( s#, p#, sdate, amount )

Part( p#, pname, colour, weight, quality )

primary key

primary key

primary key

6

SELECT statementSELECT statement

• Sample database

Supplier( s#, sname, city, dob )

SP( s#, p#, sdate, amount )

Part( p#, pname, colour, weight, quality )

• Sample database

Supplier( s#, sname, city, dob )

SP( s#, p#, sdate, amount )

Part( p#, pname, colour, weight, quality )

primary key

primary key

primary key

foreign key

foreign key

7

SELECT statementSELECT statement

• Sample database

Supplier( s#, sname, city, dob )

SP( s#, p#, sdate, amount )

Part( p#, pname, colour, weight, quality )

• Sample database

Supplier( s#, sname, city, dob )

SP( s#, p#, sdate, amount )

Part( p#, pname, colour, weight, quality )

8

SELECT statementSELECT statement

• SELECT statement template• SELECT statement template

9

SELECT statementSELECT statement

• SELECT statement template

SELECT < names of attributes >

• SELECT statement template

SELECT < names of attributes >

10

SELECT statementSELECT statement

• SELECT statement template

SELECT <names of attributes>

FROM <names of relational tables>

• SELECT statement template

SELECT <names of attributes>

FROM <names of relational tables>

11

SELECT statementSELECT statement

• SELECT statement template

SELECT <names of attributes>

FROM <names of relational tables>

WHERE <condition>

• SELECT statement template

SELECT <names of attributes>

FROM <names of relational tables>

WHERE <condition>

12

SELECT statementSELECT statement

• Basic query• Basic query

13

SELECT statementSELECT statement

• Basic query

“Find full information about all suppliers”

• Basic query

“Find full information about all suppliers”

14

SELECT statementSELECT statement

• Basic query

“Find full information about all suppliers”

SELECT s#, sname, city, dob

FROM Supplier

or

• Basic query

“Find full information about all suppliers”

SELECT s#, sname, city, dob

FROM Supplier

or

15

SELECT statementSELECT statement

• Basic query

“Find full information about all suppliers”

SELECT s#, sname, city, dob

FROM Supplier

or

SELECT *

FROM Supplier;

• Basic query

“Find full information about all suppliers”

SELECT s#, sname, city, dob

FROM Supplier

or

SELECT *

FROM Supplier;

16

SELECT statementSELECT statement

• Projection query• Projection query

17

SELECT statementSELECT statement

• Projection query

“Find the numbers, names, and prices of all parts”

• Projection query

“Find the numbers, names, and prices of all parts”

18

SELECT statementSELECT statement

• Projection query

“Find the numbers, names, and prices of all parts”

SELECT p#, pname, price

FROM Part;

• Projection query

“Find the numbers, names, and prices of all parts”

SELECT p#, pname, price

FROM Part;

19

SELECT statementSELECT statement

• Projection query with duplicates• Projection query with duplicates

20

SELECT statementSELECT statement

• Projection query with duplicates

“Find the numbers of all suppliers that shipped at least one part”

• Projection query with duplicates

“Find the numbers of all suppliers that shipped at least one part”

21

SELECT statementSELECT statement

• Projection query with duplicates

“Find the numbers of all suppliers that shipped at least one part”

SELECT s#

FROM SP;

• Projection query with duplicates

“Find the numbers of all suppliers that shipped at least one part”

SELECT s#

FROM SP;

22

SELECT statementSELECT statement

• Projection query with no duplicates• Projection query with no duplicates

23

SELECT statementSELECT statement

• Projection query with no duplicates

“Find the numbers of all suppliers that shipped at least one part”

• Projection query with no duplicates

“Find the numbers of all suppliers that shipped at least one part”

24

SELECT statementSELECT statement

• Projection query with no duplicates

“Find the numbers of all suppliers that shipped at least one part”

SELECT DISTINCT s#

FROM SP;

• Projection query with no duplicates

“Find the numbers of all suppliers that shipped at least one part”

SELECT DISTINCT s#

FROM SP;

25

SELECT statementSELECT statement

• Queries with standard functions• Queries with standard functions

26

SELECT statementSELECT statement

• Queries with standard functions

“Find the total number of shipments”

• Queries with standard functions

“Find the total number of shipments”

27

SELECT statementSELECT statement

• Queries with standard functions

“Find the total number of shipments”

SELECT count(*)

FROM SP;

• Queries with standard functions

“Find the total number of shipments”

SELECT count(*)

FROM SP;

28

SELECT statementSELECT statement

• Queries with standard functions

“Find the total number of all parts shipped”

• Queries with standard functions

“Find the total number of all parts shipped”

29

SELECT statementSELECT statement

• Queries with standard functions

“Find the total number of all parts shipped”

SELECT sum(quantity)

FROM SP;

• Queries with standard functions

“Find the total number of all parts shipped”

SELECT sum(quantity)

FROM SP;

30

SELECT statementSELECT statement

• Queries with standard functions

“Find an average price of all parts”

• Queries with standard functions

“Find an average price of all parts”

31

SELECT statementSELECT statement

• Queries with standard functions

“Find an average price of all parts”

SELECT avg(price)

FROM Part;

• Queries with standard functions

“Find an average price of all parts”

SELECT avg(price)

FROM Part;

32

SELECT statementSELECT statement

• Queries with standard functions

“Find the highest and the lowest price”

• Queries with standard functions

“Find the highest and the lowest price”

33

SELECT statementSELECT statement

• Queries with standard functions

“Find the highest and the lowest price”

SELECT max(price), min(price)

FROM Part;

• Queries with standard functions

“Find the highest and the lowest price”

SELECT max(price), min(price)

FROM Part;

34

SELECT statementSELECT statement

• Queries with elementary condition• Queries with elementary condition

35

SELECT statementSELECT statement

• Queries with elementary condition

“Find the names and numbers of all gold parts”

• Queries with elementary condition

“Find the names and numbers of all gold parts”

36

SELECT statementSELECT statement

• Queries with elementary condition

“Find the names and numbers of all gold parts”

SELECT p#, pname

FROM Part

WHERE colour = ‘gold’;

• Queries with elementary condition

“Find the names and numbers of all gold parts”

SELECT p#, pname

FROM Part

WHERE colour = ‘gold’;

37

SELECT statementSELECT statement

• Queries with elementary condition

“Find the names and dates of birth of all suppliers born before 1960”

• Queries with elementary condition

“Find the names and dates of birth of all suppliers born before 1960”

38

SELECT statementSELECT statement

• Queries with elementary condition

“Find the names and dates of birth of all suppliers born before 1960”

SELECT sname, dob

FROM Supplier

WHERE dob < ‘1-Jan-1960’;

• Queries with elementary condition

“Find the names and dates of birth of all suppliers born before 1960”

SELECT sname, dob

FROM Supplier

WHERE dob < ‘1-Jan-1960’;

39

SELECT statementSELECT statement

• Queries with elementary condition

“Find full information about all suppliers living in Paris “

• Queries with elementary condition

“Find full information about all suppliers living in Paris “

40

SELECT statementSELECT statement

• Queries with elementary condition

“Find full information about all suppliers living in Paris “

SELECT *

FROM Supplier

WHERE city = ‘Paris’;

• Queries with elementary condition

“Find full information about all suppliers living in Paris “

SELECT *

FROM Supplier

WHERE city = ‘Paris’;

41

SELECT statementSELECT statement

• Queries with elementary condition

“Find full information about all suppliers whose name starts from letter ‘J’ “

• Queries with elementary condition

“Find full information about all suppliers whose name starts from letter ‘J’ “

42

SELECT statementSELECT statement

• Queries with elementary condition

“Find full information about all suppliers whose name starts from letter ‘J’ “

SELECT *

FROM Supplier

WHERE sname LIKE ‘J%’;

• Queries with elementary condition

“Find full information about all suppliers whose name starts from letter ‘J’ “

SELECT *

FROM Supplier

WHERE sname LIKE ‘J%’;

43

SELECT statementSELECT statement

• Queries with elementary condition

“Find full information about all shipments done between 1990 and 1996”

• Queries with elementary condition

“Find full information about all shipments done between 1990 and 1996”

44

SELECT statementSELECT statement

• Queries with elementary condition

“Find full information about all shipments done between 1990 and 1996”

SELECT *

FROM SP

WHERE sdate BETWEEN ‘01-Jan-1990’ AND

31-Dec-1996’;

• Queries with elementary condition

“Find full information about all shipments done between 1990 and 1996”

SELECT *

FROM SP

WHERE sdate BETWEEN ‘01-Jan-1990’ AND

31-Dec-1996’;

45

SELECT statementSELECT statement

• Queries with Boolean expression• Queries with Boolean expression

46

SELECT statementSELECT statement

• Queries with Boolean expression

“Find the prices of all bolts that cost more than 50.0”

• Queries with Boolean expression

“Find the prices of all bolts that cost more than 50.0”

47

SELECT statementSELECT statement

• Queries with Boolean expression

“Find the prices of all bolts that cost more than 50.0”

SELECT price

FROM Part

WHERE (pname = ‘bolt’) AND

(price > 50.0);

• Queries with Boolean expression

“Find the prices of all bolts that cost more than 50.0”

SELECT price

FROM Part

WHERE (pname = ‘bolt’) AND

(price > 50.0);

48

SELECT statementSELECT statement

• Queries with Boolean expression

“Find the prices of all gold or silver bolts”

• Queries with Boolean expression

“Find the prices of all gold or silver bolts”

49

SELECT statementSELECT statement

• Queries with Boolean expression

“Find the prices of all gold or silver bolts”

SELECT price

FROM Part

WHERE (pname = ‘bolt’) AND

( (colour = ‘gold’) OR

(colour = ‘silver’));

• Queries with Boolean expression

“Find the prices of all gold or silver bolts”

SELECT price

FROM Part

WHERE (pname = ‘bolt’) AND

( (colour = ‘gold’) OR

(colour = ‘silver’));

50

SELECT statementSELECT statement

• Queries with Boolean expression

“Find the prices of all gold or silver bolts”

SELECT price

FROM Part

WHERE (pname = ‘bolt’) AND

(colour in (‘gold’, ‘silver’));

• Queries with Boolean expression

“Find the prices of all gold or silver bolts”

SELECT price

FROM Part

WHERE (pname = ‘bolt’) AND

(colour in (‘gold’, ‘silver’));

51

SELECT statementSELECT statement

• Queries with Boolean expression

“Find the prices of all bolts except gold and silver”

• Queries with Boolean expression

“Find the prices of all bolts except gold and silver”

52

SELECT statementSELECT statement

• Queries with Boolean expression

“Find the prices of all bolts except gold and silver”

SELECT price

FROM Part

WHERE (pname = ‘bolt’) AND

(colour NOT IN (‘gold’, ‘silver’));

• Queries with Boolean expression

“Find the prices of all bolts except gold and silver”

SELECT price

FROM Part

WHERE (pname = ‘bolt’) AND

(colour NOT IN (‘gold’, ‘silver’));

53

SELECT statementSELECT statement

• Queries with Boolean expression

“Find the prices of all parts except silver bolts”

• Queries with Boolean expression

“Find the prices of all parts except silver bolts”

54

SELECT statementSELECT statement

• Queries with Boolean expression

“Find the prices of all parts except silver bolts”

SELECT price

FROM Part

WHERE NOT( (pname = ‘bolt’) AND

(colour = ‘silver’) );

• Queries with Boolean expression

“Find the prices of all parts except silver bolts”

SELECT price

FROM Part

WHERE NOT( (pname = ‘bolt’) AND

(colour = ‘silver’) );

55

SELECT statementSELECT statement

• Queries with Boolean expression

“Find the prices of all parts except silver bolts”

SELECT price

FROM Part

WHERE (pname <> ‘bolt’) OR

(colour <> ‘silver’);

• Queries with Boolean expression

“Find the prices of all parts except silver bolts”

SELECT price

FROM Part

WHERE (pname <> ‘bolt’) OR

(colour <> ‘silver’);

56

SELECT statementSELECT statement

• Sorting• Sorting

57

SELECT statementSELECT statement

• Sorting

“Find all suppliers sorted by names in ascending order

• Sorting

“Find all suppliers sorted by names in ascending order

58

SELECT statementSELECT statement

• Sorting

“Find all suppliers sorted by names in ascending order

SELECT *

FROM Supplier

ORDER BY sname ASC;

• Sorting

“Find all suppliers sorted by names in ascending order

SELECT *

FROM Supplier

ORDER BY sname ASC;

59

SELECT statementSELECT statement

• Sorting

“Find all parts sorted by prices in descending order”

• Sorting

“Find all parts sorted by prices in descending order”

60

SELECT statementSELECT statement

• Sorting

“Find all parts sorted by prices in descending order”

SELECT *

FROM Part

ORDER BY price DESC;

• Sorting

“Find all parts sorted by prices in descending order”

SELECT *

FROM Part

ORDER BY price DESC;

61

SELECT statementSELECT statement

• Queries with NULL values• Queries with NULL values

62

SELECT statementSELECT statement

• Queries with NULL values

“Find full information about all suppliers with unknown date of birth”

• Queries with NULL values

“Find full information about all suppliers with unknown date of birth”

63

SELECT statementSELECT statement

• Queries with NULL values

“Find full information about all suppliers with unknown date of birth”

SELECT *

FROM Supplier

WHERE dob IS NULL;

• Queries with NULL values

“Find full information about all suppliers with unknown date of birth”

SELECT *

FROM Supplier

WHERE dob IS NULL;

64

SELECT statementSELECT statement

• Queries with NULL values

“Find full information about all suppliers with known date of birth”

• Queries with NULL values

“Find full information about all suppliers with known date of birth”

65

SELECT statementSELECT statement

• Queries with NULL values

“Find full information about all suppliers with known date of birth”

SELECT *

FROM Supplier

WHERE dob IS NOT NULL;

• Queries with NULL values

“Find full information about all suppliers with known date of birth”

SELECT *

FROM Supplier

WHERE dob IS NOT NULL;

66

SELECT statementSELECT statement

• Bibliography

R. K. Stephens, et al. Teach Yourself SQL in 21 Days, day 9, week 2

P. O’Neil, Database - Principles, Programming, Performance, chapter 3.3

R. Elmasri, S.B. Navathe, Fundamentals of Database Systems, chapter .2

• Bibliography

R. K. Stephens, et al. Teach Yourself SQL in 21 Days, day 9, week 2

P. O’Neil, Database - Principles, Programming, Performance, chapter 3.3

R. Elmasri, S.B. Navathe, Fundamentals of Database Systems, chapter .2