48
SQL queries basics

SQL queries basics. RHS – SOC 2 SQL query An SQL query is an SQL statement, which specifies a subset of the data in the database A subset in terms of

Embed Size (px)

Citation preview

SQL queriesbasics

RHS – SOC 2

SQL query

• An SQL query is an SQL statement, which specifies a subset of the data in the database

• A subset in terms of– Tables– Fields– Conditions on fields

RHS – SOC 3

SQL query

• We use a movie information database as example

Movie movieid title country prodyear genre oscars

Actor actorid name country birth living oscars

Casting movieid actorid

RHS – SOC 4

SQL querymovieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven USA 1995 Thriller 1

RHS – SOC 5

SQL query

• The most basic SQL query looks like:

SELECT <fieldlist>

FROM <tablename>

Which fields do I want

From what table do I want the fields

RHS – SOC 6

SQL query

movieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven USA 1995 Thriller 1

SELECT title, prodyearFROM Movie

RHS – SOC 7

SQL query

movieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven USA 1995 Thriller 1

SELECT title, prodyearFROM Movie

RHS – SOC 8

SQL query

title prodyear

E.T. 1982

Taxi 1998

Hunger 1966

Leon 1994

Hard Boiled 1992

1984 1984

Seven 1995

SELECT title, prodyearFROM Movie

RHS – SOC 9

SQL query

movieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven USA 1995 Thriller 1

SELECT movieid, title, country,…FROM Movie

RHS – SOC 10

SQL query

movieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven USA 1995 Thriller 1

SELECT *FROM Movie

* means ”all fields”

RHS – SOC 11

SQL query

• A slightly more complex SQL statement looks like:

SELECT <fieldlist>

FROM <tablename>

WHERE <condition>

Which fields do I want

From what table do I want the fields

What conditions must the fields fulfill

RHS – SOC 12

SQL query

• The WHERE part is a logical expression, specifying conditions on certain fields

• Five fundamental types of criteria– Comparison (<, > , =)– Range (< AND >)– Set membership (belongs to a set of values)– Pattern match (for string fields)– Null (is the value of the field a null value)

RHS – SOC 13

SQL query

• Note that we can build arbitrarily complex logical expressions, using the usual logical operators: AND, OR, NOT

• Rules are the same as for logical expres-sions in Java

• Use () to make expressions easier to read, and/or to ”overrule” evaluation rules

RHS – SOC 14

SQL query

movieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven USA 1995 Thriller 1

SELECT *FROM MovieWHERE prodyear < 1990

RHS – SOC 15

SQL query

movieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven USA 1995 Thriller 1

SELECT *FROM MovieWHERE prodyear < 1990

RHS – SOC 16

SQL query

movieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

3 Hunger Denmark 1966 Drama 1

6 1984 UK 1984 Sci-Fi 2

SELECT *FROM MovieWHERE prodyear < 1990

RHS – SOC 17

SQL query

title prodyear genre

E.T. 1982 Sci-Fi

Hunger 1966 Drama

1984 1984 Sci-Fi

SELECT title, prodyear, genreFROM MovieWHERE prodyear < 1990

RHS – SOC 18

Exercise 1 – SQL queries

• Create a MovieInformation database, as defined in the presentation

• Add records to the Movie table, as defined in the presentation

• With the data in place, run the below queries on the database– SELECT * FROM Movie WHERE (oscars = 1)– SELECT title, prodyear, oscars FROM Movie WHERE (country = ’USA’)– SELECT title, prodyear, genre FROM Movie WHERE (prodyear >= 1995)– SELECT * FROM Movie WHERE ((oscars = 0) AND (country = ’USA’))

• Now formulate queries yourself, in order to retrieve the below data:– Get all fields for movies where the genre is ’Action’– Get all fields for movies that did not win any Oscars– Get title, year and genre for movies from after 1993– Get title, year for movies that were not made in USA– Get all fields for all movies from before 1983 that won an Oscar– Get all fields for movies from either USA or UK that won an Oscar

