Presentation is loading. Please wait.

Presentation is loading. Please wait.

CCT1343, Week 2 Single-Table SQL Yuri Takhteyev University of Toronto

Similar presentations


Presentation on theme: "CCT1343, Week 2 Single-Table SQL Yuri Takhteyev University of Toronto"— Presentation transcript:

1 CCT1343, Week 2 Single-Table SQL Yuri Takhteyev University of Toronto
January 10, 2011 This presentation is licensed under Creative Commons Attribution License, v To view a copy of this license, visit This presentation incorporates images from the Crystal Clear icon collection by Everaldo Coelho, available under LGPL from

2 Connect to the Server ssh kenobio7@yoda.ischool.utoronto.ca
If you are on Windows and do not have git-bash, install from:

3 Important Keys command completion earlier commands quit
“Ctrl+C” is usually represented as “^C” image source:

4 MySQL Prompt mysql> do not confuse with the bash prompt!
Hint: type “exit” or ^C to exit. What do we enter at the mysql prompt?

5 mysql> show databases;
Available Databases mysql> show databases; | Database | | information_schema | | child_care | | diveshop | | kenobio | | menagerie | | starwars | 6 rows in set (0.00 sec)

6 mysql> use menagerie;
Selecting a Database mysql> use menagerie; Database changed

7 mysql> show tables;
Listing Tables mysql> show tables; | Tables_in_menagerie | | event | | owner | | pet | | species | 4 rows in set (0.00 sec)

8 mysql> describe pet;
Describing a Table mysql> describe pet; | Field | Type | Null | Key | Default | Extra | | name | varchar(20) | YES | | NULL | | | owner | varchar(20) | YES | | NULL | | | species | varchar(20) | YES | | NULL | | | sex | char(1) | YES | | NULL | | | birth | date | YES | | NULL | | | death | date | YES | | NULL | | | weight | float | YES | | NULL | | 7 rows in set (0.00 sec)

9 Relation “name” “species” “birth” “owner” “sex”
(Fluffy, Harold, cat, f, ) (Buffy, Harold, dog, f, ) (Chirpy, Gwen, bird, f, ) “name” “species” “birth” “owner” “sex”

10 A Table name Fluffy Bluffy Chirpy owner Harold Gwen species cat dog
bird sex f birth

11 Order Doesn't Matter (in most cases) birth 1993-02-04 1989-05-13
owner Harold Gwen name Fluffy Bluffy Chirpy sex f species cat dog bird

12 Projection name Fluffy Bluffy Chirpy owner Harold Gwen species cat dog
bird sex f birth

13 Projection πname, species, sex(R) name Fluffy Bluffy Chirpy species
cat dog bird sex f πname, species, sex(R)

14 (“Restriction” in Harrington)
Selection (“Restriction” in Harrington) name Fluffy Bluffy Chirpy owner Harold Gwen species cat dog bird sex f birth

15 (“Restriction” in Harrington)
Selection (“Restriction” in Harrington) name Fluffy Bluffy owner Harold species cat dog sex f birth σFluffy, Buffy(R) owner=”Harold” name = “Fluffy” or name = “Bluffy”

16 Columns vs Rows Projection: choosing columns (fields) by name
Selection: choosing rows with a condition

17 selection followed by projection
Basic SELECT 3. 1. 2. select «list of fields» from «souce table» where «conditions»; selection followed by projection

18 For Instance: select name, weight from pet where owner="Harold";
From table pet, select rows satisfying the condition owner=”Harold”, then return fields name and weight.

19 mysql> select name from pet -> where owner="Harold";
Selection in SQL mysql> select name from pet -> where owner="Harold"; | name | weight | | Fluffy | | | Buffy | | 2 rows in set (0.00 sec)

20 select * from «table» where «condition»;
Skipping Projection select * from «table» where «condition»; For instance: select * from pet where owner="Harold";

21 +--------+--------+---------+------+------------+-------+--------+
| name | owner | species | sex | birth | death | weight | | Fluffy | Harold | cat | f | | NULL | | | Buffy | Harold | dog | f | | NULL | | 2 rows in set (0.01 sec)

22 select «fields» from «table» where «condition1» and «condition2»;
Complex Conditions select «fields» from «table» where «condition1» and «condition2»; For instance: select name, weight from pet where owner="Harold" and species="dog";

23 | name | weight | | Buffy | | 1 row in set (0.00 sec)

24 select «fields» from «table» where «condition1» or «condition2»;
Or We Can Use “OR" select «fields» from «table» where «condition1» or «condition2»; For instance: select name, owner, species from pet where owner="Harold" or species="dog";

25 +--------+--------+---------+
| name | owner | species | | Fluffy | Harold | cat | | Buffy | Harold | dog | | Fang | Benny | dog | | Bowser | Diane | dog | 4 rows in set (0.00 sec)

