Download SQL Queries

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
SQL queries
basics
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
2
SQL query
• We use a movie information database
as example
Movie
movieid
title
country
prodyear
genre
oscars
Actor
Casting
movieid
actorid
RHS – SOC
actorid
name
country
birth
living
oscars
3
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
RHS – SOC
4
SQL query
• The most basic SQL
query looks like:
Which fields
do I want
SELECT <fieldlist>
FROM <tablename>
From what table
do I want the fields
RHS – SOC
5
SQL query
movieid
title
country
prodyear genre
oscars
1
E.T.
USA
1982
Sci-Fi
4
2
Taxi
France
1998
Comedy
0
3
SELECT
title, 1966
prodyear
Hunger
Denmark
Drama
1
4
Movie
LeonFROM 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
6
SQL query
movieid
title
country
prodyear
genre
oscars
1
E.T.
USA
1982
Sci-Fi
4
2
Taxi
France
1998
Comedy 0
3
SELECT
title, 1966
prodyear
Hunger
Denmark
Drama
1
4
Movie
LeonFROM 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
7
SQL query
title
prodyear
E.T.
1982
Taxi
1998
Hunger
1966
Leon
1994
Hard Boiled
1992
1984
1984
Seven
1995
SELECT title, prodyear
FROM
Movie
RHS – SOC
8
SQL query
movieid
title
country
prodyear genre
oscars
1
E.T.
USA
1982
Sci-Fi
4
2
Taxi
France
1998
Comedy
0
SELECT
movieid,
title, country,…
Hunger
Denmark 1966
Drama
FROM
Movie
3
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
9
SQL query
movieid
title
country
prodyear genre
oscars
1
E.T.
USA
1982
Sci-Fi
4
2
Taxi
France
1998
Comedy
0
* means ”all fields”
SELECT
* Denmark
3
Hunger
FROM
Movie
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
10
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
11
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
12
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 expressions in Java
• Use () to make expressions easier to read,
and/or to ”overrule” evaluation rules
RHS – SOC
13
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
Thriller
0
Action
0
SELECT *
4
Leon
FROM
Movie France 1994
5
Hardprodyear
Boiled
HK< 1990
1992
WHERE
6
1984
UK
1984
Sci-Fi
2
7
Seven
USA
1995
Thriller
1
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
Drama
1
Thriller
0
SELECT
*
3
Hunger
Denmark 1966
FROM
Movie France 1994
4
Leon
WHERE prodyear < 1990
5
Hard Boiled
HK
1992
Action
0
6
1984
UK
1984
Sci-Fi
2
7
Seven
USA
1995
Thriller
1
RHS – SOC
15
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
Sci-Fi
2
1984
SELECT *
FROM
Movie
WHERE prodyear < 1990
RHS – SOC
16
SQL query
title
prodyear
genre
E.T.
1982
Sci-Fi
Hunger
1966
Drama
1984
1984
Sci-Fi
SELECT title, prodyear, genre
FROM
Movie
WHERE prodyear < 1990
RHS – SOC
17
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
18
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
19
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
SELECT
*
FROM
4
LeonMovie
France
1994
Thriller 0
WHERE
((prodyear<= 1992) AND (prodyear >= 1980))
5
Hard Boiled
HK
1992
Action
0
6
1984
UK
1984
Sci-Fi
2
7
Seven
USA
1995
Thriller
1
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
SELECT
*
FROM
4
LeonMovie
France
1994
Thriller 0
WHERE
((prodyear<= 1992) AND (prodyear >= 1980))
5
Hard Boiled
HK
1992
Action
0
6
1984
UK
1984
Sci-Fi
2
7
Seven
USA
1995
Thriller
1
RHS – SOC
21
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
WHERE
*
Movie
((prodyear<= 1992) AND (prodyear >= 1980))
RHS – SOC
22
SQL query - range
• Another notation for range seach uses the
keyword BETWEEN
SELECT *
FROM Movie
WHERE prodyear BETWEEN 1980 AND 1992
RHS – SOC
23
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
24
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
25
SQL query – set membership
movieid
title
country
prodyear genre
oscars
1
E.T.
USA
1982
Sci-Fi
4
2
Taxi
France
1998
Comedy
0
Drama
1
Thriller
0
Action
0
3
Hunger
Denmark 1966
SELECT
*
4
Leon
France
1994
FROM
Movie
WHERE
genre
IN (’Action’,’Drama’)
5
Hard
Boiled
HK
1992
6
1984
UK
1984
Sci-Fi
2
7
Seven
USA
1995
Thriller
1
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
Drama
1
Thriller
0
Action
0
3
Hunger
Denmark 1966
SELECT
*
4
Leon
France
1994
FROM
Movie
WHERE
genre
IN (’Action’,’Drama’)
5
Hard
Boiled
HK
1992
6
1984
UK
1984
Sci-Fi
2
7
Seven
USA
1995
Thriller
1
RHS – SOC
27
SQL query – set membership
movieid
title
country
prodyear genre
3
Hunger
Denmark 1966
Drama
1
5
Hard Boiled
HK
Action
0
1992
oscars
SELECT *
FROM Movie
WHERE genre IN (’Action’,’Drama’)
RHS – SOC
28
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
29
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
30
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
31
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
32
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
33
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
34
SQL query – pattern match
SELECT *
FROM Movie
WHERE title LIKE ’H%’
SELECT *
FROM Movie
WHERE title LIKE ’_ _ _ _’
RHS – SOC
35
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
FranceSELECT
1994
5
Hard Boiled
HK
6
1984
UK
7
Seven
USA
* Thriller 0
FROM
1992 Movie
Action
0
WHERE title LIKE ’H%’
1984
Sci-Fi
2
1995
Thriller
1
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
FranceSELECT
1994
5
Hard Boiled
HK
6
1984
UK
7
Seven
USA
* Thriller 0
FROM
1992 Movie
Action
0
WHERE title LIKE ’H%’
1984
Sci-Fi
2
1995
Thriller
1
RHS – SOC
37
SQL query – pattern match
movieid
title
country
3
Hunger
5
Hard Boiled
prodyear
genre
oscars
Denmark 1966
Drama
1
HK
Action
0
1992
SELECT *
FROM Movie
WHERE title LIKE ’H%’
RHS – SOC
38
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
SELECT
5
Hard Boiled
6
1984
7
Seven
* Thriller 0
MovieAction 0
HK FROM
1992
WHERE title LIKE ’_ _ _ _’
UK
1984
Sci-Fi
2
USA
1995
Thriller
1
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
SELECT
5
Hard Boiled
6
1984
7
Seven
* Thriller 0
MovieAction 0
HK FROM
1992
WHERE title LIKE ’_ _ _ _’
UK
1984
Sci-Fi
2
USA
1995
Thriller
1
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
4
Leon
France
1994
Thriller
0
6
1984
UK
1984
Sci-Fi
2
SELECT *
FROM Movie
WHERE title LIKE ’_ _ _ _’
RHS – SOC
41
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
42
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 ”undefined” or null value, if it makes sense
RHS – SOC
43
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>
6
1984
UK
7
Seven
<null>
SELECT *
1992
Action
0
FROM Movie
1984
Sci-Fi
2IS NULL
WHERE
country
1995
RHS – SOC
Thriller
1
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>
6
1984
UK
7
Seven
<null>
SELECT *
1992
Action
0
FROM Movie
1984
Sci-Fi
2IS NULL
WHERE
country
1995
RHS – SOC
Thriller
1
45
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 Movie
WHERE country IS NULL
RHS – SOC
46
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
47
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
RHS – SOC
48
Related documents