Information Resources Management February 27, 2001.

Slides:



Advertisements
Similar presentations
Union, Intersection, Difference (subquery) UNION (subquery) produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and.
Advertisements

TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Database Systems: Design, Implementation, and Management Tenth Edition
1 Database Systems Relations as Bags Grouping and Aggregation Database Modification.
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Information Resources Management April 3, Agenda n Administrivia n Physical Database Design n Database Integrity n Performance.
Introduction to Structured Query Language (SQL)
SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
--The SQL Query Language DML--1 LIKE  LIKE allows to select character strings which have some element in common by using wild cards:  Wild cards:  “%”
SQL SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
Structured Query Language Part I Chapter Three CIS 218.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Information Resources Management March 6, Agenda n Administrivia n SQL Part 2 n Homework #6.
Microsoft Access 2010 Chapter 7 Using SQL.
Chapter 7: SQL, the Structured Query Language Soid Quintero & Ervi Bongso CS157B.
SQL Operations Aggregate Functions Having Clause Database Access Layer A2 Teacher Up skilling LECTURE 5.
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
Introduction to SQL J.-S. Chou Assistant Professor.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Microsoft Access 2010 Chapter 7 Using SQL. Change the font or font size for SQL queries Create SQL queries Include fields in SQL queries Include simple.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Using Special Operators (LIKE and IN)
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
1 Announcements Reading for next week: Chapter 4 Your first homework will be assigned as soon as your database accounts have been set up.  Expect an .
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
April 2002 Information Systems Design John Ogden & John Wordsworth 1 Database Design SQL (1) John Wordsworth Department of Computer Science The University.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Big Data Yuan Xue CS 292 Special topics on.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
SQL Query Getting to the data ……..
Structured Query Language
Chapter 3 Introduction to SQL
SQL Structured Query Language 11/9/2018 Introduction to Databases.
IST 210: Organization of Data
Chapter 4 Summary Query.
CSCE 315 – Programming Studio Spring 2010 Project 1, Lecture 4
Chapter 7 Introduction to Structured Query Language (SQL)
Contents Preface I Introduction Lesson Objectives I-2
CSC 453 Database Systems Lecture
Section 4 - Sorting/Functions
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

Information Resources Management February 27, 2001

Agenda n Administrivia n Exam n SQL Part 1 n Homework #5

Administrivia n Homework #4

Exam n Results n Review

SQL Structured Query Language n The standard relational database language n Two Parts n DDL - Data Definition Language n DML - Data Manipulation Language

SQL - DDL n Data Definition: Define schemas, delete relations, create indices, modify schemas n View Definition n Authorization n Integrity

SQL - DML n Select, Insert, Modify, Delete Tuples n Interactive n Embedded n Transaction Control

SQL - DML n SELECT n Single table n Multiple tables n INSERT n UPDATE n DELETE

SELECT SELECT attributes FROM table(s) WHERE conditions Result is a relation/table

SELECT Example n List all information in the Office table SELECT * FROM Office

SELECT Example n List all offices in New York state SELECT * FROM Office WHERE State = ‘NY’

SELECT Example n List owners with at least 75% of some property SELECT * FROM PctOwned WHERE PctOwned >= 75

Select Example n List all offices in Springfield, IL

Select Example n List all offices in Springfield, IL SELECT * FROM Office WHERE City = ‘Springfield’ AND State = ‘IL’

Select Example n List all properties listed by office 100 or 150

Select Example n List all properties listed by office 100 or 150 SELECT * FROM Property WHERE OfficeNbr = 100 OR OfficeNbr = 150

Select Example n List the office number and phone number for all offices in New York

Select Example n List the office number and phone number for all offices in New York SELECT OfficeNbr, PhoneNbr FROM Office WHERE State = ‘NY’

String Operations - LIKE n % - match any substring (*) n __ - match any character (?) SELECT EmpID, Name FROM Employee WHERE Name LIKE ‘Tom%’

LIKE Examples n List all properties whose description includes a fireplace

