Introduction to Databases Chapter 7: Data Access and Manipulation.

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Chapter 5: Database Forms and Reports
Database Systems: Design, Implementation, and Management Tenth Edition
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
Chapter 14 Organizing and Manipulating the Data in Databases
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Introduction to Structured Query Language (SQL)
Chapter 5 The Relational Database Model: Introduction
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Accounting Information Systems, 1st Edition
Introduction to Databases
Chapter 6 The Relational Database Model: Additional Concepts
Introduction to SQL J.-S. Chou Assistant Professor.
Chapter 9 Relational Data Retrieval: SQL
Introduction to Databases Chapter 1: Introducing Data and Data Management.
Rationale Aspiring Database Developers should be able to efficiently query and maintain databases. This module will help students learn the Structured.
4-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Introduction to Databases Chapter 8: Improving Data Access.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
Introduction to Databases Chapter 6: Understanding the SQL Language.
4-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
PowerPoint Presentation for Dennis & Haley Wixom, Systems Analysis and Design, 2 nd Edition Copyright 2003 © John Wiley & Sons, Inc. All rights reserved.
Fundamentals of Database Management Systems, 2nd ed
Today’s Agenda  Any questions about the assignment (due Mon)?  Quiz  Quiz review  Homework for Friday:  Watch the two videos on the Coursera db website.
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
PowerPoint Presentation for Dennis & Haley Wixom, Systems Analysis and Design Copyright 2000 © John Wiley & Sons, Inc. All rights reserved. Slide 1 Systems.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Copyright © 2000 John Wiley & Sons, Inc. All rights reserved
9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 6 Procedural Language SQL and Advanced SQL Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Copyright © 2000 John Wiley & Sons, Inc. All rights reserved. Reproduction or translation of this work beyond that permitted in Section 117 of the 1976.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Slide 8-1 Chapter 8 Terms Programming Languages Introduction to Information Systems Judith C. Simon.
A- 1. A- 2 Appendix B Standards of Ethical Conduct for Management Accountants The Institute of Management Accountants has published and promoted the following.
K-1. K-2 Appendix K Standards of Ethical Conduct for Management Accountants The Institute of Management Accountants has published and promoted the following.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Introduction to Business Information Systems by Mark Huber, Craig Piercy, Patrick McKeown, and James Norrie Tech Guide D: The Details of SQL, Data Modelling,
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Relational Database Design
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Physical Changes That Don’t Change the Logical Design
02 | Advanced SELECT Statements
Database Systems: Design, Implementation, and Management Tenth Edition
David M. Kroenke and David J
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Introduction To Structured Query Language (SQL)
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
Presentation transcript:

Introduction to Databases Chapter 7: Data Access and Manipulation

SELECT Statement Primary statement for data retrieval Clauses let you specify precise information retrieved, sorted, organized, and grouped Source can be tables, views, and table- valued user-defined functions Most basic syntax is: SELECT column_list FROM source WITH search_conditions

SELECT Statement Advanced Syntax SELECT column_list [ALL | DISTINCT] [TOP expression [PERCENT] [WITH TIES]] [INTO table_name] FROM source [[INNER|OUTER|CROSS] JOIN] WHERE search_condition [ORDER BY order_expression [ASC] | [DESC]] [GROUP BY [ALL] group_expression] [HAVING search_condition] Keywords: ALL DISTINCTTOP INTO JOIN ORDER BY GROUP BY HAVING

Filtering Results Filter results using comparison operators: =, >, >=,, != AND and OR: To combine conditions SELECT CUSTNUM, CUSTNAME FROM CUSTOMER WHERE HQCITY=‘New York’ OR (CUSTNUM>1500 AND HQCITY=‘Atlanta’) BETWEEN and IN: For ranges SELECT CUSTNUM, CUSTNAME FROM CUSTOMER WHERE CUSTNUM BETWEEN 1000 AND 1700 LIKE: Adds flexibility SELECT CUSTNAME FROM CUSTOMER WHERE CUSTNAME LIKE ‘C_t%’

