SQL進階---第二單元(第一到第四課)、Queries



SQL進階---第二單元(第一到第四課)、Manipulation

第二單元、Queries

第十一課、SELECT

QUERIES Queri

In this lesson you will be learningdifferent SQLcommands to query a single table in a database.

One of the core purposes of the SQL language is to retrieveinformation stored in a database. This is commonly referred to as querying.Queries allow us to communic-ate with the database by asking questions and having theresult set return data releva-nt to the question.

In this lesson, you will be querying a database with one tablenamed movies. Let's getstarted.

Instructions

1Let's take a look at the nameand imdb_rating of every movie in thedatabase.

In the code editor type

SELECT name,imdb_ratin

       FROM movies;

SELECT name,imdb_rating

FROM movies;

QueryResults

name

imdb_rating

Avatar

7.9

Jurassic World

7.3

The Avengers

8.1

The Dark Knight

9.0

Star Wars: Episode I - The Phantom Menace

6.6

Star Wars

8.7

Avengers: Age of Ultron

7.9

The Dark Knight Rises

8.5

Pirates of the Caribbean: Dead Mans Chest

7.3

Iron Man 3

7.3

Spider-Man

7.3

Transformers: Revenge of the Fallen

6.0

Star Wars: Episode III - Revenge of the Sith

7.7

Spider-Man 2

7.3

Transformers: Dark of the Moon

6.3

American Sniper

7.4

Furious Seven

7.4

Spider-Man 3

6.2

Guardians of the Galaxy

8.1

Transformers

7.1

Iron Man

7.9

Indiana Jones and the Kingdom of the Crystal Skull

6.2

Iron Man 2

7.1

Star Wars: Episode II - Attack of the Clones

6.7

Pirates of the Caribbean: At Worlds End

7.1

Star Wars: Episode VI - Return of the Jedi

8.4

Independence Day

6.9

Pirates of the Caribbean: The Curse of the Black Pearl

8.1

Skyfall

7.8

Inception

8.8

Man of Steel

7.2

Star Wars: Episode V - The Empire Strikes Back

8.8

The Matrix Reloaded

7.2

The Amazing Spider-Man

7.1

The Incredibles

8.0

Captain America: The Winter Soldier

7.8

The Lego Movie

7.8

Star Trek

8.0

Batman

7.6

Night at the Museum

6.4

Transformers: Age of Extinction

5.8

Twister

6.3

Maleficent

7.0

Pirates of the Caribbean: On Stranger Tides

6.7

Fast & Furious 6

7.2

Beverly Hills Cop

7.3

X-Men: The Last Stand

6.8

X-Men: Days of Future Past

8.1

The Lost World: Jurassic Park

6.5

Star Trek Into Darkness

7.8

Shrek 2

7.2

Toy Story 3

8.4

Frozen

7.6

Finding Nemo

8.2

Despicable Me 2

7.5

Inside Out

8.6

Shrek the Third

6.0

Minions

6.7

Up

8.3

Monsters, Inc.

8.1

Home Alone

7.4

Meet the Fockers

6.3

The Hangover

7.8

Monsters University

7.4

Shrek

7.9

How the Grinch Stole Christmas

6.0

The Hangover Part II

6.5

Despicable Me

7.7

Men in Black

7.2

Toy Story 2

7.9

Cars

7.2

Bruce Almighty

6.7

My Big Fat Greek Wedding

6.6

Shrek Forever After

6.4

Brave

7.2

Ghostbusters

7.8

Rush Hour 2

6.6

Big Hero 6

7.9

Alvin and the Chipmunks: The Squeakquel

4.4

Mrs. Doubtfire

6.9

Ted

7.0

Aladdin

8.0

Alvin and the Chipmunks

5.3

Madagascar 3: Europes Most Wanted

7.0

Kung Fu Panda

7.6

The Lorax

6.5

Austin Powers in Goldmember

6.2

Back to the Future

8.5

Wedding Crashers

7.0

Charlie and the Chocolate Factory

6.7

Ratatouille

8.0

Austin Powers: The Spy Who Shagged Me

6.6

Tangled

7.8

Monsters vs. Aliens

6.6

Happy Feet

6.5

Ice Age: Dawn of the Dinosaurs

7.0

Ice Age: The Meltdown

6.9

World War Z

7.0

What Lies Beneath

