0221 cypher for sql professionals

128
Cypher Query Language Michael Hunger @neo4j Sunday, February 24, 13

Upload: neo4j

Post on 09-May-2015

543 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: 0221 Cypher for SQL Professionals

Cypher Query LanguageMichael Hunger

@neo4j

Sunday, February 24, 13

Page 2: 0221 Cypher for SQL Professionals

The plan

• Why graphs?

• How graphs?

• Why Cypher?

• How Cypher - Cypher & SQL side by side

Sunday, February 24, 13

Page 3: 0221 Cypher for SQL Professionals

Why graphs, why now?

Sunday, February 24, 13

Page 4: 0221 Cypher for SQL Professionals

• Big Data is the trend

Why graphs, why now?

!"#

$

%

&

'

+,

-

.

0

1

23

4

5

7

8

9

:

;

<⚾

>

?@

A

B

C

D E

FGH

IJ

KL

M

N

O !

PQ

R

ST

UV

W

X

Y

Z"

$

✈⛽ -

1

3

4

5☕ [

E

I KMP

S WX

\ [

#

%&

'

+

,.

0

2

7

8

9

:;<

\

⚾ >

?@

A

B

C

]

D

FG

LN

O

Q

R TU

VY

Z

J]

H

!"#

$

%

&

'

+,

-

.

0

1

23

4

5

7

8

9

:

;

<⚾

>

?@

A

B

C

D E

FGH

IJ

KL

M

N

O !

PQ

R

ST

UV

W

X

Y

Z"

$

✈⛽ -

1

3

4

5☕ [

E

I KMP

S WX

\ [

#

%&

'

+

,.

0

2

7

8

9

:;<

\

⚾ >

?@

A

B

C

]

D

FG

LN

O

Q

R TU

VY

Z

J]

HSunday, February 24, 13

Page 5: 0221 Cypher for SQL Professionals

• NOSQL is the answer• Big Data is the trend

Why graphs, why now?

!"#

$

%

&

'

+,

-

.

0

1

23

4

5

7

8

9

:

;

<⚾

>

?@

A

B

C

D E

FGH

IJ

KL

M

N

O !

PQ

R

ST

UV

W

X

Y

Z"

$

✈⛽ -

1

3

4

5☕ [

E

I KMP

S WX

\ [

#

%&

'

+

,.

0

2

7

8

9

:;<

\

⚾ >

?@

A

B

C

]

D

FG

LN

O

Q

R TU

VY

Z

J]

H

!"#

$

%

&

'

+,

-

.

0

1

23

4

5

7

8

9

:

;

<⚾

>

?@

A

B

C

D E

FGH

IJ

KL

M

N

O !

PQ

R

ST

UV

W

X

Y

Z"

$

✈⛽ -

1

3

4

5☕ [

E

I KMP

S WX

\ [

#

%&

'

+

,.

0

2

7

8

9

:;<

\

⚾ >

?@

A

B

C

]

D

FG

LN

O

Q

R TU

VY

Z

J]

H

!"#

$

%

&

'

+,

-

.

0

1

23

4

5

7

8

9

:

;

<⚾

>

?@

A

B

C

D E

FGH

IJ

KL

M

N

O !

PQ

R

ST

UV

W

X

Y

Z"

$

✈⛽ -

1

3

4

5☕ [

E

I KMP

S WX

\ [

#

%&

'

+

,.

0

2

7

8

9

:;<

\

⚾ >

?@

A

B

C

]

D

FG

LN

O

Q

R TU

VY

Z

J]

H

!"#

$

%

&

'

+,

-

.

0

1

23

4

5

7

8

9

:

;

<⚾

>

?@

A

B

C

D E

FGH

IJ

KL

M

N

O !

PQ

R

ST

UV

W

X

Y

Z"

$

✈⛽ -

1

3

4

5☕ [

E

I KMP

S WX

\ [

#

%&

'

+

,.

0

2

7

8

9

:;<

\

⚾ >

?@

A

B

C

]

D

FG

LN

O

Q

R TU

VY

Z

J]

H

Sunday, February 24, 13

Page 6: 0221 Cypher for SQL Professionals

• NOSQL is the answer• Big in size and in complexity

• Big Data is the trend

Why graphs, why now?

!"#

$

%

&

'

+,

-

.

0

1

23

4

5

7

8

9

:

;

<⚾

>

?@

A

B

C

D E

FGH

IJ

KL

M

N

O !

PQ

R

ST

UV

W

X

Y

Z"

$

✈⛽ -

1

3

4

5☕ [

E

I KMP

S WX

\ [

#

%&

'

+

,.

0

2

7

8

9

:;<

\

⚾ >

?@

A

B

C

]

D

FG

LN

O

Q

R TU

VY

Z

J]

H

!"#

$

%

&

'

+,

-

.

0

1

23

4

5

7

8

9

:

;

<⚾

>

?@

A

B

C

D E

FGH

IJ

KL

M

N

O !

PQ

R

ST

UV

W

X

Y

Z"

$

✈⛽ -

1

3

4

5☕ [

E

I KMP

S WX

\ [

#

%&

'

+

,.

0

2

7

8

9

:;<

\

⚾ >

?@

A

B

C

]

D

FG

LN

O

Q

R TU

VY

Z

J]