LIKE Examples n List all properties whose description includes a fireplace SELECT * FROM Property WHERE Description LIKE ‘%fireplace%’

LIKE Examples n List all employees whose name is Rita and whose last name is 4 characters long

LIKE Examples n List all employees whose name is Rita and whose last name is 4 characters long SELECT * FROM Employee WHERE Name LIKE ‘Rita _ _ _ _’

LIKE Examples n List all employees whose name is Rita and whose last name is at least 4 characters long

LIKE Examples n List all employees whose name is Rita and whose last name is at least 4 characters long SELECT * FROM Employee WHERE Name LIKE ‘Rita _ _ _ _%’

Nulls n An attribute that does not have any value is assigned a value of NULL n not the same as zero n not the same as empty string n indicates no or unknown value

Testing for Nulls n WHERE attribute IS NULL n WHERE attribute IS NOT NULL

Selecting Expressions n A mathematical expression can be selected instead of an attribute SELECT col1, col2*col3, col4+100 FROM table WHERE conditions

Expressions Example n Assuming 5% commission, give the address and potential commission for all Erie properties SELECT address, price*0.05 FROM property WHERE city=‘Erie’ AND state = ‘PA’

Selecting Expressions - 2 n Selected expressions can be given a name using AS SELECT col1, col2*col3 AS newname FROM table WHERE conditions

Expressions Example - 2 n Assuming 5% commission, give the address and potential commission for all Erie properties SELECT address, price*0.05 AS comm FROM property WHERE city=‘Erie’ AND state = ‘PA’

Renaming with AS n Can also use AS in FROM n SELECT * FROM somelongtbl AS A n rename table n useful with multiple tables

Eliminating Duplicates n SELECT DISTINCT … n Entire tuple must be the same to be eliminated as a duplicate

Specifying the Order n SELECT … ORDER BY attribute(s) n SELECT … ORDER BY attrib ASC, attrib DESC n attributes must be SELECTed

Order & Duplicate Example n List all office numbers for offices with employees; sort by office number SELECT DISTINCT OfficeNbr FROM Employee ORDER BY OfficeNbr

Order & Duplicate Example n List the name, city, and state for all owners that own at least 50% of a property; sort by state (descending) and city

Order & Duplicate Example n List the name, city, and state for all owners that own at least 50% of a property; sort by state (descending) and city SELECT DISTINCT Name, City, State FROM Owner AS O, PctOwned AS P WHERE O.OwnerSSN = P.OwnerSSN AND PctOwned >= 50 ORDER BY State DESC, City

Set Operations n UNION (  ) n INTERSECT (  ) n EXCEPT (-) n Add ALL to keep duplicates

Aggregation Functions n AVG n MIN n MAX n SUM n COUNT

Aggregation Examples n How many employees work at office 200? SELECT COUNT(*) FROM Employee WHERE OfficeNbr = 200

Aggregation Examples n What is the average price of the listings for office 225?

Aggregation Examples n What is the average price of the listings for office 225? SELECT AVG(Price) FROM Property WHERE OfficeNbr = 225

GROUP BY & HAVING SELECT attributes, aggregation(attributes) FROM table WHERE conditions GROUP BY attributes HAVING condition n HAVING only applies to each group (optional)

GROUP BY Example n What is the average price of a listing for each office? SELECT OfficeNbr, AVG(Price) FROM Property GROUP BY OfficeNbr

GROUP BY Example n For offices with more than 100 listings, what is the total value of each office’s listings?

GROUP BY Example n For offices with more than 100 listings, what is the total value of each office’s listings? SELECT OfficeNbr, SUM(Price) FROM Property GROUP BY OfficeNbr HAVING COUNT(PropertyID) > 100

Cartesian Product Example n List the names of all employees who work in offices in PA SELECT name FROM Employee, Office WHERE state = ‘PA’

Employee

Office

FROM Employee, Office

FROM Employee,Office WHERE state = ‘PA’ What is needed to fix this?

Multiple Tables Example n List the names of all employees who work in offices in PA SELECT E.Name FROM Employee AS E, Office AS O WHERE E.OfficeNbr = O.OfficeNBR AND O.State = ‘PA’

