Chapter 7 Introduction to SQL SELECT Statement Modern

  • Slides: 43
Download presentation
Chapter 7: Introduction to SQL SELECT Statement Modern Database Management 8 th Edition Jeffrey

Chapter 7: Introduction to SQL SELECT Statement Modern Database Management 8 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. Mc. Fadden © 2007 by Prentice Hall 1

SELECT Statement n n Used for queries on single or multiple tables Clauses of

SELECT Statement n n Used for queries on single or multiple tables Clauses of the SELECT statement: n SELECT n n FROM n n Indicategorization of results HAVING n n Indicate the conditions under which a row will be included in the result GROUP BY n n Indicate the table(s) or view(s) from which data will be obtained WHERE n n List the columns (and expressions) that should be returned from the query Indicate the conditions under which a category (group) will be included ORDER BY n Chapter 7 Sorts the result according to specified criteria © 2007 by Prentice Hall 2

Figure 7 -10 SQL statement processing order (adapted from van der Lans, p. 100)

Figure 7 -10 SQL statement processing order (adapted from van der Lans, p. 100) Chapter 7 © 2007 by Prentice Hall 3

SELECT Example n Find products with standard price less than $275 SELECT PRODUCT_NAME, STANDARD_PRICE

SELECT Example n Find products with standard price less than $275 SELECT PRODUCT_NAME, STANDARD_PRICE FROM PRODUCT_V WHERE STANDARD_PRICE < 275; Table 7 -3: Comparison Operators in SQL Chapter 7 © 2007 by Prentice Hall 4

SELECT Example Using Alias n Alias is an alternative column or table name SELECT

SELECT Example Using Alias n Alias is an alternative column or table name SELECT CUSTOMER AS NAME, CUSTOMER_ADDRESS FROM CUSTOMER_V CUST WHERE NAME = ‘Home Furnishings’; Chapter 7 © 2007 by Prentice Hall 5

SELECT Example Using a Function n Using the COUNT aggregate function to find totals

SELECT Example Using a Function n Using the COUNT aggregate function to find totals SELECT COUNT(*) FROM ORDER_LINE_V WHERE ORDER_ID = 1004; Note: with aggregate functions you can’t have single-valued columns included in the SELECT clause Chapter 7 © 2007 by Prentice Hall 6

SELECT Example–Boolean Operators n AND, OR, and NOT Operators for customizing conditions in WHERE

SELECT Example–Boolean Operators n AND, OR, and NOT Operators for customizing conditions in WHERE clause SELECT PRODUCT_DESCRIPTION, PRODUCT_FINISH, STANDARD_PRICE FROM PRODUCT_V WHERE (PRODUCT_DESCRIPTION LIKE ‘%Desk’ OR PRODUCT_DESCRIPTION LIKE ‘%Table’) AND UNIT_PRICE > 300; Note: the LIKE operator allows you to compare strings using wildcards. For example, the % wildcard in ‘%Desk’ indicates that all strings that have any number of characters preceding the word “Desk” will be allowed Chapter 7 © 2007 by Prentice Hall 7

Venn Diagram from Previous Query Chapter 7 © 2007 by Prentice Hall 8

Venn Diagram from Previous Query Chapter 7 © 2007 by Prentice Hall 8

SELECT Example – Sorting Results with the ORDER BY Clause n Sort the results

SELECT Example – Sorting Results with the ORDER BY Clause n Sort the results first by STATE, and within a state by CUSTOMER_NAME SELECT CUSTOMER_NAME, CITY, STATE FROM CUSTOMER_V WHERE STATE IN (‘FL’, ‘TX’, ‘CA’, ‘HI’) ORDER BY STATE, CUSTOMER_NAME; Note: the IN operator in this example allows you to include rows whose STATE value is either FL, TX, CA, or HI. It is more efficient than separate OR conditions Chapter 7 © 2007 by Prentice Hall 9

SELECT Example– Categorizing Results Using the GROUP BY Clause n For use with aggregate

SELECT Example– Categorizing Results Using the GROUP BY Clause n For use with aggregate functions n Scalar aggregate: single value returned from SQL query with aggregate function n Vector aggregate: multiple values returned from SQL query with aggregate function (via GROUP BY) SELECT CUSTOMER_STATE, COUNT(CUSTOMER_STATE) FROM CUSTOMER_V GROUP BY CUSTOMER_STATE; Note: you can use single-value fields with aggregate functions if they are included in the GROUP BY clause Chapter 7 © 2007 by Prentice Hall 10

SELECT Example– Qualifying Results by Categories Using the HAVING Clause n For use with

SELECT Example– Qualifying Results by Categories Using the HAVING Clause n For use with GROUP BY SELECT CUSTOMER_STATE, COUNT(CUSTOMER_STATE) FROM CUSTOMER_V GROUP BY CUSTOMER_STATE HAVING COUNT(CUSTOMER_STATE) > 1; Like a WHERE clause, but it operates on groups (categories), not on individual rows. Here, only those groups with total numbers greater than 1 will be included in final result Chapter 7 © 2007 by Prentice Hall 11

 ﺗﻌﻠﻴﻤﺎﺕ SQL Statement SELECT