H

!"#

$

%

&

'

+,

-

.

0

1

23

4

5

7

8

9

:

;

<⚾

>

?@

A

B

C

D E

FGH

IJ

KL

M

N

O !

PQ

R

ST

UV

W

X

Y

Z"

$

✈⛽ -

1

3

4

5☕ [

E

I KMP

S WX

\ [

#

%&

'

+

,.

0

2

7

8

9

:;<

\

⚾ >

?@

A

B

C

]

D

FG

LN

O

Q

R TU

VY

Z

J]

H

!"#

$

%

&

'

+,

-

.

0

1

23

4

5

7

8

9

:

;

<⚾

>

?@

A

B

C

D E

FGH

IJ

KL

M

N

O !

PQ

R

ST

UV

W

X

Y

Z"

$

✈⛽ -

1

3

4

5☕ [

E

I KMP

S WX

\ [

#

%&

'

+

,.

0

2

7

8

9

:;<

\

⚾ >

?@

A

B

C

]

D

FG

LN

O

Q

R TU

VY

Z

J]

H

!"#

$

%

&

'

+,

-

.

0

1

23

4

5

7

8

9

:

;

<⚾

>

?@

A

B

C

D E

FGH

IJ

KL

M

N

O !

PQ

R

ST

UV

W

X

Y

Z"

$

✈⛽ -

1

3

4

5☕ [

E

I KMP

S WX

\ [

#

%&

'

+

,.

0

2

7

8

9

:;<

\

⚾ >

?@

A

B

C

]

D

FG

LN

O

Q

R TU

VY

Z

J]

H

!"#

$

%

&

'

+,

-

.

0

1

23

4

5

7

8

9

:

;

<⚾

>

?@

A

B

C

D E

FGH

IJ

KL

M

N

O !

PQ

R

ST

UV

W

X

Y

Z"

$

✈⛽ -

1

3

4

5☕ [

E

I KMP

S WX

\ [

#

%&

'

+

,.

0

2

7

8

9

:;<

\

⚾ >

?@

A

B

C

]

D

FG

LN

O

Q

R TU

VY

Z

J]

H

!"#

$

%

&

'

+,

-

.

0

1

23

4

5

7

8

9

:

;

<⚾

>

?@

A

B

C

D E

FGH

IJ

KL

M

N

O !

PQ

R

ST

UV

W

X

Y

Z"

$

✈⛽ -

1

3

4

5☕ [

E

I KMP

S WX

\ [

#

%&

'

+

,.

0

2

7

8

9

:;<

\

⚾ >

?@

A

B

C

]

D

FG

LN

O

Q

R TU

VY

Z

J]

H

!"#

$

%

&

'

+,

-

.

0

1

23

4

5

7

8

9

:

;

<⚾

>

?@

A

B

C

D E

FGH

IJ

KL

M

N

O !

PQ

R

ST

UV

W

X

Y

Z"

$

✈⛽ -

1

3

4

5☕ [

E

I KMP

S WX

\ [

#

%&

'

+

,.

0

2

7

8

9

:;<

\

⚾ >

?@

A

B

C

]

D

FG

LN

O

Q

R TU

VY

Z

J]

H

!"#

$

%

&

'

+,

-

.

0

1

23

4

5

7

8

9

:

;

<⚾

>

?@

A

B

C

D E

FGH

IJ

KL

M

N

O !

PQ

R

ST

UV

W

X

Y

Z"

$

✈⛽ -

1

3

4

5☕ [

E

I KMP

S WX

\ [

#

%&

'

+

,.

0

2

7

8

9

:;<

\

⚾ >

?@

A

B

C

]

D

FG

LN

O

Q

R TU

VY

Z

J]

H

!"#

$

%

&

'

+,

-

.

0

1

23

4

5

7

8

9

:

;

<⚾

>

?@

A

B

C

D E

FGH

IJ

KL

M

N

O !

PQ

R

ST

UV

W

X

Y

Z"

$

✈⛽ -

1

3

4

5☕ [

E

I KMP

S WX

\ [

#

%&

'

+

,.

0

2

7

8

9

:;<

\

⚾ >

?@

A

B

C

]

D

FG

LN

O

Q

R TU

VY

Z

J]

H

!"#

$

%

&

'

+,

-

.

0

1

23

4

5

7

8

9

:

;

<⚾

>

?@

A

B

C

D E

FGH

IJ

KL

M

N

O !

PQ

R

ST

UV

W

X

Y

Z"

$

✈⛽ -

1

3

4

5☕ [

E

I KMP

S WX

\ [

#

%&

'

+

,.

0

2

7

8

9

:;<

\

⚾ >

?@

A

B

C

]

D

FG

LN

O

Q

R TU

VY

Z

J]

H

!"#

$

%

&

'

+,

-

.

0

1

23

4

5

7

8

9

:

;

<⚾

>

?@

A

B

C

D E

FGH

IJ

KL

M

N

O !

PQ

R

ST

UV

W

X

Y

Z"

$

✈⛽ -

1

3

4

5☕ [

E

I KMP

S WX

\ [

#

%&

'

+

,.

0

2

7

8

9

:;<

\

⚾ >

?@

A

B

C

]

D

FG

LN

O

Q

R TU

VY

Z

J]