26 select «fields» from «table» where not «condition»;
And Why Not “NOT”? select «fields» from «table» where not «condition»; For instance: select name, owner from pet where not owner="Harold";

27 +----------+-------+
| name | owner | | Claws | Gwen | | Fang | Benny | | Bowser | Diane | | Chirpy | Gwen | | Whistler | Gwen | | Slim | Benny | | Puffball | Diane | 7 rows in set (0.00 sec)

28 «condition1» and not («condition2» or «condition3»)
Combinations «condition1» and not («condition2» or «condition3») For instance: select name from pet where species="dog" and not (owner="Harold" or owner="Gwen");

29 | name | | Fang | | Bowser | 2 rows in set (0.00 sec)

30 «field» = «value» «value» = «value» «field» = «field»
Fields and Values «field» = «value» «value» = «value» «field» = «field» For instance: select name, owner from pet where name=owner;

31 | name | owner | | Margie | Margie | 1 row in set (0.00 sec)

32 An Odd Case "Harold"="Harold" What will we get back?
select name, owner from pet where "Harold"="Harold";

33 +----------+--------+
| name | owner | | Fluffy | Harold | | Claws | Gwen | | Buffy | Harold | | Fang | Benny | | Bowser | Diane | | Chirpy | Gwen | | Whistler | Gwen | | Slim | Benny | | Puffball | Diane | | Margie | Margie | 10 rows in set (0.00 sec)

34 Another Odd Case "Harold"="Gwen" What will we get back?
select name, owner from pet where "Harold"="Gwen";

35 Empty set (0.00 sec)

36 «value» = «function»(«value»)
Functions «value» = «function»(«value») For instance: select name, owner from pet where upper(name)="buffy";

37 | name | owner | | Buffy | Harold | 1 row in set (0.00 sec)

38 where «value»=«value»;
Tests: Equality where «value»=«value»; For instance: select name from pet where owner="Harold";

39 where «value»!=«value»;
Tests: Inequality where «value»!=«value»; For instance: select name, owner from pet where owner!="Harold";

40 +----------+-------+
| name | owner | | Claws | Gwen | | Fang | Benny | | Bowser | Diane | | Chirpy | Gwen | | Whistler | Gwen | | Slim | Benny | | Puffball | Diane | 7 rows in set (0.00 sec)

41 where «field» like «pattern»;
Tests: LIKE where «field» like «pattern»; For instance: select name from pet where name like "%uff%";

42 | name | | Fluffy | | Buffy | | Puffball | 3 rows in set (0.00 sec)

43 More: >, <, <=, >=
where «value»<«value»; For instance: select name, birth from pet where birth<" ";

44 +--------+------------+
| name | birth | | Bowser | | 1 row in set (0.00 sec)

45 «expression» > «expression»
Expressions «expression» > «expression» For instance: select name from pet where weight>birth_weight*50;

46 | name | | Fluffy | | Fang | | Bowser | 3 rows in set (0.01 sec)

47 Combinations For instance: select name from pet
where species="dog" and not (owner="Harold" or owner="Gwen") and weight>birth_weight*50;

48 Combinations For instance: select name from pet where species="dog"
and not (owner="Harold" or owner="Harold") and weight>birth_weight*50;

49 | name | | Fang | | Bowser | 2 rows in set (0.00 sec)

50 Complex Projection For instance: select upper(name), weight/1000
from pet where species="dog";

51 Complex Projection For instance: select upper(name),
round(weight/1000,3) from pet where species="dog";

52 +-------------+----------------------+
| upper(name) | round(weight/1000,3) | | BUFFY | | | FANG | | | BOWSER | | 3 rows in set (0.00 sec)

53 mysql> describe pet;
The 6th Column mysql> describe pet; | Field | Type | Null | Key | Default | Extra | | name | varchar(20) | YES | | NULL | | | owner | varchar(20) | YES | | NULL | | | species | varchar(20) | YES | | NULL | | | sex | char(1) | YES | | NULL | | | birth | date | YES | | NULL | | | death | date | YES | | NULL | | | weight | float | YES | | NULL | | 7 rows in set (0.00 sec)

54 mysql> select name, death > from pet;
What’s in it? mysql> select name, death > from pet; | name | death | | Fluffy | NULL | | Claws | NULL | | Buffy | NULL | | Fang | NULL | | Bowser | | | Chirpy | NULL | | Whistler | NULL | | Slim | NULL | | Puffball | NULL | 9 rows in set (0.00 sec) NULL = no value provided (unknown, does not apply, etc.)

55 IS NULL where «field» is null; For instance:
select name, death from pet where death is null;

56 +----------+-------+
| name | death | | Fluffy | NULL | | Claws | NULL | | Buffy | NULL | | Fang | NULL | | Chirpy | NULL | | Whistler | NULL | | Slim | NULL | | Puffball | NULL | 8 rows in set (0.00 sec)