RHS – SOC 19

SQL query - range

• A range search is an SQL query where a value should be within a certain range

• Actually just a two-part comparision query

SELECT *

FROM Movie

WHERE ((prodyear <= 1992) AND (prodyear >= 1980))

RHS – SOC 20

SQL query - range

movieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven USA 1995 Thriller 1

SELECT *FROM MovieWHERE ((prodyear<= 1992) AND (prodyear >= 1980))

RHS – SOC 21

SQL query - range

movieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven USA 1995 Thriller 1

SELECT *FROM MovieWHERE ((prodyear<= 1992) AND (prodyear >= 1980))

RHS – SOC 22

SQL query - range

movieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

SELECT *FROM MovieWHERE ((prodyear<= 1992) AND (prodyear >= 1980))

RHS – SOC 23

SQL query - range

• Another notation for range seach uses the keyword BETWEEN

SELECT *

FROM Movie

WHERE prodyear BETWEEN 1980 AND 1992

RHS – SOC 24

SQL query - range

• We can create a ”negated” version of a range query using NOT BETWEEN

SELECT *

FROM Movie

WHERE prodyear NOT BETWEEN 1980 AND 1992

RHS – SOC 25

SQL query – set membership

• A set membership search is an SQL query where a value must belong to a given set of values

• We use the IN keyword

SELECT *

FROM Movie

WHERE genre IN (’Action’,’Drama’)

RHS – SOC 26

SQL query – set membership

movieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven USA 1995 Thriller 1

SELECT *FROM MovieWHERE genre IN (’Action’,’Drama’)

RHS – SOC 27

SQL query – set membership

movieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven USA 1995 Thriller 1

SELECT *FROM MovieWHERE genre IN (’Action’,’Drama’)

RHS – SOC 28

SQL query – set membership

movieid title country prodyear genre oscars

3 Hunger Denmark 1966 Drama 1

5 Hard Boiled HK 1992 Action 0

SELECT *FROM MovieWHERE genre IN (’Action’,’Drama’)

RHS – SOC 29

SQL query – set membership

• Note that these two queries are equivalent

SELECT *

FROM Movie

WHERE genre IN (’Action’,’Drama’)

SELECT *

FROM Movie

WHERE ((genre = ’Action’) OR (genre = ’Drama’))

RHS – SOC 30

SQL query – set membership

• We can create a ”negated” version of a set membership query using NOT IN

SELECT *

FROM Movie

WHERE genre NOT IN (’Action’,’Drama’)

RHS – SOC 31

Exercise 2 – SQL queries

• Use the MovieInformation database, defined in exercise 1• With the data in place, run the below queries on the database

– SELECT * FROM Movie WHERE ((oscars > 0) AND (oscars < 3))

– SELECT * FROM Movie WHERE prodyear BETWEEN 1990 AND 1995

– SELECT * FROM Movie WHERE genre NOT IN (’Drama’, ’Sci-Fi’)

– SELECT * FROM Movie WHERE oscars IN (0,2,4)

• Now formulate queries yourself, in order to retrieve the below data:– Get movies made before 1980 or after 1990

– Get movies from USA made between 1985 and 1995

– Get movies winning at most 1 Oscar, in the genre Thriller’ or ’Sci-Fi’

– Get movies made in USA, HK or Denmark

– Get movies that won 2 or 4 Oscars, made before 1990

RHS – SOC 32

SQL query – pattern match

• A pattern match search is an SQL query where a (string) value must match a given pattern

• We use the LIKE keyword

• The hard part is choosing the correct pattern to match against – several ways to formulate a pattern

RHS – SOC 33

SQL query – pattern match

• A pattern is formulated using two special characters % and _

• % : wildcard: any sequence of zero or more characters

• _ : any single character

RHS – SOC 34

SQL query – pattern match

Pattern Meaning

’s%’ Any string starting with ’S’, of any length (at least 1)