H

Sunday, February 24, 13

Page 7: 0221 Cypher for SQL Professionals

A graph?Yes, a graph

Sunday, February 24, 13

Page 8: 0221 Cypher for SQL Professionals

A graph database...

• Mathematical graphs

• remember linked lists, trees?

• the general-purpose data structure

• “A relational database may tell you the average age of everyone in this room, but a graph database will tell you who is most likely to buy you a beer.”

Sunday, February 24, 13

Page 9: 0221 Cypher for SQL Professionals

Some well-known graphs

Sunday, February 24, 13

Page 10: 0221 Cypher for SQL Professionals

Some well-known graphs

diamond

Sunday, February 24, 13

Page 11: 0221 Cypher for SQL Professionals

Some well-known graphs

diamond butterfly

Sunday, February 24, 13

Page 12: 0221 Cypher for SQL Professionals

Some well-known graphs

diamond butterfly star

Sunday, February 24, 13

Page 13: 0221 Cypher for SQL Professionals

Some well-known graphs

diamond butterfly bullstar

Sunday, February 24, 13

Page 14: 0221 Cypher for SQL Professionals

Some well-known graphs

diamond butterfly bullstar

franklin

Sunday, February 24, 13

Page 15: 0221 Cypher for SQL Professionals

robertson

Some well-known graphs

diamond butterfly bullstar

franklin

Sunday, February 24, 13

Page 16: 0221 Cypher for SQL Professionals

robertson

Some well-known graphs

diamond butterfly bullstar

franklin horton

Sunday, February 24, 13

Page 17: 0221 Cypher for SQL Professionals

robertson

Some well-known graphs

diamond butterfly bullstar

franklin horton neo4j

Sunday, February 24, 13

Page 18: 0221 Cypher for SQL Professionals

Sunday, February 24, 13

Page 19: 0221 Cypher for SQL Professionals

Graphs are everywhere

Sunday, February 24, 13

Page 20: 0221 Cypher for SQL Professionals

The property graph model

Sunday, February 24, 13

Page 21: 0221 Cypher for SQL Professionals

The property graph model

Sunday, February 24, 13

Page 22: 0221 Cypher for SQL Professionals

The property graph model

Sunday, February 24, 13

Page 23: 0221 Cypher for SQL Professionals

The property graph model

:LOVES

:LOVES

:KNOWS

:KNOWS

:KNOWS

Sunday, February 24, 13

Page 24: 0221 Cypher for SQL Professionals

The property graph model

:LOVES

:LOVES

:KNOWS

:KNOWS

:KNOWS

name: “A”age: 35 name: “B”

age: 35

name: “D”age: 35

name: “C”age: 35

since: 2012

since: 2008

Sunday, February 24, 13

Page 25: 0221 Cypher for SQL Professionals

Sunday, February 24, 13

Page 26: 0221 Cypher for SQL Professionals

You know relational

Sunday, February 24, 13

Page 27: 0221 Cypher for SQL Professionals

You know relational

Sunday, February 24, 13

Page 28: 0221 Cypher for SQL Professionals

You know relational

foo

Sunday, February 24, 13

Page 29: 0221 Cypher for SQL Professionals

You know relational

foo bar

Sunday, February 24, 13

Page 30: 0221 Cypher for SQL Professionals

You know relational

foo barfoo_bar

Sunday, February 24, 13

Page 31: 0221 Cypher for SQL Professionals

You know relational

foo barfoo_bar

Sunday, February 24, 13

Page 32: 0221 Cypher for SQL Professionals

You know relational

foo barfoo_bar

Sunday, February 24, 13

Page 33: 0221 Cypher for SQL Professionals

You know relational

foo barfoo_bar

Sunday, February 24, 13

Page 34: 0221 Cypher for SQL Professionals

You know relationalnow consider relationships...

Sunday, February 24, 13

Page 35: 0221 Cypher for SQL Professionals

You know relationalnow consider relationships...

Sunday, February 24, 13

Page 36: 0221 Cypher for SQL Professionals

You know relationalnow consider relationships...

Sunday, February 24, 13

Page 37: 0221 Cypher for SQL Professionals

You know relationalnow consider relationships...

Sunday, February 24, 13

Page 38: 0221 Cypher for SQL Professionals

You know relationalnow consider relationships...

Sunday, February 24, 13

Page 39: 0221 Cypher for SQL Professionals

You know relationalnow consider relationships...

Sunday, February 24, 13

Page 40: 0221 Cypher for SQL Professionals

Neo4j is a graph database

• Open Source

• High Availability

• Backed by Neo Technology

• In production for 10+ years

Sunday, February 24, 13

Page 41: 0221 Cypher for SQL Professionals

Why Cypher?

• Not only scale of size is hard

• Scale of complexity is pretty hard as well

Sunday, February 24, 13

Page 42: 0221 Cypher for SQL Professionals

Cypher attributes

Sunday, February 24, 13

Page 43: 0221 Cypher for SQL Professionals

Cypher attributes

#1 Declarative

Sunday, February 24, 13

Page 44: 0221 Cypher for SQL Professionals

Cypher attributes

#1 Declarative

You tell Cypher what you want, not how to get it

Sunday, February 24, 13

