1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,

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

CSC271 Database Systems Lecture # 11.
Concepts of Database Management Seventh Edition
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
1 Today’s Class  Relational Model  SQL CS F212 Database Systems.
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.
Introduction to Structured Query Language (SQL)
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language.
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.
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
©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
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
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.
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
SQL Unit 5 Aggregation, GROUP BY, and HAVING Kirk Scott 1.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
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.
Chapter 4 Multiple-Table Queries
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
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.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
STRUCTURED QUERY LANGUAGE SQL-II IST 210 Organization of Data IST210 1.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
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.
Structured Query Language
© 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.
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.
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.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
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.
SQL Query Getting to the data ……..
More SQL: Complex Queries,
Chapter 4 Summary Query.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Structured Query Language
Section 4 - Sorting/Functions
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

1 Single Table Queries

2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY, HAVING  SUM, COUNT, AVG, MIN, MAX, DISTINCT functions  Subqueries and Nulls

3 Simple Queries  Query - a question represented in a way that the DBMS can understand  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 Note  Generally SQL is not case sensitive  Exception: 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”

5 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

6 AND / OR / NOT Conditions  List the description of every part that is in warehouse number 3 and that has more than 100 units on hand  List the description of every part that is in warehouse number 3 or that has more than 100 units on hand  List the description of every part that is not in warehouse number 3

7 Use of BETWEEN  BETWEEN operator Not an essential feature Can arrive at same answer without it using AND Does make certain SELECT commands simpler  List the customer number, last name, first name, and balance for every customer whose balance is between $500 and $1000

8 Computed Columns  Computed columns Do not exist in the database Can be computed using data in existing columns Use arithmetic operators  Find the customer number, last name, first name, and available credit for every customer who has a credit limit of at least $1,500

9 Use of LIKE  LIKE operator is used when exact matches will not work  Use LIKE with a wildcard symbol  Percent symbol (%) represents any collection of characters ‘%Pine%’  Underscore (_) Represents any individual character ‘T_m’

10 Use of IN  The IN clause provides concise way of phrasing certain conditions  List the customer number, last name, and first name for every customer with a credit limit of $1,000, $1,500, or $2,000

11 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  List the customer number, last name, first name, and balance of every customer  Order the output in ascending (increasing) order by balance

12 Sorting with Multiple Keys  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

13 Sorting with Multiple Keys  List the customer number, last name, first name, and credit limit of every customer, ordered by credit liming in descending order and by last name within credit limit  The output should be sorted by credit limit in descending order  Sort the output by last name within each group of customers with the same credit limit

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

15 Use of COUNT Function  Counts the number of rows in a table  The specific row to be counted is not important because each count should provide the same answer  Most implementations of SQL allow the use of the asterisk (*) to represent any column

16 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  Find the number of customers and the total of their balance

17 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

18 Use of DISTINCT  DISTINCT operator is not a function  Useful when used in conjunction with COUNT function  Find the customer number of every customer who currently has an open order (List each customer only once)  Count the number of customers who currently have open orders

19 Nesting Queries  Sometimes obtaining the results you need is a two- step process (or more).  What is the largest credit limit given to any customer of sales rep 06?  Display the customer number, last name, and first name of every customer in the Premiere Products database who has the above determined credit limit

20 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  Rewrite the previous query in one step  List the customer number, first name, last name, and balance for every customer whose balance is greater than the average balance

21 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

22 Grouping  Grouping creates groups of rows that share some common characteristics  Calculations are performed for the entire group  GROUP BY command allows data to be grouped in a particular order  Statistics are calculated on the groups

23 HAVING vs. WHERE  WHERE clause limits rows  HAVING clause limits groups  List each credit limit and the total number of customers of sales rep 03 who have this limit  Repeat above, but list only those credit limits held by more than one customer

24 Null Values  List the customer number, last name, and first name of every customer whose street value is null (unknown)

25 Summary  The basic form of a SQL command is SELECT-FROM. Specify the columns to be listed after the word SELECT (or type * to select all columns), and then specify the table name that contains these columns after the word FROM. Optionally, you can include conditions after the word WHERE  Simple conditions are written in the form: column name, comparison operator, column name or value. Simple conditions can involve any of the comparison operators: =, >, >=, or != (not equal to).

26 Summary  You can form compound conditions by combining in simple conditions, using the operators AND, OR, or NOT.  Use the BETWEEN operator to indicate a range of values in a condition  Use computed columns in SQL commands by using arithmetic operators and writing the computation in place of a column name

27 Summary  To check for a value in a character column that is similar to a particular string of characters, use the LIKE clause The % wildcard represents any collection of characters The _ wildcard represents any single character  To check whether a column contains one of a particular set of values, use the IN clause  Use the ORDER BY clause to sort data List sort keys in order of importance To sort in descending order, follow the sort key with DESC

28 Summary  SQL contains the functions COUNT, SUM, AVG, MAX, AND MIN  To avoid duplicates, either when listing or counting values, precede the column name with the DISTINCT operator  When one SQL query is placed inside another, it is called a nested query The inner query, called a subquery, is evaluated first.  Use the GROUP BY clause to group data

29 Summary  Use the HAVING clause to restrict the output to certain groups  Use the phrase IS NULL in the WHERE clause to find rows containing a null value in some column