Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 SQL queries basics

2 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

3 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

4 RHS – SOC 4 SQL query movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1

5 RHS – SOC 5 SQL query The most basic SQL query looks like: SELECT FROM Which fields do I want From what table do I want the fields

6 RHS – SOC 6 SQL query movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT title, prodyear FROM Movie

7 RHS – SOC 7 SQL query movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT title, prodyear FROM Movie

8 RHS – SOC 8 SQL query titleprodyear E.T.1982 Taxi1998 Hunger1966 Leon1994 Hard Boiled1992 1984 Seven1995 SELECT title, prodyear FROM Movie

9 RHS – SOC 9 SQL query movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT movieid, title, country,… FROM Movie

10 RHS – SOC 10 SQL query movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT * FROM Movie * means ”all fields”

11 RHS – SOC 11 SQL query A slightly more complex SQL statement looks like: SELECT FROM WHERE Which fields do I want From what table do I want the fields What conditions must the fields fulfill

12 RHS – SOC 12 SQL query The WHERE part is a logical expression, specifying conditions on certain fields Five fundamental types of criteria –Comparison (, =) –Range ( ) –Set membership (belongs to a set of values) –Pattern match (for string fields) –Null (is the value of the field a null value)

13 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

14 RHS – SOC 14 SQL query movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT * FROM Movie WHEREprodyear < 1990

15 RHS – SOC 15 SQL query movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT * FROM Movie WHEREprodyear < 1990

16 RHS – SOC 16 SQL query movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 3HungerDenmark1966Drama1 61984UK1984Sci-Fi2 SELECT * FROM Movie WHEREprodyear < 1990

17 RHS – SOC 17 SQL query titleprodyeargenre E.T.1982Sci-Fi Hunger1966Drama 1984 Sci-Fi SELECT title, prodyear, genre FROM Movie WHEREprodyear < 1990

18 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

19 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 = 1980))

20 RHS – SOC 20 SQL query - range movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT * FROM Movie WHERE((prodyear = 1980))

21 RHS – SOC 21 SQL query - range movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT * FROM Movie WHERE((prodyear = 1980))

22 RHS – SOC 22 SQL query - range movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 SELECT * FROM Movie WHERE((prodyear = 1980))

23 RHS – SOC 23 SQL query - range Another notation for range seach uses the keyword BETWEEN SELECT * FROM Movie WHERE prodyear BETWEEN 1980 AND 1992

24 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

25 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’)

26 RHS – SOC 26 SQL query – set membership movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT * FROM Movie WHERE genre IN (’Action’,’Drama’)

27 RHS – SOC 27 SQL query – set membership movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT * FROM Movie WHERE genre IN (’Action’,’Drama’)

28 RHS – SOC 28 SQL query – set membership movieidtitlecountryprodyeargenreoscars 3HungerDenmark1966Drama1 5Hard BoiledHK1992Action0 SELECT * FROM Movie WHERE genre IN (’Action’,’Drama’)

29 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’))

30 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’)

31 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

32 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

33 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

34 RHS – SOC 34 SQL query – pattern match PatternMeaning ’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…

35 RHS – SOC 35 SQL query – pattern match SELECT * FROM Movie WHERE title LIKE ’H%’ SELECT * FROM Movie WHERE title LIKE ’_ _ _ _’

36 RHS – SOC 36 SQL query – pattern match movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT * FROM Movie WHERE title LIKE ’H%’

37 RHS – SOC 37 SQL query – pattern match movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT * FROM Movie WHERE title LIKE ’H%’

38 RHS – SOC 38 SQL query – pattern match movieidtitlecountryprodyeargenreoscars 3HungerDenmark1966Drama1 5Hard BoiledHK1992Action0 SELECT * FROM Movie WHERE title LIKE ’H%’

39 RHS – SOC 39 SQL query – pattern match movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT * FROM Movie WHERE title LIKE ’_ _ _ _’

40 RHS – SOC 40 SQL query – pattern match movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT * FROM Movie WHERE title LIKE ’_ _ _ _’

41 RHS – SOC 41 SQL query – pattern match movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 4LeonFrance1994Thriller0 61984UK1984Sci-Fi2 SELECT * FROM Movie WHERE title LIKE ’_ _ _ _’

42 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%’

43 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

44 RHS – SOC 44 SQL query – pattern match movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3Hunger 1966Drama1 4LeonFrance1994Thriller0 5Hard Boiled 1992Action0 61984UK1984Sci-Fi2 7Seven 1995Thriller1 SELECT * FROM Movie WHERE country IS NULL

45 RHS – SOC 45 SQL query – pattern match movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3Hunger 1966Drama1 4LeonFrance1994Thriller0 5Hard Boiled 1992Action0 61984UK1984Sci-Fi2 7Seven 1995Thriller1 SELECT * FROM Movie WHERE country IS NULL

46 RHS – SOC 46 SQL query – pattern match movieidtitlecountryprodyeargenreoscars 3Hunger 1966Drama1 5Hard Boiled 1992Action0 7Seven 1995Thriller1 SELECT * FROM Movie WHERE country IS NULL

47 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

48 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


Download ppt "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."

Similar presentations


Ads by Google