Page 45: 0221 Cypher for SQL Professionals

Cypher attributes

Sunday, February 24, 13

Page 46: 0221 Cypher for SQL Professionals

Cypher attributes

#2 Expressive

Sunday, February 24, 13

Page 47: 0221 Cypher for SQL Professionals

Cypher attributes

#2 Expressive

Optimize syntax for reading

Sunday, February 24, 13

Page 48: 0221 Cypher for SQL Professionals

Cypher attributes

Sunday, February 24, 13

Page 49: 0221 Cypher for SQL Professionals

Cypher attributes

#3 Pattern Matching

Sunday, February 24, 13

Page 50: 0221 Cypher for SQL Professionals

Cypher attributes

#3 Pattern Matching

Patterns are easy for your brain

Sunday, February 24, 13

Page 51: 0221 Cypher for SQL Professionals

Cypher attributes

Sunday, February 24, 13

Page 52: 0221 Cypher for SQL Professionals

Cypher attributes

#4 Idempotent

Sunday, February 24, 13

Page 53: 0221 Cypher for SQL Professionals

Cypher attributes

#4 Idempotent

State change should be expressed idempotently

Sunday, February 24, 13

Page 54: 0221 Cypher for SQL Professionals

2 Databases5 Queries

Sunday, February 24, 13

Page 55: 0221 Cypher for SQL Professionals

Sunday, February 24, 13

Page 56: 0221 Cypher for SQL Professionals

Sunday, February 24, 13

Page 57: 0221 Cypher for SQL Professionals

Give me all persons older than 30 years

Q1

Sunday, February 24, 13

Page 58: 0221 Cypher for SQL Professionals

START n=node:Person("name:*") WHERE n.age>30 RETURN n

SELECT *FROM PersonWHERE age > 30

VS

Q1

Sunday, February 24, 13

Page 59: 0221 Cypher for SQL Professionals

START n=node:Person("name:*") WHERE n.age>30 RETURN n

SELECT *FROM PersonWHERE age > 30

VS

Q1

Sunday, February 24, 13

Page 60: 0221 Cypher for SQL Professionals

START n=node:Person("name:*") WHERE n.age>30 RETURN n

SELECT *FROM PersonWHERE age > 30

VS

Q1

Sunday, February 24, 13

Page 61: 0221 Cypher for SQL Professionals

START n=node:Person("name:*") WHERE n.age>30 RETURN n

SELECT *FROM PersonWHERE age > 30

VS

Q1

Sunday, February 24, 13

Page 62: 0221 Cypher for SQL Professionals

Show me all actors, and which movies they starred in

Q2

Sunday, February 24, 13

Page 63: 0221 Cypher for SQL Professionals

Neo4j is whiteboard friendly

Sunday, February 24, 13

Page 64: 0221 Cypher for SQL Professionals

Neo4j is whiteboard friendly

(actor)-[:ACTS_IN]->(movie)

Sunday, February 24, 13

Page 65: 0221 Cypher for SQL Professionals

Neo4j is whiteboard friendly

(actor)-[:ACTS_IN]->(movie)

Sunday, February 24, 13

Page 66: 0221 Cypher for SQL Professionals

START actor=node:Person("name:*") MATCH (actor)-[:ACTS_IN]->(movie)RETURN actor.name, movie.title

SELECT Person.name, Movie.titleFROM Person JOIN Actor on Person.person_id = Actor.person_id JOIN Movie on Movie.movie_id = Actor.movie_id

VS

Q2

Sunday, February 24, 13

Page 67: 0221 Cypher for SQL Professionals

START actor=node:Person("name:*") MATCH (actor)-[:ACTS_IN]->(movie)RETURN actor.name, movie.title

SELECT Person.name, Movie.titleFROM Person JOIN Actor on Person.person_id = Actor.person_id JOIN Movie on Movie.movie_id = Actor.movie_id

VS

Q2

Sunday, February 24, 13

Page 68: 0221 Cypher for SQL Professionals

START actor=node:Person("name:*") MATCH (actor)-[:ACTS_IN]->(movie)RETURN actor.name, movie.title

SELECT Person.name, Movie.titleFROM Person JOIN Actor on Person.person_id = Actor.person_id JOIN Movie on Movie.movie_id = Actor.movie_id

VS

Q2

Sunday, February 24, 13

Page 69: 0221 Cypher for SQL Professionals

START actor=node:Person("name:*") MATCH (actor)-[:ACTS_IN]->(movie)RETURN actor.name, movie.title

SELECT Person.name, Movie.titleFROM Person JOIN Actor on Person.person_id = Actor.person_id JOIN Movie on Movie.movie_id = Actor.movie_id

VS

Q2

Sunday, February 24, 13

Page 70: 0221 Cypher for SQL Professionals

START actor=node:Person("name:*") MATCH (actor)-[:ACTS_IN]->(movie)RETURN actor.name, movie.title

SELECT Person.name, Movie.titleFROM Person JOIN Actor on Person.person_id = Actor.person_id JOIN Movie on Movie.movie_id = Actor.movie_id

VS

Q2

Sunday, February 24, 13

Page 71: 0221 Cypher for SQL Professionals

START actor=node:Person("name:*") MATCH (actor)-[:ACTS_IN]->(movie)RETURN actor.name, movie.title