57 “IS NULL” vs “= NULL” death="NULL" death=NULL
true if the value of death is equal to the four-letter sting “NULL” death=NULL always evaluates to NULL (the condition fails) death is NULL evaluates to TRUE is death is null and to FALSE if death is not null

58 where «field» is not null;
For instance: select name, death from pet where death is not null;

59 +--------+------------+
| name | death | | Bowser | | 1 row in set (0.00 sec)

60 Null with And and Or Null and True → Null Null and False → False
Null or True → True Null or False → Null

61 Duplicates For instance: select species from pet where owner="Gwen";

62 | species | | cat | | bird | 3 rows in set (0.00 sec)

63 select distinct «fields» ...
Removing Duplicates select distinct «fields» ... For instance: select distinct species from pet where owner="Gwen";

64 vs +---------+ | species | | cat | | bird | 2 rows in set (0.00 sec)

65 More SELECT 4. 1. 2. 3. 5. select «list of fields» from «souce table»
where «conditions» order by «field» limit «field»;

66 select ... from ... where ... order by «expression»;
Sorting the Results select ... from ... where ... order by «expression»; For instance: select name, birth from pet order by birth;

67 +----------+------------+
| name | birth | | Bowser | | | Buffy | | | Fang | | | Fluffy | | | Claws | | | Slim | | | Whistler | | | Chirpy | | | Puffball | | 9 rows in set (0.01 sec)

68 select ... from ... where ... order by «expression» desc;
Descending Order select ... from ... where ... order by «expression» desc; For instance: select name, birth from pet order by birth desc;

69 LIMIT select ... from ... limit «N»; For instance:
select name, birth from pet order by birth limit 5;

70 +--------+------------+
| name | birth | | Bowser | | | Buffy | | | Fang | | | Fluffy | | | Claws | | 5 rows in set (0.00 sec)

71 getting back less then one row per matched record
Aggregation getting back less then one row per matched record

72 select upper(name) from pet where birth<"1994-12-31";
A “Normal” Function select upper(name) from pet where birth<" ";

73 | upper(name) | | FLUFFY | | CLAWS | | BUFFY | | FANG | | BOWSER | 5 rows in set (0.00 sec)

74 select count(<fields>) from ...;
Aggregating: COUNT select count(<fields>) from ...; For instance: select count(name) from pet where birth<" ";

75 | count(name) | | | 1 row in set (0.00 sec)

76 Counting Distinct select count(distinct owner) from pet
where birth<" ";

77 +-----------------------+
| count(distinct owner) | | | 1 row in set (0.00 sec)

78 Summation select sum(weight) from pet where species="cat";

79 | sum(weight) | | | 1 row in set (0.00 sec)

80 More Aggregation AVG, MIN, MAX For instance:
select min(weight) from pet;

81 +-------------------+
| min(weight) | | | 1 row in set (0.00 sec)

82 Grouping ... group by <fields>; For instance:
select species, sum(weight) from pet group by species;

83 +---------+-------------------+
| species | sum(weight) | | bird | | | cat | | | dog | | | hamster | | | snake | | 5 rows in set (0.02 sec)

84 HAVING select species, sum(weight) from pet group by species
having sum(weight)<1; Similar to “where” conditions, but uses aggregate functions (or the grouping fields).

85 +---------+-------------------+
| species | sum(weight) | | bird | | | hamster | | | snake | | 3 rows in set (0.00 sec)

86 HAVING group by species... ...having sum(weight)>10
...having avg(weight)>10 ...having weight<1 ...having species="dog" ...having name="Buffy" ...having count(name)>1 ...having count(species)>1 + -

87 HAVING select species, sum(weight) from pet group by species
having sum(weight)<1; Similar to “where” conditions, but uses aggregate functions (or the grouping fields).

88 The Order 6. 1. 2. 3. 4. 5. 7. select «list of fields»
from «souce table» where «conditions» group by «field» having «conditions» order by «field» limit «field»;

89 The Order 6. 1. 2. 3. 4. 5. 7. weight>1 species count(name)>1
sum(weight) select weight from pet where group by having order by limit 1;

90 | birth | | | 1 row in set (0.00 sec)

91 Questions?

92 INSERT add a new record UPDATE modify existing record
Data Modification INSERT add a new record UPDATE modify existing record

93 UPDATE update pet set weigh=100 where name="Buffy";

94 INSERT insert into pet values ("Margie", "Margie", "pony", "f", " ", NULL, 72); here order matters!

95 Assignment 1 1. Will be posted later today 2. To be done individually*
3. Provide explanations for queries 4. Build queries in steps 5. Plan for trial and error


Download ppt "CCT1343, Week 2 Single-Table SQL Yuri Takhteyev University of Toronto"

Similar presentations


Ads by Google