Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.

Slides:



Advertisements
Similar presentations
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
Advertisements

Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Chapter 6 Set Functions.
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
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 Lab 2: Single-Table Selections.
Introduction to Structured Query Language (SQL)
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 6: Set Functions.
Using Relational Databases and SQL
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 9: Data Manipulation Language.
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 4: Joins Part II.
Introduction to Structured Query Language (SQL)
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 7:
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 4: Joins Part II.
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 8: Subqueries.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Using Relational Databases and SQL John Hurley Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
SQL (DDL & DML Commands)
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 6: Midterm Review.
IS 325 Notes for Monday October 21, Set Theory Concepts Understanding set theory helps us to: see things in terms of systems organize things into.
Databases MIS 21. Some database terminology  Database: integrated collection of data  Database Management System (DBMS): environment that provides mechanisms.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Using Relational Databases and SQL John Hurley Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 4: Joins Part II.
DAT602 Database Application Development Lecture 3 Review of SQL Language.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
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
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
Using SQL Connecting, Retrieving Data, Executing SQL Commands, … Svetlin Nakov Technical Trainer Software University
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Working with Columns, Characters, and Rows. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Apply the concatenation.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
1 DBS201: More on SQL Lecture 2. 2 Agenda Select command review How to create a table How to insert data into a table.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 2. Single Table Queries.
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,
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 4. Subqueries and joins.
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Chapter 5 Introduction to SQL.
CS122 Using Relational Databases and SQL
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Writing Basic SQL SELECT Statements
 2012 Pearson Education, Inc. All rights reserved.
Basic select statement
CS122 Using Relational Databases and SQL
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL LANGUAGE and Relational Data Model TUTORIAL
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
CS4222 Principles of Database System
CS122 Using Relational Databases and SQL
Introduction To Structured Query Language (SQL)
CS122 Using Relational Databases and SQL
Restricting and Sorting Data
CS122 Using Relational Databases and SQL
Presentation transcript:

Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections

Miscellany CSNS Please sign up Please specify a valid . No = no grades. s might come in as spam (Yahoo!) Wiki Page New office hours New tutorials Fun stuff (well... not really...) Lyric database (for Labs) Books database (for Homework)

Review: Part I Database Database Model Relational Model Tables Attributes Types Tuples Relationships Schema

Review: Part II Database Management System Query Language Structured Query Language

Candidate Keys A unique identifier (may contain multiple attributes). Example: Artists table  ArtistID, ArtistName,...

Primary Keys A candidate key that has been singled out to uniquely identify each record. Difference between candidate key and primary key? A table may have more than one candidate key, while only one primary key.

Foreign Keys Most table relationships almost always involve a primary key. A foreign key is just a key at the other end of the relationship. We usually say a foreign key ‘references’ a primary key (you’ll see this when we design our own database the last week of class).

SQL Data Types Strings (C-syntax) ‘Peterson’, ‘Joe\’s’, “Peterson”, “Joe’s”, “A\\B” Integers 1, 2, 3, etc. Decimals 1.45, 2.83, NULL NULL, null

Basic Queries Basic format is: SELECT field_name1, field_name2,... FROM table_name [WHERE conditions] To automatically select ALL fields: SELECT * FROM table_name [WHERE conditions] Code in [] is optional

Basic Examples List all attributes for all artists. SELECT * FROM Artists; List the first name of all members. SELECT FirstName FROM Members List all artists from the United States. SELECT ArtistName FROM Artists WHERE Country='USA'

Ordering By Attributes Syntax: SELECT A 1, A 2,... FROM tablename [WHERE conditions] ORDER BY F 1 [ASC | DESC], F 2 [ASC | DESC],... Example: List all artist names in decreasing order. SELECT ArtistName FROM Artists ORDER BY ArtistName DESC;

Ordering By Attributes Note that the ORDER BY clause can take multiple field names. Example: List all artists and their respective countries, sorted by country first, then by artist name. SELECT ArtistName, Country FROM Artists ORDER BY Country, ArtistName;

Ordering By Attributes Note that you can also mix ASC and DESC. Example: List all member’s first and last names along with their respective countries, sorted by country first in ascending order, followed by first name in descending order. SELECT FirstName, LastName, Country FROM Members ORDER BY Country ASC, FirstName DESC;

Operators Arithmetic +, -, *, /, % Conditional operators:, =, =, <>, BETWEEN Logical operators: AND, OR, NOT Other operators: IS NULL