SELECT Person.name, Movie.titleFROM Person JOIN Actor on Person.person_id = Actor.person_id JOIN Movie on Movie.movie_id = Actor.movie_id

VS

Q2

Sunday, February 24, 13

Page 72: 0221 Cypher for SQL Professionals

Show me all persons that are both actors and directors

Q3

Sunday, February 24, 13

Page 73: 0221 Cypher for SQL Professionals

START person=node:Person("name:*") WHERE (person)-[:ACTS_IN]->() AND (person)-[:DIRECTED]->()RETURN person.name

SELECT nameFROM PersonWHERE person_id IN (

SELECT person_id FROM Actor) AND person_id IN (

SELECT person_id FROM Director)

VS

Q3

Sunday, February 24, 13

Page 74: 0221 Cypher for SQL Professionals

START person=node:Person("name:*") WHERE (person)-[:ACTS_IN]->() AND (person)-[:DIRECTED]->()RETURN person.name

SELECT nameFROM PersonWHERE person_id IN (

SELECT person_id FROM Actor) AND person_id IN (

SELECT person_id FROM Director)

VS

Q3

Sunday, February 24, 13

Page 75: 0221 Cypher for SQL Professionals

START person=node:Person("name:*") WHERE (person)-[:ACTS_IN]->() AND (person)-[:DIRECTED]->()RETURN person.name

SELECT nameFROM PersonWHERE person_id IN (

SELECT person_id FROM Actor) AND person_id IN (

SELECT person_id FROM Director)

VS

Q3

Sunday, February 24, 13

Page 76: 0221 Cypher for SQL Professionals

START person=node:Person("name:*") WHERE (person)-[:ACTS_IN]->() AND (person)-[:DIRECTED]->()RETURN person.name

SELECT nameFROM PersonWHERE person_id IN (

SELECT person_id FROM Actor) AND person_id IN (

SELECT person_id FROM Director)

VS

Q3

Sunday, February 24, 13

Page 77: 0221 Cypher for SQL Professionals

START person=node:Person("name:*") WHERE (person)-[:ACTS_IN]->() AND (person)-[:DIRECTED]->()RETURN person.name

SELECT nameFROM PersonWHERE person_id IN (

SELECT person_id FROM Actor) AND person_id IN (

SELECT person_id FROM Director)

VS

Q3

Sunday, February 24, 13

Page 78: 0221 Cypher for SQL Professionals

Show me all Lucy’s co-actors

Q4

Sunday, February 24, 13

Page 79: 0221 Cypher for SQL Professionals

START lucy=node:Person(name="Lucy Liu")MATCH lucy-[:ACTS_IN]->movie, co_actor-[:ACTS_IN]->movieRETURN DISTINCT co_actor.name

SELECT DISTINCT co_actor.nameFROM Person lucy JOIN Actor a1 ON lucy.person_id = a1.person_id JOIN Actor a2 ON a1.movie_id = a2.movie_id JOIN Person co_actor ON co_actor.person.id = a2.person_idWHERE lucy.name = "Lucy Liu"

VS

Q4

Sunday, February 24, 13

Page 80: 0221 Cypher for SQL Professionals

START lucy=node:Person(name="Lucy Liu")MATCH lucy-[:ACTS_IN]->movie, co_actor-[:ACTS_IN]->movieRETURN DISTINCT co_actor.name

SELECT DISTINCT co_actor.nameFROM Person lucy JOIN Actor a1 ON lucy.person_id = a1.person_id JOIN Actor a2 ON a1.movie_id = a2.movie_id JOIN Person co_actor ON co_actor.person.id = a2.person_idWHERE lucy.name = "Lucy Liu"

VS

Q4

Sunday, February 24, 13

Page 81: 0221 Cypher for SQL Professionals

START lucy=node:Person(name="Lucy Liu")MATCH lucy-[:ACTS_IN]->movie, co_actor-[:ACTS_IN]->movieRETURN DISTINCT co_actor.name

SELECT DISTINCT co_actor.nameFROM Person lucy JOIN Actor a1 ON lucy.person_id = a1.person_id JOIN Actor a2 ON a1.movie_id = a2.movie_id JOIN Person co_actor ON co_actor.person.id = a2.person_idWHERE lucy.name = "Lucy Liu"

VS

Q4

Sunday, February 24, 13

Page 82: 0221 Cypher for SQL Professionals

START lucy=node:Person(name="Lucy Liu")MATCH lucy-[:ACTS_IN]->movie, co_actor-[:ACTS_IN]->movieRETURN DISTINCT co_actor.name

SELECT DISTINCT co_actor.nameFROM Person lucy JOIN Actor a1 ON lucy.person_id = a1.person_id JOIN Actor a2 ON a1.movie_id = a2.movie_id JOIN Person co_actor ON co_actor.person.id = a2.person_idWHERE lucy.name = "Lucy Liu"

VS

Q4

Sunday, February 24, 13

Page 83: 0221 Cypher for SQL Professionals

START lucy=node:Person(name="Lucy Liu")MATCH lucy-[:ACTS_IN]->movie, co_actor-[:ACTS_IN]->movieRETURN DISTINCT co_actor.name

