Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 2. Single Table Queries.

Similar presentations


Presentation on theme: "CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 2. Single Table Queries."— Presentation transcript:

1 CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 2. Single Table Queries

2 2. Single Table Queries CS122_W16 Outline r Remove duplicates r Create computed columns r Use multiple conditions in the where clause r Use wildcards r Sort query results

3 2. Single Table Queries CS122_W16 General SQL Tips r Generally not case sensitive for field names, SQL keywords and strings r Table names are CASE SENSITIVE in MySQL r Field and table names with spaces must be enclosed in ‘ ‘ r Field and table names that match SQL keywords must be enclosed in ‘ ‘

4 2. Single Table Queries CS122_W16 Basic SQL Structure SELECT column(s) FROM Tablename(s) WHERE condition(s)

5 2. Single Table Queries CS122_W16 Eliminate duplicate results r What are duplicates? State CA VA CA VA SatecityStreet CABurbankMain st. VAFairfaxMain st. CABurbankStar st. VARichmondMain st. Statecity CABurbank VAFairfax CABurbank VARichmond State CA VA Statecity CABurbank VAFairfax VARichmond Remove Duplicates! No Duplicates!

6 2. Single Table Queries CS122_W16 Eliminate duplicate results r Format SELECT DISTINCT field1, field2,… FROM Table [WHERE] Conditions  Ex. List each region where a member lives, list each only once SELECT DISTINCT region FROM Members

7 2. Single Table Queries CS122_W16 Calculated Columns r Columns can be derived from calculations m The calculations use the names of columns and constants r Assign names to columns with AS m Column alias r We focus on numeric calculations

8 2. Single Table Queries CS122_W16 Calculated Columns (cont)

9 2. Single Table Queries CS122_W16 Calculated Columns (cont) r List the track title and length in minutes of each track SELECT tracktitle, lengthseconds/60 AS lengthminutes FROM Tracks

10 2. Single Table Queries CS122_W16 Two word column alias r Report all webaddresses in the Artitsts table, labeling the column Web Site SELECT webaddress AS 'Web Site' FROM Artists Note: Aliases can NOT be used in the WHERE clause.

11 2. Single Table Queries CS122_W16 Basic Where Clauses r List the title and lengthseconds of every track that runs more than 240 seconds SELECT tracktitle, lengthseconds FROM Tracks WHERE lengthseconds > 240

12 2. Single Table Queries CS122_W16 Comparison Operators r Attribute types that can be compared: m Numerical m Currency m Text m Date and Time r Generally speaking, different attribute types cannot compare to each other Equal: = Not equal: <> Less than: < Greater than: > Greater than or equal to: >= Less than or equal to: <=

13 2. Single Table Queries CS122_W16 r List the first name, last name and birthday of any member born on or after June 1, 1975 r Select firstname, lastname, birthday r From Members r Where birthday>='1975-06-01'; Using Dates in Where Clauses

14 2. Single Table Queries CS122_W16 Using Texts in Where Clauses r List the firstname and lastname of every member from Canada SELECT firstname, lastname FROM Members WHERE country = 'Canada'

15 2. Single Table Queries CS122_W16 WHERE clauses with multiple conditions (AND & OR) r Combine multiple conditions with AND, OR m AND: all conditions should be satisfied for a record to be reported m OR: If any conditions is true, the record is reported SELECT fields FROM Tables WHERE (condition1) AND |OR (condition 2) ……….

16 2. Single Table Queries CS122_W16 And & Or (cont) r List the firstname, lastname, region and salesID of every member from Georgia who worked with salesperson 2 SELECT Firstname, Lastname, Region, SalesID FROM Members WHERE Region='GA' And SalesID=2

17 2. Single Table Queries CS122_W16 And & Or (cont) r List the firstname, lastname, region and salesID of every member who is either from Georgia or who worked with salesperson 2 SELECT Firstname, Lastname, Region, SalesID FROM Members WHERE Region='GA' OR SalesID=2

18 2. Single Table Queries CS122_W16 And & Or (cont) r Parentheses can group conditions so they are treated as a unit r Should always use parentheses when mixing ANDs and Ors r Ex: list the firstname, lastname, region and salesid of every member who worked with salesperson 2 and is either from Georgia(GA) or Texas(TX).

19 2. Single Table Queries CS122_W16 And & Or (cont) 1. Select Firstname, Lastname, Region, SalesID From Members Where Region='GA' Or Region='TX' And SalesID=2 2. Select Firstname, Lastname, Region, SalesID From Members Where (Region='GA' Or Region='TX') And SalesID=2

20 2. Single Table Queries CS122_W16 Booleans (True/False) Data Types in Where Clauses SELECT tracktitle, realaud FROM Tracks WHERE realaud = FALSE Note: false NOT ‘FALSE’ In MySQL, TRUE =1, FALSE=0 r List the tracktitles and realaud fields for all track records that do not have a real audio file

