Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tarik Booker CS 122. What we will cover… Tables (review) SELECT statement DISTINCT, Calculated Columns FROM Single tables (for now…) WHERE Date clauses,

Similar presentations


Presentation on theme: "Tarik Booker CS 122. What we will cover… Tables (review) SELECT statement DISTINCT, Calculated Columns FROM Single tables (for now…) WHERE Date clauses,"— Presentation transcript:

1 Tarik Booker CS 122

2 What we will cover… Tables (review) SELECT statement DISTINCT, Calculated Columns FROM Single tables (for now…) WHERE Date clauses, Boolean clauses, LIKE, BETWEEN, IS NULL, IN, AND, OR, NOT, CASE ORDER BY

3 Tables (Review) Remember Tables! Organized list of elements. Lyric Database Composed of Various Tables Different Keys Primary Key – Entirely Distinct Values Primary Key for GET? Now we want to retrieve data from those tables.

4 The SELECT Statement Remember this statement from the MySQL install verification SELECT * FROM Salespeople; Used to retrieve info from a database Can use to select and display specific data Specified by user

5 SELECT Statement (2) Structure of a SELECT Statement SELECT columns FROM tablename WHERE condition ORDER BY columns Rows Columns

6 SELECT Statement (3) SELECT Selects what columns you want to display Using * Selects ALL Columns SELECT * FROM Salespeople; For a few columns, (not all) use comma to separate: SELECT ArtistName, City FROM Artists; FROM Determines what table to SELECT from SELECT FROM most common SQL statement SELECT ArtistName FROM Artists; SELECT * FROM Artists;

7 Select Statement (4) After loading (source) the Lyric Database: Try: SELECT * from Artists; SELECT ArtistName from Artists;

8 SELECT DISTINCT Eliminates Duplicates from the selection SELECT DISTINCT ArtistName FROM Artists; Will only display one copy of each name For multiple fields, will only display unique combinations SELECT DISTINCT City, Region FROM Members;

9 Calculated Columns You can perform calculations on columns! SELECT Expression AS new_column_name FROM Table; The results of the expression is made into “new_column_name” Called a column Alias AS Keyword not required (in MySQL), but still good practice Required in MS Access!!! SELECT LengthSeconds/60 AS LengthMinutes FROM Tracks;

10 Calculated Columns (2) Common Mathematic Operators (for MySQL) +Addition -Subtraction *Multiplication /Division Mod (x,y)Modulo Function Returns Remainder Mod (5,3) Mod (9,3) Mod (8,2)