SELECT DISTINCT co_actor.nameFROM Person lucy JOIN Actor a1 ON lucy.person_id = a1.person_id JOIN Actor a2 ON a1.movie_id = a2.movie_id JOIN Person co_actor ON co_actor.person.id = a2.person_idWHERE lucy.name = "Lucy Liu"

VS

Q4

Sunday, February 24, 13

Page 84: 0221 Cypher for SQL Professionals

START lucy=node:Person(name="Lucy Liu")MATCH lucy-[:ACTS_IN]->movie, co_actor-[:ACTS_IN]->movieRETURN DISTINCT co_actor.name;

SELECT DISTINCT co_actor.nameFROM Person lucy JOIN Actor a1 ON lucy.person_id = a1.person_id JOIN Actor a2 ON a1.movie_id = a2.movie_id JOIN Person co_actor ON co_actor.person.id = a2.person_idWHERE lucy.name = "Lucy Liu"

VS

Q4

Sunday, February 24, 13

Page 85: 0221 Cypher for SQL Professionals

Show me Lucy’s favorite directors

Q5

Sunday, February 24, 13

Page 86: 0221 Cypher for SQL Professionals

Q5

VS

START lucy=node:Person(name="Lucy Liu")MATCH lucy-[:ACTS_IN]->movie, director-[:DIRECTED]->movieRETURN director.name, count(*)ORDER BY count(*) desc

SELECT dir.name, count(*)FROM Person lucy JOIN Actor on Person.person_id = Actor.person_id JOIN Director on Actor.movie_id = Director.movie_id JOIN Person dir on Director.person.id = dir.person_idWHERE lucy.name = "Lucy Liu"GROUP BY dir.nameORDER BY count(*) DESC

Sunday, February 24, 13

Page 87: 0221 Cypher for SQL Professionals

START lucy=node:Person(name="Lucy Liu")MATCH lucy-[:ACTS_IN]->movie, director-[:DIRECTED]->movieRETURN director.name, count(*)ORDER BY count(*) desc

Q5

VSSELECT dir.name, count(*)FROM Person lucy JOIN Actor on Person.person_id = Actor.person_id JOIN Director on Actor.movie_id = Director.movie_id JOIN Person dir on Director.person.id = dir.person_idWHERE lucy.name = "Lucy Liu"GROUP BY dir.nameORDER BY count(*) DESC

Sunday, February 24, 13

Page 88: 0221 Cypher for SQL Professionals

START lucy=node:Person(name="Lucy Liu")MATCH lucy-[:ACTS_IN]->movie, director-[:DIRECTED]->movieRETURN director.name, count(*)ORDER BY count(*) desc

Q5

VSSELECT dir.name, count(*)FROM Person lucy JOIN Actor on Person.person_id = Actor.person_id JOIN Director on Actor.movie_id = Director.movie_id JOIN Person dir on Director.person.id = dir.person_idWHERE lucy.name = "Lucy Liu"GROUP BY dir.nameORDER BY count(*) DESC

Sunday, February 24, 13

Page 89: 0221 Cypher for SQL Professionals

Q5

VS

START lucy=node:Person(name="Lucy Liu")MATCH lucy-[:ACTS_IN]->movie, director-[:DIRECTED]->movieRETURN director.name, count(*)ORDER BY count(*) desc

SELECT dir.name, count(*)FROM Person lucy JOIN Actor on Person.person_id = Actor.person_id JOIN Director on Actor.movie_id = Director.movie_id JOIN Person dir on Director.person.id = dir.person_idWHERE lucy.name = "Lucy Liu"GROUP BY dir.nameORDER BY count(*) DESC

Sunday, February 24, 13

Page 90: 0221 Cypher for SQL Professionals

Q5

VS

START lucy=node:Person(name="Lucy Liu")MATCH lucy-[:ACTS_IN]->movie, director-[:DIRECTED]->movieRETURN director.name, count(*)ORDER BY count(*) desc

SELECT dir.name, count(*)FROM Person lucy JOIN Actor on Person.person_id = Actor.person_id JOIN Director on Actor.movie_id = Director.movie_id JOIN Person dir on Director.person.id = dir.person_idWHERE lucy.name = "Lucy Liu"GROUP BY dir.nameORDER BY count(*) DESC

Sunday, February 24, 13

Page 91: 0221 Cypher for SQL Professionals

Q5

VS

START lucy=node:Person(name="Lucy Liu")MATCH lucy-[:ACTS_IN]->movie, director-[:DIRECTED]->movieRETURN director.name, count(*)ORDER BY count(*) desc

SELECT dir.name, count(*)FROM Person lucy JOIN Actor on Person.person_id = Actor.person_id JOIN Director on Actor.movie_id = Director.movie_id JOIN Person dir on Director.person.id = dir.person_idWHERE lucy.name = "Lucy Liu"GROUP BY dir.nameORDER BY count(*) DESC

Sunday, February 24, 13

Page 92: 0221 Cypher for SQL Professionals

Q5

VS

START lucy=node:Person(name="Lucy Liu")MATCH lucy-[:ACTS_IN]->movie, director-[:DIRECTED]->movieRETURN director.name, count(*)ORDER BY count(*) desc