6.6

Gremlins

7.2

The Blair Witch Project

6.4

The Conjuring

7.5

The Ring

7.1

The Grudge

5.9

Paranormal Activity

6.3

Interview with the Vampire: The Vampire Chronicles

7.6

Paranormal Activity 3

5.9

Scream

7.2

Jaws 2

5.7

Scream 2

6.1

Sleepy Hollow

7.4

The Others

7.6

The Haunting

4.9

Scream 3

5.5

Saw II

6.6

The Amityville Horror

6.2

Aliens

8.4

Paranormal Activity 2

5.7

Annabelle

5.4

Scooby-Doo 2: Monsters Unleashed

4.9

Insidious: Chapter 2

6.6

Dracula

7.5

Freddy vs. Jason

5.8

Blade II

6.7

Saw III

6.2

The Texas Chainsaw Massacre

6.2

Cloverfield

7.1

Dark Shadows

6.2

The Ring Two

5.4

Zombieland

7.7

Constantine

6.9

The Exorcism of Emily Rose

6.7

I Know What You Did Last Summer

5.6

1408

6.8

Mama

6.2

The Purge: Anarchy

6.5

Scary Movie 2

5.2

Blade

7.1

End of Days

5.7

The Forgotten

5.8

The Final Destination

5.2

Warm Bodies

6.9

Anaconda

4.6

Wolf

6.2

Friday the 13th

5.6

The Purge

5.6

Titanic

7.7

Forrest Gump

8.8

The Twilight Saga: Eclipse

4.9

The Twilight Saga: New Moon

4.6

The Twilight Saga: Breaking Dawn - Part 2

5.6

The Twilight Saga: Breaking Dawn - Part 1

4.9

Beauty and the Beast

8.0

King Kong

7.3

Ghost

7.0

Cinderella

7.1

Gone with the Wind

8.2

Pearl Harbor

6.0

Twilight

5.2

Mr. & Mrs. Smith

6.5

Snow White and the Seven Dwarfs

7.7

What Women Want

6.4

Top Gun

6.8

Pretty Woman

6.9

Hitch

6.7

Tootsie

7.4

Theres Something About Mary

7.1

Elf

6.9

Tarzan

7.2

Bridesmaids

6.8

Fifty Shades of Grey

4.2

Robin Hood: Prince of Thieves

6.9

The Proposal

6.7

The Sound of Music

8.0

Jerry Maguire

7.3

Sex and the City

5.5

Runaway Bride

5.4

Les Misérables

7.6

Knocked Up

7.1

Little Fockers

5.5

As Good as It Gets

7.8

American Pie 2

6.4

The Great Gatsby

7.3

Mamma Mia!

6.4

Juno

7.5

Marley & Me

7.1

Pocahontas

6.6

Slumdog Millionaire

8.0

Look Whos Talking

5.8

Click

6.4

The Lion King

8.5

Harry Potter and the Deathly Hallows: Part 2

8.1

The Sixth Sense

8.2

Jaws

8.1

I Am Legend

7.2

The Blind Side

7.7

Cast Away

7.7

Signs

6.7

Saving Private Ryan

8.6

Dawn of the Planet of the Apes

7.7

Interstellar

8.7

Gladiator

8.5

Dances with Wolves

8.0

The Perfect Storm

6.4

Lincoln

7.4

Rise of the Planet of the Apes

7.6

The Karate Kid

6.2

Rain Man

8.0

Air Force One

6.4

True Grit

7.7

A Beautiful Mind

8.2

The Help

8.1

3 Men and a Baby

5.9

Gone Girl

8.2

Kung Fu Panda 2

7.3

Hannibal

6.7

Catch Me If You Can

8.0

Big Daddy

6.4

Se7en

8.6

Seven

6.1

DatabaseSchema

movies          220 rows

id

INTEGER

name

TEXT

genre

TEXT

year

INTEGER

imdb_rating

REAL

第十二課、Select-l

QUERIES Select-II

SELECT name, imdb_rating 
        FROM movies;

In Lesson 1 you learned that SELECTis used every time you wantto query data from a database.

Multiplecolumns can be queried at once by separating column names with a comma. By specifying name, imdb_rating, the result set contains a name and 

imdb_rating column.

   Instructions

1.Let's continue with the SELECT statement. In the code editor, type

