Download presentation
Presentation is loading. Please wait.
1
CS122 Using Relational Databases and SQL
* CS122 Using Relational Databases and SQL 2. Single Table Queries Randy Moss Department of Computer Science California State University, Los Angeles 1
2
Outline Recap Remove duplicates Create computed columns
* Outline Recap Remove duplicates Create computed columns Use multiple conditions in the where clause Use wildcards Sort query results 2. Single Table Queries CS1222_F2017 2-2 2
3
Recap SELECT column(s) FROM Tablename(s) WHERE condition(s)
* Recap SELECT column(s) FROM Tablename(s) WHERE condition(s) 2. Single Table Queries CS1222_F2017 2-3 3
4
Recap show databases; show tables; describe Artists;
* Recap show databases; show tables; describe Artists; 2. Single Table Queries CS1222_F2017 2-4 4
5
General SQL Tips Generally not case sensitive for field names, SQL keywords and strings Table names are CASE SENSITIVE in MySQL Field and table names with spaces must be enclosed in ‘ ’ Field and table names that match SQL keywords must be enclosed in ‘ ’ 2. Single Table Queries CS1222_F2017 2-5
6
Eliminate duplicate results
* Eliminate duplicate results What are duplicates? State CA VA State city CA Burbank VA Fairfax Richmond Sate city Street CA Burbank Main st. VA Fairfax Star st. Richmond Remove Duplicates! State CA VA State city CA Burbank VA Fairfax Richmond No Duplicates! 2. Single Table Queries CS1222_F2017 2-6 6
7
Eliminate duplicate results
* Eliminate duplicate results Format SELECT DISTINCT field1, field2,… FROM Table [WHERE] Conditions Ex. List each city where a member lives, list each only once SELECT DISTINCT region FROM Members 2. Single Table Queries CS1222_F2017 2-7 7
8
Calculated Columns Columns can be derived from calculations
* Calculated Columns Columns can be derived from calculations The calculations use the the names of columns and constants Assign names to columns with AS Column alias We focus on numeric calculations 2. Single Table Queries CS1222_F2017 2-8 8
9
Calculated Columns (cont)
* Calculated Columns (cont) 2. Single Table Queries CS1222_F2017 2-9 9
10
Calculated Columns (cont)
* Calculated Columns (cont) List the track title and length in minutes of each track SELECT tracktitle, lengthseconds/60 AS lengthminutes FROM Tracks 2. Single Table Queries CS1222_F2017 2-10 10
11
* Two word column alias Report all webaddresses in the Artists table, labeling the column Web Site SELECT webaddress AS 'Web Site' FROM Artists Note: Aliases can NOT be used in the WHERE clause. 2. Single Table Queries CS1222_F2017 2-11 11
12
* Basic Where Clauses List the title and lengthseconds of every track that runs more than 240 seconds SELECT tracktitle, lengthseconds FROM Tracks WHERE lengthseconds > 240 2. Single Table Queries CS1222_F2017 2-12 12
13
Comparison Operators Equal: = Not equal: <> Less than: <
* Comparison Operators Equal: = Not equal: <> Less than: < Greater than: > Greater than or equal to: >= Less than or equal to: <= Attribute types that can be compared: Numerical Currency Text Date and Time Generally speaking, different attribute types cannot compare to each other 2. Single Table Queries CS1222_F2017 2-13 13
14
Using Dates in Where Clauses
* Using Dates in Where Clauses List the first name, last name and birthday of any member born on or after June 1, 1975 Select firstname, lastname, birthday From Members Where birthday>=' '; 2. Single Table Queries CS1222_F2017 2-14 14
15
Using Texts in Where Clauses
* Using Texts in Where Clauses List the firstname and lastname of every member from Canada SELECT firstname, lastname FROM Members WHERE country = 'Canada' 2. Single Table Queries CS1222_F2017 2-15 15
16
WHERE clauses with multiple conditions (AND & OR)
* WHERE clauses with multiple conditions (AND & OR) Combine multiple conditions with AND, OR AND: all conditions should be satisfied for a record to be reported OR: If any conditions is true, the record is reported SELECT fields FROM Tables WHERE (condition1) AND |OR (condition 2) ………. The more ANDs you use, the fewer records will be returned The more ORs you use, the more records will be returned 2. Single Table Queries CS1222_F2017 2-16 16
17
* And & Or (cont) 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 2. Single Table Queries CS1222_F2017 2-17 17
18
And & Or (cont) 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 2. Single Table Queries CS1222_F2017 2-18
19
* And & Or (cont) Parentheses can group conditions so they are treated as a unit Should always use parentheses when mixing ANDs and Ors 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). 2. Single Table Queries CS1222_F2017 2-19 19
20
And & Or (cont) Select Firstname, Lastname, Region, SalesID
* And & Or (cont) Select Firstname, Lastname, Region, SalesID From Members Where Region='GA' Or Region='TX' And SalesID=2 Select Firstname, Lastname, Region, SalesID From Members Where (Region='GA' Or Region='TX') And SalesID=2 2. Single Table Queries CS1222_F2017 2-20 20
21
Booleans (True/False) Data Types in Where Clauses
* Booleans (True/False) Data Types in Where Clauses List the tracktitles and realaud fields for all track records that do not have a real audio file SELECT tracktitle, realaud FROM Tracks WHERE realaud = FALSE Note: false NOT ‘FALSE’ (no quotes) In MySQL, TRUE =1, FALSE=0 2. Single Table Queries CS1222_F2017 2-21 21
22
Like and Wildcards LIKE Wildcards
* Like and Wildcards LIKE Special keyword that tests for a group of letters anywhere in the field value. Wildcards _: stands for any single character %: stands for any number of characters 2. Single Table Queries CS1222_F2017 2-22 22
23
Wildcard examples Mac MacIntosh Mackenzie LIKE 'Mac%'
* 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’ 2. Single Table Queries CS1222_F2017 2-23 23
24
Like List any track titles with the word ‘time’ anywhere in the title
* Like List any track titles with the word ‘time’ anywhere in the title SELECT TrackTitle FROM Tracks WHERE TrackTitle LIKE '%time%' 2. Single Table Queries CS1222_F2017 2-24 24
25
Wildcards can only be used with LIKE!
* Like (cont.) Is the following correct? SELECT TrackTitle FROM Tracks WHERE TrackTitle = '%time%' Wildcards can only be used with LIKE! 2. Single Table Queries CS1222_F2017 2-25 25
26
Like (cont) List the website of any studio with a .com domain
* Like (cont) List the website of any studio with a .com domain SELECT webaddress FROM Studios WHERE webaddress Like ‘%.com' 2. Single Table Queries CS1222_F2017 2-26 26
27
Between Select values that fall between two values
* Between Select values that fall between two values Most often used with dates Also works with numeric and text values Inclusive of beginning & ending values 2. Single Table Queries CS1222_F2017 2-27 27
28
* Between (cont.) List the artist name and entry date for all artists with entry dates in August 2003 SELECT artistname, entrydate FROM Artists WHERE entrydate BETWEEN ' ' AND ' ' Tests if field values matches any items in a list List is placed inside parentheses Individual items in list are separated by commas Individual items also enclosed in appropriate characters if non-numeric 2. Single Table Queries CS1222_F2017 2-28 28
29
Is Null Tests for empty field values
* Is Null Tests for empty field values List the artists without a web page SELECT Artistname FROM Artists WHERE WebAddress Is Null 2. Single Table Queries CS1222_F2017 2-29 29
30
In Test if the value of a field matches any items in a list
* In Test if the value of a field matches any items in a list List is placed inside parentheses Individual items in list are separated by commas Individual items also enclosed in appropriate characters if non-numeric SELECT fields FROM Table WHERE field IN (value1, value2,….) 2. Single Table Queries CS1222_F2017 2-30 30
31
* In (cont.) 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') 2. Single Table Queries CS1222_F2017 2-31 31
32
Not Reverses the selection criterion
* Not Reverses the selection criterion Parentheses help indicate what Not applies to SELECT fields FROM Tables WHERE NOT (conditions) 2. Single Table Queries CS1222_F2017 2-32 32
33
Not (cont.) List titles whose genre is not alternative Select *
From Titles Where NOT (genre='alternative') Where genre <>'alternative' 2. Single Table Queries CS1222_F2017 2-33 33
34
NOT (cont.) List the artist name and web address of all artists who have a non-blank web address. SELECT artistname, webaddress FROM Artists WHERE NOT (webaddress IS NULL) SELECT artistname, webaddress FROM Artists WHERE webaddress IS NOT NULL 2. Single Table Queries CS1222_F2017 2-34
35
Order By Sorts the results Defaults to ascending (A-Z, 0-9)
Use DESC for descending (Z-A, 9-0) Note proper order: SELECT – FROM - WHERE - ORDER BY SELECT field1, field2, FROM Tables WHERE conditions ORDER BY field1 [DESC], field2 [DESC], 2. Single Table Queries CS1222_F2017 2-35
36
Order By 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 2. Single Table Queries CS1222_F2017 2-36
37
Summary Calculated columns/ AS AND/OR BETWEEN DISTINCT IN IS NULL
LIKE/Wildcards NOT ORDER BY 2. Single Table Queries CS1222_F2017 2-37
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.