SELECT dir.name, count(*)FROM Person lucy JOIN Actor on Person.person_id = Actor.person_id JOIN Director on Actor.movie_id = Director.movie_id JOIN Person dir on Director.person.id = dir.person_idWHERE lucy.name = "Lucy Liu"GROUP BY dir.nameORDER BY count(*) DESC

Sunday, February 24, 13

Page 93: 0221 Cypher for SQL Professionals

Q5

VS

START lucy=node:Person(name="Lucy Liu")MATCH lucy-[:ACTS_IN]->movie, director-[:DIRECTED]->movieRETURN director.name, count(*)ORDER BY count(*) desc

SELECT dir.name, count(*)FROM Person lucy JOIN Actor on Person.person_id = Actor.person_id JOIN Director on Actor.movie_id = Director.movie_id JOIN Person dir on Director.person.id = dir.person_idWHERE lucy.name = "Lucy Liu"GROUP BY dir.nameORDER BY count(*) DESC

Sunday, February 24, 13

Page 94: 0221 Cypher for SQL Professionals

...fasten your seat belt Dorothy, 'cause Kansas is going bye-bye

Sunday, February 24, 13

Page 95: 0221 Cypher for SQL Professionals

Show me all Lucy’s co-actors

Q4’

Sunday, February 24, 13

Page 96: 0221 Cypher for SQL Professionals

START lucy=node:Person(name="Lucy Liu")MATCH lucy-[:ACTS_IN]->movie, co_actor-[:ACTS_IN]->movieRETURN DISTINCT co_actor.name

Q4

Sunday, February 24, 13

Page 97: 0221 Cypher for SQL Professionals

START lucy=node:Person(name="Lucy Liu")MATCH lucy-[:ACTS_IN*2..2]-co_actorRETURN DISTINCT co_actor.name

Q4’

Sunday, February 24, 13

Page 98: 0221 Cypher for SQL Professionals

START lucy=node:Person(name="Lucy Liu")MATCH lucy-[:ACTS_IN*2..2]-co_actorRETURN DISTINCT co_actor.name

Q4’

Sunday, February 24, 13

Page 99: 0221 Cypher for SQL Professionals

Bacon Lucy

Q6

Sunday, February 24, 13

Page 100: 0221 Cypher for SQL Professionals

START lucy=node:Person(name="Lucy Liu"), kevin=node:Person(name="Kevin Bacon")MATCH p = shortestPath( lucy-[:ACTS_IN*]-kevin )RETURN EXTRACT(n in NODES(p) : COALESCE(n.name?, n.title?))

Sunday, February 24, 13

Page 101: 0221 Cypher for SQL Professionals

START lucy=node:Person(name="Lucy Liu"), kevin=node:Person(name="Kevin Bacon")MATCH p = shortestPath( lucy-[:ACTS_IN*]-kevin )RETURN EXTRACT(n in NODES(p) : COALESCE(n.name?, n.title?))

Sunday, February 24, 13

Page 102: 0221 Cypher for SQL Professionals

START lucy=node:Person(name="Lucy Liu"), kevin=node:Person(name="Kevin Bacon")MATCH p = shortestPath( lucy-[:ACTS_IN*]-kevin )RETURN EXTRACT(n in NODES(p) : COALESCE(n.name?, n.title?))

Sunday, February 24, 13

Page 103: 0221 Cypher for SQL Professionals

START lucy=node:Person(name="Lucy Liu"), kevin=node:Person(name="Kevin Bacon")MATCH p = shortestPath( lucy-[:ACTS_IN*]-kevin )RETURN EXTRACT(n in NODES(p) : COALESCE(n.name?, n.title?))

Q6

Sunday, February 24, 13

Page 104: 0221 Cypher for SQL Professionals

START lucy=node:Person(name="Lucy Liu"), kevin=node:Person(name="Kevin Bacon")MATCH p = shortestPath( lucy-[:ACTS_IN*]-kevin )RETURN EXTRACT(n in NODES(p) : COALESCE(n.name?, n.title?))

Sunday, February 24, 13

Page 105: 0221 Cypher for SQL Professionals

START lucy=node:Person(name="Lucy Liu"), kevin=node:Person(name="Kevin Bacon")MATCH p = shortestPath( lucy-[:ACTS_IN*]-kevin )RETURN EXTRACT(n in NODES(p) : COALESCE(n.name?, n.title?))

Sunday, February 24, 13

Page 106: 0221 Cypher for SQL Professionals

START lucy=node:Person(name="Lucy Liu"), kevin=node:Person(name="Kevin Bacon")MATCH p = shortestPath( lucy-[:ACTS_IN*]-kevin )RETURN EXTRACT(n in NODES(p) : COALESCE(n.name?, n.title?))

Sunday, February 24, 13

Page 107: 0221 Cypher for SQL Professionals

Create Cypher 2(the movie)

Sunday, February 24, 13

Page 108: 0221 Cypher for SQL Professionals

START lucy = node:Person(name="Lucy Liu"), david = node:Person(name="David Lynch")CREATE andres={name:"Andrés Taylor"}, cypher2={title:"Cypher 2"}, lucy-[:ACTS_IN {name:"Rita Foster"}]->cypher2, andres-[:ACTS_IN {name:"Manuel"}]->cypher2, david-[:DIRECTED]->cypher2RETURN cypher2