SELECT DISTINCT genre 
        FROM movies;

SELECT DISTINCT genre

FROM movies;

QueryResults

genre

action

comedy

horror

romance

drama

DatabaseSchema

movies       220 rows

id

INTEGER

name

TEXT

genre

TEXT

year

INTEGER

imdb_rating

REAL

第十三課、Select Distinct

QUERIES Select Distinct

SELECT DISTINCT genre 
        FROM movies;

SELECT DISTINCT is used to return uniquevalues in the result set. It filters out all duplicate values. Here, the result set lists each genre in the movies table exactly once.

1. SELECT DISTINCT specifies that the statementis going to be a query that returns unique values in the specified column(s)

2. genre is the name of the column to display in the result set.

3. FROM movies indicates the table name to query fro

Filtering the results of a query is an important skill in SQL.It is easier to see the different possible genres a movie can have after thedata has been filtered, than to scan every row in the table.

The rest of this lesson willteach you different commands in SQL to filter the results of a query.

1.The way tofilter queries in SQL is to use the WHEREclause. In thecode editor type

SELECT * FROM movies WHERE imdb_rating > 8;

SELECT * FROM movies

        WHEREimdb_rating > 8;

QueryResults

id

name

genre

year

imdb_rating

3

The Avengers

action

2012

8.1

4

The Dark Knight

action

2008

9.0

6

Star Wars

action

1977

8.7

8

The Dark Knight Rises

action

2012

8.5

19

Guardians of the Galaxy

action

2014

8.1

26

Star Wars: Episode VI - Return of the Jedi

action

1983

8.4

28

Pirates of the Caribbean: The Curse of the Black Pearl

action

2003

8.1

30

Inception

action

2010

8.8

32

Star Wars: Episode V - The Empire Strikes Back

action

1980

8.8

48

X-Men: Days of Future Past

action

2014

8.1

52

Toy Story 3

comedy

2010

8.4

54

Finding Nemo

comedy

2003

8.2

56

Inside Out

comedy

2015

8.6

59

Up

comedy

2009

8.3

60

Monsters, Inc.

comedy

2001

8.1

88

Back to the Future

comedy

1985

8.5

117

Aliens

horror

1986

8.4

148

Forrest Gump

romance

1994

8.8

157

Gone with the Wind

romance

1939

8.2

191

The Lion King

drama

1994

8.5

192

Harry Potter and the Deathly Hallows: Part 2

drama

2011

8.1

193

The Sixth Sense

drama

1999

8.2

194

Jaws

drama

1975

8.1

199

Saving Private Ryan

drama

1998

8.6

201

Interstellar

drama

2014

8.7

202

Gladiator

drama

2000

8.5

211

A Beautiful Mind

drama

2001

8.2

212

The Help

drama

2011

8.1

214

Gone Girl

drama

2014

8.2

219

Se7en

drama

1995

8.6


DatabaseSchema

Movies          220 rows

id

INTEGER

name

TEXT

genre

TEXT

year

INTEGER

imdb_rating

REAL

第十四課、Where

QUERIES Where

SELECT * FROM movies 
        WHERE imdb_rating > 8;

This statement filters the result set to only include movieswith IMDb ratings greater than 8. How does it work?

  1. WHERE is a clause that indicatesyou want to filter the result set to include only rows where the following condition is true.

2. imdb_rating> 8 is a condition that filters theresult set. Here, only rows with a value greater than 8 in the imdb_rating column will be returned in the result set.

3. > is an operator. Operators create a condition that can be evaluated as eithertrue or false.

Commonoperators used with the WHEREclause are;

  • = equals

  • != not equals

  • > greater than

  • < less than

  • >= greater than or equal to

  • <= less than or equal to

There are also some specialoperators that we will learn more about in the upcoming exercises.

Instructions

1.LIKE is a special operator that can be used in a WHEREclause. In the code editor type

SELECT * FROM movies 
        WHERE name LIKE 'Se_en';

SELECT * FROM movies

                 WHEREname LIKE 'Se_en';

QueryResults

id

name

genre

year

imdb_rating

219

Se7en

drama

1995

8.6

220

Seven

drama

1979

6.1

DatabaseSchema

Movies       220 rows

id

INTEGER

name

TEXT

genre

TEXT

year

INTEGER

imdb_rating

REAL


 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章