ﺗﻌﻠﻴﻤﺎﺕ SQL Statement SELECT

 ﺍﻟﺸﻜﻞ ﺍﻟﻌﺎﻡ ﻟﺘﻌﻠﻴﻤﺔ SELECT [DISTINCT] Column. Name(s) FROM Table(s) [WHERE Condition] [GROUP BY

ﺍﻟﺸﻜﻞ ﺍﻟﻌﺎﻡ ﻟﺘﻌﻠﻴﻤﺔ SELECT [DISTINCT] Column. Name(s) FROM Table(s) [WHERE Condition] [GROUP BY Column. Name(s)] [HAVING Condition] [ORDER BY Column. Name(s)] ; Chapter 7 © 2007 by Prentice Hall

 ﺩﻭﺍﻝ ﺍﻷﻌﻤﺪﺓ Column Functions ﺃﻘﻞ ﻗﻴﻤﺔ ﻓﻲ ﻗﻴﻢ ﻋﻤﻮﺩ MIN ﻭﺃﻜﺒﺮ ﻗﻴﻤﺔ ﻓﻲ

ﺩﻭﺍﻝ ﺍﻷﻌﻤﺪﺓ Column Functions ﺃﻘﻞ ﻗﻴﻤﺔ ﻓﻲ ﻗﻴﻢ ﻋﻤﻮﺩ MIN ﻭﺃﻜﺒﺮ ﻗﻴﻤﺔ ﻓﻲ ﻗﻴﻢ ﻋﻤﻮﺩ MAX SELECT MIN ([ DISTINCT ] Column. Name) FROM. . . WHERE. . . ; SELECT MAX ([ DISTINCT ] Column. Name) FROM. . . WHERE. . . ; ﺘﺎﺭﻴﺦ ﻤﻴﻼﺩ ﺃﻜﺒﺮ ﻭﺃﺼﻐﺮ ﻤﺒﺮﻤﺞ : 3 ﻣﺜﺎﻝ SELECT MAX(DOB), MIN(DOB) FROM EMP WHERE JOB = ’PRG' ; Chapter 7 © 2007 by Prentice Hall

 ﺍﻟﻤﺠﻤﻮﻋـﺎﺕ Grouping ﺘﺠﻤﻴﻊ ﺍﻟﺼﻔﻮﻑ ﻓﻲ ﻤﺠﻤﻮﻋﺎﺕ ﻓﺮﻋﻴﺔ GROUP BY SELECT Column. Name[, Column.

ﺍﻟﻤﺠﻤﻮﻋـﺎﺕ Grouping ﺘﺠﻤﻴﻊ ﺍﻟﺼﻔﻮﻑ ﻓﻲ ﻤﺠﻤﻮﻋﺎﺕ ﻓﺮﻋﻴﺔ GROUP BY SELECT Column. Name[, Column. Name][, Column. Function], . . . FROM … WHERE. . . GROUP BY Column. Name[, Column. Name. . . ] ORDER BY Column. Name [DESC][, Column. Name [DESC]. . . ] Sequence. Number [DESC] [, Sequence. Number [DESC]. . . ]; Chapter 7 © 2007 by Prentice Hall

Using and Defining Views n n n Views provide users controlled access to tables

Using and Defining Views n n n Views provide users controlled access to tables Base Table –table containing the raw data Dynamic View n n A “virtual table” created dynamically upon request by a user No data actually stored; instead data from base table made available to user Based on SQL SELECT statement on base tables or other views Materialized View n n n Copy or replication of data Data actually stored Must be refreshed periodically to match the corresponding base tables Chapter 7 © 2007 by Prentice Hall 38

Sample CREATE VIEW EXPENSIVE_STUFF_V AS SELECT PRODUCT_ID, PRODUCT_NAME, UNIT_PRICE FROM PRODUCT_T WHERE UNIT_PRICE >300

Sample CREATE VIEW EXPENSIVE_STUFF_V AS SELECT PRODUCT_ID, PRODUCT_NAME, UNIT_PRICE FROM PRODUCT_T WHERE UNIT_PRICE >300 WITH CHECK_OPTION; §View has a name §View is based on a SELECT statement §CHECK_OPTION works only for Chapter 7 updateable views and prevents updates that would create rows not included in the view © 2007 by Prentice Hall 39

Advantages of Views n n n n Simplify query commands Assist with data security

Advantages of Views n n n n Simplify query commands Assist with data security (but don't rely on views for security, there are more important security measures) Enhance programming productivity Contain most current base table data Use little storage space Provide customized view for user Establish physical data independence Chapter 7 © 2007 by Prentice Hall 40

Disadvantages of Views n n Use processing time each time view is referenced May

Disadvantages of Views n n Use processing time each time view is referenced May or may not be directly updateable Chapter 7 © 2007 by Prentice Hall 41

Schema Definition n n Control processing/storage efficiency Some techniques used to tune dbase performance:

Schema Definition n n Control processing/storage efficiency Some techniques used to tune dbase performance: n n n Choosing to index keys to increase the speed of row selection, table joining, and row ordering. Selecting File organizations for base tables that match type of processing (keeping table physically sorted by a frequently used sort key) Selecting File organizations for indexes appropriate to the way the indexes are used. Data clustering so that related rows of frequently joined tables are stored close together in secondary storage Statistics maintenance about tables and their indexes so that DBMS can find the most efficient ways to perform various database operations. Chapter 7 © 2007 by Prentice Hall 42

n Creating indexes n n n DBMS uses Indexes to Speed up random/sequential access

n Creating indexes n n n DBMS uses Indexes to Speed up random/sequential access to base table data Although users do not directly refer to indexes when writing any SQL command, the DBMS recognizes which existing indexes would improve query performance Indexes are usually created for both primary and foreign keys and both single and compound keys Indexes could be in ascending or descending sequence Example n n n CREATE INDEX NAME_IDX ON CUSTOMER_T(CUSTOMER_NAME) This makes an index for the CUSTOMER_NAME field of the CUSTOMER_T table To remove the index n Chapter 7 Drop INDEX NAME_IDX © 2007 by Prentice Hall 43