Presentation is loading. Please wait.

Presentation is loading. Please wait.

INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR.

Similar presentations


Presentation on theme: "INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR."— Presentation transcript:

1 INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR

2 ADMINISTRATIVIA Assignment 3: New drop for any updates related to AdSense only Database tutorial led by Varun: Tuesday 3/27 12:30pm-1:45 Assignment 4: Posted on the web due Friday 3/30 Midterm: end of class

3 ADMINISTRATIVIA II Groups: Please fill out your forms by SUNDAY 3/25 2-way feedback: 1. Please fill out your surveys! 2. I will send you a brief summary of your current grade.

4 LEARNING OBJECTIVES 1.Be able to query single tables using SQL. 2.Be able to perform 1:1, 1:M joins to query relational information. 3.Be able to compute aggregates information using where and having clauses. 4.Be able to perform N:M joins using a join table.

5 REVIEW: RELATIONAL DATABASES Information is stored in tables Each table contains information about a real word “entity” (e.g., a book, a customer) Each table contains fields (e.g., BookName, Author, Price) Each row of the table contains a unique identifier, a.k.a. primary key (e.g., ISBN)

6 REVIEW: NORMALIZATION A technique for designing relational database tables to minimize duplication of information and to safeguard the database against certain types of logical or structural problems, namely data anomalies. For example, when multiple instances of a given piece of information occur in a table, the possibility exists that these instances will not be kept consistent when the data within the table is updated, leading to a loss of data integrity. Prevents insertion, deletion and update anomalies. Rule of thumb: If you find yourself typing the same information in a field again and again, then the design is bad You will encounter anomalies in the future

7 SQL: INTRODUCTION What is SQL? Review: Database and DBMS (chapter 1) SQL is a standard command language use by relational DBMS to perform database operations Some facts about SQL SQL 92 is the most commonly supported version English-like (not programming) Case insensitive Venders have different implementations

8 SQL STATEMENTS DDL - data definition language Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation language Manipulating user data: insert, update, search, etc. DCL - data control language Control data access: permissions, etc.

9 SELECT STATEMENT SELECT statement retrieves data from database (query) The result is usually another table (but not necessarily a relation) We will learn Defining selection criteria Sorting Calculation Grouping

10 SELECT STATEMENT SYNTAX SELECT Column(s) or other expressions FROM Table(s) [WHERE …] [ORDER BY ColumnName]

11 SELECT COLUMNS Syntax SELECT * (or a list of columns ) FROM TableName Wild card: * Example SELECT * FROM Book SELECT BookName, Price FROM Books

12 SELECT ROWS Use WHERE clause to specify selection criteria Example SELECT * FROM Book WHERE Price = 29.99 SELECT BookName, Price FROM Books WHERE Price < 20 2.1 Comparison Operators “=“, “>”, “ =“, “ ”

13 DATA TYPES IN COMPARISON Text, String, Memo, etc. – '…' BookName='database' BookName>'2008'//alphabetical order Number, integer, decimal, currency, etc. Price > 20.99 Data/Time Access 2007: >#08/30/2008# //after the date SQL Server: <'08/30/2008' //before the date

14 MORE COMPARISON OPERATORS IN (value list) SELECT * FROM Book WHERE Price IN (19.99, 29.99, 39.99) BETWEEN min AND max SELECT * FROM Book WHERE Price BETWEEN 9.99 AND 19.99 IS NULL SELECT * FROM Book WHERE Author IS NULL

15 STRING PATTERN MATCH Fuzzy query using LIKE _ (underscore): single character wildcard ? in Access 2007 % (percentage): multiple character wildcard * in Access 2007 Example SELECT * FROM Book WHERE BookName LIKE '*information systems*'

16 NOT Reversal criteria NOT (expression) Examples: NOT Price > 20 NOT BookName LIKE '*information*' NOT IS NULL or IS NOT NULL

17 COMPOUND CONDITIONS Use logical operators to connect multiple conditions AND: an intersection of the data sets (higher precedence) OR: a union of the data sets Best practice: use parentheses () to explicitly mark comparison order Examples SELECT * FROM Book WHERE Price = 9.99 SELECT * FROM Book WHERE PubDate=#10/1/2003# OR PubDate=#10/1/2004# SELECT * from Book WHERE (Publisher = 'Que' OR Publisher = 'Alpha') AND Price = 29.99

18 SORTING Syntax ORDER BY Column(s) [ASC/DESC] Examples SELECT * FROM Books ORDER BY PubDate Multiple columns SELECT * FROM Book ORDER BY Publisher DESC, PubDate

19 COLUMN BASED CALCULATION Column based calculation Calculated (derived) columns are not designed directly into the table Using +, -, *, / with columns and numbers Example SELECT BookName, Price, Price/numberofPages AS PricePerPage FROM Books SELECT BookName, Price, Price * 0.1 AS Discount FROM Book WHERE Price * 0.1 >= 15

20 A COMPLETE QUERY EXAMPLE SELECT ISBN, BookName, Price, Publisher FROM Book WHERE BookName like '*Information Systems*' AND PubDate > #1/1/2002# AND Price < 100 ORDER BY Price

21 ROW BASED CALCULATION Using aggregate functions based on rows MIN (minimum) MAX (maximum) COUNT(The number of) AVG(Average) SUM(Sum of) Example SELECT COUNT(*) FROM Book SELECT AVG(Price) FROM Book WHERE Publisher = 'Prentice Hall'

22 GROUPING GROUP BY: doing math with groups SELECT COUNT(*) FROM Book WHERE Publisher = 'Prentice Hall'; SELECT COUNT(*) FROM Book WHERE Publisher = ‘Springer'; … Or: SELECT Publisher, COUNT(*) FROM Book GROUP BY Publisher

23 GROUP BY … HAVING Use “Having” clause to filter aggregation result SELECT Publisher, COUNT(*) FROM Book GROUP BY Publisher Having Count(*) > 2 Use “where” clause to filter records to be aggregated SELECT Publisher, COUNT(*) as total FROM Book Where Price < 100 GROUP BY Publisher Having Count(*) > 10 Order by Count(*)

24 UNIQUENESS Using the keyword “DISTINCT” Example: SELECT DISTINCT Publisher FROM Book

25 TABLE JOIN Querying data from multiple tables The query result consists of columns from more than one table How do rows match? Specifying matching/joining criteria: usually by the pair of primary key/foreign key, or candidate key/foreign key

26 TABLE JOIN EXAMPLE SELECT BookName, Date FROM Book, Order Where Book.ISBN = Order.ISBN Order By Book.ISBN, Order.Date Joining/matching criteria: very important, don’t forget! Use “table.column” format to avoid ambiguity ISBN is a Foreign Key here

27 JOINING MORE TABLES SELECT Book.BookName, Order.Date, Customer.LastName FROM Book, Order, Customer WHERE Books.ISBN = Order.ISBN and Order.CustomerID = Customer.CustomerID ORDER BY Books.ISBN, Rating

28 NEXT CLASS: DATA MINING Read “Diamond in the Data Mine” Work on A4 Groups form

29 MIDTERM REVIEW PROCESS Consult the answer key. Photocopy the page(s) of your exam that you wish to dispute. Write why you think you deserve points. Submit to my mailbox on the 8 th floor by Thursday 3/29.


Download ppt "INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR."

Similar presentations


Ads by Google