Field Arithmetic You can use simple mathematical expressions in the field parameters of the SELECT clause. Examples: What is ((2 + 2)*4 – 7)/3? SELECT ((2 + 2)*4 – 7)/3; All sales people desire a 2 times pay raise. Display all sales people and their desired salaries. SELECT FirstName, LastName, 2*Base FROM SalesPeople;

The WHERE Clause Use the WHERE clause to filter results. Examples: SELECT * FROM Artists; SELECT * FROM Artists WHERE Country=‘Canada’; The statement in the WHERE clause gets executed once per row.

AND, OR, and NOT Examples: Display all member names from California or Texas. SELECT FirstName, LastName FROM Members WHERE Region=‘CA’ OR Region=‘TX’; Display all titles whose genre is not alternative. SELECT * FROM Titles WHERE NOT (Genre=‘alternative’); Display all member names from California or Texas who have a sales ID of 2. SELECT FirstName, LastName FROM Members WHERE (Region=‘CA’ OR Region=‘TX’) AND SalesID=2;

AND, OR, and NOT Be careful of parentheses: SELECT FirstName, LastName FROM Members WHERE (Region=‘CA’ OR Region=‘TX’) AND SalesID=2; SELECT FirstName, LastName FROM Members WHERE Region=‘CA’ OR Region=‘TX’ AND SalesID=2; OUCH! 2 nd query does not return the right result.

BETWEEN Like saying fieldname >= x AND fieldname <= y List the titles and tracks of all tracks with lengths between 240 and 300 seconds. SELECT TrackTitle, LengthSeconds FROM Tracks WHERE LengthSeconds BETWEEN 240 AND 300;

IS NULL Examples: Select all artist names who do not have a web page. SELECT ArtistName FROM Artists WHERE WebAddress IS NULL; Select all artist names who do have a web page. SELECT ArtistName FROM Artists WHERE NOT (WebAddress IS NULL);

LIKE Used for pattern matching %  any zero or more characters _  any single character Example: List all member names and their s whose s have a.org domain. SELECT FirstName, LastName, FROM Members WHERE LIKE '%.org';

NOT LIKE Negation of LIKE Ignores comparing NULL strings Example: List all member names and their s whose s do not come from a.org domain. SELECT FirstName, LastName, FROM Members WHERE NOT LIKE '%.org';

CASE Statements Use in the SELECT clause when you want to check and possibly modify/replace a column value before it is displayed Let’s go over the syntax first, and then go over some tips and examples

CASE: Switch Style Syntax: CASE case_value WHEN when_value THEN statement_list WHEN when_value THEN statement_list … ELSE statement_list END It’s like saying: if(case_value=when_value) then statement_list; else if(case_value=when_value) then statement_list; … else statement_list;

CASE: IF-ELSE Style Syntax: CASE WHEN condition THEN statement_list WHEN condition THEN statement_list … ELSE statement_list END It’s like saying: if(condition) then statement_list; else if(condition) then statement_list; … else statement_list;

CASE Examples List all track titles and their length in minutes. If the length of the track title is less than 3 minutes, display ‘Short Track’; otherwise, display ‘Long Track.’ SELECT TrackTitle, LengthSeconds/60, CASE WHEN LengthSeconds/60 < 3 THEN 'Short Track‘ ELSE 'Long Track’ END FROM Tracks;

CASE Examples List all track titles and their length in minutes. If the length of the track title is less than 3 minutes, display ‘Short Track’; less than 4 minutes, display ‘Medium Track’; otherwise, display ‘Long Track.’ SELECT TrackTitle, LengthSeconds/60, CASE WHEN LengthSeconds/60 < 3 THEN ‘Short Track’ WHEN LengthSeconds/60 < 4 THEN ‘Medium Track’ ELSE ‘Long Track’ END FROM Tracks;

Column Aliases Last CASE example had a really long column name. To get rid of that we can use column aliases SELECT expression AS alias FROM … Example: SELECT TrackTitle, LengthSeconds/60, CASE WHEN LengthSeconds/60 < 3 THEN ‘Short Track’ WHEN LengthSeconds/60 < 4 THEN ‘Medium Track’ ELSE ‘Long Track’ END AS ‘Track Description’ FROM Tracks;

DISTINCT Use when you want to remove duplicate results Example: Display a list of unique countries that all members come from. SELECT Country FROM Members; // WRONG!!! SELECT DISTINCT Country FROM Members; // CORRECT!!!