习题四


        习题十九、NULL

 in a given row, there is no value at all fora given column.---IS NULL or IS NOT NULL.

 return all of the rows of family_members where favorite_book is not null?

select * from family_members

where favorite_book is not null

Result:

id

name

gender

species

favorite_book

1

Dave

male

human

To Kill a Mockingbird

2

Mary

female

human

Gone with the Wind

Current tables 4:

celebs_born

id

name

birthdate

1

Michael Jordan

1963-02-17

2

Justin Timberlake

1981-01-31

3

Taylor Swift

1989-12-13

          习题二十、DATE

The first 4 digits represents the year, thenext 2 digits represents the month, and the next 2 digits represents the day ofthe month. 

 < and >. Forexample, 

SELECT * FROM celebs_born

WHERE birthdate < '1985-08-17'; 

returns a list of celebrities that were bornbefore August 17th, 1985.

return a list of celebrities that were bornafter September 1st, 1980?

SELECT * from celebs_born w

here birthdate > '1980-09-01';

Result:

id

name

birthdate

2

Justin Timberlake

1981-01-31

3

Taylor Swift

1989-12-13

涉及到的表格

Current tables5:

character

id

name

1

Doogie Howser

2

Barney Stinson

3

Lily Aldrin

4

Willow Rosenberg

character_tv_show

id

character_id

tv_show_name

1

4

Buffy the Vampire Slayer

2

3

How I Met Your Mother

3

2

How I Met Your Mother

4

1

Doogie Howser, M.D.

character_actor

id

character_id

actor_name

1

4

Alyson Hannigan

2

3

Alyson Hannigan

3

2

Neil Patrick Harris

4

1

Neil Patrick Harris

        习题二十一、INNER JIONS

Different parts of information can be stored in differenttables, and in order to put them together, we use INNERJOIN ... ON. 

To get each character name with his/her TV show name, wecan write 
SELECT character.name, character_tv_show.tv_show_name
             FROM character 


INNER  JOIN character_tv_show

            ON character.id = character_tv_show.character_id;

!!Note:
- We use the syntax table_name.column_name. If weonly used column_name, SQL mightincorrectly assume which table it is coming from.
- The example query above is written overmultiple lines for readability, but that does not affect the query. 
!!
Result:

name

tv_show_name

Doogie Howser

Doogie Howser, M.D.

Barney Stinson

How I Met Your Mother

Lily Aldrin

How I Met Your Mother

Willow Rosenberg

Buffy the Vampire Slayer



你可能感兴趣的:(SQL基础学习)