(’super’, ’s’, ’s123’, ’s 123’)

’s_ _ _’ Any string starting with ’S’, of length exactly 4

(’such’, ’s123’, ’ssss’, ’s 1’)

’%s’ Any string ending with ’s’, of any length (at least 1)

(’Spurs’, ’s’, ’123s’, ’ s’, ’1 2s’)

’%s%’ Any string containing an ’s’, of any length (at least 1)

(’Spurs’, ’s’, ’basin’, ’ s ’, ’12s34’)

’%s_ _ _% Exercise…

RHS – SOC 35

SQL query – pattern match

SELECT *

FROM Movie

WHERE title LIKE ’H%’

SELECT *

FROM Movie

WHERE title LIKE ’_ _ _ _’

RHS – SOC 36

SQL query – pattern match

movieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven USA 1995 Thriller 1

SELECT *FROM MovieWHERE title LIKE ’H%’

RHS – SOC 37

SQL query – pattern match

movieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven USA 1995 Thriller 1

SELECT *FROM MovieWHERE title LIKE ’H%’

RHS – SOC 38

SQL query – pattern match

movieid title country prodyear genre oscars

3 Hunger Denmark 1966 Drama 1

5 Hard Boiled HK 1992 Action 0

SELECT *FROM MovieWHERE title LIKE ’H%’

RHS – SOC 39

SQL query – pattern match

movieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven USA 1995 Thriller 1

SELECT *FROM MovieWHERE title LIKE ’_ _ _ _’

RHS – SOC 40

SQL query – pattern match

movieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven USA 1995 Thriller 1

SELECT *FROM MovieWHERE title LIKE ’_ _ _ _’

RHS – SOC 41

SQL query – pattern match

movieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

4 Leon France 1994 Thriller 0

6 1984 UK 1984 Sci-Fi 2

SELECT *FROM MovieWHERE title LIKE ’_ _ _ _’

RHS – SOC 42

SQL query – pattern match

• We can create a ”negated” version of a pattern match query using NOT LIKE

SELECT *

FROM Movie

WHERE title NOT LIKE ’H%’

RHS – SOC 43

SQL query – null

• A null search is an SQL query where a value must be a null value

• We use the IS NULL keyword

• A null value…?

• We may allow a field to have an ”unde-fined” or null value, if it makes sense

RHS – SOC 44

SQL query – pattern match

movieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger <null> 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled <null> 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven <null> 1995 Thriller 1

SELECT *FROM MovieWHERE country IS NULL

RHS – SOC 45

SQL query – pattern match

movieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger <null> 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled <null> 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven <null> 1995 Thriller 1

SELECT *FROM MovieWHERE country IS NULL

RHS – SOC 46

SQL query – pattern match

movieid title country prodyear genre oscars

3 Hunger <null> 1966 Drama 1

5 Hard Boiled <null> 1992 Action 0

7 Seven <null> 1995 Thriller 1

SELECT *FROM MovieWHERE country IS NULL

RHS – SOC 47

SQL query – pattern match

• We can create a ”negated” version of a null query using IS NOT NULL

SELECT *

FROM Movie

WHERE country IS NOT NULL

RHS – SOC 48

Exercise 3 – SQL queries

• Use the MovieInformation database, defined in exercise 1• With the data in place, run the below queries on the database

– SELECT * FROM Movie WHERE title LIKE ’%a%’– SELECT * FROM Movie WHERE title LIKE ’%n’– SELECT * FROM Movie WHERE title LIKE ’%_ _ _ _ _ %’– SELECT * FROM Movie WHERE country IS NOT NULL

• Now formulate queries yourself, in order to retrieve the below data:– Get movies with a title containing an ’i’– Get movies with a title starting with ’A’, ’T’ or ’S’– Get movies with a title shorter than 6 characters– Get movies with a title containing an ’e’ and an ’r’– Get movies with a title consisting of more than one word