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

Slides:



Advertisements
Similar presentations
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Subqueries and Set Operations.
Advertisements

Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Aggregates.
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 7: Aggregates.
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.
SQL for Data Retrieval. Save your SQL Scripts When working with SQL Management Studio, you should keep saving your scripts as a.sql file to somewhere.
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.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
 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.
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.
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.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
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,
Concepts of Database Management Seventh Edition
Using Relational Databases and SQL John Hurley Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
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.
SQL for Data Retrieval. Save your SQL Scripts When working with SQL Management Studio, you should keep saving your scripts as a.sql file to somewhere.
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.
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.
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.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
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.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
CS 122: Lecture 3 Joins (Part 1) Tarik Booker CS 122 California State University, Los Angeles October 7, 2014.
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
CS122 Using Relational Databases and SQL
Writing Basic SQL SELECT Statements
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Restricting and Sorting Data
CS122 Using Relational Databases and SQL
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
CS122 Using Relational Databases and SQL
CS1222 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 Huiping Guo Department of Computer Science California State University, Los Angeles 2. Single Table Queries

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

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 ‘ ‘

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

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!

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

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

2. Single Table Queries CS122_W16 Calculated Columns (cont)

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

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.

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

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: <=

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>=' '; Using Dates in Where Clauses

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'

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) ……….

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

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

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).

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

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

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

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’

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%'

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!

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'

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

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 ' ' AND ' '

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

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,….)

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')

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)

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'

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)

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

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

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

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' )

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%'

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%'

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 ' ' and ' '

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