21 2. Single Table Queries CS122_W16 Like and Wildcards r LIKE m Special keyword that tests for a group of letters anywhere in the field value. r Wildcards m _: stands for any single character m %: stands for any number of characters

22 2. Single Table Queries CS122_W16 Wildcard examples LIKE 'Mac%' Mac MacIntosh Mackenzie LIKE 'J%n' Jon Johnson Jason Juan LIKE 'Mac_' Mack Macs Anderson Johnson san sun LIKE ‘%s_n’

23 2. Single Table Queries CS122_W16 Like List any track titles with the word ‘time’ anywhere in the title SELECT TrackTitle FROM Tracks WHERE TrackTitle LIKE '%time%'

24 2. Single Table Queries CS122_W16 Like (cont.) Is the following correct? SELECT TrackTitle FROM Tracks WHERE TrackTitle = '%time%' Wildcards can only be used with LIKE!

25 2. Single Table Queries CS122_W16 Like (cont) r List the website of any studio with a.com domain SELECT webaddress FROM Studios WHERE webaddress Like '%.com'

26 2. Single Table Queries CS122_W16 Between r Select values that fall between two values r Most often used with dates r Also works with numeric and text values r Inclusive of beginning & ending values

27 2. Single Table Queries CS122_W16 Between (cont.) r List the artist name and entry date for all artists with entry dates in August 2003 SELECT artistname, entrydate FROM Artists WHERE entrydate BETWEEN '2003-08-01' AND ' 2003-08-31'

28 2. Single Table Queries CS122_W16 Is Null r Tests for empty field values r List the artists without a web page SELECT Artistname FROM Artists WHERE WebAddress Is Null

29 2. Single Table Queries CS122_W16 In r Test if the value of a field matches any items in a list SELECT fields FROM Table WHERE field IN (value1, value2,….)

30 2. Single Table Queries CS122_W16 In (cont.) r List the names, cities and regions of all members living in Indianna(IN), Illinois(IL), or Ohio(OH) SELECT lastname, firstname, city, region FROM Members WHERE region IN ('IN', 'IL', 'OH')

31 2. Single Table Queries CS122_W16 Not r Reverses the selection criterion r Parentheses help indicate what Not applies to SELECT fields FROM Tables WHERE NOT (conditions)

32 2. Single Table Queries CS122_W16 Not (cont.) r List titles whose genre is not alternative 1. Select * From Titles Where NOT (genre='alternative') 2. Select * From Titles Where genre <>'alternative'

33 2. Single Table Queries CS122_W16 NOT (cont.) r List the artist name and web address of all artists who have a non-bland web address. SELECT artistname, webaddress FROM Artists WHERE webaddress IS NOT NULL SELECT artistname, webaddress FROM Artists WHERE NOT (webaddress IS NULL)

34 2. Single Table Queries CS122_W16 Order By SELECT field1, field2, FROM Tables WHERE conditions ORDER BY field1 [DESC], field2 [DESC],

35 2. Single Table Queries CS122_W16 Order By r List the title and genre of each title sorted by genre and then sorted within genre by title in descending order SELECT title, genre FROM Titles ORDER BY genre, title DESC

36 2. Single Table Queries CS122_W16 Summary r Calculated columns/ AS r AND/OR r BETWEEN r DISTINCT r IN r IS NULL r LIKE/Wildcards r NOT r ORDER BY

37 2. Single Table Queries CS122_W16 Exercise 1 r List the first name, last name, home phone, and gender of all members who have a home phone area code 822 and are female r Is the query right? SELECT firstname, lastname, homephone, gender FROM Members WHERE (home = '822%') OR ( gender='F' )

38 2. Single Table Queries CS122_W16 Exercise 2 r List all artist names with the letter 's' anywhere in the name SELECT artistname FROM Artists WHERE artistname LIKE '%s%'

39 2. Single Table Queries CS122_W16 Exercise 3 r List the TitleID, Title, and UPC of any titles that have '8' in the third left-most digit in UPC SELECT titleid, title, upc FROM Titles WHERE upc LIKE '__8%'

40 2. Single Table Queries CS122_W16 Exercise 4 r List the Artist name and entry date for all artists with entry dates in June 2003 SELECT artistname, entrydate FROM Artists WHERE entrydate BETWEEN '2003-06- 01' and '2003-06-30'

41 2. Single Table Queries CS122_W16 Exercise 5 r List the Title, StudioID, and Genre of all titles sorted by StudioID in ascending order and then by Genre in descending order SELECT title, studioid, genre FROM Titles ORDER BY studioid, genre DESC


Download ppt "CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 2. Single Table Queries."

Similar presentations


Ads by Google