CS122 Using Relational Databases and SQL

Slides:



Advertisements
Similar presentations
Introduction to Structured Query Language (SQL)
Advertisements

Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
Restricting and Sorting Data. Consider the table employee(employee_id,last_name,job_id, department_id ) assume that you want to display all the employees.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 5: Subqueries and Set Operations.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
Microsoft Access 2010 Chapter 7 Using SQL.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Ceng 356-Lab2. Objectives After completing this lesson, you should be able to do the following: Limit the rows that are retrieved by a query Sort the.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 8: Subqueries.
Chapter 2 Basic SQL SELECT Statements
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
ASP.NET Programming with C# and SQL Server First Edition
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Chapter 3 Single-Table Queries
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Using Relational Databases and SQL John Hurley Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Restricting and Sorting Data. ◦ Limiting rows with:  The WHERE clause  The comparison conditions using =,
2 Copyright © Oracle Corporation, All rights reserved. Restricting and Sorting Data.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
4 Copyright © 2006, Oracle. All rights reserved. Restricting and Sorting Data.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Selecting Data.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Using Relational Databases and SQL John Hurley Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
Introduction to SQL PART Ⅰ 第一讲 Writing Basic SQL SELECT Statements.
2 第二讲 Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query.
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.
Structured Query Language
1 Querying a Single Table Structured Query Language (SQL) - Part II.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
SQL SELECT Getting Data from the Database. Basic Format SELECT, FROM WHERE (=, >, LIKE, IN) ORDER BY ; SELECT LastName, FirstName, Phone, City FROM Customer.
Web Programming MySql JDBC Web Programming.
Session 9 Accessing Data from a Database. RDBMS and Data Management/ Session 9/2 of 34 Session Objectives Describe the SELECT statement, its syntax and.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
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.
CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 2. Single Table Queries.
Lecture 7: Subqueries Tarik Booker California State University, Los Angeles.
Tarik Booker CS 122. What we will cover… Tables (review) SELECT statement DISTINCT, Calculated Columns FROM Single tables (for now…) WHERE Date clauses,
1 Section 3 - Select Statement u The Select statement allows you to... –Display Data –Specify Selection Criteria –Sort Data –Group Data for reporting –Use.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 4. Subqueries and joins.
Restricting and Sorting Data
SQL Query Getting to the data ……..
CS3220 Web and Internet Programming More SQL
Chapter 5 Introduction to SQL.
CS122 Using Relational Databases and SQL
Writing Basic SQL SELECT Statements
 2012 Pearson Education, Inc. All rights reserved.
Basic select statement
CS122 Using Relational Databases and SQL
JDBC.
Writing Basic SQL SELECT Statements
Restricting and Sorting Data
CS122 Using Relational Databases and SQL
CS4222 Principles of Database System
Restricting and Sorting Data
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Shelly Cashman: Microsoft Access 2016
Restricting and Sorting Data
CS122 Using Relational Databases and SQL
Presentation transcript:

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

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

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

Recap show databases; show tables; describe Artists; * Recap show databases; show tables; describe Artists; 2. Single Table Queries CS1222_F2017 2-4 4

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

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

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

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

Calculated Columns (cont) * Calculated Columns (cont) 2. Single Table Queries CS1222_F2017 2-9 9

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

* 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

* 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

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

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>='1975-06-01'; 2. Single Table Queries CS1222_F2017 2-14 14

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

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

* 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

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

* 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

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

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

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

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

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

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

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

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

* 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 '2003-08-01' AND ' 2003-08-31' 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

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

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

* 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

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

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

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

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

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

Summary Calculated columns/ AS AND/OR BETWEEN DISTINCT IN IS NULL LIKE/Wildcards NOT ORDER BY 2. Single Table Queries CS1222_F2017 2-37