Managing Result Sets Specifying columns SELECT PRODNUM, (QOH * UNITPRICE) AS [VALUE] FROM PRODUCT Specifying rows SELECT TOP 3 PRODNUM, (QOH * UNITPRICE) AS [VALUE] FROM PRODUCT Writing results to new table SELECT PRODNUM, (QOH * UNITPRICE) AS [VALUE] INTO PRODVAL FROM PRODUCT Limiting results to unique values SELECT DISTINCT SPNUM FROM CUSTOMER –With DISTINCT, entire result must be duplicated for row to be dropped

Sorting and Organizing ORDER BY: Defines order; can use multiple columns SELECT * FROM SALES ORDER BY SPNUM, PRODNUM ASC and DESC: Returns rows in ascending or descending order SELECT PRODNUM, PRODNAME, UNITPRICE FROM PRODUCT ORDER BY UNITPRICE DESC

Grouping GROUP BY: To group rows in result in order to perform functions such as SUM SELECT SPNUM, SUM(QUANTITY) AS [SUM] FROM SALES WHERE SPNUM> =150 GROUP BY SPNUM GROUP BY column list and ORDER BY column list must be aggregate values or called out in GROUP BY clause GROUP BY can use HAVING clause to limit results from group calculations performed by built- in function SELECT SPNUM, SUM(QUANTITY) AS [SUM] FROM SALES WHERE SPNUM>=150 GROUP BY SPNUM HAVING SUM(QUANTITY)>=5000

Operator Precedence SQL Server Transact SQL Precedence Level 1: ~ Level 2: *, /, % Level 3: +, -, & Level 4: =, >, =,, !>, !<, Level 5: ^ Level 6: NOT Level 7: AND Level 8: ALL, ANY, BETWEEN, IN, LIKE, SOME Level 9: =

Operator Precedence Operators at same precedence level evaluated from left to right When same symbol used for two operations (e.g. “ =”), type of operation being performed can be determined by context in which symbol used Precedence varies by DBMS implementation

Combining Statement Results To combine, statements must have: –Same number of columns –Columns in same order –Columns with compatible data types UNION: Combines two results SELECT HQCITY FROM CUSTOMER WHERE SPNUM = 137 UNION SELECT HQCITY FROM CUSTOMER WHERE SPNUM = 186 EXCEPT: Returns results in first query that do not appear in second INTERSECT: Returns values that appear in both queries

Using SELECT with Other Commands SELECT can be value source in other statements Replacing values clause: INSERT EmpCopy SELECT SPNUM, SPNAME, SPFIRSTNAME FROM SALESPERSON Replacing scalar input values: UPDATE SALESPERSON SET COMMPERCT = (SELECT AVG(COMMPERCT) FROM SALESPERSON)

Joins and Subqueries Join: –Combines columns from two sources –Can combine with other joins to join more than two sources Subquery: –Uses multiple SELECT statements in single query –Nested query; can include several layers of nestings, with innermost executed first

Joins Two specifications must be made in SELECT statement: 1.Tables to join must be listed in FROM clause 2.Join attributes in tables must be declared and matched If same column name used in both tables, qualify column names with table name in SELECT statement SELECT SPNAME FROM SALESPERSON, CUSTOMER WHERE SALESPERSON.SPNUM=CUSTOMER.SPNUM AND CUSTNUM=1525 Declaring and using table alias: SELECT SPNAME FROM SALESPERSON s, CUSTOMER c WHERE s.SPNUM=c.SPNUM AND CUSTNUM=1525

Joins Joining three tables SELECT PRODNAME FROM SALESPERSON, PRODUCT, SALES WHERE SALESPERSON.SPNUM=SALES.SPNUM AND SALES.PRODNUM=PRODUCT.PRODNUM AND SPNAME=’Adams’ AND QUANTITY>2000 Types of Joins: –Inner join: Only qualifying results are returned –Outer join: Returns qualifying rows from one table and all rows from other (outer) table –Cross join: Returns all possible rows, whether or not they meet qualifying join logic, in every possible combination