Multiple Tables Example n List the employee ID and name of all employees (not managers) who have ‘Full’ access to PC # 173

Multiple Tables Example n List the employee ID and name of all employees (not managers) who have ‘Full’ access to PC # 173 SELECT E.EmpID, E.Name FROM Employee AS E, PCAccess AS P WHERE E.EmpID = P.EmpID AND P.AccessType = ‘Full’ AND P.PC# = 173

Multiple Tables - Join Conditions n Usually joined via foreign keys n A - B - C - D (4 tables) n 3 join conditions (in WHERE) n A - B n B - C or A - C n C - D or A - D or B - D n composite keys - more conditions

Duplicate Attribute Names n Office x Property n Attributes are OfficeNbr, Address, City, etc. and PropertyID, Address, City, etc. n To distinguish office address from property address, attach relation name (dot) to the front of those attributes that are duplicated

Duplicate Attribute Names n OfficeNbr, Office.Address, Office.City, Office.State, etc… n PropertyID, Property.Address, Property.City, Property.State, etc… n Can also use AS

Subqueries n Result of a SELECT is a new table n Can compare an attribute against a table (one attribute only) n Is it there? n How does it compare to the values in the table?

Subqueries n Can also test how many rows are in a table n Are there any? n Is there only one?

Subqueries n IN n NOT IN n EXISTS n NOT EXISTS n UNIQUE n NOT UNIQUE n SOME n ( =, =, <>) n ALL n ( =, =, <>)

Subquery Example n List property for sale in PA at a price greater than the average price in PA SELECT * FROM Property WHERE state = ‘PA’ AND price > ALL (SELECT avg(price) FROM Property WHERE state = ‘PA’)

Using Subqueries n Often, either a join or a subquery can be used for the same result n Subquery may be easier n If single value compared against list n If existence (unique) needed n If simple query identified & “nested”

Subquery Example n List the names of all managers whose offices have at least 1 property listed for at least $1,000,000

Subquery Example n List the names of all managers whose offices have at least 1 property listed for at least $1,000,000 SELECT OfficeNbr FROM Property WHERE Price >=

Subquery Example n List the names of all managers whose offices have at least 1 property listed for at least $1,000,000 SELECT E.Name FROM Employee AS E, Manager AS M WHERE E.EmpID = M.EmpID AND M.OfficeNbr IN (SELECT OfficeNbr FROM Property WHERE Price >= )

Subquery Example n List the names of all managers whose offices have at least 1 property listed for at least $1,000,000 SELECT E.Name FROM Employee AS E, Manager AS M WHERE E.EmpID = M.EmpID AND EXISTS (SELECT * FROM Property as P WHERE P.OfficeNbr = M.OfficeNbr AND Price >= )

Subquery Example n List the names of all managers whose offices have at least 1 property listed for at least $1,000,000 SELECT E.Name FROM Employee AS E, Manager AS M WHERE E.EmpID = M.EmpID AND NOT UNIQUE (SELECT * FROM Property as P WHERE P.OfficeNbr = M.OfficeNbr AND Price >= ) What’s wrong with this query?

Subquery Example n List the names of all managers whose offices only have one listing whose price is at least $1,000,000

Subquery Example n List the names of all managers whose offices only have one listing whose price is at least $1,000,000 SELECT E.Name FROM Employee AS E, Manager AS M WHERE E.EmpID = M.EmpID AND UNIQUE (SELECT P.OfficeNbr FROM Property as P WHERE P.OfficeNbr = M.OfficeNbr AND P.Price >= )

Subqueries n A subquery can also be used in the HAVING clause of a SELECT statement SELECT AVG(price), state FROM Property GROUP BY state HAVING AVG(price) > ALL (SELECT AVG(price) FROM Property)

In-Class Exercise n SQL n All queries

Homework #5 n Do SQL n First 10 n Choose 10 of next 15 n Do remaining 5 with next week’s HW n Keep problem numbers n May want to implement in Access