11 Other (Note: If your field or table names contain a space in them, use square brackets [] around them. Also use if names are the same as SQL commands (such as group, order, etc.) Good practice to not use SQL keywords as names

12 WHERE Clauses Remember: SELECT columns FROM tablename WHERE condition ORDER BY columns SELECT retrieves ALL the rows What if I only want to retrieve a few rows? Use WHERE to restrict rows in the result set Can compare a field to a value, a field to a field Can also compare any calculated expression to a field, value, or other calculated expression (super fancy!)

13 WHERE (2) Keep correct order! The WHERE clause is always AFTER the FROM statement! Not case-sensitive in MySQL, but is in others! Use Comparisons! Comparison Operators (MySQL): = <> or != =

14 WHERE (3) Example: SELECT TrackTitle, LengthSeconds FROM Tracks WHERE LengthSeconds > 240;

15 Date WHERE Clauses WHERE can compare Dates MySQL uses single quotes to specify dates ‘30-Sep-2014’ SELECT FirstName, LastName, Birthday FROM Members WHERE Birthday >=‘01-Jun-1979’; Read as “on or after”

16 Boolean Where Clauses Boolean – Results in True or False Usually True = 1 and False = 0 (for MySQL) This convention not used for all DBMS Not even used for all Programming Languages! SELECT TrackTitle, RealAud FROM Tracks WHERE RealAud = 0;

17 LIKE Allows you to test text fields for a group of letters Uses symbols to stand for other letters WHERE TrackTitle LIKE ‘%time%’ % Can Represent any string of characters Called a wildcard What happens if I use ‘%time’ ? ‘time%’ ? ‘%a%k%’ ? Remember, MySQL is not case-sensitive!

18 BETWEEN Way to choose results between two values Inclusive ( Includes both values in result) Works well with dates, numeric and text values Dates and Text, fairly obvious Text Value Sorting Alphabetically Nulls before any character! Null – Empty, or End of Text Value Ex: Tom vs. Tommy Ex: WHERE LastName Between ‘A’ and ‘Mzzzz’ Be careful with ending Nulls!!!

19 BETWEEN (2) BETWEEN some_value AND some_other_value SELECT LastName, FirstName FROM Members WHERE LastName BETWEEN ‘A’ and ‘Mzzz’; What happens if I use: WHERE LastName BETWEEN ‘A’ and ‘M’; ? Watch your ending nulls.

20 IS NULL Tests for Empty Field Values Explained Null!! However… Text Fields can Vary Can be text value, null, or empty text value Empty text? WHERE condition: fieldname=‘’ Two single quotes with nothing between Counts as “empty text” instead of null! Sometimes used in databases! Be careful!!

21 IS NULL (2) SELECT ArtistName FROM Artists WHERE WebAddress IS NULL; Where are the nulls?

22 IN Tests if a field matches a specified value WHERE field_value IN (value1, value2, etc) Use single quotes for text ‘test1’ For multiple values it’s either/or Either in value1, or value2, or… Note: MySQL can not use IN with dates! SELECT LastName, FirstName, City, Region FROM Members WHERE Region IN (‘IN’, ‘IL’, ‘OH’);

23 AND & OR Boolean operators Operate on boolean values True (1) or False (0) WHERE Region=‘GA’ AND SalesID=2 WHERE Region=‘GA OR SalesID=2 AND True AND True = ? True AND False = ? False AND True = ? False AND False = ? OR True OR True = ? True OR False = ? False OR True = ? False OR False = ?

24 AND & OR (2) If one thing is FALSE, the AND statement is always FALSE The only way an AND statement is TRUE is if all statements are TRUE If one thing is TRUE, the OR statement is always TRUE The only way to make an OR statement FALSE is if all statements are FALSE AND True AND True = T True AND False = F False AND True = F False AND False = F OR True OR True = T True OR False = T False OR True = T False OR False = F

25 AND & OR (3) Can be used with parentheses! WHERE (Region=‘GA’ OR Region=‘TX’) AND SalesID=2 Process parentheses first! You can string together as many AND’s and OR’s as you’d like Use parentheses when needed

26 NOT Another boolean operator Returns values that don’t match the value Use parentheses around statement you want to declare NOT Parentheses are mandatory in MySQL (for NOT) SELECT ArtistName, WebAddress FROM Artists WHERE NOT (WebAddress IS NULL);

27 NOT (2) SELECT ArtistName, WebAddress FROM Artists WHERE NOT (WebAddress IS NULL); What is returned?

28 Mixing AND, OR, NOT You can always mix these operators to create various statements Be careful Use parentheses when necessary SELECT FirstName, Region, Gender FROM Members WHERE Not (Region=’VA’ And Gender=’F’) Results?

29 CASE Statement SQL Has CASE statements Will see in other languages (Java, C++, etc) Allows you to evaluate multiple fields at once Returns into an column Alias (remember?) Two versions of CASE Simple CASE Searched CASE

30 Simple CASE Statement SELECT Field, Field, CASE Field | Expression WHEN value THEN result ELSE result END As alias FROM Table ELSE Statement is optional

31 Simple CASE Statement (2) Select Artistname, Region, Case Region When 'NC' Then 'South' When 'VA' Then 'South' When 'IL' Then 'Midwest' When 'VT' Then 'New England' Else 'Somewhere Else' End As Area From Artists

32 Searched CASE Statement SELECT Field, Field, CASE WHEN Field | Expression comparison Value | Field | Expression THEN result WHEN Field | Expression comparison Value | Field | Expression THEN result ELSE result END As alias FROM Table

33 Searched CASE Statement (2) Select TrackNum, TrackTitle, LengthSeconds, Case When TrackNum=1 And LengthSeconds<240 Then ‘Short 1st Track’ When TrackNum=1 And LengthSeconds>480 Then ‘Long 1st Track’ Else ‘Another Track’ End as Eval From Tracks Where TrackNum<3

34 ORDER BY Sorts Results in a Particular Order Default is ascending order Choose descending order with DESC SELECT ArtistName FROM Artists ORDER BY ArtistName DESC; ORDER BY is the last line in the SQL Statement!

35 ORDER BY (2) You can use multiple ORDER BY statements Used as “tie-breakers” Second statement orders duplicate fields in the first statement (and so on) Select Title, Genre From Titles Order By Genre, Title

36 ORDER BY (3) Can use expressions in ORDER BY The extreme case: Select TrackNum, TrackTitle, LengthSeconds, Case When TrackNum=1 And LengthSeconds<240 Then ‘Short 1st Track’ When TrackNum=1 And LengthSeconds>480 Then ‘Long 1st Track’ Else ‘Another Track’ End as Eval From Tracks Where TrackNum < 3 Order By Case When TrackNum = 1 And LengthSeconds < 240 Then ‘Short 1st Track’ When TrackNum = 1 And LengthSeconds > 480 Then ‘Long 1st Track’ Else ‘Another Track’ End


Download ppt "Tarik Booker CS 122. What we will cover… Tables (review) SELECT statement DISTINCT, Calculated Columns FROM Single tables (for now…) WHERE Date clauses,"

Similar presentations


Ads by Google