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.

Slides:



Advertisements
Similar presentations
WHERE Clause Chapter 2. Objectives Limit rows by using a WHERE clause Use the LIKE operator Effect of NULL values Use compound conditions Use the BETWEEN.
Advertisements

Databases. A database program can be used to:  sort a file into a different order  Maintain contact with clients  search through the records for a.
CSC271 Database Systems Lecture # 11.
Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete.
1 Query Languages. 2 Boolean Queries Keywords combined with Boolean operators: –OR: (e 1 OR e 2 ) –AND: (e 1 AND e 2 ) –BUT: (e 1 BUT e 2 ) Satisfy e.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
1 CSE Students: Please do not log in yet. Check-in with Brian in the back. Review Days 3 and 4 in the book. Others: Please save your work and logout.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
CORE 2: Information systems and Databases STORAGE & RETRIEVAL 2 : SEARCHING, SELECTING & SORTING.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
IFS Intro. to Data Management Chapter 6 Filtering your data.
1 Microsoft Access 2002 Tutorial 5 – Enhancing a Table’s Design, and Creating Advanced Queries and Custom Forms.
DAY 16: ACCESS CHAPTER 2 Tazin Afrin October 10,
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
Microsoft Access 2010 Building and Using Queries.
Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
® Microsoft Office 2010 Access Tutorial 3 Maintaining and Querying a Database.
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
SQL Chapter Two. Overview Basic Structure Verifying Statements Specifying Columns Specifying Rows.
SQL queries subqueries and joining. RHS – SOC 2 SQL query – beyond one table So far, we have only applied queries to a single table It is possible – and.
SQL introduction. RHS – SOC 2 Getting data out of databases Databases are ”just” containers of data We could – in principle – just put data in a text.
CS 1308 Computer Literacy and the Internet
SQL queries ordering and grouping and joins
SQL - DML. Data Manipulation Language(DML) Are used for managing data: –SELECT retrieve data from the a database –INSERT insert data into a table –UPDATE.
® Microsoft Office 2013 Access Maintaining and Querying a Database.
Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:
Microsoft Office 2013: In Practice Chapter 3 Creating and Using Queries Copyright © 2014 by The McGraw-Hill Companies, Inc. All rights reserved.McGraw-Hill/Irwin.
Computer Apps 1 Queries-part b. Queries Query – a database object that requests information from a database and creates a dataset (list of selected.
Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement Satrio Agung Wicaksono, S.Kom., M.Kom.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
Databases Letts Chapter 11. A database program can be used to:  sort a file into a different order;  search through the records for a matching string.
School of Geography FACULTY OF ENVIRONMENT Querying with ArcGIS.
SQL queries Data Maintenance. RHS – SOC 2 Data maintenance In addition to asking question to the database (queries), we can also maintain the data itself.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
Restrictions Objectives of the Lecture : To consider the algebraic Restrict operator; To consider the Restrict operator and its comparators in SQL.
IST 210 More SQL Todd Bacastow IST 210: Organization of Data.
# 1# 1 QueriesQueries How do we ask questions of the data? What is SELECT? What is FROM? What is WHERE? What is a calculated field? Spring 2010 CS105.
SQL queries ordering and grouping. RHS – SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.
Texas State Technical College DISCOVER! Common Predicates Would you like paper or plastic?
SQL queries ordering and grouping. SWC – SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.
Fall Lab 131 CS105 Lab 13 – Logical Operator Precedence and Joining Tables Announcements: MP 3 released Friday, 11/20 Honors project due: Tuesday,
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements.
9/29/2005From Introduction to Oracle:SQL and PL/SQL, Oracle 1 Restricting and Sorting Data Kroenke, Chapter Two.
Limiting Selected Rows. 2-2 Objectives Sort row output using the ORDER BY clause. Sort row output using the ORDER BY clause. Enter search criteria using.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
Lecture 11 SQL. Agenda SQL Select statement WHERE clause BindingSource filtering.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements.
Restricting and Sorting Data
Microsoft Office Access 2010 Lab 3
Chapter 5 Introduction to SQL.
Access Maintaining and Querying a Database
Writing Basic SQL SELECT Statements
ATS Application Programming: Java Programming
Chapter 12 Information Systems.
Access Maintaining and Querying a Database
Query Languages.
Lesson 3 Chapter 10.
Queries and SQL in Access
Restricting and Sorting Data
Presentation transcript:

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 –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 query movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action UK1984Sci-Fi2 7SevenUSA1995Thriller1

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

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

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

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

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

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

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

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)

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 movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT * FROM Movie WHEREprodyear < 1990

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

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

RHS – SOC 17 SQL query titleprodyeargenre E.T.1982Sci-Fi Hunger1966Drama 1984 Sci-Fi SELECT title, prodyear, genre FROM Movie WHEREprodyear < 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 = 1980))

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

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

RHS – SOC 22 SQL query - range movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 5Hard BoiledHK1992Action UK1984Sci-Fi2 SELECT * FROM Movie WHERE((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 movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT * FROM Movie WHERE genre IN (’Action’,’Drama’)

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

RHS – SOC 28 SQL query – set membership movieidtitlecountryprodyeargenreoscars 3HungerDenmark1966Drama1 5Hard BoiledHK1992Action0 SELECT * FROM Movie WHERE 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 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…

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 movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT * FROM Movie WHERE title LIKE ’H%’

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

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

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

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

RHS – SOC 41 SQL query – pattern match movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 4LeonFrance1994Thriller UK1984Sci-Fi2 SELECT * FROM Movie WHERE 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 movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3Hunger 1966Drama1 4LeonFrance1994Thriller0 5Hard Boiled 1992Action UK1984Sci-Fi2 7Seven 1995Thriller1 SELECT * FROM Movie WHERE country IS NULL

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

RHS – SOC 46 SQL query – pattern match movieidtitlecountryprodyeargenreoscars 3Hunger 1966Drama1 5Hard Boiled 1992Action0 7Seven 1995Thriller1 SELECT * FROM Movie WHERE 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