Different Join Syntaxes Inner join SELECT SPNAME, CUSTNAME FROM SALESPERSON JOIN CUSTOMER ON (SALESPERSON.SPNUM=CUSTOMER.SPNUM) Outer join –May be LEFT or RIGHT, returning unqualified results from either table to left or right of join syntax SELECT SPNAME, CUSTNAME FROM SALESPERSON LEFT OUTER JOIN CUSTOMER ON (SALESPERSON.SPNUM=CUSTOMER.SPNUM) Cross join SELECT SPNAME, CUSTNAME FROM SALESPERSON CROSS JOIN CUSTOMER

Basic Subqueries Subqueries: –Nested queries using several levels of SELECT statements contained in pairs of parentheses Noncorrelated subquery: –Simplest type of subquery –Inner query does not depend on outer query –Nested (inner) query is used to retrieve a value that is passed up to outer query and used as input to SELECT statement

Basic Subqueries Noncorrelated subquery (cont.): –Used in qualifying logic in WHERE clause –Innermost SELECT statement executed first; results are provided as input to SELECT statement at next level up –Can be used as alternative to join SELECT SPNAME FROM SALESPERSON WHERE SPNUM=(SELECT SPNUM FROM CUSTOMER WHERE CUSTNUM=1525)

Batches and Scripts Batch: Set of SQL commands that run as group SELECT * INTO ECOPY FROM EMPLOYEES SELECT * FROM ECOPY Script: –Automates periodic procedures –May contain batches USE GeneralHardware GO CREATE TABLE EmpTest (SPNUM CHAR(3), SPNAME VARCHAR(20)) GO INSERT EmpTest SELECT SPNUM, SPNAME FROM SALESPERSON

Programming Concepts: Variables Variables: –Used to temporarily store value in memory –Include Identifier (unique name) – Local variable names begin symbol Data type To create variable: DECLARE To assign value: SET, SELECT int = 5

Programming Concepts: Variables Value may be static (constant) or expression Value must be compatible with data type REAL = (SELECT AVG(COMMPERCT) FROM SALESPERSON) UPDATE SALESPERSON SET COMMPERCT

Programming Concepts: Control Statements Allow batch to: –Make decisions based on values –Control order in which statements are executed –Repeatedly loop through statements Common controls: –BEGIN and END –IF…ELSE

Programming Concepts: Control Statements IF boolean_expression [BEGIN] statement_block [END] [ELSE [BEGIN] statement_block [END]]

Summary SELECT statement basic syntax: SELECT column_list FROM source WITH search_conditions. Advanced SELECT syntax keywords: ALL, DISTINCT, TOP, INTO, JOIN, ORDER BY, GROUP BY, HAVING. SELECT can be used as value source for other statements. Comparison operators used to filter and modify results: AND, OR, BETWEEN, IN, and LIKE. Result sets can be limited to specified columns or rows, written to new tables, limited to unique values, ordered, organized, and grouped.

Summary Operator precedence: Defines execution order of operators. Joins: Combine columns from two or more tables. Subqueries: Nested queries using several levels of SELECT statements contained in pairs of parentheses. Batches: Used to execute two or more statements as group. Scripts: Used to automate procedures; may be constructed using variables and control statements.

Key Terms AND Bitwise operations Boolean expression Cartesian product Control statement Control-of-flow statement Correlated subquery Cross join Identifier Inner join Join Local variable Noncorrelated subquery OR Outer join Subquery Table alias Table order User-defined function Variable XML document fragment

Copyright Notice Copyright 2008 John Wiley & Sons, Inc. All rights reserved. Reproduction or translation of this work beyond that permitted in section 117 of the 1976 United States Copyright Act without express permission of the copyright owner is unlawful. Requests for further information should be addressed to the Permissions Department, John Wiley & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages caused by the use of these programs or from the use of the information herein.