1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.

Slides:



Advertisements
Similar presentations
WHERE Clause Chapter 2. Objectives Limit rows by using a WHERE clause Use the LIKE operator Effect of NULL values Use compound conditions Use the BETWEEN.
Advertisements

Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Concepts of Database Management Seventh Edition
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
Chapter 11 Group Functions
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Introduction to Structured Query Language (SQL)
The University of Akron Dept of Business Technology Computer Information Systems The Relational Model: Query-By-Example (QBE) 2440: 180 Database Concepts.
XP Chapter 3 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Analyzing Data For Effective Decision Making.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Introduction to Structured Query Language (SQL)
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language.
SQL – Part II Yong Choi School of Business CSU, Bakersfield.
Introduction to Oracle9i: SQL1 SQL Group Functions.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Structured Query Language Part I Chapter Three CIS 218.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Concepts of Database Management, 4th Edition, Pratt & Adamski
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Concepts of Database Management Sixth Edition
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Microsoft Access 2010 Chapter 7 Using SQL.
SQL Operations Aggregate Functions Having Clause Database Access Layer A2 Teacher Up skilling LECTURE 5.
Introduction to SQL J.-S. Chou Assistant Professor.
Concepts of Database Management, Fifth Edition
SQL 資料庫查詢語言 取材自 EIS, 3 rd edition By Dunn et al..
©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.
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
Chapter 3 Single-Table Queries
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.
Concepts of Database Management Seventh Edition
Analyzing Data For Effective Decision Making Chapter 3.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
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 Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
Querying a Database Access Project 2. 2 What is a Query?  In general, a query is a form of questioning, in a line of inquiry. A query may also refer.
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.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Select your database – Your database name is.
1 Multiple Table Queries. 2 Objectives  Retrieve data from more than one table by joining tables  Using IN and EXISTS to query multiple tables  Nested.
Microsoft Office XP Illustrated Introductory, Enhanced Tables and Queries Using.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
STRUCTURED QUERY LANGUAGE SQL-II IST 210 Organization of Data IST210 1.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
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.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
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 2 Concepts of Database Management, 4 th Edition, Pratt & Adamski Chapter 2 The Relational Model 1: Introduction, QBE, and Relational Algebra.
© 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.
1 SQL II CIS*2450 Advanced Programming Concepts. 2 Data Types INTEGER –numbers without a decimal point –range is to SMALLINT –like.
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.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
Southern Methodist University CSE CSE 2337 Introduction to Data Management Chapter 2.
VOCAB REVIEW. A field that can be computed from other fields Calculated field Click for the answer Next Question.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Chapter 4 Summary Query.
Structured Query Language
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

1 Chapter 3 Single Table Queries

2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional SELCT-FROM-WHERE

3 SELECT Command SELECT clause Followed by the columns to be included in the query. FROM clause followed by name of the table that contains the data to query WHERE clause (optional) followed by conditions that apply to the data to be retrieved

4 SELECT Command There are no special formatting rules for SQL FROM command and WHERE command should appear on separate lines to aid readability and understanding

5 SELECT Command to Select Certain Columns (i.e., a Projections)

6 SELECT Command With a Condition

7 Comparison Operators

8 Note Generally SQL is not case sensitive Exception: Character values within quotation marks Use the correct case for these values. Example: WHERE LAST ‘adams’ will not select any rows if the stored value is “Adams”

9 SELECT Command Involving a Comparison

10 Compound Conditions Compound conditions Formed by connecting two or more simple conditions Uses AND, OR, and NOT operators AND: all conditions must be true OR: any one of conditions is true NOT: reverses the truth of the original condition

11 SELECT Command Involving an AND Condition

12 SELECT Command Involving an OR Condition

13 SELECT Command Involving a NOT Condition

14 Use of BETWEEN BETWEEN operator Not an essential feature Can arrive at same answer without it using AND Does make certain SELECT commands simpler

15 SELECT Command Involving a BETWEEN Condition

16 Computed Columns Computed columns Results do not exist in the database Can be computed using data in existing columns Use arithmetic operators

17 SELECT Command With a Computation in the Condition

18 Use of LIKE LIKE operator is used when exact matches will not work Use LIKE with a wildcard symbol

19 Example 12 List the customer number, last name, first name, and complete address of every customer who lives on Pine; that is, whose address contains the letters “Pine”

20 SELECT Command With Wildcards

21 Wildcard Symbols Percent symbol (%) represents any collection of characters ‘%Pine%’ Underscore (_) Represents any individual character ‘T_m’

22 Use of IN The IN clause provides concise way of phrasing certain conditions

23 Example 13 List the customer number, last name, and first name for every customer with a credit limit of $1,000, $1,500, or $2,000

24 SELECT Command Involving an IN Condition

25 Sorting Generally, the order of rows is immaterial to the DBMS There is no defined order in which results are displayed Rows can displayed in the order in which they were entered

26 Use of ORDER BY Use the ORDER BY command to list data in a specific order The column on which data is to be sorted is called a sort key or simply key

27 SELECT Command to Order Rows

28 Sorting with Multiple Keys in Descending Order When sorting more than one column the more important column is called the major key (or primary sort key) the less important column is called the minor key (or secondary sort key) List keys in the order of importance in the ORDER BY clause Sort descending by using the DESC operator

29 SELECT Command With Multiple Sort Keys

30 Using Functions SQL has functions to calculate Sums Averages Counts Maximum values Minimum values

31 SQL Functions

32 SELECT Command to Count Rows

33 Count without the Asterisk SELECT COUNT(PART_NUMBER) FROM PART WHERE ITEM_CLASS = ‘HW’;

34 Use of the SUM Function The SUM function is used to calculate totals The column to be totaled must be specified The column to be totaled must be numeric

35 SELECT Command to Calculate a COUNT and a SUM

36 Using AVG, MAX, and MIN AVG, MAX and MIN functions are similar to the SUM SUM, AVG, MAX and MIN functions ignore (eliminate) null values Null values can cause strange results when calculated

37 SELECT Command With Several Functions

38 Nesting Queries Sometimes obtaining the results you need is a two-step process (or more).

39 Example 21 What is the largest credit limit given to any customer of sales rep 06?

40 Selecting the Maximum Credit Limit

41 Query Using Previous Result

42 Subqueries It is possible to place one query inside another Inner query is called a subquery and it is evaluated first Outer query can use the results of the subquery to find its results

43 Using IN and a Subquery

44 Query Using an EQUAL Condition and a Subquery

45 Note SQL will not allow the use of the condition BALANCE > AVG(BALANCE) in the WHERE clause A subquery must be used to obtain the average balance Results of the subquery can be used in the condition as shown in Figure 3.29

46 Grouping Grouping creates groups of rows that share some common characteristics Calculations are performed for the entire group Use the GROUP BY command

47 Using GROUP BY GROUP BY command allows data to be grouped in a particular order Statistics are calculated on the groups

48 Grouping Column

49 HAVING HAVING command is used for groups

50 Query Using a HAVING Clause

51 HAVING vs. WHERE WHERE clause limits rows HAVING clause limits groups

52 Displaying Groups That Contain More Than One Row

53 Restricting the Rows and Groups

54 Selecting Rows Containing Null Values

55 SQL Query Clauses and Operators