Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 SELECT statement. 2 Sample database Supplier Part supplies (0,n) colour s# snamep# pname amount citydob price qualitydate."— Presentation transcript:

1 1 SELECT statement

2 2 Sample database Supplier Part supplies (0,n) colour s# snamep# pname amount citydob price qualitydate

3 3 SELECT statement Sample database Supplier( s#, sname, city, dob ) Sample database Supplier( s#, sname, city, dob ) primary key

4 4 SELECT 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

5 5 SELECT 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

6 6 SELECT 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 foreign key

7 7 SELECT 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 8 SELECT statement SELECT statement template

9 9 SELECT statement SELECT statement template SELECT SELECT statement template SELECT

10 10 SELECT statement SELECT statement template SELECT FROM SELECT statement template SELECT FROM

11 11 SELECT statement SELECT statement template SELECT FROM WHERE SELECT statement template SELECT FROM WHERE

12 12 SELECT statement Basic query

13 13 SELECT statement Basic query “Find full information about all suppliers” Basic query “Find full information about all suppliers”

14 14 SELECT statement Basic query “Find full information about all suppliers” SELECTs#, sname, city, dob FROMSupplier or Basic query “Find full information about all suppliers” SELECTs#, sname, city, dob FROMSupplier or

15 15 SELECT statement Basic query “Find full information about all suppliers” SELECTs#, sname, city, dob FROMSupplier or SELECT* FROM Supplier; Basic query “Find full information about all suppliers” SELECTs#, sname, city, dob FROMSupplier or SELECT* FROM Supplier;

16 16 SELECT statement Projection query

17 17 SELECT statement Projection query “Find the numbers, names, and prices of all parts” Projection query “Find the numbers, names, and prices of all parts”

18 18 SELECT statement Projection query “Find the numbers, names, and prices of all parts” SELECTp#, pname, price FROM Part; Projection query “Find the numbers, names, and prices of all parts” SELECTp#, pname, price FROM Part;

19 19 SELECT statement Projection query with duplicates

20 20 SELECT 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 21 SELECT statement Projection query with duplicates “Find the numbers of all suppliers that shipped at least one part” SELECTs# FROM SP; Projection query with duplicates “Find the numbers of all suppliers that shipped at least one part” SELECTs# FROM SP;

22 22 SELECT statement Projection query with no duplicates

23 23 SELECT 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 24 SELECT statement Projection query with no duplicates “Find the numbers of all suppliers that shipped at least one part” SELECTDISTINCT s# FROM SP; Projection query with no duplicates “Find the numbers of all suppliers that shipped at least one part” SELECTDISTINCT s# FROM SP;

25 25 SELECT statement Queries with standard functions

26 26 SELECT statement Queries with standard functions “Find the total number of shipments” Queries with standard functions “Find the total number of shipments”

27 27 SELECT statement Queries with standard functions “Find the total number of shipments” SELECTcount(*) FROM SP; Queries with standard functions “Find the total number of shipments” SELECTcount(*) FROM SP;

28 28 SELECT 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 29 SELECT statement Queries with standard functions “Find the total number of all parts shipped” SELECTsum(quantity) FROM SP; Queries with standard functions “Find the total number of all parts shipped” SELECTsum(quantity) FROM SP;

30 30 SELECT statement Queries with standard functions “Find an average price of all parts” Queries with standard functions “Find an average price of all parts”

31 31 SELECT statement Queries with standard functions “Find an average price of all parts” SELECTavg(price) FROM Part; Queries with standard functions “Find an average price of all parts” SELECTavg(price) FROM Part;

32 32 SELECT statement Queries with standard functions “Find the highest and the lowest price” Queries with standard functions “Find the highest and the lowest price”

33 33 SELECT statement Queries with standard functions “Find the highest and the lowest price” SELECTmax(price), min(price) FROM Part; Queries with standard functions “Find the highest and the lowest price” SELECTmax(price), min(price) FROM Part;

34 34 SELECT statement Queries with elementary condition

35 35 SELECT 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 36 SELECT statement Queries with elementary condition “Find the names and numbers of all gold parts” SELECTp#, pname FROM Part WHERE colour = ‘gold’; Queries with elementary condition “Find the names and numbers of all gold parts” SELECTp#, pname FROM Part WHERE colour = ‘gold’;

37 37 SELECT 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 38 SELECT statement Queries with elementary condition “Find the names and dates of birth of all suppliers born before 1960” SELECTsname, dob FROM Supplier WHEREdob < ‘1-Jan-1960’; Queries with elementary condition “Find the names and dates of birth of all suppliers born before 1960” SELECTsname, dob FROM Supplier WHEREdob < ‘1-Jan-1960’;

39 39 SELECT 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 40 SELECT 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 41 SELECT 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 42 SELECT 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 43 SELECT 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 44 SELECT 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 45 SELECT statement Queries with Boolean expression

46 46 SELECT 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 47 SELECT statement Queries with Boolean expression “Find the prices of all bolts that cost more than 50.0” SELECTprice 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” SELECTprice FROM Part WHERE(pname = ‘bolt’) AND (price > 50.0);

48 48 SELECT 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 49 SELECT statement Queries with Boolean expression “Find the prices of all gold or silver bolts” SELECTprice FROM Part WHERE(pname = ‘bolt’) AND ( (colour = ‘gold’) OR (colour = ‘silver’)); Queries with Boolean expression “Find the prices of all gold or silver bolts” SELECTprice FROM Part WHERE(pname = ‘bolt’) AND ( (colour = ‘gold’) OR (colour = ‘silver’));

50 50 SELECT statement Queries with Boolean expression “Find the prices of all gold or silver bolts” SELECTprice FROM Part WHERE(pname = ‘bolt’) AND (colour in (‘gold’, ‘silver’)); Queries with Boolean expression “Find the prices of all gold or silver bolts” SELECTprice FROM Part WHERE(pname = ‘bolt’) AND (colour in (‘gold’, ‘silver’));

51 51 SELECT 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 52 SELECT statement Queries with Boolean expression “Find the prices of all bolts except gold and silver” SELECTprice 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” SELECTprice FROM Part WHERE(pname = ‘bolt’) AND (colour NOT IN (‘gold’, ‘silver’));

53 53 SELECT 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 54 SELECT statement Queries with Boolean expression “Find the prices of all parts except silver bolts” SELECTprice FROM Part WHERENOT( (pname = ‘bolt’) AND (colour = ‘silver’) ); Queries with Boolean expression “Find the prices of all parts except silver bolts” SELECTprice FROM Part WHERENOT( (pname = ‘bolt’) AND (colour = ‘silver’) );

55 55 SELECT statement Queries with Boolean expression “Find the prices of all parts except silver bolts” SELECTprice FROM Part WHERE(pname <> ‘bolt’) OR (colour <> ‘silver’); Queries with Boolean expression “Find the prices of all parts except silver bolts” SELECTprice FROM Part WHERE(pname <> ‘bolt’) OR (colour <> ‘silver’);

56 56 SELECT statement Sorting

57 57 SELECT statement Sorting “Find all suppliers sorted by names in ascending order Sorting “Find all suppliers sorted by names in ascending order

58 58 SELECT 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 59 SELECT statement Sorting “Find all parts sorted by prices in descending order” Sorting “Find all parts sorted by prices in descending order”

60 60 SELECT 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 61 SELECT statement Queries with NULL values

62 62 SELECT 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 63 SELECT 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 64 SELECT 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 65 SELECT 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 66 SELECT 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


Download ppt "1 SELECT statement. 2 Sample database Supplier Part supplies (0,n) colour s# snamep# pname amount citydob price qualitydate."

Similar presentations


Ads by Google