Sunday, February 24, 13

Page 109: 0221 Cypher for SQL Professionals

START lucy = node:Person(name="Lucy Liu"), david = node:Person(name="David Lynch")CREATE andres={name:"Andrés Taylor"}, cypher2={title:"Cypher 2"}, lucy-[:ACTS_IN {name:"Rita Foster"}]->cypher2, andres-[:ACTS_IN {name:"Manuel"}]->cypher2, david-[:DIRECTED]->cypher2RETURN cypher2

Sunday, February 24, 13

Page 110: 0221 Cypher for SQL Professionals

START lucy = node:Person(name="Lucy Liu"), david = node:Person(name="David Lynch")CREATE andres={name:"Andrés Taylor"}, cypher2={title:"Cypher 2"}, lucy-[:ACTS_IN {name:"Rita Foster"}]->cypher2, andres-[:ACTS_IN {name:"Manuel"}]->cypher2, david-[:DIRECTED]->cypher2RETURN cypher2

Sunday, February 24, 13

Page 111: 0221 Cypher for SQL Professionals

START lucy = node:Person(name="Lucy Liu"), david = node:Person(name="David Lynch")CREATE andres={name:"Andrés Taylor"}, cypher2={title:"Cypher 2"}, lucy-[:ACTS_IN {name:"Rita Foster"}]->cypher2, andres-[:ACTS_IN {name:"Manuel"}]->cypher2, david-[:DIRECTED]->cypher2RETURN cypher2

Sunday, February 24, 13

Page 112: 0221 Cypher for SQL Professionals

START lucy = node:Person(name="Lucy Liu"), david = node:Person(name="David Lynch")CREATE andres={name:"Andrés Taylor"}, cypher2={title:"Cypher 2"}, lucy-[:ACTS_IN {name:"Rita Foster"}]->cypher2, andres-[:ACTS_IN {name:"Manuel"}]->cypher2, david-[:DIRECTED]->cypher2RETURN cypher2

Sunday, February 24, 13

Page 113: 0221 Cypher for SQL Professionals

START lucy = node:Person(name="Lucy Liu"), david = node:Person(name="David Lynch")CREATE andres={name:"Andrés Taylor"}, cypher2={title:"Cypher 2"}, lucy-[:ACTS_IN {name:"Rita Foster"}]->cypher2, andres-[:ACTS_IN {name:"Manuel"}]->cypher2, david-[:DIRECTED]->cypher2RETURN cypher2

Sunday, February 24, 13

Page 114: 0221 Cypher for SQL Professionals

Add genre nodes

Sunday, February 24, 13

Page 115: 0221 Cypher for SQL Professionals

Person

Movie

Root

Genre:GENRE

:IS_GENRE

:ACTS_IN:DIRECTED

Sunday, February 24, 13

Page 116: 0221 Cypher for SQL Professionals

Movietitle:"The Matrix"

genre: "Action"

Root

Genrename: "Action"

:GENRE

:IS_GENRE

Movietitle:"Highlander"

genre: "Action"

:GENRE

Sunday, February 24, 13

Page 117: 0221 Cypher for SQL Professionals

START ref=node(0), movie=node:Movie("title:*") WHERE HAS(movie.genre) CREATE UNIQUE ref<-[:IS_GENRE]->(genre {name:movie.genre}) genre-[:GENRE]->movie

Sunday, February 24, 13

Page 118: 0221 Cypher for SQL Professionals

START ref=node(0), movie=node:Movie("title:*") WHERE HAS(movie.genre) CREATE UNIQUE ref<-[:IS_GENRE]->(genre {name:movie.genre}) genre-[:GENRE]->movie

Sunday, February 24, 13

Page 119: 0221 Cypher for SQL Professionals

START ref=node(0), movie=node:Movie("title:*") WHERE HAS(movie.genre) CREATE UNIQUE ref<-[:IS_GENRE]->(genre {name:movie.genre}) genre-[:GENRE]->movie

Sunday, February 24, 13

Page 120: 0221 Cypher for SQL Professionals

START ref=node(0), movie=node:Movie("title:*") WHERE HAS(movie.genre) CREATE UNIQUE ref<-[:IS_GENRE]->(genre {name:movie.genre}) genre-[:GENRE]->movie

Sunday, February 24, 13

Page 121: 0221 Cypher for SQL Professionals

Rehash

Sunday, February 24, 13

Page 122: 0221 Cypher for SQL Professionals

STARTdesignates the start points

Sunday, February 24, 13

Page 123: 0221 Cypher for SQL Professionals

MATCHdescribes the pattern

Sunday, February 24, 13

Page 124: 0221 Cypher for SQL Professionals

WHEREfilters the result set

Sunday, February 24, 13

Page 125: 0221 Cypher for SQL Professionals

CREATEcreates nodes and relationships

Sunday, February 24, 13

Page 126: 0221 Cypher for SQL Professionals

CREATE UNIQUEcreates unique patterns

Sunday, February 24, 13

Page 127: 0221 Cypher for SQL Professionals

RETURNcreates the result table

Sunday, February 24, 13

Page 128: 0221 Cypher for SQL Professionals

ALL THE THINGS!

QUESTION

Sunday, February 24, 13